+ if tag.lower() == u'br':
+ self.handle_br()
+ elif tag.lower() == u'img':
+ self.handle_image(attrs)
+ return
+
+ def handle_br(self):
+ self.handle_curdata()
+ self.opentags.append(u'br')
+ self.handle_curdata()
+ self.opentags.pop()
+
+ def handle_image(self, attrs):
+ alt = u''
+ url = u''
+ for attr in attrs:
+ if attr[0] == 'alt':
+ alt = attr[1].decode('utf-8')
+ elif attr[0] == 'src':
+ url = attr[1].decode('utf-8')
+ if url:
+ if alt:
+ if self.images.has_key(alt):
+ if self.images[alt]["url"] == url:
+ self.curdata = self.curdata \
+ + u'|%s|' %(alt,)
+ else:
+ while self.images.has_key(alt):
+ alt = alt + "_"
+ self.images[alt] = {"url": url}
+ self.curdata = self.curdata \
+ + u'|%s|' %(alt,)
+ else:
+ self.images[alt] = {"url": url}
+ self.curdata = self.curdata \
+ + u'|%s|' %(alt,)
+ else:
+ if self.images.has_key(url):
+ self.curdata = self.curdata \
+ + u'|%s|' %(url,)
+ else:
+ self.images[url] = {}
+ self.images[url]["url"] =url
+ self.curdata = self.curdata \
+ + u'|%s|' %(url,)
+
+ def handle_curdata(self):
+
+ if len(self.opentags) == 0:
+ return
+
+ tag_thats_done = self.opentags[-1]
+
+ if len(self.curdata) == 0:
+ return
+
+ if tag_thats_done == u'br':
+ if len(self.text) == 0 or self.text[-1] != '\n':
+ self.text = self.text + '\n'
+ self.ignorenodata = True
+ return
+
+ if len(self.curdata.strip()) == 0:
+ return
+
+ if tag_thats_done in self.blockleveltags:
+ newlinerequired = self.text != u''
+ if self.ignorenodata:
+ newlinerequired = False
+ self.ignorenodata = False
+ if newlinerequired:
+ if tag_thats_done in [u'dt', u'dd', u'li'] \
+ and len(self.text) > 1 \
+ and self.text[-1] != u'\n':
+ self.text = self.text + u'\n'
+ elif len(self.text) > 2 \
+ and self.text[-1] != u'\n' \
+ and self.text[-2] != u'\n':
+ self.text = self.text + u'\n\n'
+
+ if tag_thats_done in ["h1", "h2", "h3", "h4", "h5", "h6"]:
+ underline = u''
+ underlinechar = u'='
+ headingtext = " ".join(self.curdata.split())
+ seperator = u'\n' + u' '*self.indentlevel
+ headingtext = seperator.join( \
+ textwrap.wrap( \
+ headingtext, \
+ self.textwidth - self.indentlevel \
+ ) \
+ )
+
+ if tag_thats_done == u'h2':
+ underlinechar = u'-'
+ elif tag_thats_done != u'h1':
+ underlinechar = u'~'
+
+ if u'\n' in headingtext:
+ underline = u' ' * self.indentlevel \
+ + underlinechar * (self.textwidth - self.indentlevel)
+ else:
+ underline = u' ' * self.indentlevel \
+ + underlinechar * len(headingtext)
+ self.text = self.text \
+ + headingtext + u'\n' \
+ + underline
+ elif tag_thats_done in [u'p', u'div']:
+ paragraph = unicode( \
+ " ".join(self.curdata.strip().encode("utf-8").split()), \
+ "utf-8")
+ seperator = u'\n' + u' ' * self.indentlevel
+ self.text = self.text \
+ + u' ' * self.indentlevel \
+ + seperator.join( \
+ textwrap.wrap( \
+ paragraph, self.textwidth - self.indentlevel))
+ elif tag_thats_done == "pre":
+ self.text = self.text + unicode( \
+ self.curdata.encode("utf-8"), "utf-8")
+ elif tag_thats_done == u'blockquote':
+ quote = unicode( \
+ " ".join(self.curdata.encode("utf-8").strip().split()), \
+ "utf-8")
+ seperator = u'\n' + u' ' * self.indentlevel + u' '
+ if len(self.text) > 0 and self.text[-1] != u'\n':
+ self.text = self.text + u'\n'
+ self.text = self.text \
+ + u' ' \
+ + seperator.join( \
+ textwrap.wrap( \
+ quote, \
+ self.textwidth - self.indentlevel - 2 \
+ )
+ )
+ self.curdata = u''
+ elif tag_thats_done == "li":
+ item = unicode(self.curdata.encode("utf-8").strip(), "utf-8")
+ if len(self.text) > 0 and self.text[-1] != u'\n':
+ self.text = self.text + u'\n'
+ # work out if we're in an ol rather than a ul
+ latesttags = self.opentags[-4:]
+ latesttags.reverse()
+ isul = None
+ for thing in latesttags:
+ if thing == 'ul':
+ isul = True
+ break
+ elif thing == 'ol':
+ isul = False
+ break
+
+ listindent = 3
+ if not isul:
+ listindent = 4
+
+ listmarker = u' * '
+ if isul == False:
+ listmarker = u' %2d. ' %(self.listcount[-1])
+ self.listcount[-1] = self.listcount[-1] + 1
+
+ seperator = u'\n' \
+ + u' ' * self.indentlevel \
+ + u' ' * listindent
+ self.text = self.text \
+ + u' ' * self.indentlevel \
+ + listmarker \
+ + seperator.join( \
+ textwrap.wrap( \
+ item, \
+ self.textwidth - self.indentlevel - listindent \
+ ) \
+ )
+ self.curdata = u''
+ elif tag_thats_done == u'dt':
+ definition = unicode(" ".join( \
+ self.curdata.encode("utf-8").strip().split()), \
+ "utf-8")
+ if len(self.text) > 0 and self.text[-1] != u'\n':
+ self.text = self.text + u'\n\n'
+ elif len(self.text) > 1 and self.text[-2] != u'\n':
+ self.text = self.text + u'\n'
+ definition = u' ' * (self.indentlevel - 4) + definition + "::"
+ indentstring = u'\n' + u' ' * (self.indentlevel - 3)
+ self.text = self.text \
+ + indentstring.join(
+ textwrap.wrap(definition, \
+ self.textwidth - self.indentlevel - 4))
+ self.curdata = u''
+ elif tag_thats_done == u'dd':
+ definition = unicode(" ".join( \
+ self.curdata.encode("utf-8").strip().split()),
+ "utf-8")
+ if len(definition) > 0:
+ if len(self.text) > 0 and self.text[-1] != u'\n':
+ self.text = self.text + u'\n'
+ indentstring = u'\n' + u' ' * self.indentlevel
+ self.text = self.text \
+ + indentstring \
+ + indentstring.join( \
+ textwrap.wrap( \
+ definition, \
+ self.textwidth - self.indentlevel \
+ ) \
+ )
+ self.curdata = u''
+ elif tag_thats_done == u'a':
+ self.curdata = self.curdata + u'`__'
+ pass
+ elif tag_thats_done in self.liststarttags:
+ pass
+
+ if tag_thats_done in self.blockleveltags:
+ self.curdata = u''
+
+ self.ignorenodata = False