comparison gpp/legacy/html2md.py @ 290:64c188a9d31f

Adding a legacy app to contain management commands to convert the old data to the new database format. This first commit has the import_old_users command.
author Brian Neal <bgneal@gmail.com>
date Fri, 24 Dec 2010 05:28:58 +0000
parents
children 2367c4795c92
comparison
equal deleted inserted replaced
289:0dd8989abef2 290:64c188a9d31f
1 """
2 This module contains a class derived from Python's HTMLParser to convert HTML to
3 Markdown. Currently this class only supports those HTML tags that have counter-
4 parts in BBCode used by stock phpBB 2.x.
5
6 In other words, this class was created to help convert data from a phpBB
7 forum to Markdown syntax and its scope is currently limited to that task.
8
9 """
10 from HTMLParser import HTMLParser
11 import htmlentitydefs
12
13
14 # Let's call Markdown markup entities "elements" to avoid confusion
15 # with HTML tags.
16
17 class ElementBase(object):
18 """
19 Base class for all Markdown elements.
20
21 """
22 def __init__(self, attrs=None):
23 self.data = u''
24 self.attrs = dict(attrs) if attrs else {}
25
26 def add_data(self, data):
27 self.data += data
28
29 def markdown(self):
30 return self.data
31
32
33 class TextElement(ElementBase):
34 """
35 TextElements represent text fragments not inside HTML tags.
36 """
37 pass
38
39
40 class EmphasisElement(ElementBase):
41 """
42 An EmphasisElement is a Markdown element used to indicate emphasis and is
43 represented by placing characters around text. E.g. _em_, **bold**
44
45 """
46 def __init__(self, tag, attrs):
47 super(EmphasisElement, self).__init__(attrs)
48 self.tag = tag
49
50 def markdown(self):
51 return u'%s%s%s' % (self.tag, self.data, self.tag)
52
53
54 def create_emphasis(tag):
55 """
56 Returns a function that creates an EmphasisElement using the supplied
57 tag.
58
59 """
60 def inner(attrs):
61 return EmphasisElement(tag, attrs)
62 return inner
63
64
65 class HtmlElement(ElementBase):
66 """
67 Markdown also accepts HTML markup. This element represents a HTML tag that
68 maps to itself in Markdown.
69
70 """
71 def __init__(self, tag, attrs):
72 super(HtmlElement, self).__init__(attrs)
73 self.tag = tag
74
75 def markdown(self):
76 return u'<%s>%s</%s>' % (self.tag, self.data, self.tag)
77
78
79 def create_html(tag):
80 """
81 Returns a function that creates a HtmlElement using the supplied tag.
82
83 """
84 def inner(attrs):
85 return HtmlElement(tag, attrs)
86 return inner
87
88
89 class QuoteElement(ElementBase):
90 """
91 Class to represent a blockquote in Markdown.
92
93 """
94 def markdown(self):
95 return u'> %s\n\n' % self.data.replace('\n', '\n> ')
96
97
98 class BreakElement(ElementBase):
99 """
100 Class to represent a linebreak in Markdown.
101
102 """
103 def markdown(self):
104 return u' \n'
105
106
107 class DivElement(ElementBase):
108 """
109 This class maps a HTML <div> into a block of text surrounded by newlines.
110
111 """
112 def markdown(self):
113 return u'\n%s\n' % self.data
114
115
116 class LinkElement(ElementBase):
117 """
118 This class maps HTML <a> tags into Markdown links.
119 If no data is present, the actual href is used for the link text.
120
121 """
122 def markdown(self):
123 url = self.attrs['href']
124 text = self.data if self.data else url
125 return u'[%s](%s)' % (text, url)
126
127
128 class ImageElement(ElementBase):
129 """
130 This class maps HTML <img> tags into Markdown.
131 This element assumes no alt text is present, and simply uses the word
132 'image' for the alt text.
133
134 """
135 def markdown(self):
136 url = self.attrs['src']
137 return u'![image](%s)' % url
138
139
140 class CodeElement(ElementBase):
141 """
142 This class is used to create code blocks in Markdown.
143
144 """
145 def markdown(self):
146 return u' %s\n' % self.data.replace('\n', '\n ')
147
148
149 # List (ordered & unordered) support:
150
151 class ListElement(ElementBase):
152 """
153 This class creates Markdown for unordered lists. The bullet() method can be
154 overridden to create ordered lists.
155
156 """
157 def __init__(self, attrs=None):
158 super(ListElement, self).__init__(attrs)
159 self.items = []
160 self.list_nesting = 1
161
162 def add_data(self, data):
163 self.items.append(data)
164
165 def bullet(self):
166 return u'*'
167
168 def markdown(self):
169 bullet_str = self.bullet()
170 indent = u' ' * (4 * (self.list_nesting - 1))
171 s = u''
172 for item in self.items:
173 s += u'\n%s%s %s' % (indent, bullet_str, item)
174 return s
175
176
177 class OrderedListElement(ListElement):
178 """
179 This class creates Markdown for ordered lists.
180
181 """
182 def bullet(self):
183 return '1.'
184
185
186 class ItemElement(ElementBase):
187 """
188 This element is used to represent ordered & unordered list items.
189
190 """
191 pass
192
193 ###############################################################################
194 ###############################################################################
195
196 class MarkdownWriter(HTMLParser):
197 """
198 This class is an HTMLParser that converts a subset of HTML to Markdown.
199
200 """
201
202 elem_factories = {
203 'a': LinkElement,
204 'blockquote': QuoteElement,
205 'br': BreakElement,
206 'div': DivElement,
207 'em': create_emphasis('_'),
208 'img': ImageElement,
209 'li': ItemElement,
210 'ol': OrderedListElement,
211 'pre': CodeElement,
212 's': create_html('strike'),
213 'strong': create_emphasis('**'),
214 'u': create_html('u'),
215 'ul': ListElement,
216 }
217
218 def __init__(self):
219 HTMLParser.__init__(self)
220 self.reset()
221
222 def handle_starttag(self, tag, attrs):
223 if tag in self.elem_factories:
224 factory = self.elem_factories[tag]
225 element = factory(attrs)
226 else:
227 element = TextElement()
228
229 self._push_elem(element)
230
231 def handle_endtag(self, tag):
232 self._pop_elem()
233
234 def handle_data(self, data):
235 if len(self.elem_stack) == 0:
236 self._push_elem(TextElement())
237 self._add_data(data)
238
239 def handle_entityref(self, name):
240 try:
241 text = unichr(htmlentitydefs.name2codepoint[name])
242 except KeyError:
243 text = name
244 self.handle_data(text)
245
246 def handle_charref(self, name):
247 self.handle_data(unichr(int(name)))
248
249 def reset(self):
250 HTMLParser.reset(self)
251 self.elem_stack = []
252 self.elements = []
253 self.list_nesting = 0
254
255 def _push_elem(self, tag):
256 if len(self.elem_stack) and isinstance(self.elem_stack[-1], TextElement):
257 self._pop_elem()
258 if isinstance(tag, ListElement):
259 self.list_nesting += 1
260 tag.list_nesting = self.list_nesting
261 self.elem_stack.append(tag)
262
263 def _pop_elem(self):
264 element = self.elem_stack.pop()
265 if isinstance(element, ListElement):
266 self.list_nesting -= 1
267 if len(self.elem_stack):
268 self.elem_stack[-1].add_data(element.markdown())
269 else:
270 self.elements.append(element)
271
272 def _add_data(self, data):
273 self.elem_stack[-1].add_data(data)
274
275 def markdown(self):
276 while len(self.elem_stack):
277 self._pop_elem()
278 text_list = [e.markdown() for e in self.elements]
279 return u''.join(text_list)