1
- import mistune
2
1
from mistune import Markdown
3
- from mistune .core import BlockState
4
- from mistune .util import strip_end
5
- from mistune .renderers ._list import render_list
6
- from mistune .renderers .markdown import MarkdownRenderer
7
- from typing import Dict , Any
8
- from textwrap import indent
9
2
import argparse
10
3
import re
11
-
12
- UNDERLINED = r'\b_{1,3}(?=[^\s_])'
13
- UNDERLINED_END_RE = {
14
- '_' : re .compile (r'(?:(?<!\\)(?:\\\\)*\\_|[^\s_])_(?!_)\b' ),
15
- '__' : re .compile (r'(?:(?<!\\)(?:\\\\)*\\_|[^\s_])__(?!_)\b' ),
16
- '___' : re .compile (r'(?:(?<!\\)(?:\\\\)*\\_|[^\s_])___(?!_)\b' ),
17
- }
18
-
19
- def parse_underlined (inline , m , state ):
20
- text = m .group (2 )
21
- print ('rrr' )
22
- state .append_token ({'type' : 'underlined' , 'raw' : text })
23
- return m .end ()
24
-
25
- def parse_underlined (self , m , state ) -> int :
26
- pos = m .end ()
27
- marker = m .group (0 )
28
- mlen = len (marker )
29
- _end_re = UNDERLINED_END_RE [marker ]
30
- m1 = _end_re .search (state .src , pos )
31
- if not m1 :
32
- state .append_token ({'type' : 'text' , 'raw' : marker })
33
- return pos
34
- end_pos = m1 .end ()
35
- text = state .src [pos :end_pos - mlen ]
36
- prec_pos = self .precedence_scan (m , state , end_pos )
37
- if prec_pos :
38
- return prec_pos
39
- new_state = state .copy ()
40
- new_state .src = text
41
- new_state .in_underlined = True
42
- state .append_token ({
43
- 'type' : 'underlined' ,
44
- 'children' : self .render (new_state ),
45
- })
46
- return end_pos
47
-
48
- def render_underlined (self , token , state ) -> str :
49
- return '`_' + self .render_children (token , state ) + '`_'
50
-
51
- class Markdown2Micron (MarkdownRenderer ):
52
- NAME = 'micron'
53
-
54
- def __call__ (self , tokens , state : BlockState ):
55
- out = self .render_tokens (tokens , state )
56
- # special handle for line breaks
57
- out += '\n \n ' .join (self .render_referrences (state )) + '\n '
58
- return strip_end (out )
59
-
60
- def render_children (self , token , state : BlockState ):
61
- children = token ['children' ]
62
- return self .render_tokens (children , state )
63
-
64
- def text (self , token : Dict [str , Any ], state : BlockState ) -> str :
65
- return token ['raw' ]
66
-
67
- def emphasis (self , token : Dict [str , Any ], state : BlockState ) -> str :
68
- return '`*' + self .render_children (token , state ) + '`*'
69
-
70
- def strong (self , token : Dict [str , Any ], state : BlockState ) -> str :
71
- return '`!' + self .render_children (token , state ) + '`!'
72
-
73
- def link (self , token : Dict [str , Any ], state : BlockState ) -> str :
74
- label = token .get ('label' )
75
- text = self .render_children (token , state )
76
- out = '`[' + text + '`'
77
- if label :
78
- return out + '`[' + label + '`'
79
- attrs = token ['attrs' ]
80
- url = attrs ['url' ]
81
- if text == url :
82
- return '`[' + text + '`'
83
- elif 'mailto:' + text == url :
84
- return '`[' + text + '`'
85
- out += url
86
- return out + ']'
87
-
88
- def image (self , token : Dict [str , Any ], state : BlockState ) -> str :
89
- return self .link (token , state )
90
-
91
- def codespan (self , token : Dict [str , Any ], state : BlockState ) -> str :
92
- return '`=' + token ['raw' ] + '`='
93
-
94
- def linebreak (self , token : Dict [str , Any ], state : BlockState ) -> str :
95
- return ' \n '
96
-
97
- def softbreak (self , token : Dict [str , Any ], state : BlockState ) -> str :
98
- return '\n '
99
-
100
- def blank_line (self , token : Dict [str , Any ], state : BlockState ) -> str :
101
- return ''
102
-
103
- def inline_html (self , token : Dict [str , Any ], state : BlockState ) -> str :
104
- return ''
105
-
106
- def paragraph (self , token : Dict [str , Any ], state : BlockState ) -> str :
107
- text = self .render_children (token , state )
108
- return text + '\n \n '
109
-
110
- def heading (self , token : Dict [str , Any ], state : BlockState ) -> str :
111
- level = token ['attrs' ]['level' ]
112
- if level > 3 :
113
- level = 3
114
- marker = '>' * level
115
- text = self .render_children (token , state )
116
- return marker + ' ' + text + '\n \n '
117
- def thematic_break (self , token : Dict [str , Any ], state : BlockState ) -> str :
118
- return '-\n \n '
119
-
120
- def block_text (self , token : Dict [str , Any ], state : BlockState ) -> str :
121
- return self .render_children (token , state ) + '\n '
122
-
123
- def block_code (self , token : Dict [str , Any ], state : BlockState ) -> str :
124
- code = token ['raw' ]
125
- if code and code [- 1 ] != '\n ' :
126
- code += '\n '
127
- marker = '`='
128
- return marker + '\n ' + code + marker + '\n \n '
129
-
130
- def block_quote (self , token : Dict [str , Any ], state : BlockState ) -> str :
131
- text = indent (self .render_children (token , state ), '>>>>' )
132
- return text + '\n \n '
133
-
134
- def block_html (self , token : Dict [str , Any ], state : BlockState ) -> str :
135
- return ''
136
-
137
- def block_error (self , token : Dict [str , Any ], state : BlockState ) -> str :
138
- return ''
139
-
140
- def list (self , token : Dict [str , Any ], state : BlockState ) -> str :
141
- return render_list (self , token , state )
142
-
143
- def m2μ ():
144
- m2μr = Markdown2Micron ()
145
- m2μ = Markdown (renderer = m2μr )
146
- m2μ .inline .register ('underlined' , UNDERLINED , parse_underlined , before = 'emphasis' )
147
- m2μ .renderer .register ('underlined' , render_underlined )
148
- return m2μ
4
+ from underlined import underlined
5
+ from micron import MicronRenderer
149
6
150
7
def main ():
151
8
@@ -157,10 +14,13 @@ def main():
157
14
158
15
with open (args .md_file , 'r' ) as mdf :
159
16
md_str = mdf .read ()
160
- md2mu = m2μ ()
17
+
18
+ m2μr = MicronRenderer ()
19
+ m2μ = Markdown (renderer = m2μr )
20
+ underlined (m2μ )
161
21
162
22
with open (args .mu_file , 'w' ) as muf :
163
- md_str = muf .write (md2mu (md_str ))
23
+ md_str = muf .write (m2μ (md_str ))
164
24
165
25
if __name__ == "__main__" :
166
26
main ()
0 commit comments