-
Notifications
You must be signed in to change notification settings - Fork 10
/
parse.py
61 lines (46 loc) · 1.78 KB
/
parse.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
# -*- coding: utf-8 -*-
from gettext import gettext as _
from BeautifulSoup import BeautifulSoup
def parse_dita(dita_str):
soup = BeautifulSoup(dita_str)
html = open('article.html', 'r').read().decode('utf-8')
html_tags = []
title = soup.find('title').string.strip()
h1_title = '<h1>%(title)s</h1>' % \
{'title': title}
index_link = '<p><a href="librarymap.html">' + \
_('Return to index') + '</a></p>'
html_tags.append(index_link)
html_tags.append(h1_title)
for section in soup.findAll('section'):
for p in section.findAll('p'):
images = p.findAll('image')
for img in images:
html_tags.append('<img src="%(src)s" />' % \
{'src': img.get('href')})
html_tags.append('<p>')
for ph in p.findAll('ph'):
html_tags.append(ph.string.strip())
html_tags.append('</p>')
html = html % {'title': title,
'body': '\n'.join(html_tags)}
return html
def parse_ditamap(ditamap_str):
soup = BeautifulSoup(ditamap_str)
html = open('article.html', 'r').read().decode('utf-8')
html_tags = []
title = soup.find('map').get('title')
h1_title = '<h1>%(title)s</h1>' % \
{'title': title}
html_tags.append(h1_title)
html_tags.append('<li>')
for topic in soup.findAll('topicref'):
dita_path = topic.get('href')
html_tags.append('<ul><a href="%(href)s">%(name)s</a></ul>' % \
{'href': dita_path.replace('.dita', '.html'),
'name': topic.get('navtitle')})
html_tags.append('</li>')
html = html % {'title': title,
'body': '\n'.join(html_tags)}
return html