annotate MoinMoin/converter/markdown_in.py @ 2568:44a132511e24

Add popular extensions to markdown parser (markdown_in.py)
author RogerHaase <haaserd@gmail.com>
date Sat, 08 Mar 2014 07:50:19 -0700
parents 791bdedb0c20
children 92a9c6ef1ec7
rev   line source
1666
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1 # Copyright: 2008-2010 MoinMoin:BastianBlank
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
2 # Copyright: 2012 MoinMoin:AndreasKloeckner
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
3 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
4
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
5 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
6 MoinMoin - Markdown input converter
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
7
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
8 http://daringfireball.net/projects/markdown/
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
9 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
10
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
11
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
12 from __future__ import absolute_import, division
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
13
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
14 import re
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
15 import htmlentitydefs
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
16
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
17 from MoinMoin.util.tree import moin_page, xml, html, xlink
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
18 from ._util import allowed_uri_scheme, decode_data
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
19
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
20 from MoinMoin import log
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
21 logging = log.getLogger(__name__)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
22
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
23 from emeraldtree import ElementTree as ET
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
24
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
25 from markdown import Markdown
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
26 import markdown.util as md_util
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
27
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1666
diff changeset
28
1666
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
29 def postproc_text(markdown, text):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
30 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
31 Removes HTML or XML character references and entities from a text string.
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
32
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
33 @param text The HTML (or XML) source text.
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
34 @return The plain text, as a Unicode string, if necessary.
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
35 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
36
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
37 # http://effbot.org/zone/re-sub.htm#unescape-html
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
38
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
39 if text is None:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
40 return None
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
41
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
42 for pp in markdown.postprocessors.values():
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
43 text = pp.run(text)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
44
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
45 def fixup(m):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
46 text = m.group(0)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
47 if text[:2] == "&#":
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
48 # character reference
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
49 try:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
50 if text[:3] == "&#x":
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
51 return unichr(int(text[3:-1], 16))
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
52 else:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
53 return unichr(int(text[2:-1]))
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
54 except ValueError:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
55 pass
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
56 else:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
57 # named entity
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
58 try:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
59 text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
60 except KeyError:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
61 pass
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1666
diff changeset
62 return text # leave as is
1666
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
63
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
64 return re.sub("&#?\w+;", fixup, text)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
65
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1666
diff changeset
66
1666
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
67 class Converter(object):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
68 # {{{ html conversion
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
69
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
70 # HTML tags which can be converted directly to the moin_page namespace
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
71 symmetric_tags = set(['div', 'p', 'strong', 'code', 'quote', 'blockquote'])
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
72
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
73 # HTML tags to define a list, except dl which is a little bit different
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
74 list_tags = set(['ul', 'dir', 'ol'])
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
75
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
76 # HTML tags which can be convert without attributes in a different DOM tag
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1666
diff changeset
77 simple_tags = { # Emphasis
2011
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
78 'em': moin_page.emphasis, 'i': moin_page.emphasis,
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
79 # Strong
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
80 'b': moin_page.strong, 'strong': moin_page.strong,
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
81 # Code and Blockcode
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
82 'pre': moin_page.blockcode, 'tt': moin_page.code,
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
83 'samp': moin_page.code,
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
84 # Lists
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
85 'dt': moin_page.list_item_label, 'dd': moin_page.list_item_body,
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
86 # TODO : Some tags related to tables can be also simplify
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
87 }
1666
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
88
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
89 # HTML Tag which does not have equivalence in the DOM Tree
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
90 # But we keep the information using <span element>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
91 inline_tags = set(['abbr', 'acronym', 'address', 'dfn', 'kbd'])
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
92
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
93 # HTML tags which are completely ignored by our converter.
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
94 # We even do not process children of these elements.
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
95 ignored_tags = set(['applet', 'area', 'button', 'caption', 'center', 'fieldset',
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
96 'form', 'frame', 'frameset', 'head', 'iframe', 'input', 'isindex',
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
97 'label', 'legend', 'link', 'map', 'menu', 'noframes', 'noscript',
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
98 'optgroup', 'option', 'param', 'script', 'select', 'style',
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
99 'textarea', 'title', 'var',
2011
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
100 ])
1666
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
101
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
102 # standard_attributes are html attributes which are used
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
103 # directly in the DOM tree, without any conversion
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
104 standard_attributes = set(['title', 'class', 'style'])
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
105
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
106 # Regular expression to detect an html heading tag
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
107 heading_re = re.compile('h[1-6]')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
108
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
109 def new(self, tag, attrib, children):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
110 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
111 Return a new element for the DOM Tree
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
112 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
113 return ET.Element(tag, attrib=attrib, children=children)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
114
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
115 def new_copy(self, tag, element, attrib):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
116 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
117 Function to copy one element to the DOM Tree.
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
118
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
119 It first converts the child of the element,
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
120 and the element itself.
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
121 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
122 attrib_new = self.convert_attributes(element)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
123 attrib.update(attrib_new)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
124 children = self.do_children(element)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
125 return self.new(tag, attrib, children)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
126
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
127 def new_copy_symmetric(self, element, attrib):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
128 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
129 Create a new QName, with the same tag of the element,
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
130 but with a different namespace.
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
131
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
132 Then, we handle the copy normally.
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
133 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
134 tag = ET.QName(element.tag, moin_page)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
135 return self.new_copy(tag, element, attrib)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
136
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
137 def convert_attributes(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
138 result = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
139 for key, value in element.attrib.iteritems():
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
140 if key in self.standard_attributes:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
141 result[html(key)] = value
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
142 if key == 'id':
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
143 result[xml('id')] = value
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
144 return result
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
145
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
146 def visit_heading(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
147 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
148 Function to convert an heading tag into a proper
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
149 element in our moin_page namespace
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
150 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
151 heading_level = element.tag[1]
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
152 key = moin_page('outline-level')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
153 attrib = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
154 attrib[key] = heading_level
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
155 return self.new_copy(moin_page.h, element, attrib)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
156
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
157 def visit_br(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
158 return moin_page.line_break()
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
159
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
160 def visit_big(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
161 key = moin_page('font-size')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
162 attrib = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
163 attrib[key] = '120%'
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
164 return self.new_copy(moin_page.span, element, attrib)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
165
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
166 def visit_small(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
167 key = moin_page('font-size')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
168 attrib = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
169 attrib[key] = '85%'
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
170 return self.new_copy(moin_page.span, element, attrib)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
171
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
172 def visit_sub(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
173 key = moin_page('baseline-shift')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
174 attrib = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
175 attrib[key] = 'sub'
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
176 return self.new_copy(moin_page.span, element, attrib)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
177
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
178 def visit_sup(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
179 key = moin_page('baseline-shift')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
180 attrib = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
181 attrib[key] = 'super'
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
182 return self.new_copy(moin_page.span, element, attrib)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
183
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
184 def visit_u(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
185 key = moin_page('text-decoration')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
186 attrib = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
187 attrib[key] = 'underline'
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
188 return self.new_copy(moin_page.span, element, attrib)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
189
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
190 def visit_ins(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
191 key = moin_page('text-decoration')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
192 attrib = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
193 attrib[key] = 'underline'
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
194 return self.new_copy(moin_page.span, element, attrib)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
195
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
196 def visit_del(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
197 key = moin_page('text-decoration')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
198 attrib = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
199 attrib[key] = 'line-through'
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
200 return self.new_copy(moin_page.span, element, attrib)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
201
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
202 def visit_s(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
203 key = moin_page('text-decoration')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
204 attrib = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
205 attrib[key] = 'line-through'
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
206 return self.new_copy(moin_page.span, element, attrib)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
207
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
208 def visit_strike(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
209 key = moin_page('text-decoration')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
210 attrib = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
211 attrib[key] = 'line-through'
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
212 return self.new_copy(moin_page.span, element, attrib)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
213
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
214 def visit_hr(self, element, min_class=u'moin-hr1', max_class=u'moin-hr6', default_class=u'moin-hr3'):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
215 hr_class = element.attrib.get('class')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
216 if not (min_class <= hr_class <= max_class):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
217 element.attrib[html('class')] = default_class
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
218 return self.new_copy(moin_page.separator, element, {})
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
219
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
220 def visit_img(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
221 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
222 <img src="URI" /> --> <object xlink:href="URI />
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
223 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
224 key = xlink('href')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
225 attrib = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
226 attrib[key] = element.attrib.get("src")
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
227 return moin_page.object(attrib)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
228
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
229 def visit_object(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
230 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
231 <object data="href"></object> --> <object xlink="href" />
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
232 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
233 key = xlink('href')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
234 attrib = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
235 if self.base_url:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
236 attrib[key] = ''.join([self.base_url, element.get(html.data)])
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
237 else:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
238 attrib[key] = element.get(html.data)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
239
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
240 # Convert the href attribute into unicode
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
241 attrib[key] = unicode(attrib[key])
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
242 return moin_page.object(attrib)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
243
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
244 def visit_inline(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
245 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
246 For some specific inline tags (defined in inline_tags)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
247 We just return <span element="tag.name">
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
248 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
249 key = html('class')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
250 attrib = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
251 attrib[key] = ''.join(['html-', element.tag.name])
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
252 return self.new_copy(moin_page.span, element, attrib)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
253
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
254 def visit_li(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
255 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
256 NB : A list item (<li>) is like the following snippet :
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
257 <list-item>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
258 <list-item-label>label</list-item-label>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
259 <list-item-body>Body</list-item-body>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
260 </list-item>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
261
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
262 For <li> element, there is no label
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
263 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
264 list_item_body = ET.Element(moin_page.list_item_body,
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
265 attrib={}, children=self.do_children(element))
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
266 return ET.Element(moin_page.list_item, attrib={}, children=[list_item_body])
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
267
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
268 def visit_list(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
269 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
270 Convert a list of item (whatever the type : ordered or unordered)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
271 So we have a html code like :
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
272 <ul>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
273 <li>Item 1</li>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
274 <li>Item 2</li>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
275 </ul>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
276
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
277 Which will be convert like :
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
278 <list>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
279 <list-item>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
280 <list-item-body>Item 1</list-item-body>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
281 </list-item>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
282 <list-item>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
283 <list-item-body>Item 2</list-item-body>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
284 </list-item>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
285 </list>
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
286 """
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
287 # We will define the appropriate attribute
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
288 # according to the type of the list
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
289 attrib = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
290 if element.tag == "ul" or element.tag == "dir":
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
291 attrib[moin_page('item-label-generate')] = 'unordered'
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
292 elif element.tag == "ol":
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
293 attrib[moin_page('item-label-generate')] = 'ordered'
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
294
2011
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
295 return ET.Element(moin_page.list, attrib=attrib, children=self.do_children(element))
1666
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
296
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
297 def visit_a(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
298 key = xlink('href')
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
299 attrib = {}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
300 href = postproc_text(self.markdown, element.attrib.get("href"))
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
301 if allowed_uri_scheme(href):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
302 attrib[key] = href
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
303 else:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
304 return href
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
305 return self.new_copy(moin_page.a, element, attrib)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
306
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
307 def visit(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
308 # Our element can be converted directly, just by changing the namespace
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
309 if element.tag in self.symmetric_tags:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
310 return self.new_copy_symmetric(element, attrib={})
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
311
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
312 # Our element is enough simple to just change the tag name
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
313 if element.tag in self.simple_tags:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
314 return self.new_copy(self.simple_tags[element.tag], element, attrib={})
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
315
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
316 # Our element defines a list
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
317 if element.tag in self.list_tags:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
318 return self.visit_list(element)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
319
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
320 # We convert our element as a span tag with element attribute
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
321 if element.tag in self.inline_tags:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
322 return self.visit_inline(element)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
323
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
324 # We have a heading tag
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
325 if self.heading_re.match(element.tag):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
326 return self.visit_heading(element)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
327
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
328 # Otherwise we need a specific procedure to handle it
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
329 method_name = 'visit_' + element.tag
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
330 method = getattr(self, method_name, None)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
331 if method:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
332 return method(element)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
333
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
334 # We should ignore this tag
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
335 if element.tag in self.ignored_tags:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
336 logging.info("INFO : Ignored tag : {0}".format(element.tag))
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
337 return
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
338
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
339 logging.info("INFO : Unhandled tag : {0}".format(element.tag))
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
340 return
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
341
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
342 def do_children(self, element):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
343 new = []
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
344 if hasattr(element, "text") and element.text is not None:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
345 new.append(postproc_text(self.markdown, element.text))
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
346
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
347 for child in element:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
348 r = self.visit(child)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
349 if r is None:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
350 r = ()
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
351 elif not isinstance(r, (list, tuple)):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
352 r = (r, )
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
353 new.extend(r)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
354 if hasattr(child, "tail") and child.tail is not None:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
355 new.append(postproc_text(self.markdown, child.tail))
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
356 return new
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
357
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
358 # }}}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
359
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
360 def __init__(self):
2568
44a132511e24 Add popular extensions to markdown parser (markdown_in.py)
RogerHaase <haaserd@gmail.com>
parents: 2011
diff changeset
361 self.markdown = Markdown(extensions=['extra', 'toc', ])
1666
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
362
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
363 @classmethod
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
364 def _factory(cls, input, output, **kw):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
365 return cls()
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
366
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
367 def __call__(self, data, contenttype=None, arguments=None):
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
368 text = decode_data(data, contenttype)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
369
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
370 # {{{ stolen from Markdown.convert
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
371
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
372 # Fixup the source text
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
373 try:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
374 text = unicode(text)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
375 except UnicodeDecodeError, e:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
376 # Customise error message while maintaining original traceback
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
377 e.reason += '. -- Note: Markdown only accepts unicode input!'
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
378 raise
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
379
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
380 text = text.replace(md_util.STX, "").replace(md_util.ETX, "")
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
381 text = text.replace("\r\n", "\n").replace("\r", "\n") + "\n\n"
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
382 text = re.sub(r'\n\s+\n', '\n\n', text)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
383 text = text.expandtabs(8)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
384
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
385 # Split into lines and run the line preprocessors.
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
386 lines = text.split("\n")
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
387 for prep in self.markdown.preprocessors.values():
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
388 lines = prep.run(lines)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
389
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
390 # Parse the high-level elements.
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
391 md_root = self.markdown.parser.parseDocument(lines).getroot()
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
392
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
393 # Run the tree-processors
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
394 for treeprocessor in self.markdown.treeprocessors.values():
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
395 new_md_root = treeprocessor.run(md_root)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
396 if new_md_root:
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
397 md_root = new_md_root
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
398
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
399 # }}}
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
400
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
401 # md_root is a list of plain old Python ElementTree objects.
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
402
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
403 converted = self.do_children(md_root)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
404 body = moin_page.body(children=converted)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
405 root = moin_page.page(children=[body])
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
406
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
407 return root
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
408
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
409 from . import default_registry
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
410 from MoinMoin.util.mime import Type, type_moin_document
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
411 default_registry.register(Converter._factory, Type("text/x-markdown"), type_moin_document)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
412 default_registry.register(Converter._factory, Type('x-moin/format;name=markdown'), type_moin_document)
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
413
fa66e4447d63 add markdown converter, thanks to Andreas Kloeckner
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
414 # vim: foldmethod=marker