-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.py
184 lines (129 loc) · 4.83 KB
/
markdown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"Markdown parser."
import re
import marko
import marko.ast_renderer
import marko.inline
import marko.helpers
import yaml
import constants
import utils
from utils import Tx
class Subscript(marko.inline.InlineElement):
"Markdown extension for subscript."
pattern = re.compile(r"(?<!~)(~)([^~]+)\1(?!~)")
priority = 5
parse_children = True
parse_group = 2
class SubscriptRenderer:
"Output subscript text."
def render_subscript(self, element):
return f"<sub>{self.render_children(element)}</sub>"
class Superscript(marko.inline.InlineElement):
"Markdown extension for superscript."
pattern = re.compile(r"(?<!\^)(\^)([^\^]+)\1(?!\^)")
priority = 5
parse_children = True
parse_group = 2
class SuperscriptRenderer:
"Output superscript text."
def render_superscript(self, element):
return f"<sup>{self.render_children(element)}</sup>"
class Emdash(marko.inline.InlineElement):
"Markdown extension for em-dash."
pattern = re.compile(r"(?<!-)(--)(?!-)")
parse_children = False
class EmdashRenderer:
"Output em-dash character."
def render_emdash(self, element):
return constants.EM_DASH
class Lastedit(marko.inline.InlineElement):
"Markdown extension for the position of the last edit in the text."
pattern = re.compile("(LASTEDIT)")
parse_children = False
class LasteditRenderer:
"Output position of last edit in the text."
def render_lastedit(self, element):
return '<span id="lastedit"></span>'
class Indexed(marko.inline.InlineElement):
"Markdown extension for indexed term."
pattern = re.compile(r"\[#(.+?)(\|(.+?))?\]") # I know, this isn't quite right.
parse_children = False
def __init__(self, match):
self.term = match.group(1).strip()
if match.group(3): # Because of the not-quite-right regexp...
self.canonical = match.group(3).strip()
else:
self.canonical = self.term
class IndexedRenderer:
"Output a link to the index page and item."
def render_indexed(self, element):
if element.term == element.canonical:
title = utils.Tx("Indexed")
else:
title = utils.Tx("Indexed") + ": " + element.canonical
return f'<a class="contrast" title="{title}" href="/index#{element.canonical}">{element.term}</a>'
class Reference(marko.inline.InlineElement):
"Markdown extension for reference."
pattern = re.compile(r"\[@(.+?)\]")
parse_children = False
def __init__(self, match):
self.name = match.group(1).strip()
self.id = utils.nameify(self.name)
class ReferenceRenderer:
"Output a link to the reference page and item."
def render_reference(self, element):
return f'<strong><a href="/refs/{element.id}">{element.name}</a></strong>'
html_converter = marko.Markdown()
html_converter.use("footnote")
html_converter.use(
marko.helpers.MarkoExtension(
elements=[Subscript, Superscript, Emdash, Lastedit, Indexed, Reference],
renderer_mixins=[
SubscriptRenderer,
SuperscriptRenderer,
EmdashRenderer,
LasteditRenderer,
IndexedRenderer,
ReferenceRenderer,
],
)
)
class Fragmenter:
"Fragment content into paragraphs with separate edit buttons."
PATTERN = re.compile(r"\n\n")
def __init__(self, content, href=None):
self.href = href
self.content = content
self.current = 0
self.ranges = []
self.processed = self.PATTERN.subn(self, content)[0]
if self.ranges:
self.ranges.append((self.ranges[-1][1] + 1, len(content)))
self.processed += self.get_href(self.ranges[-1][0] + 1, self.ranges[-1][1])
else:
self.ranges.append((0, len(content)))
self.processed += self.get_href(self.ranges[-1][0], self.ranges[-1][1])
self.fragments = [self.content[s:e] for s, e in self.ranges]
def __call__(self, match):
start = match.start()
self.ranges.append((self.current, start))
result = self.get_href(self.current, start)
self.current = match.end()
return result
def get_href(self, first, last):
if self.href:
return f' <a href="{self.href}?first={first}&last={last}" title="{Tx("Edit paragraph")}"><img src="/edit.svg" class="white"></a>\n\n'
else: # No change.
return "\n\n"
def convert_to_html(content, href=None):
if href:
content = Fragmenter(content, href=href).processed
return html_converter.convert(content)
ast_converter = marko.Markdown(renderer=marko.ast_renderer.ASTRenderer)
ast_converter.use("footnote")
ast_converter.use(
marko.helpers.MarkoExtension(
elements=[Subscript, Superscript, Emdash, Lastedit, Indexed, Reference],
)
)
convert_to_ast = ast_converter.convert