-
Notifications
You must be signed in to change notification settings - Fork 2
/
mdx_tangle.py
364 lines (287 loc) · 13.9 KB
/
mdx_tangle.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python
"""
Embeds Tangle reactive elements
All resulting HTML is XHTML Strict compatible.
Examples:
>>> import markdown
>>> md = markdown.Markdown(extensions=['tangle', 'fenced_code', 'def_list])
## Good ol' adjustable number
>>> md.convert("t[number](cookies)")
u'<p><span class="TKAdjustableNumber" data-var="cookies"></span></p>'
>>> md.convert("t[number](0<percentCompliance..5<100 percent '%')")
u'<p><span class="TKAdjustableNumber" data-format="percent" data-max="100" data-min="0" data-step="5" data-var="percentCompliance">%</span></p>'
>>> md.convert('t[number](0<percentCompliance..5<100 percent "%")')
u'<p><span class="TKAdjustableNumber" data-format="percent" data-max="100" data-min="0" data-step="5" data-var="percentCompliance">%</span></p>'
## Plain ol' number field
>>> md.convert('t[field](cookies " cookies")')
u'<p><span class="TKNumberField" data-var="cookies"> cookies</span></p>'
>>> md.convert('t[field 5](cookies " cookies")')
u'<p><span class="TKNumberField" data-size="5" data-var="cookies"> cookies</span></p>'
>>> md.convert('t[field]( cookies )')
u'<p><span class="TKNumberField" data-var="cookies"></span></p>'
## The toggle
>>> md.convert('t[toggle](newAdmissionAppliesToEveryone)[those who paid the charge][everyone]')
u'<p><span class="TKToggle" data-var="newAdmissionAppliesToEveryone"><span>those who paid the charge</span><span>everyone</span></span></p>'
>>> md.convert('t[toggle]( newAdmissionAppliesToEveryone )[ those who paid the charge ][ everyone ]')
u'<p><span class="TKToggle" data-var="newAdmissionAppliesToEveryone"><span>those who paid the charge</span><span>everyone</span></span></p>'
## Oy. The empty case. Just shows a number
>>> md.convert('__t[](calories " calories")__')
u'<p><strong><span data-var="calories"> calories</span></strong></p>'
>>> md.convert('t[](restorationTime)')
u'<p><span data-var="restorationTime"></span></p>'
>>> md.convert('t[](surplus e6)')
u'<p><span data-format="e6" data-var="surplus"></span></p>'
## The Positive(or 0)/Negative switch
>>> md.convert('t[posneg](deltaBudget)[collect an extra][lose]')
u'<p><span class="TKSwitchPositiveNegative" data-var="deltaBudget"><span>collect an extra</span><span>lose</span></span></p>'
## The evil huge switch. Probably need to support this format for others.
>>> md.convert('''t[switch](scenarioIndex)
... | This is not sufficient to maintain the parks, and t[](closedParkCount)
... parks would be shut down at least part-time.
...
... | This is sufficient to maintain the parks in their current state, but
... not fund a program to bring safety and cleanliness up to acceptable
... standards.
...
... | This is sufficient to maintain the parks in their current state, plus
... fund a program to bring safety and cleanliness up to acceptable standards
... over the next t[](restorationTime) years.
...
... | This is sufficient to maintain the parks and bring safety and cleanliness
... up to acceptable standards, leaving a $t[](surplus e6) million per year
... surplus''')
u'<span class="TKSwitch" data-var="scenarioIndex"><span class="TKBranchOption">This is not sufficient to maintain the parks, and <span data-var="closedParkCount"></span> \\nparks would be shut down at least part-time.</span><span class="TKBranchOption">This is sufficient to maintain the parks in their current state, but \\nnot fund a program to bring safety and cleanliness up to acceptable \\nstandards.</span><span class="TKBranchOption">This is sufficient to maintain the parks in their current state, plus \\nfund a program to bring safety and cleanliness up to acceptable standards \\nover the next <span data-var="restorationTime"></span> years.</span><span class="TKBranchOption">This is sufficient to maintain the parks and bring safety and cleanliness\\nup to acceptable standards, leaving a $<span data-format="e6" data-var="surplus"></span> million per year \\nsurplus</span></span>'
>>> md.convert('''t[if](scenarioIndex)
... ~ This is not sufficient to maintain the parks, and t[](closedParkCount)
... parks would be shut down at least part-time.''')
u'<span class="TKIf" data-var="scenarioIndex"><span class="TKBranchOption">This is not sufficient to maintain the parks, and <span data-var="closedParkCount"></span> \\nparks would be shut down at least part-time.</span></span>'
>>> md.convert('''t[if]( scenarioIndex invert )
... ~ This is not sufficient to maintain the parks, and t[](closedParkCount)
... parks would be shut down at least part-time.''')
u'<span class="TKIf" data-invert="true" data-var="scenarioIndex"><span class="TKBranchOption">This is not sufficient to maintain the parks, and <span data-var="closedParkCount"></span> \\nparks would be shut down at least part-time.</span></span>'
>>> md.convert('''t[if]( scenarioIndex invert )
... ~ This is not sufficient to maintain the parks, and t[](closedParkCount)
... parks would be shut down at least part-time.
...
... t[switch](scenarioIndex)
... | Foo
... | Bar''')
u'<span class="TKIf" data-invert="true" data-var="scenarioIndex"><span class="TKBranchOption">This is not sufficient to maintain the parks, and <span data-var="closedParkCount"></span> \\nparks would be shut down at least part-time.</span></span><span class="TKSwitch" data-var="scenarioIndex"><span class="TKBranchOption">Foo</span><span class="TKBranchOption">Bar</span></span>'
>>> md.convert('''If you eat t[number](cookies ' cookies'), you consume t[](calories ' calories').
... This constitutes t[](dailypercent '%') of a daily intake of t[number](dailycalories 'calories').
...
... ~~~~.initialize
... #cookies = 3
... #calories = 150
... #dailypercent = 1
... #dailycalories = 2100
... ~~~~
...
... ~~~~.update
... #calories = #cookies * 50
... #dailypercent = (#cookies * 50) / #dailycalories
... ~~~~''')
u'foo'
"""
import re
import markdown
version = "0.0"
def makeExtension(configs=None) :
return TangleExtension(configs=configs)
class TangleExtension(markdown.Extension):
tangle_inline_classes = []
def __init__(self, configs):
self.config = {
#TODO: meh?
}
# Override defaults with user settings
for key, value in configs:
self.setConfig(key, value)
def extendMarkdown(self, md, md_globals):
for cls in self.tangle_inline_classes:
md.inlinePatterns.add(cls.__name__, cls(cls.find_pattern, md), "<reference")
""" Add an instance of DefListProcessor to BlockParser. """
md.parser.blockprocessors.add('tkswitchlist',
TKSwitchProcessor(md.parser),
'>ulist')
md.parser.blockprocessors.add('tkif',
TKIfProcessor(md.parser),
'>ulist')
def tk_span(cls_or_class, text=None, ignore=[], children=[], **kwargs):
"""
Helper function. Creates the "normal" TangleKit spans.
"""
kwargs = dict([('data-'+k, v) for k, v in kwargs.items() if v not in [None,""] and k not in ignore])
obj = markdown.util.etree.Element('span')
[obj.append(c) for c in children]
if text:
obj.text = text
if isinstance(cls_or_class, str):
obj.set('class', cls_or_class)
else:
obj.set('class', cls_or_class.__class__.__name__)
[obj.set(k, v) for k, v in kwargs.items()]
return obj
class TKBranchingProcessor(markdown.blockprocessors.BlockProcessor):
"""
Process Definition Lists.
Shamelessly plundered from markdown.extensions.deflist... should work together, though.
TODO: Nesting?
"""
RE = re.compile(r'(^|\n)[ ]{0,3}\|[ ]{1,3}(.*?)(\n|$)')
NO_INDENT_RE = re.compile(r'^[ ]{0,3}[^ \|]')
def test(self, parent, block):
return bool(self.RE.search(block))
def run(self, parent, blocks):
block = blocks.pop(0)
m = self.RE.search(block)
terms = [l.strip() for l in block[:m.start()].split('\n') if l.strip()]
block = block[m.end():]
no_indent = self.NO_INDENT_RE.match(block)
if no_indent:
d, theRest = (block, None)
else:
d, theRest = self.detab(block)
if d:
d = '%s\n%s' % (m.group(2), d)
else:
d = m.group(2)
sibling = self.lastChild(parent)
if not terms and sibling.tag == 'p':
# The previous paragraph contains the terms
state = 'looselist'
terms = sibling.text.split('\n')
parent.remove(sibling)
# Aquire new sibling
sibling = self.lastChild(parent)
else:
state = 'list'
if sibling and sibling.get('class') == self.tree_class:
# This is another item on an existing list
dl = sibling
if len(dl) and dl[-1].get('class') == 'TKBranchOption' and len(dl[-1]):
state = 'looselist'
else:
# This is a new list
dl = markdown.util.etree.SubElement(parent, 'span')
dl.set("class", self.tree_class)
# Add terms
for term in terms:
self.term_handler(dl, term)
# Add definition
self.parser.state.set(state)
dd = markdown.util.etree.SubElement(dl, 'span')
dd.set('class', 'TKBranchOption')
self.parser.parseBlocks(dd, [d])
self.parser.state.reset()
if theRest:
blocks.insert(0, theRest)
class TKSwitchProcessor(TKBranchingProcessor):
tree_class = 'TKSwitch'
RE = re.compile(r'(^|\n)[ ]{0,3}\|[ ]{1,3}(.*?)(\n|$)')
def term_handler(self, tree, term):
term_match = re.match(r't\[switch\]\(\s*(?P<var>[^\)\s]*)\s*\)', term)
if term_match:
tree.set('data-var', term_match.groupdict()['var'])
class TKIfProcessor(TKBranchingProcessor):
tree_class = 'TKIf'
RE = re.compile(r'(^|\n)[ ]{0,3}\~[ ]{1,3}(.*?)(\n|$)')
def term_handler(self, tree, term):
term_match = re.match(r't\[if\]\(\s*(?P<var>[^\) ]*)\s*(?P<invert>invert)?\s*\)', term)
if term_match:
tree.set('data-var', term_match.groupdict()['var'])
if term_match.groupdict()['invert']:
tree.set('data-invert', 'true')
class TKVoid(markdown.inlinepatterns.Pattern):
find_pattern = r't\[\]\((?P<stuff>[^\)]*)\)'
stuff_pattern = re.compile(r'''
\s*
(?P<var>[^\. ]*) #required variable
\s* #maybe some space
(?P<format>[^\"\'\s]*) #optional sprintf format
\s* #maybe some spaces
((?P<qt>[\"\'])
(?P<label>[^\4]*) #optional quoted label
\4|$)
\s*
''', re.X)
def handleMatch(self, m):
bits = self.stuff_pattern.match(m.groupdict()['stuff'])
kwargs = {}
text = ""
if bits:
bits = bits.groupdict()
text = bits['label']
kwargs = bits
return tk_span(None, text, ignore=['qt','label'], **kwargs)
TangleExtension.tangle_inline_classes.append(TKVoid)
class TKToggle(markdown.inlinepatterns.Pattern):
find_pattern = r't\[toggle\]\(\s*(?P<var>[^\)\s]*)\s*\)\[\s*(?P<op0>[^\]]*[^\s])\s*\]\[\s*(?P<op1>[^\]]*[^\s])\s*\]'
def handleMatch(self, m):
kwargs = {}
children = []
text = ""
bits = m.groupdict()
kwargs['var'] = bits['var']
for op_text in [bits['op0'], bits['op1']]:
op = markdown.util.etree.Element('span')
op.text = op_text
children.append(op)
kwargs = bits
return tk_span('TKToggle TKSwitch', children=children, ignore=['op0', 'op1'], **kwargs)
TangleExtension.tangle_inline_classes.append(TKToggle)
class TKSwitchPositiveNegative(TKToggle):
find_pattern = r't\[posneg\]\(\s*(?P<var>[^\)\s]*)\s*\)\[\s*(?P<op0>[^\]]*[^\s])\s*\]\[\s*(?P<op1>[^\]]*[^\s])\s*\]'
TangleExtension.tangle_inline_classes.append(TKSwitchPositiveNegative)
class TKNumberField(markdown.inlinepatterns.Pattern):
find_pattern = r't\[field( (?P<size>\d*))?\]\((?P<stuff>[^\)]*)\)'
stuff_pattern = re.compile(r'''
\s*
(?P<var>[^ ]*) #required variable
\s* #maybe some spaces
(([\"\'])
(?P<label>[^\3]*) #optional quoted label
\3|$)
\s*
''', re.X)
def handleMatch(self, m):
bits = self.stuff_pattern.match(m.groupdict()['stuff'])
kwargs = {}
text = ""
if bits:
bits = bits.groupdict()
if m.groupdict()['size']:
bits['size'] = m.groupdict()['size']
text = bits['label']
kwargs = bits
return tk_span(self, text, ignore=['label'], **kwargs)
TangleExtension.tangle_inline_classes.append(TKNumberField)
class TKAdjustableNumber(markdown.inlinepatterns.Pattern):
find_pattern = r't\[number\]\((?P<stuff>[^\)]*)\)'
stuff_pattern = re.compile(r'''
\s*
((?P<min>[\d\-.]*)<|) #optional minimum
(?P<var>[^\. ]*) #required variable
(\.\.(?P<step>[\d\-.]*)|) #optional step
(<(?P<max>[\d\-.]*)|) #optional maximum
\s* #maybe some space
(?P<format>[^\"\'\s]*) #optional sprintf format
\s* #maybe some spaces
((?P<qt>[\"\'])
(?P<label>[^\10]*) #optional quoted label
\10|$)
\s*
''', re.X)
def handleMatch(self, m):
bits = self.stuff_pattern.match(m.groupdict()['stuff'])
kwargs = {}
text = ""
if bits:
bits = bits.groupdict()
text = bits['label']
kwargs = bits
return tk_span(self, text, ignore=['label', 'qt'], **kwargs)
TangleExtension.tangle_inline_classes.append(TKAdjustableNumber)
if __name__ == "__main__":
import doctest
doctest.testmod()