File: qthelp_generator.py

package info (click to toggle)
cantor 4%3A25.04.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,828 kB
  • sloc: cpp: 43,775; ansic: 5,802; pascal: 1,027; python: 642; xml: 457; sh: 308; makefile: 207; lisp: 91
file content (160 lines) | stat: -rw-r--r-- 5,977 bytes parent folder | download | duplicates (2)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#
#     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 2
#     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, write to the Free Software
#     Foundation, Inc., 51 Franklin Street, Fifth Floor,
#     Boston, MA  02110-1301, USA.

#     ---
#     Copyright (C) 2020 Shubham <aryan100jangid@gmail.com>
#

# Script to parse index and generate a txt file containing all the keywords
# and then generate QtHelp files using the keywords generated

import os
import re
from bs4 import BeautifulSoup

# QtHelp files
qhp = open('./help.qhp', 'w')
qhcp = open('./help.qhcp', 'w')

index = open("genindex-all.html", "r")
index2 = open("genindex-_.html", "r")

#######################################
#code for generation of QtHelp files##
######################################

# populate qhp file with headers and table of contents
qhp.writelines("""<?xml version="1.0" encoding="UTF-8"?>
<QtHelpProject version="1.0">
    <namespace>org.kde.python.3.8.4</namespace>

    <virtualFolder>doc</virtualFolder>

    <customFilter name="Python">
        <filterAttribute>Python Documentation</filterAttribute>
        <filterAttribute>3.8.4</filterAttribute>
    </customFilter>

    <filterSection>
        <toc>
            <section title="Python 3.8.4 Documentation" ref="index.html">

                <section title="Parts of the documentation:" ref="index.html">
                    <section title="What's new in Python 3.8?" ref="whatsnew/3.8.html"></section>
                    <section title="Tutorial" ref="tutorial/index.html"></section>
                    <section title="Library Reference" ref="library/index.html"></section>
                    <section title="Language Reference" ref="reference/index.html"></section>
                    <section title="Python Setup and Usage" ref="using/index.html"></section>
                    <section title="Python HOWTOs" ref="howto/index.html"></section>
                    <section title="Installing Python Modules" ref="installing/index.html"></section>
                    <section title="Distributing Python Modules" ref="distributing/index.html"></section>
                    <section title="Extending and Embedding" ref="extending/index.html"></section>
                    <section title="Python/C API" ref="c-api/index.html"></section>
                    <section title="FAQs" ref="faq/index.html"></section>
                </section>

                <section title="Indices and tables:" ref="index.html">
                    <section title="Global Module Index" ref="py-modindex.html"></section>
                    <section title="General Index" ref="genindex.html"></section>
                    <section title="Glossary" ref="glossary.html"></section>
                    <section title="Search page" ref="search.html"></section>
                    <section title="Complete Table of Contents" ref="contents.html"></section>
                </section>

            </section>
        </toc>\n
        <keywords>""")

# code to write keywords to qhp file
html = index.read()
soup = BeautifulSoup(html, features='html.parser')

for i in soup.find_all('a'):
    # replace the characters which produces error while qhcp file
    keyword = i.text.replace("<", "").replace("&", "")
    keyword = re.sub(r" ?\([^)]+\)", "", keyword)
    link = i['href']

    if keyword != "":
        line = '<keyword name = "{}" ref = "{}"/>\n'.format(keyword, link)
        qhp.write(line)

html2 = index2.read()
soup2 = BeautifulSoup(html, features='html.parser')

for i in soup2.find_all('a'):
    # replace the characters which produces error while qhcp file
    keyword = i.text.replace("<", "").replace("&", "")
    keyword = re.sub(r" ?\([^)]+\)", "", keyword)
    link = i['href']

    if keyword != "":
        line = '<keyword name = "{}" ref = "{}"/>\n'.format(keyword, link)
        qhp.write(line)

# write the tail
qhp.writelines("""</keywords>
            <files>
            <file>./*</file>
            <file>_images/*</file>
            <file>_sources/*</file>
            <file>_static/*</file>
            <file>c-api/*</file>
            <file>distributing/*</file>
            <file>distutils/*</file>
            <file>extending/*</file>
            <file>faq/*</file>
            <file>howto/*</file>
            <file>install/*</file>
            <file>installing/*</file>
            <file>library/*</file>
            <file>reference/*</file>
            <file>tutorial/*</file>
            <file>using/*</file>
            <file>whatsnew/*</file>
        </files>
    </filterSection>
</QtHelpProject> """)

# populate qhcp file
qhcp.writelines("""<?xml version="1.0" encoding="utf-8" ?>
            <QHelpCollectionProject version="1.0">

                <docFiles>
                    <generate>
                        <file>
                            <input>help.qhp</input>
                            <output>help.qch</output>
                        </file>
                    </generate>

                    <register>
                        <file>help.qch</file>
                    </register>
                </docFiles>

            </QHelpCollectionProject>""")

##############################################################
#qhp, qhcp input files are generate, now generate output files
#############################################################

stream = os.popen('qhelpgenerator help.qhcp -o help.qhc')

qhp.close()
qhcp.close()
index.close()
index2.close()