Skip to content

Commit b9c5d20

Browse files
committed
Devhelp: add tests for convert_devhelp_to_qch()
1 parent 592a8cd commit b9c5d20

File tree

6 files changed

+239
-99
lines changed

6 files changed

+239
-99
lines changed

devhelp2qch.py

Lines changed: 1 addition & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -25,101 +25,6 @@
2525
import sys
2626
import argparse
2727

28-
def convert_toc_lines(source_line, in_section):
29-
i = 0
30-
for el_sub in source_line.getchildren():
31-
el_section_1 = etree.XML('<section/>')
32-
el_section_1.set('title', el_sub.get('name'))
33-
el_section_1.set('ref', el_sub.get('link'))
34-
35-
if el_sub.getchildren() != []:
36-
in_section.append(convert_toc_lines(el_sub, el_section_1))
37-
else:
38-
in_section.append(el_section_1)
39-
40-
return in_section
41-
42-
def convert_toc(in_root_t):
43-
el_toc = etree.XML('<toc/>')
44-
el_section = etree.XML('<section/>')
45-
el_section.set('title', in_root_t.get('title'))
46-
el_section.set('ref', in_root_t.get('link'))
47-
48-
chapters_el = in_root_t[0]
49-
if chapters_el.tag != '{http://www.devhelp.net/book}chapters':
50-
raise Exception('Unexpected input document structure')
51-
52-
el_toc.append(convert_toc_lines(chapters_el, el_section))
53-
return el_toc
54-
55-
def convert_keywords(in_root_k):
56-
el_keywords = etree.XML('<keywords/>')
57-
58-
functions_el = in_root_k[1]
59-
if functions_el.tag != '{http://www.devhelp.net/book}functions':
60-
raise Exception('Unexpected input document structure')
61-
62-
for el_function in functions_el:
63-
el_keyword = etree.XML('<keyword/>')
64-
el_keyword.set('name', el_function.get('name'))
65-
el_keyword.set('id', el_function.get('name'))
66-
el_keyword.set('ref', el_function.get('link'))
67-
68-
el_keywords.append(el_keyword)
69-
if el_function.get('name').startswith('std::'):
70-
el_keyword = etree.XML('<keyword/>')
71-
el_keyword.set('name', el_function.get('name'))
72-
73-
# Add an additional id for libc++ users
74-
name_without_std = el_function.get('name')[5:]
75-
76-
el_keyword.set('id', 'std::__LIBCPP_ABI_VERSION::' + name_without_std)
77-
el_keyword.set('ref', el_function.get('link'))
78-
79-
el_keywords.append(el_keyword)
80-
81-
el_keyword = etree.XML('<keyword/>')
82-
el_keyword.set('name', el_function.get('name'))
83-
el_keyword.set('id', 'std::__1::' + name_without_std)
84-
el_keyword.set('ref', el_function.get('link'))
85-
86-
el_keywords.append(el_keyword)
87-
return el_keywords
88-
89-
# Adds files list from external library
90-
def add_files_list(files_root_f):
91-
el_files = etree.XML('<files/>')
92-
for file_item in files_root_f:
93-
el_file = etree.XML('<file/>')
94-
el_file.text = file_item.text
95-
el_files.append(el_file)
96-
return el_files
97-
98-
def convert_devhelp_to_qch(in_root, files_root, out_root, virtual_folder):
99-
el = etree.XML('<namespace/>')
100-
el.text = 'cppreference.com.' + in_root.get('name')
101-
out_root.append(el)
102-
103-
el = etree.XML('<virtualFolder/>')
104-
el.text = virtual_folder
105-
out_root.append(el)
106-
107-
el = etree.XML('<customFilter/>')
108-
el.set('name', in_root.get('title'))
109-
el_filter = etree.XML('<filterAttribute/>')
110-
el_filter.text = in_root.get('name')
111-
el.append(el_filter)
112-
out_root.append(el)
113-
114-
el = etree.XML('<filterSection/>')
115-
el_filter = etree.XML('<filterAttribute/>')
116-
el_filter.text = in_root.get('name')
117-
el.append(el_filter)
118-
el.append(convert_toc(in_root))
119-
el.append(convert_keywords(in_root))
120-
el.append(add_files_list(files_root))
121-
out_root.append(el)
122-
12328
def main():
12429
parser = argparse.ArgumentParser(prog='devhelp2qch.py')
12530
parser.add_argument('--src', type=str, help='The path to the XML input file')
@@ -136,12 +41,9 @@ def main():
13641
parser = etree.XMLParser(encoding='UTF-8', recover=True)
13742
in_tree = etree.parse(src_path, parser)
13843
file_tree = etree.parse(file_path, parser)
139-
out_el = etree.XML('<QtHelpProject xmlns:devhelp="http://www.devhelp.net/book" xmlns:str="http://exslt.org/strings" version="1.0"/>')
140-
141-
convert_devhelp_to_qch(in_tree.getroot(), file_tree.getroot(), out_el, v_folder)
14244

