|
| 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 output |
| 21 | +# including files list from library 'files_root' at the end. |
| 22 | + |
| 23 | +from lxml import etree |
| 24 | +from copy import deepcopy |
| 25 | +import sys |
| 26 | +import argparse |
| 27 | + |
| 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 | + el_toc.append(convert_toc_lines(in_root_t[1], el_section)) |
| 48 | + return el_toc |
| 49 | + |
| 50 | +def convert_keywords(in_root_k): |
| 51 | + el_keywords = etree.XML('<keywords/>') |
| 52 | + for el_function in in_root_k[2]: |
| 53 | + el_keyword = etree.XML('<keyword/>') |
| 54 | + el_keyword.set('name', el_function.get('name')) |
| 55 | + el_keyword.set('id', el_function.get('name')) |
| 56 | + el_keyword.set('ref', el_function.get('link')) |
| 57 | + |
| 58 | + el_keywords.append(el_keyword) |
| 59 | + if el_function.get('name').startswith('std::'): |
| 60 | + el_keyword = etree.XML('<keyword/>') |
| 61 | + el_keyword.set('name', el_function.get('name')) |
| 62 | + |
| 63 | + # Add an additional id for libc++ users |
| 64 | + name_without_std = el_function.get('name')[5:] |
| 65 | + |
| 66 | + el_keyword.set('id', 'std::__LIBCPP_ABI_VERSION::' + name_without_std) |
| 67 | + el_keyword.set('ref', el_function.get('link')) |
| 68 | + |
| 69 | + el_keywords.append(el_keyword) |
| 70 | + |
| 71 | + el_keyword = etree.XML('<keyword/>') |
| 72 | + el_keyword.set('name', el_function.get('name')) |
| 73 | + el_keyword.set('id', 'std::__1::' + name_without_std) |
| 74 | + el_keyword.set('ref', el_function.get('link')) |
| 75 | + |
| 76 | + el_keywords.append(el_keyword) |
| 77 | + return el_keywords |
| 78 | + |
| 79 | +# Adds files list from external library |
| 80 | +def add_files_list(files_root_f): |
| 81 | + el_files = etree.XML('<files/>') |
| 82 | + for file_item in files_root_f: |
| 83 | + el_file = etree.XML('<file/>') |
| 84 | + el_file.text = file_item.text |
| 85 | + el_files.append(el_file) |
| 86 | + return el_files |
| 87 | + |
| 88 | +def convert_devhelp_to_qch(in_root, files_root, out_root, virtual_folder): |
| 89 | + el = etree.XML('<namespace/>') |
| 90 | + el.text = 'cppreference.com.' + in_root.get('name') |
| 91 | + out_root.append(el) |
| 92 | + |
| 93 | + el = etree.XML('<virtualFolder/>') |
| 94 | + el.text = virtual_folder |
| 95 | + out_root.append(el) |
| 96 | + |
| 97 | + el = etree.XML('<customFilter/>') |
| 98 | + el.set('name', in_root.get('title')) |
| 99 | + el_filter = etree.XML('<filterAttribute/>') |
| 100 | + el_filter.text = in_root.get('name') |
| 101 | + el.append(el_filter) |
| 102 | + out_root.append(el) |
| 103 | + |
| 104 | + el = etree.XML('<filterSection/>') |
| 105 | + el_filter = etree.XML('<filterAttribute/>') |
| 106 | + el_filter.text = in_root.get('name') |
| 107 | + el.append(el_filter) |
| 108 | + el.append(convert_toc(in_root)) |
| 109 | + el.append(convert_keywords(in_root)) |
| 110 | + el.append(add_files_list(files_root)) |
| 111 | + out_root.append(el) |
| 112 | + |
| 113 | +def main(): |
| 114 | + parser = argparse.ArgumentParser(prog='devhelp2qch.py') |
| 115 | + parser.add_argument('--src', type=str, help='The path to the XML input file') |
| 116 | + parser.add_argument('--dst', type=str, help='The path to the destination XML file') |
| 117 | + parser.add_argument('--virtual_folder', type=str, help='Virtual folder name') |
| 118 | + parser.add_argument('--file_list', type=str, help='The path to the file list in XML file') |
| 119 | + args = parser.parse_args() |
| 120 | + |
| 121 | + src_path = args.src |
| 122 | + dst_path = args.dst |
| 123 | + v_folder = args.virtual_folder |
| 124 | + file_path = args.file_list |
| 125 | + |
| 126 | + parser = etree.XMLParser(encoding='UTF-8', recover=True) |
| 127 | + in_tree = etree.parse(src_path, parser) |
| 128 | + file_tree = etree.parse(file_path, parser) |
| 129 | + out_el = etree.XML('<QtHelpProject xmlns:devhelp="http://www.devhelp.net/book" xmlns:str="http://exslt.org/strings" version="1.0"/>') |
| 130 | + |
| 131 | + convert_devhelp_to_qch(in_tree.getroot(), file_tree.getroot(), out_el, v_folder) |
| 132 | + |
| 133 | + out_f = open(dst_path, 'wb') |
| 134 | + out_f.write(etree.tostring(out_el, encoding="utf-8", pretty_print=True, xml_declaration=True)) |
| 135 | + out_f.close() |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + main() |
0 commit comments