Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 86 lines (66 sloc) 2.387 kB
f4b2212 @peter17 Attempt to deal with HTML entities (1 test broken)
authored
1 from constants import html_entities
2
acf65dc @peter17 Improve the structure of the postprocessors.
authored
3 def toolset():
4 def render_tag_p(attributes):
5 return '\n'
6
7 def render_tag_br(attributes):
8 return '\n'
9
10 allowed_tags = {'p': render_tag_p,
11 'br': render_tag_br}
12
13 def render_title2(node):
14 node.value += '\n'
15
16 def render_title6(node):
17 node.value += '\n'
18
19 def render_raw_text(node):
bcfad00 @peter17 Add rendering for allowed and disallowed HTML tags
authored
20 pass
acf65dc @peter17 Improve the structure of the postprocessors.
authored
21
22 def render_paragraph(node):
23 node.value += '\n'
24
25 def render_body(node):
26 pass
27
28 def render_entity(node):
29 value = '%s' % node.leaf()
30 if value in html_entities:
31 node.value = '%s' % unichr(html_entities[value])
32 else:
33 node.value = '&%s;' % value
34
35 def render_lt(node):
36 pass
37
38 def render_gt(node):
39 pass
40
41 def process_attributes(node, allowed_tag):
42 result = ''
43 if len(node.value) == 1:
44 pass
45 elif len(node.value) == 2:
46 attributes = node.value[1].value
47 for i in range(len(attributes)):
48 attribute_name = attributes[i].value[0].value
49 attribute_value = attributes[i].value[1].value
50 result += ' %s="%s"' % (attribute_name, attribute_value)
51 else:
52 raise Exception("Bad AST shape!")
53 return result
54
55 def render_tag_open(node):
56 tag_name = node.value[0].value
57 if tag_name in allowed_tags:
58 attributes = process_attributes(node, True)
59 tag_processor = allowed_tags[tag_name]
60 node.value = tag_processor(attributes)
61 else:
62 attributes = process_attributes(node, False)
63 node.value = '<%s%s>' % (tag_name, attributes)
64
65 def render_tag_close(node):
66 node.value = ''
67
68 def render_tag_autoclose(node):
69 tag_name = node.value[0].value
70 if tag_name in allowed_tags:
71 attributes = process_attributes(node, True)
72 tag_processor = allowed_tags[tag_name]
73 node.value = tag_processor(attributes)
74 else:
75 attributes = process_attributes(node, False)
76 node.value = '<%s%s />' % (tag_name, attributes)
77
78 return locals()
79
80 toolset = toolset()
8c3e0b1 @peter17 Allow creating more than one instance of the parser, to avoid memoïza…
authored
81
6b0586a @peter17 Improve coding style for imports
authored
82 from mediawiki_parser import wikitextParser
83
8c3e0b1 @peter17 Allow creating more than one instance of the parser, to avoid memoïza…
authored
84 def make_parser():
6b0586a @peter17 Improve coding style for imports
authored
85 return wikitextParser.make_parser(toolset)
Something went wrong with that request. Please try again.