-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown
More file actions
612 lines (489 loc) · 20.2 KB
/
Copy pathmarkdown
File metadata and controls
612 lines (489 loc) · 20.2 KB
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:rdfe="http://redfoot.net/3.0/rdf#"
xmlns:code="http://redfoot.net/3.0/code#"
>
<rdfe:RDFXMLDocument rdf:about="">
</rdfe:RDFXMLDocument>
<code:Module rdf:ID="module">
<rdfs:label>module for transforming markdown format</rdfs:label>
<code:python rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
<![CDATA[
#!/usr/bin/env python
# Python-Markdown
#
# Started by Manfred Stienstra (manfred.stienstra [at] dwerg.net,
# http://www.dwerg.net/projects/markdown/). Extended and maintained
# by Yuri Takhteyev (yuri [at] freewisdom.org, http://www.freewisdom.org)
#
# Project website: http://www.freewisdom.org/projects/python-markdown
#
# License: GPL 2 (http://www.gnu.org/copyleft/gpl.html)
#
# Version: 0.4
import re
import sys
import os
import xml.dom.minidom
HTML_PLACEHOLDER = "HTML_BLOCK_GOES_HERE_21738940712938470198_ALKJDL"
def print_error(string):
"""
Print an error string to stderr
"""
#sys.stderr.write(string.toString()+'\n')
sys.stderr.write(string +'\n')
debug_file = None
def debug(data, prefix) :
return
global debug_file
if debug_file is None:
debug_file = open("md.log", "w")
debug_file.write( "---------- %s ------------------\n" % prefix )
if type(data) == type('asdfa') :
debug_file.write(data + "\n")
elif type(data) == type([]) :
for x in data :
debug_file.write(x + "\n")
class Markdown:
"""Markdown formatter.
This the class for creating a html document from Markdown text
"""
regExp = {
'reference-def' : re.compile(r'^( )?\[([^\]]*)\]:\s*([^ ]*)(.*)'),
'header': re.compile(r'^(#*)([^#]*)(#*)$'),
'backtick': re.compile(r'^([^\`]*)\`([^\`]*)\`(.*)$'),
'escape': re.compile(r'^([^\\]*)\\(.)(.*)$'),
'emphasis': re.compile(r'^([^\*]*)\*([^\*]*)\*(.*)$|^([^\_]*)\_([^\_]*)\_(.*)$'),
'link': re.compile(r'^([^\[]*)\[([^\]]*)\]\s*\(([^\)]*)\)(.*)$'),
'reference-use' : re.compile(r'^([^\[]*)\[([^\]]*)\]\s*\[([^\]]*)\](.*)$'),
'strong': re.compile(r'^([^\*]*)\*\*(.*)\*\*([^\*]*)$|^([^\_]*)\_\_(.*)\_\_([^\_]*)$'),
'containsline': re.compile(r'^([-]*)$|^([=]*)$', re.M),
'ol': re.compile(r'^[\d]*\.\s+(.*)$'),
'ul': re.compile(r'^[*+-]\s+(.*)$'),
'isline': re.compile(r'^(\**)$|^([-]*)$'),
'tabbed': re.compile(r'^((\t)|( ))(.*)$'),
'quoted' : re.compile(r'^> ?(.*)$'),
'empty' : re.compile(r'^\s*$')
}
def __init__(self, text):
"""
Create a new Markdown instance.
@param text: The text in Markdown format.
"""
self.references={}
self.text = self._preprocess(text)
self.rawHtmlBlocks=[]
def _transform(self):
"""
Transforms the Markdown text into a XHTML body document
@returns: An xml.dom.minidom.Document
"""
doc = xml.dom.minidom.Document()
#body = doc.createElementNS("http://www.w3.org/1999/xhtml", "body")
body = doc.createElement("span")
body.appendChild(doc.createTextNode('\n'))
body.setAttribute('class', 'markdown')
doc.appendChild(body)
self.doc = doc
self._processSection(body, self.text.split('\n'))
body.appendChild(doc.createTextNode('\n'))
return doc
def _preprocess(self, text):
"""
Preprocess the source text. In particular, this includes
normalizing new lines and building a set of reference
definitions.
@param text: The text in Markdown format.
@returns: Normalized text without reference definitions.
"""
# Remove whitespace.
text = text.strip()
# Zap carriage returns.
text = text.replace("\r\n", "\n")
text = text.replace("\r", "\n")
# Extract references
# E.g., [id]: http://example.com/ "Optional Title Here"
text_no_ref = "";#\n"
for line in text.split("\n") :
m = self.regExp['reference-def'].match(line)
if m:
self.references[m.group(2)] = (m.group(3), m.group(4).strip())
else :
text_no_ref += line
text_no_ref += "\n"
return text_no_ref #+ "\n"
def _processSection(self, parent_elem, lines, inList = 0) :
"""
Process a section of a source document, looking for high
level structural elements like lists, block quotes, code
segments, html blocks, etc. Some those then get stripped
of their high level markup (e.g. get unindented) and the
lower-level markup is processed recursively.
@param parent_elem: DOM element to which the content will be added
@param lines: a list of lines
@param inList: a level
@returns: None
"""
debug (lines, "Section: %d" % inList)
if not lines :
return
# Deal with HR lines (needs to be done before processing lists)
if self._isLine(lines[0]) :
parent_elem.appendChild(self.doc.createElement('hr'))
self._processSection(parent_elem, lines[1:], inList)
return
# Check if this section starts with a list, a blockquote or
# a code block
processFn = { 'ul' : self._processUList,
'ol' : self._processOList,
'quoted' : self._processQuote,
'tabbed' : self._processCodeBlock }
for regexp in ['ul', 'ol', 'quoted', 'tabbed'] :
m = self.regExp[regexp].match(lines[0])
if m :
processFn[regexp](parent_elem, lines, inList)
return
# We are not looking at one of the high-level structures like
# lists or blockquotes. So, it's just a regular paragraph
# (though perhaps nested inside a list or something else).
# However, we are not in the list, we just need to look for a
# blank line. If we _are_ inside a list, however, we need to
# consider that a sublist does not need to be separated by a
# blank line. Rather, the following markup is legal:
#
# * The top level list item
#
# Another paragraph of the list. This is where we are now.
# * Underneath we might have a sublist.
#
if inList :
start, theRest = self._linesUntil(lines, (lambda line:
self.regExp['ul'].match(line)
or self.regExp['ol'].match(line)
or not line.strip()))
self._processSection(parent_elem, start, inList - 1)
self._processSection(parent_elem, theRest, inList - 1)
else : # Ok, we it's just a simple block
paragraph, theRest = self._linesUntil(lines, lambda line:
not line.strip())
debug(paragraph, "Para: ")
parent_elem.appendChild(self._handleSimpleBlock(self.doc,
"\n".join(paragraph)))
if theRest :
theRest = theRest[1:] # skip the first (blank) line
debug(theRest[:1], "%%")
self._processSection(parent_elem, theRest, inList)
def _processUList(self, parent_elem, lines, inList) :
self._processList(parent_elem, lines, inList,
listexpr='ul', tag = 'ul')
def _processOList(self, parent_elem, lines, inList) :
self._processList(parent_elem, lines, inList,
listexpr='ol', tag = 'ol')
def _processList(self, parent_elem, lines, inList, listexpr, tag) :
"""
Given a list of document lines starting with a list item,
finds the end of the list, breaks it up, and recursively
processes each list item and the remainder of the text file.
@param parent_elem: DOM element to which the content will be added
@param lines: a list of lines
@param inList: a level
@returns: None
"""
ul = self.doc.createElement(tag) # ul might actually be '<ol>'
parent_elem.appendChild(ul)
# Make a list of list items
items = []
item = -1
i = 0 # a counter to keep track of where we are
for line in lines :
if not line.strip() :
# If we see a blank line, this _might_ be the end of the list
i += 1
# Find the next non-blank line
for j in range(i, len(lines)) :
if lines[j].strip() :
next = lines[j]
break
else :
# There is no more text => end of the list
break
# Check if the next non-blank line is still a part of the list
if ( self.regExp[listexpr].match(next) or
self.regExp['tabbed'].match(next) ):
items[item].append(line)
continue
else :
break # found end of the list
# Now we need to detect list items (at the current level)
# while also detabing child elements if necessary
for expr in [listexpr, 'tabbed']:
m = self.regExp[expr].match(line)
if m :
if expr == listexpr : # We are looking at a new item
if m.group(1) :
items.append([m.group(1)])
item += 1
else :
debug(item, "Item: ")
elif expr == 'tabbed' : # This line needs to be detabbed
#print m.groups()
items[item].append(m.group(4)) #after the 'tab'
i += 1
break
else :
items[item].append(line) # Just regular continuation
else :
i += 1
# Add the dom elements
for item in items :
li = self.doc.createElement("li")
ul.appendChild(li)
debug(item, "LI:")
self._processSection(li, item, inList + 1)
# Process the remaining part of the section
self._processSection(parent_elem, lines[i:], inList)
def _linesUntil(self, lines, condition) :
""" A utility function to break a list of lines upon the
first line that satisfied a condition. The condition
argument should be a predicate function.
"""
i = -1
for line in lines :
i += 1
if condition(line) : break
else :
i += 1
return lines[:i], lines[i:]
def _processQuote(self, parent_elem, lines, inList) :
"""
Given a list of document lines starting with a quote finds
the end of the quote, unindents it and recursively
processes the body of the quote and the remainder of the
text file.
@param parent_elem: DOM element to which the content will be added
@param lines: a list of lines
@param inList: a level
@returns: None
"""
dequoted = []
i = 0
for line in lines :
m = self.regExp['quoted'].match(line)
if m :
dequoted.append(m.group(1))
i += 1
else :
break
else :
i += 1
blockquote = self.doc.createElement('blockquote')
parent_elem.appendChild(blockquote)
self._processSection(blockquote, dequoted, inList)
self._processSection(parent_elem, lines[i:], inList)
def _processCodeBlock(self, parent_elem, lines, inList) :
"""
Given a list of document lines starting with a code block
finds the end of the block, puts it into the dom verbatim
wrapped in ("<pre><code>") and recursively processes the
the remainder of the text file.
@param parent_elem: DOM element to which the content will be added
@param lines: a list of lines
@param inList: a level
@returns: None
"""
detabbed = []
i = 0
for line in lines :
m = self.regExp['tabbed'].match(line)
if m :
detabbed.append(m.group(4))
i += 1
else :
break
else :
i += 1
pre = self.doc.createElement('pre')
code = self.doc.createElement('code')
parent_elem.appendChild(pre)
pre.appendChild(code)
pre.appendChild(self.doc.createTextNode("\n".join(detabbed)))
self._processSection(parent_elem, lines[i:], inList)
def _handleInline(self, doc, line):
"""
Transform a Markdown line with inline elements to an XHTML part
@param item: A block of Markdown text
@return: A list of xml.dom.minidom elements
"""
if not(line):
return [doc.createTextNode(' '),]
# two spaces at the end of the line denote a <br/>
if line.endswith(' '):
l = self._handleInline(doc, line.rstrip())
l.append(doc.createElement('br'))
return l
m = self.regExp['link'].match(line)
if m is not None:
ll = self._handleInline(doc, m.group(1))
lr = self._handleInline(doc, m.group(4))
a = doc.createElement('a')
a.appendChild(doc.createTextNode(m.group(2)))
a.setAttribute('href', m.group(3))
#ll.append(doc.createTextNode(" "))
ll.append(a)
#ll.append(doc.createTextNode(" "))
ll.extend(lr)
return ll
m = self.regExp['reference-use'].match(line)
if m is not None:
ll = self._handleInline(doc, m.group(1))
lr = self._handleInline(doc, m.group(4))
a = doc.createElement('a')
a.appendChild(doc.createTextNode(m.group(2)))
href, title = self.references.get(m.group(3), ('undefined', '') )
a.setAttribute('href', href)
if title :
a.setAttribute('title', title)
#ll.append(doc.createTextNode(" "))
ll.append(a)
#ll.append(doc.createTextNode(" "))
ll.extend(lr)
return ll
for type in ['escape', 'strong', 'emphasis', 'backtick']:
m = self.regExp[type].match(line)
if m is not None:
if type == 'emphasis':
el = doc.createElement('em')
elif type == 'strong':
el = doc.createElement('strong')
elif type == 'backtick' :
el = doc.createElement('code')
if m.group(2) is not None:
ll = self._handleInline(doc, m.group(1))
lr = self._handleInline(doc, m.group(3))
txtEl = doc.createTextNode(m.group(2))
elif m.group(5) is not None:
ll = self._handleInline(doc, m.group(4))
lr = self._handleInline(doc, m.group(6))
txtEl = doc.createTextNode(m.group(5))
if type in ['escape'] :
ll.append(txtEl)
else :
el.appendChild(txtEl)
ll.append(el)
ll.extend(lr)
return ll
else:
return [doc.createTextNode(line),]
def _handleHeader(self, doc, text):
"""
Transform a Markdown header to an XHTML part
@param block: A block of Markdown text
@return: An xml.dom.minidom element
"""
m = self.regExp['header'].match(text)
if m is None:
return doc.createTextNode('')
# we only allow h1 - h6
if len(m.group(1)) > 6:
el = doc.createTextNode(text)
return el
else:
el = doc.createElement("h%s" % len(m.group(1)))
txtEl = doc.createTextNode(m.group(2).strip())
el.appendChild(txtEl)
return el
def _handleParagraph(self, doc, block, noinline=False):
"""
Transform a Markdown paragraph to an XHTML part
@param block: A block of Markdown text
@param noinline: Whether to live inline elements alone or not
@return: An xml.dom.minidom element
"""
el = doc.createElement('p')
if noinline:
el.appendChild(doc.createTextNode(block))
else :
for item in self._handleInline(doc, block.replace("\n", " ")) :
el.appendChild(item)
return el
def _handleSimpleBlock(self, doc, block, noinline=False):
"""
Transform a _simple_ block of Markdown to an XHTML part. A
simple block is a block that does not contain any
structural elements that can be nested.
@param block: A block of Markdown text with non-nesting elements
@param noinline: Whether to live inline elements alone or not
@return: A list of xml.dom.minidom elements
"""
# we don't care about a bunch of newlines
if not(block):
return doc.createTextNode('')
# header
if block.startswith('#'):
return self._handleHeader(doc, block)
elif block.startswith("<") : # very permisive test for HTML
self.rawHtmlBlocks.append(block)
el = doc.createTextNode("\n" + HTML_PLACEHOLDER + "\n")
return el
# everything else
else:
# header
lineMatch = self.regExp['containsline'].search(block)
if lineMatch and (lineMatch.group(1) or lineMatch.group(2)):
lines = block.split('\n')
try:
if lines[1].startswith('='):
return self._handleHeader(doc, '#'+lines[0])
elif lines[1].startswith('-'):
return self._handleHeader(doc, '##'+lines[0])
except IndexError, e:
print_error('Error: Found a strange header block')
print_error(block)
raise e
print_error('Error: Isline was true, while there was no line')
print_error(block)
return doc.createTextNode('\n')
# this is a block with no inline elements
elif noinline:
return doc.createTextNode(block)
# this is a paragraph
else:
return self._handleParagraph(doc, block, noinline)
def _isLine(self, block) :
"""
Determines if a block should be replaced with an <HR>
"""
text = block.replace(' ', '')
match = self.regExp['isline'].match(text)
if match and (match.group(1) or match.group(2)) and len(text) > 2:
return 1
else:
return 0
def __str__(self):
"""
Return the document in XHTML format.
@returns: A serialized XHTML body.
"""
doc = self._transform()
xml = doc.toxml()
buffer = ""
self.rawHtmlBlocks.reverse()
for line in xml.split("\n")[2:-1] :
if line == HTML_PLACEHOLDER :
buffer += self.rawHtmlBlocks.pop()
buffer += "\n"
else :
buffer += line
buffer += "\n"
return buffer
toString = __str__
def markdown(text):
return Markdown(text).toString()
if __name__ == '__main__':
print Markdown(file(sys.argv[1]).read())
]]>
</code:python>
</code:Module>
</rdf:RDF>