Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import formatter
  2. import htmllib, string
  3.  
  4. class Writer(formatter.DumbWriter):
  5.  
  6. def __init__(self):
  7. formatter.DumbWriter.__init__(self)
  8. self.tag = ""
  9. self.bold = self.italic = 0
  10. self.fonts = []
  11.  
  12. def new_font(self, font):
  13. if font is None:
  14. font = self.fonts.pop()
  15. self.tag, self.bold, self.italic = font
  16. else:
  17. self.fonts.append((self.tag, self.bold, self.italic))
  18. tag, bold, italic, typewriter = font
  19. if tag is not None:
  20. self.tag = tag
  21. if bold is not None:
  22. self.bold = bold
  23. if italic is not None:
  24. self.italic = italic
  25.  
  26. def send_flowing_data(self, data):
  27. if not data:
  28. return
  29. atbreak = self.atbreak or data[0] in string.whitespace
  30. for word in string.split(data):
  31. if atbreak:
  32. self.file.write(" ")
  33. if self.tag in ("h1", "h2", "h3"):
  34. word = string.upper(word)
  35. if self.bold:
  36. word = "*" + word + "*"
  37. if self.italic:
  38. word = "_" + word + "_"
  39. self.file.write(word)
  40. atbreak = 1
  41. self.atbreak = data[-1] in string.whitespace
  42.  
  43. w = Writer()
  44. f = formatter.AbstractFormatter(w)
  45.  
  46. file = open("samples/sample.htm")
  47.  
  48. # print html body as plain text
  49. p = htmllib.HTMLParser(f)
  50. p.feed(file.read())
  51. p.close()
  52.  
  53. ## _A_ _CHAPTER._
  54. ##
  55. ## Some text. Some more text. Some *emphasised* text. A link[1].
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement