@@ -53,6 +53,7 @@ class ConverterState:
5353 underline = False
5454 strike_through = False
5555 whitespace_pre = False
56+ alignment = "" # left: < center: | right > justify: =
5657
5758 def complete_cycle (self ):
5859 self .pos += self .pos_offset
@@ -82,6 +83,11 @@ class MintToHtmlConverter:
8283 - underline /u/
8384 - deleted (strike-through) /d/
8485
86+ - align to left /</
87+ - align to center /|/
88+ - align to right />/
89+ - align justified /=/
90+
8591 - keep whitespaces /e/
8692 - line breaks /l/
8793 '''
@@ -93,8 +99,9 @@ def __init__(
9399 ):
94100 self .escape_html = escape_html
95101 self .css = css
102+ self .comment_tag = MintTagToHtml ("#" , None , "comment" )
96103 self .tags = [
97- MintTagToHtml ( "#" , None , "comment" ) ,
104+ self . comment_tag ,
98105 MintTagToHtml ("p" , "p" , "paragraph" ),
99106 MintTagToHtml ("q" , "blockquote" , "blockquote" ),
100107 MintTagToHtml ("h" , "h1" , "heading" ),
@@ -106,14 +113,24 @@ def __init__(
106113 MintTagToHtml ("e" , "pre" , "whitespace_pre" ),
107114 MintTagToHtml ("l" , "br" , None )
108115 ]
116+ self .alignments = {
117+ "<" : "left" ,
118+ "|" : "center" ,
119+ ">" : "right" ,
120+ "=" : "justify"
121+ }
109122
110123 def __call__ (self , text_in : str , ) -> str :
111124 '''Convert mint to html'''
112125 # replace unwanted characters
113126 text_in = text_in .replace ("\r " , "" )
114127 # html escape
128+ text_in = text_in .replace ("/</" , "/lalgn/" )
129+ text_in = text_in .replace ("/>/" , "/ralgn/" )
115130 if self .escape_html :
116131 text_in = escape (text_in )
132+ text_in = text_in .replace ("/lalgn/" , "/</" )
133+ text_in = text_in .replace ("/ralgn/" , "/>/" )
117134 # parsing & conversion
118135 state = ConverterState ()
119136
@@ -149,7 +166,7 @@ def process_inline_tag(c1: str, state: ConverterState, t: MintTagToHtml):
149166 break
150167 c1 = text_in [state .pos + 1 ]
151168 if c == TAG_BOUNDARY :
152- if c1 == TAG_BOUNDARY :
169+ if c1 == TAG_BOUNDARY and not state . comment :
153170 # e.g. // -> /
154171 state .pos_offset += 1
155172 state .add_output (TAG_BOUNDARY )
@@ -158,14 +175,27 @@ def process_inline_tag(c1: str, state: ConverterState, t: MintTagToHtml):
158175 if state .pos + 2 < len_text_in :
159176 c2 = text_in [state .pos + 2 ]
160177 if c2 == TAG_BOUNDARY :
161- # process tags
162- for t in self .tags :
163- process_inline_tag (c1 , state , t )
178+ if state .comment :
179+ process_inline_tag (c1 , state , self .comment_tag )
180+ else :
181+ for t in self .tags :
182+ process_inline_tag (c1 , state , t )
183+ # toggle alignment
184+ if c1 in self .alignments :
185+ state .pos_offset += 2
186+ if state .alignment == c1 :
187+ pass
188+ elif state .alignment != "" :
189+ state .add_output ("</section>" )
190+ state .add_output (f"<section style='text-align: { self .alignments [c1 ]} '>" )
191+ state .alignment = c1
164192 if not state .cycle_had_op and not state .comment :
165193 state .output += c
166194 # complete this cycle's state
167195 state .complete_cycle ()
168196 # cleanup
197+ if state .alignment != "" :
198+ state .add_output ("</section>" )
169199 state .output = state .output .strip ()
170200 # return result
171201 return state .output
0 commit comments