@@ -114,15 +114,19 @@ def main():
114
114
if help_me :
115
115
invalid_run_command (invalid_cmd )
116
116
117
- with open (file_to_print , "r" , encoding = "utf-8" ) as f :
118
- all_code = f .read ()
117
+ all_code = None
118
+ if sys .version_info [0 ] >= 3 :
119
+ with open (file_to_print , "r" , encoding = "utf-8" ) as f :
120
+ all_code = f .read ()
121
+ else :
122
+ with open (file_to_print , "r" ) as f :
123
+ all_code = f .read ()
119
124
all_code = all_code .replace ("\t " , " " )
120
125
code_lines = all_code .split ("\n " )
121
126
122
127
console_width = None # width of console output when running script
123
128
used_width = None # code_width and few spaces on right for padding
124
- magic_console = None
125
- magic_syntax = None
129
+ magic_syntax = None # the syntax generated by rich.syntax.Syntax()
126
130
try :
127
131
console_width = os .popen ("stty size" , "r" ).read ().split ()[1 ]
128
132
if console_width :
@@ -135,8 +139,7 @@ def main():
135
139
use_rich = True
136
140
137
141
if use_rich :
138
- from rich .console import Console
139
- from rich .syntax import Syntax
142
+ from seleniumbase .console_scripts import rich_helper
140
143
141
144
the_code = "\n " .join (code_lines )
142
145
code_width = 1
@@ -566,22 +569,16 @@ def main():
566
569
if console_width and (code_width + extra_r_spaces < console_width ):
567
570
used_width = code_width + extra_r_spaces
568
571
569
- try :
570
- if "🗺️" in the_code :
571
- # Fix width of an emoji
572
- the_code = the_code .replace ("🗺️" , "🗺️ " )
573
- except Exception :
574
- pass
572
+ the_code = rich_helper .fix_emoji_spacing (the_code )
575
573
576
- magic_syntax = Syntax (
574
+ magic_syntax = rich_helper . process_syntax (
577
575
the_code ,
578
576
code_lang ,
579
577
theme = "monokai" ,
580
578
line_numbers = line_numbers ,
581
579
code_width = used_width ,
582
580
word_wrap = word_wrap ,
583
581
)
584
- magic_console = Console ()
585
582
# ----------------------------------------
586
583
dash_length = 62 # May change
587
584
if used_width and used_width + w < console_width :
@@ -591,22 +588,35 @@ def main():
591
588
dashes = "-" * dash_length
592
589
print (dashes )
593
590
print_success = False
594
- if use_rich and code_lang == "markdown" :
595
- try :
596
- from rich .markdown import Markdown
597
-
598
- markdown = Markdown (all_code )
599
- markdown_console = Console ()
600
- markdown_console .print (markdown ) # noqa
601
- print_success = True
602
- except Exception :
603
- pass
591
+ if use_rich and code_lang == "markdown" and not line_numbers :
592
+ all_code = rich_helper .fix_emoji_spacing (all_code )
593
+ all_code = all_code .replace ("<br />" , "\n " )
594
+ all_code = all_code .replace (
595
+ '<p align="center">' , '\n ' )
596
+ all_code = all_code .replace (
597
+ '<p align="left">' , '\n ' )
598
+ all_code = all_code .replace (
599
+ '<p align="right">' , '\n ' )
600
+ all_code = all_code .replace ("<p>" , "\n " )
601
+ all_code = all_code .replace ("</p>" , "\n " )
602
+ if "<b>*" not in all_code and "*<b>" not in all_code :
603
+ if "</b>*" not in all_code and "*</b>" not in all_code :
604
+ all_code = all_code .replace ("<b>" , "**" )
605
+ all_code = all_code .replace ("</b>" , "**" )
606
+ if "<code>`" not in all_code and "`<code>" not in all_code :
607
+ if "</code>`" not in all_code and "`</code>" not in all_code :
608
+ all_code = all_code .replace ("<code>" , "``" )
609
+ all_code = all_code .replace ("</code>" , "``" )
610
+ # Display ALL <h> tags as an <h1> because the font size is fixed
611
+ all_code = all_code .replace ("\n <h1>" , "\n # " ).replace ("</h1>" , "" )
612
+ all_code = all_code .replace ("\n <h2>" , "\n # " ).replace ("</h2>" , "" )
613
+ all_code = all_code .replace ("\n <h3>" , "\n # " ).replace ("</h3>" , "" )
614
+ all_code = all_code .replace ("\n <h4>" , "\n # " ).replace ("</h4>" , "" )
615
+ print_success = rich_helper .display_markdown (all_code )
616
+ if all_code .endswith ("\n " ):
617
+ print () # Because "rich" skips the last line if new-line
604
618
elif use_rich and magic_syntax :
605
- try :
606
- magic_console .print (magic_syntax ) # noqa
607
- print_success = True
608
- except Exception :
609
- pass
619
+ print_success = rich_helper .display_code (magic_syntax )
610
620
if not use_rich or not magic_syntax or not print_success :
611
621
for line in code_lines :
612
622
print (line )
0 commit comments