qthelp_generator.py 5.84 KB
Newer Older
Shubham  .'s avatar
Shubham . committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#
#     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
Shubham  .'s avatar
Shubham . committed
25
import re
26
from bs4 import BeautifulSoup
Shubham  .'s avatar
Shubham . committed
27
28
29
30
31

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

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

Shubham  .'s avatar
Shubham . committed
35
36
37
38
39
40
41
42
43
44
#######################################
#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>
Shubham  .'s avatar
Shubham . committed
45

Shubham  .'s avatar
Shubham . committed
46
47
48
49
    <customFilter name="Python">
        <filterAttribute>Python Documentation</filterAttribute>
        <filterAttribute>3.8.4</filterAttribute>
    </customFilter>
Shubham  .'s avatar
Shubham . committed
50

Shubham  .'s avatar
Shubham . committed
51
52
    <filterSection>
        <toc>
Shubham  .'s avatar
Shubham . committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
            <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>
Shubham  .'s avatar
Shubham . committed
67
68
                </section>

Shubham  .'s avatar
Shubham . committed
69
70
71
72
73
74
                <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>
Shubham  .'s avatar
Shubham . committed
75
76
77
78
79
80
                </section>

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

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

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

Shubham  .'s avatar
Shubham . committed
91
92
93
    if keyword != "":
        line = '<keyword name = "{}" ref = "{}"/>\n'.format(keyword, link)
        qhp.write(line)
94
95
96
97
98

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

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

Shubham  .'s avatar
Shubham . committed
104
105
106
    if keyword != "":
        line = '<keyword name = "{}" ref = "{}"/>\n'.format(keyword, link)
        qhp.write(line)
Shubham  .'s avatar
Shubham . committed
107
108
109
110

# write the tail
qhp.writelines("""</keywords>
            <files>
Shubham  .'s avatar
Shubham . committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
            <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>
Shubham  .'s avatar
Shubham . committed
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
        </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()
159
160
index.close()
index2.close()