14345
out_f = open(dst_path, 'wb')
144-
out_f.write(etree.tostring(out_el, encoding="utf-8", pretty_print=True, xml_declaration=True))
46+
out_f.write(convert_devhelp_to_qch(in_tree.getroot(), file_tree.getroot(), v_folder)
14547
out_f.close()
14648

14749
if __name__ == "__main__":

index_transform/devhelp_qch.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright (C) 2017 Giedrius Zitkus <elink@namusauga.lt>
4+
#
5+
# This file is part of cppreference-doc
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this program. If not, see http://www.gnu.org/licenses/.
19+
20+
# devhelp2qch.py script converts 'in_root' xml source file to 'out_root' xml
21+
# output including files list from library 'files_root' at the end.
22+
23+
from lxml import etree
24+
25+
def convert_toc_lines(source_line, in_section):
26+
i = 0
27+
for el_sub in source_line.getchildren():
28+
el_section_1 = etree.XML('<section/>')
29+
el_section_1.set('title', el_sub.get('name'))
30+
el_section_1.set('ref', el_sub.get('link'))
31+
32+
if el_sub.getchildren() != []:
33+
in_section.append(convert_toc_lines(el_sub, el_section_1))
34+
else:
35+
in_section.append(el_section_1)
36+
37+
return in_section
38+
39+
def convert_toc(in_root_t):
40+
el_toc = etree.XML('<toc/>')
41+
el_section = etree.XML('<section/>')
42+
el_section.set('title', in_root_t.get('title'))
43+
el_section.set('ref', in_root_t.get('link'))
44+
45+
chapters_el = in_root_t[0]
46+
if chapters_el.tag != '{http://www.devhelp.net/book}chapters':
47+
raise Exception('Unexpected input document structure')
48+
49+
el_toc.append(convert_toc_lines(chapters_el, el_section))
50+
return el_toc
51+
52+
def convert_keywords(in_root_k):
53+
el_keywords = etree.XML('<keywords/>')
54+
55+
functions_el = in_root_k[1]
56+
if functions_el.tag != '{http://www.devhelp.net/book}functions':
57+
raise Exception('Unexpected input document structure')
58+
59+
for el_function in functions_el:
60+
el_keyword = etree.XML('<keyword/>')
61+
el_keyword.set('name', el_function.get('name'))
62+
el_keyword.set('id', el_function.get('name'))
63+
el_keyword.set('ref', el_function.get('link'))
64+
65+
el_keywords.append(el_keyword)
66+
if el_function.get('name').startswith('std::'):
67+
el_keyword = etree.XML('<keyword/>')
68+
el_keyword.set('name', el_function.get('name'))
69+
70+
# Add an additional id for libc++ users
71+
name_without_std = el_function.get('name')[5:]
72+
73+
el_keyword.set('id', 'std::__LIBCPP_ABI_VERSION::' +
74+
name_without_std)
75+
el_keyword.set('ref', el_function.get('link'))
76+
77+
el_keywords.append(el_keyword)
78+
79+
el_keyword = etree.XML('<keyword/>')
80+
el_keyword.set('name', el_function.get('name'))
81+
el_keyword.set('id', 'std::__1::' + name_without_std)
82+
el_keyword.set('ref', el_function.get('link'))
83+
84+
el_keywords.append(el_keyword)
85+
return el_keywords
86+
87+
# Adds files list from external library
88+
def add_files_list(files_root_f):
89+
el_files = etree.XML('<files/>')
90+
for file_item in files_root_f:
91+
el_file = etree.XML('<file/>')
92+
el_file.text = file_item.text
93+
el_files.append(el_file)
94+
return el_files
95+
96+
def convert_devhelp_to_qch(in_root, files_root, virtual_folder):
97+
out_root = etree.XML('<QtHelpProject ' +
98+
'xmlns:devhelp="http://www.devhelp.net/book" ' +
99+
'xmlns:str="http://exslt.org/strings" version="1.0"/>')
100+
el = etree.XML('<namespace/>')
101+
el.text = 'cppreference.com.' + in_root.get('name')
102+
out_root.append(el)
103+
104+
el = etree.XML('<virtualFolder/>')
105+
el.text = virtual_folder
106+
out_root.append(el)
107+
108+
el = etree.XML('<customFilter/>')
109+
el.set('name', in_root.get('title'))
110+
el_filter = etree.XML('<filterAttribute/>')
111+
el_filter.text = in_root.get('name')
112+
el.append(el_filter)
113+
out_root.append(el)
114+
115+
el = etree.XML('<filterSection/>')
116+
el_filter = etree.XML('<filterAttribute/>')
117+
el_filter.text = in_root.get('name')
118+
el.append(el_filter)
119+
el.append(convert_toc(in_root))
120+
el.append(convert_keywords(in_root))
121+
el.append(add_files_list(files_root))
122+
out_root.append(el)
123+
124+
return etree.tostring(out_root, encoding="utf-8", pretty_print=True,
125+
xml_declaration=True)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version='1.0' encoding='utf-8'?>
2+
<book xmlns="http://www.devhelp.net/book" title="C++ Standard Library reference" name="cppreference-doc-en-cpp" base="/usr/share/cppreference/doc/html" link="en/cpp.html" version="2" language="c++">
3+
<chapters xmlns="http://www.devhelp.net/book">
4+
<sub name="Language" link="en/path1.html"/>
5+
</chapters>
6+
<functions>
7+
<keyword type="typedef" name="std::size_t" link="en/path2.html"/>
8+
<keyword type="macro" name="NULL" link="en/path3.html"/>
9+
<keyword type="function" name="offsetof" link="en/path4.html"/>
10+
<keyword type="class" name="std::numeric_limits" link="en/path5.html"/>
11+
<keyword type="enum" name="std::float_denorm_style" link="en/path6.html"/>
12+
<keyword type="" name="std::is_copy_constructible_v" link="en/path7.html"/>
13+
</functions>
14+
</book>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version='1.0' encoding='utf-8'?>
2+
<QtHelpProject xmlns:devhelp="http://www.devhelp.net/book" xmlns:str="http://exslt.org/strings" version="1.0">
3+
<namespace>cppreference.com.cppreference-doc-en-cpp</namespace>
4+
<virtualFolder>virtual_folder</virtualFolder>
5+
<customFilter name="C++ Standard Library reference">
6+
<filterAttribute>cppreference-doc-en-cpp</filterAttribute>
7+
</customFilter>
8+
<filterSection>
9+
<filterAttribute>cppreference-doc-en-cpp</filterAttribute>
10+
<toc>
11+
<section title="C++ Standard Library reference" ref="en/cpp.html">
12+
<section title="Language" ref="en/path1.html"/>
13+
</section>
14+
</toc>
15+
<keywords>
16+
<keyword name="std::size_t" id="std::size_t" ref="en/path2.html"/>
17+
<keyword name="std::size_t" id="std::__LIBCPP_ABI_VERSION::size_t" ref="en/path2.html"/>
18+
<keyword name="std::size_t" id="std::__1::size_t" ref="en/path2.html"/>
19+
<keyword name="NULL" id="NULL" ref="en/path3.html"/>
20+
<keyword name="offsetof" id="offsetof" ref="en/path4.html"/>
21+
<keyword name="std::numeric_limits" id="std::numeric_limits" ref="en/path5.html"/>
22+
<keyword name="std::numeric_limits" id="std::__LIBCPP_ABI_VERSION::numeric_limits" ref="en/path5.html"/>
23+
<keyword name="std::numeric_limits" id="std::__1::numeric_limits" ref="en/path5.html"/>
24+
<keyword name="std::float_denorm_style" id="std::float_denorm_style" ref="en/path6.html"/>
25+
<keyword name="std::float_denorm_style" id="std::__LIBCPP_ABI_VERSION::float_denorm_style" ref="en/path6.html"/>
26+
<keyword name="std::float_denorm_style" id="std::__1::float_denorm_style" ref="en/path6.html"/>
27+
<keyword name="std::is_copy_constructible_v" id="std::is_copy_constructible_v" ref="en/path7.html"/>
28+
<keyword name="std::is_copy_constructible_v" id="std::__LIBCPP_ABI_VERSION::is_copy_constructible_v" ref="en/path7.html"/>
29+
<keyword name="std::is_copy_constructible_v" id="std::__1::is_copy_constructible_v" ref="en/path7.html"/>
30+
</keywords>
31+
<files>
32+
<file>./en/path1.html</file>
33+
<file>./en/path2.html</file>
34+
<file>./en/path3.html</file>
35+
<file>./en/path4.html</file>
36+
<file>./en/path5.html</file>
37+
<file>./en/path6.html</file>
38+
<file>./en/path7.html</file>
39+
</files>
40+
</filterSection>
41+
</QtHelpProject>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?><files>
2+
<file>./en/path1.html</file>
3+
<file>./en/path2.html</file>
4+
<file>./en/path3.html</file>
5+
<file>./en/path4.html</file>
6+
<file>./en/path5.html</file>
7+
<file>./en/path6.html</file>
8+
<file>./en/path7.html</file>
9+
</files>

tests/test_devhelp_qch.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright (C) 2018 Monika Kairaityte <monika@kibit.lt>
2+
#
3+
# This file is part of cppreference-doc
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see http://www.gnu.org/licenses/.
17+
18+
from lxml import etree
19+
import os
20+
import unittest
21+
from index_transform.devhelp_qch import *
22+
23+
class TestConvertDevhelpToQch(unittest.TestCase):
24+
def test_convert_devhelp_to_qch(self):
25+
dir_path = os.path.dirname(__file__)
26+
index_fn = os.path.join(dir_path,
27+
'devhelp_qch_data/devhelp-index.xml')
28+
file_list_fn = os.path.join(dir_path,
29+
'devhelp_qch_data/file-list.xml')
30+
expected_path = os.path.join(dir_path, 'devhelp_qch_data/expected.xml')
31+
dest_path = os.path.join(dir_path, 'devhelp_qch_data/result.xml')
32+
33+
parser = etree.XMLParser(encoding='UTF-8', recover=True)
34+
in_tree = etree.parse(index_fn, parser)
35+
in_root = in_tree.getroot()
36+
37+
file_tree = etree.parse(file_list_fn, parser)
38+
files_root = file_tree.getroot()
39+
40+
with open(expected_path, 'rb') as expected_f:
41+
expected = expected_f.read()
42+
43+
result = convert_devhelp_to_qch(in_root, files_root, 'virtual_folder')
44+
45+
if expected != result:
46+
with open(dest_path, 'wb') as result_f:
47+
result_f.write(result)
48+
49+
self.assertEqual(expected, result)

0 commit comments

Comments
 (0)