Skip to content

Commit c37c860

Browse files
committed
Improve Markdown printing with "sbase print FILE.md"
1 parent e601da5 commit c37c860

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

seleniumbase/console_scripts/rich_helper.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@ def process_syntax(code, lang, theme, line_numbers, code_width, word_wrap):
1515
return syntax
1616

1717

18+
def get_code_without_tag(code, tag):
19+
# Remove TAG from Code/HTML, but keep inner text.
20+
# Eg: <a href="LINK">Hello!</a> becomes: "Hello!"
21+
tag_start = "<%s " % tag
22+
tag_solo = "<%s>" % tag
23+
tag_end = "</%s>" % tag
24+
while tag_start in code:
25+
start = code.find(tag_start)
26+
if start == -1:
27+
break
28+
end = code.find(">", start + 1) + 1
29+
if end == 0:
30+
break
31+
code = code[:start] + code[end:]
32+
code = code.replace(tag_solo, "")
33+
code = code.replace(tag_end, "")
34+
return code
35+
36+
1837
def display_markdown(code):
1938
try:
2039
markdown = Markdown(code)
@@ -38,6 +57,7 @@ def fix_emoji_spacing(code):
3857
try:
3958
# Fix the display width of certain emojis that take up two spaces
4059
double_width_emojis = [
60+
"👁️",
4161
"🗺️",
4262
"🖼️",
4363
"🗄️",
@@ -63,6 +83,8 @@ def fix_emoji_spacing(code):
6383
for emoji in double_width_emojis:
6484
if emoji in code:
6585
code = code.replace(emoji, emoji + " ")
86+
code = code.replace("✅<", "✅ <")
87+
code = code.replace("❌<", "❌ <")
6688
except Exception:
6789
pass
6890
return code

seleniumbase/console_scripts/sb_print.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -598,12 +598,6 @@ def main():
598598
print_success = False
599599
if use_rich and code_lang == "markdown" and not line_numbers:
600600
all_code = rich_helper.fix_emoji_spacing(all_code)
601-
all_code = all_code.replace("<br />", "\n")
602-
all_code = all_code.replace('<p align="center">', "\n")
603-
all_code = all_code.replace('<p align="left">', "\n")
604-
all_code = all_code.replace('<p align="right">', "\n")
605-
all_code = all_code.replace("<p>", "\n")
606-
all_code = all_code.replace("</p>", "\n")
607601
if "<b>*" not in all_code and "*<b>" not in all_code:
608602
if "</b>*" not in all_code and "*</b>" not in all_code:
609603
all_code = all_code.replace("<b>", "**")
@@ -617,6 +611,16 @@ def main():
617611
all_code = all_code.replace("\n<h2>", "\n# ").replace("</h2>", "")
618612
all_code = all_code.replace("\n<h3>", "\n# ").replace("</h3>", "")
619613
all_code = all_code.replace("\n<h4>", "\n# ").replace("</h4>", "")
614+
all_code = rich_helper.get_code_without_tag(all_code, "summary")
615+
all_code = rich_helper.get_code_without_tag(all_code, "details")
616+
all_code = rich_helper.get_code_without_tag(all_code, "span")
617+
all_code = rich_helper.get_code_without_tag(all_code, "div")
618+
all_code = rich_helper.get_code_without_tag(all_code, "img")
619+
all_code = rich_helper.get_code_without_tag(all_code, "li")
620+
all_code = rich_helper.get_code_without_tag(all_code, "ul")
621+
all_code = rich_helper.get_code_without_tag(all_code, "a")
622+
all_code = rich_helper.get_code_without_tag(all_code, "p")
623+
all_code = all_code.replace("<br />", "\n")
620624
print_success = rich_helper.display_markdown(all_code)
621625
if all_code.endswith("\n"):
622626
print() # Because "rich" skips the last line if new-line

0 commit comments

Comments
 (0)