|
| 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) |
0 commit comments