-
Notifications
You must be signed in to change notification settings - Fork 29
/
text.py
147 lines (110 loc) · 3.7 KB
/
text.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
from constants import html_entities
from mediawiki_parser import wikitextParser
import apostrophes
def toolset():
def render_tag_p(attributes):
return '\n'
def render_tag_br(attributes):
return '\n'
allowed_tags = {'p': render_tag_p,
'br': render_tag_br}
def render_title1(node):
node.value = '%s\n' % node.leaf()
def render_title2(node):
node.value = '%s\n' % node.leaf()
def render_title3(node):
node.value = '%s\n' % node.leaf()
def render_title4(node):
node.value = '%s\n' % node.leaf()
def render_title5(node):
node.value = '%s\n' % node.leaf()
def render_title6(node):
node.value = '%s\n' % node.leaf()
def render_raw_text(node):
pass
def render_paragraph(node):
node.value = '%s\n' % node.leaf()
def render_wikitext(node):
pass
def render_body(node):
tags = {'bold': '*', 'bold_close': '*', 'italic': '_', 'italic_close': '_'}
node.value = apostrophes.parse('%s' % node.leaves(), tags)
def render_entity(node):
value = '%s' % node.leaf()
if value in html_entities:
node.value = '%s' % unichr(html_entities[value])
else:
node.value = '&%s;' % value
def render_lt(node):
pass
def render_gt(node):
pass
def process_attribute(node, allowed_tag):
assert len(node.value) == 2, "Bad AST shape!"
attribute_name = node.value[0].value
attribute_value = node.value[1].value
return '%s="%s"' % (attribute_name, attribute_value)
def process_attributes(node, allowed_tag):
result = ''
if len(node.value) == 1:
pass
elif len(node.value) == 2:
attributes = node.value[1].value
for i in range(len(attributes)):
attribute = process_attribute(attributes[i], allowed_tag)
if attribute is not '':
result += ' ' + attribute
else:
raise Exception("Bad AST shape!")
return result
def render_attribute(node):
node.value = process_attribute(node, True)
def render_tag_open(node):
tag_name = node.value[0].value
if tag_name in allowed_tags:
attributes = process_attributes(node, True)
tag_processor = allowed_tags[tag_name]
node.value = tag_processor(attributes)
else:
attributes = process_attributes(node, False)
node.value = '<%s%s>' % (tag_name, attributes)
def render_tag_close(node):
node.value = ''
def render_tag_autoclose(node):
tag_name = node.value[0].value
if tag_name in allowed_tags:
attributes = process_attributes(node, True)
tag_processor = allowed_tags[tag_name]
node.value = tag_processor(attributes)
else:
attributes = process_attributes(node, False)
node.value = '<%s%s />' % (tag_name, attributes)
def render_table(node):
pass
def render_table_line_break(node):
node.value = '\n'
def render_table_header_cell(node):
pass
def render_table_normal_cell(node):
pass
def render_table_empty_cell(node):
pass
def render_table_caption(node):
pass
def render_preformatted(node):
pass
def render_hr(node):
node.value = '------'
def render_li(node):
pass
def render_list(node):
pass
def render_url(node):
pass
def render_external_link(node):
pass
def render_internal_link(node):
pass
return locals()
def make_parser():
return wikitextParser.make_parser(toolset())