Skip to content
Newer
Older
100644 107 lines (80 sloc) 2.72 KB
f4b2212 @peter17 Attempt to deal with HTML entities (1 test broken)
authored Jul 20, 2011
1 from constants import html_entities
2
acf65dc @peter17 Improve the structure of the postprocessors.
authored Jul 22, 2011
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 Jul 21, 2011
20 pass
acf65dc @peter17 Improve the structure of the postprocessors.
authored Jul 21, 2011
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
70e5684 @peter17 Add HTML rendering for tables (not finished).
authored Jul 26, 2011
54
55 def render_attribute(node):
56 node.value = process_attributes(node, True)
57
acf65dc @peter17 Improve the structure of the postprocessors.
authored Jul 21, 2011
58 def render_tag_open(node):
59 tag_name = node.value[0].value
60 if tag_name in allowed_tags:
61 attributes = process_attributes(node, True)
62 tag_processor = allowed_tags[tag_name]
63 node.value = tag_processor(attributes)
64 else:
65 attributes = process_attributes(node, False)
66 node.value = '<%s%s>' % (tag_name, attributes)
67
68 def render_tag_close(node):
69 node.value = ''
70
71 def render_tag_autoclose(node):
72 tag_name = node.value[0].value
73 if tag_name in allowed_tags:
74 attributes = process_attributes(node, True)
75 tag_processor = allowed_tags[tag_name]
76 node.value = tag_processor(attributes)
77 else:
78 attributes = process_attributes(node, False)
79 node.value = '<%s%s />' % (tag_name, attributes)
80
70e5684 @peter17 Add HTML rendering for tables (not finished).
authored Jul 25, 2011
81 def render_table(node):
82 pass
83
84 def render_table_line_break(node):
85 node.value = '\n'
86
87 def render_table_header_cell(node):
88 pass
89
90 def render_table_normal_cell(node):
91 pass
92
93 def render_table_empty_cell(node):
94 pass
95
96 def render_table_caption(node):
97 pass
98
acf65dc @peter17 Improve the structure of the postprocessors.
authored Jul 21, 2011
99 return locals()
100
101 toolset = toolset()
8c3e0b1 @peter17 Allow creating more than one instance of the parser, to avoid memoïza…
authored Jul 13, 2011
102
6b0586a @peter17 Improve coding style for imports
authored Jul 13, 2011
103 from mediawiki_parser import wikitextParser
104
8c3e0b1 @peter17 Allow creating more than one instance of the parser, to avoid memoïza…
authored Jul 12, 2011
105 def make_parser():
6b0586a @peter17 Improve coding style for imports
authored Jul 12, 2011
106 return wikitextParser.make_parser(toolset)
Something went wrong with that request. Please try again.