@@ -78,6 +78,7 @@ def __init__(
7878 self .use_automatic_links = config .USE_AUTOMATIC_LINKS # covered in cli
7979 self .hide_strikethrough = False # covered in cli
8080 self .mark_code = config .MARK_CODE
81+ self .backquote_code_style = config .BACKQUOTE_CODE_STYLE
8182 self .wrap_list_items = config .WRAP_LIST_ITEMS # covered in cli
8283 self .wrap_links = config .WRAP_LINKS # covered in cli
8384 self .wrap_tables = config .WRAP_TABLES
@@ -111,6 +112,8 @@ def __init__(
111112 self .blockquote = 0
112113 self .pre = False
113114 self .startpre = False
115+ self .pre_indent = ""
116+ self .list_code_indent = ""
114117 self .code = False
115118 self .quote = False
116119 self .br_toggle = ""
@@ -629,6 +632,7 @@ def link_url(self: HTML2Text, link: str, title: str = "") -> None:
629632 self .lastWasList = False
630633
631634 if tag == "li" :
635+ self .list_code_indent = ""
632636 self .pbr ()
633637 if start :
634638 if self .list :
@@ -644,15 +648,16 @@ def link_url(self: HTML2Text, link: str, title: str = "") -> None:
644648 # TODO: line up <ol><li>s > 9 correctly.
645649 parent_list = None
646650 for list in self .list :
647- self .o (
648- " " if parent_list == "ol" and list .name == "ul" else " "
649- )
651+ self .list_code_indent += " " if parent_list == "ol" else " "
650652 parent_list = list .name
653+ self .o (self .list_code_indent )
651654
652655 if li .name == "ul" :
656+ self .list_code_indent += " "
653657 self .o (self .ul_item_mark + " " )
654658 elif li .name == "ol" :
655659 li .num += 1
660+ self .list_code_indent += " "
656661 self .o (str (li .num ) + ". " )
657662 self .start = True
658663
@@ -715,8 +720,11 @@ def link_url(self: HTML2Text, link: str, title: str = "") -> None:
715720 if start :
716721 self .startpre = True
717722 self .pre = True
723+ self .pre_indent = ""
718724 else :
719725 self .pre = False
726+ if self .backquote_code_style :
727+ self .out ("\n " + self .pre_indent + "```" )
720728 if self .mark_code :
721729 self .out ("\n [/code]" )
722730 self .p ()
@@ -786,17 +794,23 @@ def o(
786794 bq += " "
787795
788796 if self .pre :
789- if not self .list :
797+ if self .list :
798+ bq += self .list_code_indent
799+
800+ if not self .backquote_code_style :
790801 bq += " "
791- # else: list content is already partially indented
792- bq += " " * len (self .list )
802+
793803 data = data .replace ("\n " , "\n " + bq )
804+ self .pre_indent = bq
794805
795806 if self .startpre :
796807 self .startpre = False
797- if self .list :
808+ if self .backquote_code_style :
809+ self .out ("\n " + self .pre_indent + "```" )
810+ self .p_p = 0
811+ elif self .list :
798812 # use existing initial indentation
799- data = data .lstrip ("\n " )
813+ data = data .lstrip ("\n " + self . pre_indent )
800814
801815 if self .start :
802816 self .space = False
@@ -952,8 +966,15 @@ def optwrap(self, text: str) -> str:
952966 # because of the presence of a link in it
953967 if not self .wrap_links :
954968 self .inline_links = False
969+ start_code = False
955970 for para in text .split ("\n " ):
956- if len (para ) > 0 :
971+ # If the text is between tri-backquote pairs, it's a code block;
972+ # don't wrap
973+ if self .backquote_code_style and para .lstrip ().startswith ("```" ):
974+ start_code = not start_code
975+ if start_code :
976+ result += para + "\n "
977+ elif len (para ) > 0 :
957978 if not skipwrap (
958979 para , self .wrap_links , self .wrap_list_items , self .wrap_tables
959980 ):
0 commit comments