11#!/usr/bin/env python3
22
33# Copyright (C) 2017 Giedrius Zitkus <elink@namusauga.lt>
4+ # Copyright (C) 2018 Monika Kairaityte <monika@kibit.lt>
45#
56# This file is part of cppreference-doc
67#
1718# You should have received a copy of the GNU General Public License
1819# along with this program. If not, see http://www.gnu.org/licenses/.
1920
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-
2321from lxml import etree
2422
2523def convert_toc_lines (source_line , in_section ):
26- i = 0
2724 for el_sub in source_line .getchildren ():
28- el_section_1 = etree .XML ( '< section/> ' )
25+ el_section_1 = etree .SubElement ( in_section , ' section' )
2926 el_section_1 .set ('title' , el_sub .get ('name' ))
3027 el_section_1 .set ('ref' , el_sub .get ('link' ))
3128
3229 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 )
30+ convert_toc_lines (el_sub , el_section_1 )
3631
3732 return in_section
3833
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' ))
34+ def convert_toc (in_root ):
35+ el_toc = etree .Element ( ' toc' )
36+ el_section = etree .SubElement ( el_toc , ' section' )
37+ el_section .set ('title' , in_root .get ('title' ))
38+ el_section .set ('ref' , in_root .get ('link' ))
4439
45- chapters_el = in_root_t [0 ]
40+ chapters_el = in_root [0 ]
4641 if chapters_el .tag != '{http://www.devhelp.net/book}chapters' :
4742 raise Exception ('Unexpected input document structure' )
4843
49- el_toc . append ( convert_toc_lines (chapters_el , el_section ) )
44+ convert_toc_lines (chapters_el , el_section )
5045 return el_toc
5146
5247def convert_keywords (in_root_k ):
53- el_keywords = etree .XML ( '< keywords/> ' )
48+ el_keywords = etree .Element ( ' keywords' )
5449
5550 functions_el = in_root_k [1 ]
5651 if functions_el .tag != '{http://www.devhelp.net/book}functions' :
5752 raise Exception ('Unexpected input document structure' )
5853
5954 for el_function in functions_el :
60- el_keyword = etree .XML ( '< keyword/> ' )
55+ el_keyword = etree .SubElement ( el_keywords , ' keyword' )
6156 el_keyword .set ('name' , el_function .get ('name' ))
6257 el_keyword .set ('id' , el_function .get ('name' ))
6358 el_keyword .set ('ref' , el_function .get ('link' ))
6459
65- el_keywords .append (el_keyword )
6660 if el_function .get ('name' ).startswith ('std::' ):
67- el_keyword = etree .XML ( '< keyword/> ' )
61+ el_keyword = etree .SubElement ( el_keywords , ' keyword' )
6862 el_keyword .set ('name' , el_function .get ('name' ))
6963
7064 # Add an additional id for libc++ users
@@ -74,52 +68,41 @@ def convert_keywords(in_root_k):
7468 name_without_std )
7569 el_keyword .set ('ref' , el_function .get ('link' ))
7670
77- el_keywords .append (el_keyword )
78-
79- el_keyword = etree .XML ('<keyword/>' )
71+ el_keyword = etree .SubElement (el_keywords , 'keyword' )
8072 el_keyword .set ('name' , el_function .get ('name' ))
8173 el_keyword .set ('id' , 'std::__1::' + name_without_std )
8274 el_keyword .set ('ref' , el_function .get ('link' ))
8375
84- el_keywords .append (el_keyword )
8576 return el_keywords
8677
8778# Adds files list from external library
8879def add_files_list (files_root_f ):
89- el_files = etree .XML ( '< files/> ' )
80+ el_files = etree .Element ( ' files' )
9081 for file_item in files_root_f :
91- el_file = etree .XML ( '< file/> ' )
82+ el_file = etree .SubElement ( el_files , ' file' )
9283 el_file .text = file_item .text
93- el_files .append (el_file )
9484 return el_files
9585
9686def 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/>' )
87+ out_root = etree .Element ('QtHelpProject' )
88+ out_root .set ('version' , "1.0" )
89+ el = etree .SubElement (out_root , 'namespace' )
10190 el .text = 'cppreference.com.' + in_root .get ('name' )
102- out_root .append (el )
10391
104- el = etree .XML ( '< virtualFolder/> ' )
92+ el = etree .SubElement ( out_root , ' virtualFolder' )
10593 el .text = virtual_folder
106- out_root .append (el )
10794
108- el = etree .XML ( '< customFilter/> ' )
95+ el = etree .SubElement ( out_root , ' customFilter' )
10996 el .set ('name' , in_root .get ('title' ))
110- el_filter = etree .XML ( '< filterAttribute/> ' )
97+ el_filter = etree .SubElement ( el , ' filterAttribute' )
11198 el_filter .text = in_root .get ('name' )
112- el .append (el_filter )
113- out_root .append (el )
11499
115- el = etree .XML ( '< filterSection/> ' )
116- el_filter = etree .XML ( '< filterAttribute/> ' )
100+ el = etree .SubElement ( out_root , ' filterSection' )
101+ el_filter = etree .SubElement ( el , ' filterAttribute' )
117102 el_filter .text = in_root .get ('name' )
118- el .append (el_filter )
119103 el .append (convert_toc (in_root ))
120104 el .append (convert_keywords (in_root ))
121105 el .append (add_files_list (files_root ))
122- out_root .append (el )
123106
124107 return etree .tostring (out_root , encoding = "utf-8" , pretty_print = True ,
125- xml_declaration = True )
108+ xml_declaration = True )
0 commit comments