-
Notifications
You must be signed in to change notification settings - Fork 2
/
docmaker.py
executable file
·173 lines (139 loc) · 5.87 KB
/
docmaker.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#! /usr/bin/env python
import json, sys, os, os.path
from distutils.dir_util import copy_tree
from jinja2 import Environment,Template,FileSystemLoader
from xml.etree import ElementTree
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
exe = os.path.realpath(sys.argv[0])
template_dir = os.path.dirname(exe) + '/template'
resource_dir = os.path.dirname(exe) + '/resources'
env = Environment(loader=FileSystemLoader(template_dir))
class DocIndex():
json = {}
def __init__(self, xml):
self.xml = xml
def parse(self):
self.json = {
'site_title': self.xml.get('title'),
'author': 'Papyros',
'modules': [self.parse_module(module) for module in self.xml.findall('./namespace')]
}
def parse_module(self, xml):
return {
'name': xml.get('module'),
'classes': self.parse_list(xml, 'qmlclass', self.parse_class,
key=lambda cls: cls['name'])
}
def parse_class(self, xml):
return {
'name': xml.get('name'),
'summary': xml.get('brief'),
'url': xml.get('href').replace('.dita', '.html')
}
def render(self):
template = env.get_template('index.html')
return template.render(self.json)
def parse_list(self, xml, tag, function, key):
items = [function(item) for item in xml.findall('./' + tag)]
return sorted(items, key=key)
class Docfile():
json = {}
def __init__(self, xml):
self.xml = xml
def parse(self):
self.json = {
'title': self.xml.findtext('./apiName'),
'site_title': self.xml.findtext('./prolog/metadata/prodinfo/prodname'),
'author': 'Papyros',
'description': '',
'class': {
'name': self.xml.findtext('./apiName'),
'summary': self.xml.findtext('./shortdesc'),
'description': format_xml(self.xml.find('./qmlTypeDetail/apiDesc')),
'import': ('import ' + self.xml.findtext('./qmlTypeDetail/qmlImportModule/apiItemName') +
' ' + self.xml.findtext('./qmlTypeDetail/qmlImportModule/apiData')),
'properties': self.parse_list('qmlProperty', self.parse_property,
key=lambda property: property['name']),
'methods': self.parse_list('qmlMethod', self.parse_method,
key=lambda method: method['name']),
'signals': self.parse_list('qmlSignal', self.parse_signal,
key=lambda signal: signal['name'])
}
}
def parse_property(self, xml):
return {
'id': xml.get('id'),
'name': format_xml(xml.find('./qmlPropertyDetail/qmlPropertyDef/apiData')),
'description': format_xml(xml.find('./qmlPropertyDetail/apiDesc'))
}
def parse_method(self, xml):
return {
'id': xml.get('id'),
'name': format_xml(xml.find('./qmlMethodDetail/qmlMethodDef/apiData')),
'description': format_xml(xml.find('./qmlMethodDetail/apiDesc'))
}
def parse_signal(self, xml):
return {
'id': xml.get('id'),
'name': format_xml(xml.find('./qmlSignalDetail/qmlSignalDef/apiData')),
'description': format_xml(xml.find('./qmlSignalDetail/apiDesc'))
}
def render(self):
template = env.get_template('qmltype.html')
return template.render(self.json)
def parse_list(self, tag, function, key):
items = [function(item) for item in self.xml.findall('./' + tag)]
return sorted(items, key=key)
def format_code(code, language):
lexer = get_lexer_by_name(language, stripall=True)
formatter = HtmlFormatter()
return highlight(code, lexer, formatter)
def format_xml(xml):
if xml is None:
return None
for item in xml.iter('xref'):
item.tag = 'a'
item.set('href', item.get('href').replace('.dita', '.html'))
for item in xml.iter('codeblock'):
tail = item.tail
code = ''.join(item.itertext())
language = item.get('outputclass')
item.tag = 'blockquote'
item.clear()
item.tail = tail
item.append(ElementTree.fromstring(format_code(code, language)))
for item in xml.iter('fig'):
src = item.find('./image').get('href')
caption = item.findtext('./image/alt')
tail = item.tail
item.clear()
item.tag = 'img'
item.set('src', src)
item.set('class', 'materialboxed')
item.set('data-caption', caption)
item.tail = tail
elements = [ElementTree.tostring(item).decode("utf-8") for item in xml]
return xml.text + ''.join(elements)
if __name__=='__main__':
out_dir = sys.argv[2]
copy_tree(resource_dir, out_dir)
copy_tree(sys.argv[1] + '/images', out_dir + '/images')
for filename in os.listdir(sys.argv[1]):
if filename.endswith('.dita'):
try:
with open(sys.argv[1] + '/' + filename) as f:
docfile = Docfile(ElementTree.parse(f))
docfile.parse()
html_file = filename.replace('.dita', '.html')
with open(out_dir + '/' + html_file, 'w') as out:
out.write(docfile.render().encode('utf8'))
except Exception as e:
print("Unable to render " + filename + ": " + str(e))
elif filename.endswith('.index'):
with open(sys.argv[1] + '/' + filename) as f:
docindex = DocIndex(ElementTree.parse(f).getroot())
docindex.parse()
with open(out_dir + '/index.html', 'w') as out:
out.write(docindex.render().encode('utf8'))