-
Notifications
You must be signed in to change notification settings - Fork 2
/
formatter-example-3.py
44 lines (44 loc) · 1.41 KB
/
formatter-example-3.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
import formatter
import htmllib, string
class Writer(formatter.DumbWriter):
def __init__(self):
formatter.DumbWriter.__init__(self)
self.tag = ""
self.bold = self.italic = 0
self.fonts = []
def new_font(self, font):
if font is None:
font = self.fonts.pop()
self.tag, self.bold, self.italic = font
else:
self.fonts.append((self.tag, self.bold, self.italic))
tag, bold, italic, typewriter = font
if tag is not None:
self.tag = tag
if bold is not None:
self.bold = bold
if italic is not None:
self.italic = italic
def send_flowing_data(self, data):
if not data:
return
atbreak = self.atbreak or data[0] in string.whitespace
for word in string.split(data):
if atbreak:
self.file.write(" ")
if self.tag in ("h1", "h2", "h3"):
word = string.upper(word)
if self.bold:
word = "*" + word + "*"
if self.italic:
word = "_" + word + "_"
self.file.write(word)
atbreak = 1
self.atbreak = data[-1] in string.whitespace
w = Writer()
f = formatter.AbstractFormatter(w)
file = open("samples/sample.htm")
# print html body as plain text
p = htmllib.HTMLParser(f)
p.feed(file.read())
p.close()