comparison gpp/legacy/html2md.py @ 292:2367c4795c92

Added a legacy management command to import old news comments.
author Brian Neal <bgneal@gmail.com>
date Fri, 24 Dec 2010 22:20:30 +0000
parents 64c188a9d31f
children
comparison
equal deleted inserted replaced
291:a6357f2bcbbc 292:2367c4795c92
118 This class maps HTML <a> tags into Markdown links. 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. 119 If no data is present, the actual href is used for the link text.
120 120
121 """ 121 """
122 def markdown(self): 122 def markdown(self):
123 url = self.attrs['href'] 123 try:
124 url = self.attrs['href']
125 except KeyError:
126 return self.data if self.data else u''
127
124 text = self.data if self.data else url 128 text = self.data if self.data else url
125 return u'[%s](%s)' % (text, url) 129 return u'[%s](%s)' % (text, url)
126 130
127 131
128 class ImageElement(ElementBase): 132 class ImageElement(ElementBase):
131 This element assumes no alt text is present, and simply uses the word 135 This element assumes no alt text is present, and simply uses the word
132 'image' for the alt text. 136 'image' for the alt text.
133 137
134 """ 138 """
135 def markdown(self): 139 def markdown(self):
136 url = self.attrs['src'] 140 try:
141 url = self.attrs['src']
142 except KeyError:
143 return u' (missing image) '
137 return u'![image](%s)' % url 144 return u'![image](%s)' % url
138 145
139 146
140 class CodeElement(ElementBase): 147 class CodeElement(ElementBase):
141 """ 148 """
259 self.list_nesting += 1 266 self.list_nesting += 1
260 tag.list_nesting = self.list_nesting 267 tag.list_nesting = self.list_nesting
261 self.elem_stack.append(tag) 268 self.elem_stack.append(tag)
262 269
263 def _pop_elem(self): 270 def _pop_elem(self):
264 element = self.elem_stack.pop() 271 try:
272 element = self.elem_stack.pop()
273 except IndexError:
274 # pop from empty list => bad HTML input; ignore it
275 return
276
265 if isinstance(element, ListElement): 277 if isinstance(element, ListElement):
266 self.list_nesting -= 1 278 self.list_nesting -= 1
267 if len(self.elem_stack): 279 if len(self.elem_stack):
268 self.elem_stack[-1].add_data(element.markdown()) 280 self.elem_stack[-1].add_data(element.markdown())
269 else: 281 else: