forked from p12tic/cppreference-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevhelp_qch.py
More file actions
113 lines (86 loc) · 4.02 KB
/
devhelp_qch.py
File metadata and controls
113 lines (86 loc) · 4.02 KB
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
#!/usr/bin/env python3
# Copyright (C) 2017 Giedrius Zitkus <elink@namusauga.lt>
# Copyright (C) 2018 Monika Kairaityte <monika@kibit.lt>
#
# This file is part of cppreference-doc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
from lxml import etree
def convert_toc_lines(source_line, in_section):
for el_sub in source_line.getchildren():
el_section_1 = etree.SubElement(in_section, 'section')
el_section_1.set('title', el_sub.get('name'))
el_section_1.set('ref', el_sub.get('link'))
if el_sub.getchildren() != []:
convert_toc_lines(el_sub, el_section_1)
return in_section
def convert_toc(in_root):
el_toc = etree.Element('toc')
el_section = etree.SubElement(el_toc, 'section')
el_section.set('title', in_root.get('title'))
el_section.set('ref', in_root.get('link'))
chapters_el = in_root[0]
if chapters_el.tag != '{http://www.devhelp.net/book}chapters':
raise Exception('Unexpected input document structure')
convert_toc_lines(chapters_el, el_section)
return el_toc
def convert_keywords(in_root_k):
el_keywords = etree.Element('keywords')
functions_el = in_root_k[1]
if functions_el.tag != '{http://www.devhelp.net/book}functions':
raise Exception('Unexpected input document structure')
for el_function in functions_el:
el_keyword = etree.SubElement(el_keywords, 'keyword')
el_keyword.set('name', el_function.get('name'))
el_keyword.set('id', el_function.get('name'))
el_keyword.set('ref', el_function.get('link'))
if el_function.get('name').startswith('std::'):
el_keyword = etree.SubElement(el_keywords, 'keyword')
el_keyword.set('name', el_function.get('name'))
# Add an additional id for libc++ users
name_without_std = el_function.get('name')[5:]
el_keyword.set('id', 'std::__LIBCPP_ABI_VERSION::' +
name_without_std)
el_keyword.set('ref', el_function.get('link'))
el_keyword = etree.SubElement(el_keywords, 'keyword')
el_keyword.set('name', el_function.get('name'))
el_keyword.set('id', 'std::__1::' + name_without_std)
el_keyword.set('ref', el_function.get('link'))
return el_keywords
# Adds files list from external library
def add_files_list(files_root_f):
el_files = etree.Element('files')
for file_item in files_root_f:
el_file = etree.SubElement(el_files, 'file')
el_file.text = file_item.text
return el_files
def convert_devhelp_to_qch(in_root, files_root, virtual_folder):
out_root = etree.Element('QtHelpProject')
out_root.set('version', "1.0")
el = etree.SubElement(out_root, 'namespace')
el.text = 'cppreference.com.' + in_root.get('name')
el = etree.SubElement(out_root, 'virtualFolder')
el.text = virtual_folder
el = etree.SubElement(out_root, 'customFilter')
el.set('name', in_root.get('title'))
el_filter = etree.SubElement(el, 'filterAttribute')
el_filter.text = in_root.get('name')
el = etree.SubElement(out_root, 'filterSection')
el_filter = etree.SubElement(el, 'filterAttribute')
el_filter.text = in_root.get('name')
el.append(convert_toc(in_root))
el.append(convert_keywords(in_root))
el.append(add_files_list(files_root))
return etree.tostring(out_root, encoding="utf-8", pretty_print=True,
xml_declaration=True)