-
Notifications
You must be signed in to change notification settings - Fork 0
/
tparser.py
36 lines (31 loc) · 1.05 KB
/
tparser.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
import os
from HTMLParser import HTMLParser
class TextParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self._pretty = ''
self._textout = ''
self.paragraph = False
def handle_starttag(self, tag, attrs):
if tag == 'img':
for x in attrs:
if x[0] == 'src':
self._pretty += '<img src="{}" />'.format(x[1])
self._textout += ':{}:'.format(x[1].split(os.path.sep)[-1].split('.')[0])
elif tag == 'p':
self.paragraph = True
def handle_endtag(self, tag):
if tag == 'p':
self._pretty += '\n'
self._textout += '\n'
self.paragraph = False
def handle_data(self, data):
if self.paragraph:
self._pretty += data
self._textout += data
def produce(self):
temp = self._textout.strip()
temp2 = self._pretty.strip()
self._pretty = ''
self._textout = ''
return temp, temp2