~ubuntu-branches/ubuntu/utopic/freecad/utopic-proposed

« back to all changes in this revision

Viewing changes to src/Tools/offlinedoc/buildqhelp.py

  • Committer: Package Import Robot
  • Author(s): Anton Gladky
  • Date: 2014-07-22 20:15:18 UTC
  • mfrom: (30.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20140722201518-szma903mba26tphe
Tags: 0.14.3702+dfsg-1
* [7fc727b] Imported Upstream version 0.14.3702
* [3edf0de] Refresh patches.
* [077b63a] Redirect unittest output into stdout. Fixes autopkgtests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
#***************************************************************************
4
 
#*                                                                         *
5
 
#*   Copyright (c) 2009 Yorik van Havre <yorik@uncreated.net>              *  
6
 
#*                                                                         *
7
 
#*   This program is free software; you can redistribute it and/or modify  *
8
 
#*   it under the terms of the GNU Lesser General Public License (LGPL)    *
9
 
#*   as published by the Free Software Foundation; either version 2 of     *
10
 
#*   the License, or (at your option) any later version.                   *
11
 
#*   for detail see the LICENCE text file.                                 *
12
 
#*                                                                         *
13
 
#*   This program is distributed in the hope that it will be useful,       *
14
 
#*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15
 
#*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16
 
#*   GNU Library General Public License for more details.                  *
17
 
#*                                                                         *
18
 
#*   You should have received a copy of the GNU Library General Public     *
19
 
#*   License along with this program; if not, write to the Free Software   *
20
 
#*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
21
 
#*   USA                                                                   *
22
 
#*                                                                         *
23
 
#***************************************************************************
24
 
 
25
 
__title__="wiki2qhelp"
26
 
__author__ = "Yorik van Havre <yorik@uncreated.net>"
27
 
__url__ = "http://www.freecadweb.org"
28
 
 
29
 
"""
30
 
This script builds qhrlp files from a local copy of the wiki
31
 
"""
32
 
 
33
 
import sys, os, re, tempfile, getopt, shutil
34
 
from urllib2 import urlopen, HTTPError
35
 
 
36
 
#    CONFIGURATION       #################################################
37
 
 
38
 
FOLDER = "./localwiki"
39
 
INDEX = "Online_Help_Toc" # the start page from where to crawl the wiki
40
 
VERBOSE = True # to display what's going on. Otherwise, runs totally silent.
41
 
QHELPCOMPILER = 'qhelpgenerator'
42
 
QCOLLECTIOMGENERATOR = 'qcollectiongenerator'
43
 
RELEASE = '0.14'
44
 
 
45
 
#    END CONFIGURATION      ##############################################
46
 
 
47
 
fcount = dcount = 0
48
 
 
49
 
def crawl():
50
 
    "downloads an entire wiki site"
51
 
 
52
 
    # tests ###############################################
53
 
    
54
 
    if os.system(QHELPCOMPILER +' -v'):
55
 
        print "Error: QAssistant not fully installed, exiting."
56
 
        return 1
57
 
    if os.system(QCOLLECTIOMGENERATOR +' -v'):
58
 
        print "Error: QAssistant not fully installed, exiting."
59
 
        return 1
60
 
 
61
 
    # run ########################################################
62
 
 
63
 
    qhp = buildtoc()
64
 
    qhcp = createCollProjectFile()
65
 
    if generate(qhcp) or compile(qhp):
66
 
        print "Error at compiling"
67
 
        return 1
68
 
    if VERBOSE: print "All done!"
69
 
    i=raw_input("Copy the files to their correct location in the source tree? y/n (default=no) ")
70
 
    if i.upper() in ["Y","YES"]:
71
 
        shutil.copy("localwiki/freecad.qch","../../Doc/freecad.qch")
72
 
        shutil.copy("localwiki/freecad.qhc","../../Doc/freecad.qhc")
73
 
    else:
74
 
        print 'Files are in localwiki. Test with "assistant -collectionFile localwiki/freecad.qhc"'
75
 
    return 0
76
 
    
77
 
def compile(qhpfile):
78
 
    "compiles the whole html doc with qassistant"
79
 
    qchfile = FOLDER + os.sep + "freecad.qch"
80
 
    if not os.system(QHELPCOMPILER + ' '+qhpfile+' -o '+qchfile):
81
 
        if VERBOSE: print "Successfully created",qchfile
82
 
        return 0
83
 
 
84
 
def generate(qhcpfile):
85
 
    "generates qassistant-specific settings like icon, title, ..."
86
 
    txt="""
87
 
<center>FreeCAD """+RELEASE+""" help files<br/>
88
 
<a href="http://www.freecadweb.org">http://www.freecadweb.org</a></center>
89
 
    """
90
 
    about=open(FOLDER + os.sep + "about.txt","w")
91
 
    about.write(txt)
92
 
    about.close()
93
 
    qhcfile = FOLDER + os.sep + "freecad.qhc"
94
 
    if not os.system(QCOLLECTIOMGENERATOR+' '+qhcpfile+' -o '+qhcfile):
95
 
        if VERBOSE: print "Successfully created ",qhcfile
96
 
        return 0
97
 
 
98
 
def createCollProjectFile():
99
 
    qprojectfile = '''<?xml version="1.0" encoding="UTF-8"?>
100
 
<QHelpCollectionProject version="1.0">
101
 
    <assistant>
102
 
        <title>FreeCAD User Manual</title>
103
 
        <applicationIcon>64px-FreeCAD05.svg.png</applicationIcon>
104
 
        <cacheDirectory>freecad/freecad</cacheDirectory>
105
 
        <startPage>qthelp://org.freecad.usermanual/doc/Online_Help_Startpage.html</startPage>
106
 
        <aboutMenuText>
107
 
            <text>About FreeCAD</text>
108
 
        </aboutMenuText>
109
 
        <aboutDialog>
110
 
            <file>about.txt</file>
111
 
            <!--
112
 
            <icon>images/icon.png</icon>
113
 
            -->
114
 
            <icon>64px-FreeCAD05.svg.png</icon>
115
 
        </aboutDialog>
116
 
        <enableDocumentationManager>true</enableDocumentationManager>
117
 
        <enableAddressBar>true</enableAddressBar>
118
 
        <enableFilterFunctionality>true</enableFilterFunctionality>
119
 
    </assistant>
120
 
    <docFiles>
121
 
        <generate>
122
 
            <file>
123
 
                <input>freecad.qhp</input>
124
 
                <output>freecad.qch</output>
125
 
                </file>
126
 
            </generate>
127
 
        <register>
128
 
            <file>freecad.qch</file>
129
 
        </register>
130
 
    </docFiles>
131
 
</QHelpCollectionProject>
132
 
'''
133
 
    if VERBOSE: print "Building project file..."
134
 
    qfilename = FOLDER + os.sep + "freecad.qhcp"
135
 
    f = open(qfilename,'w')
136
 
    f.write(qprojectfile)
137
 
    f.close()
138
 
    if VERBOSE: print "Done writing qhcp file",qfilename
139
 
    return qfilename
140
 
 
141
 
def buildtoc():
142
 
    '''
143
 
    gets the table of contents page and parses its
144
 
    contents into a clean lists structure
145
 
    '''
146
 
    
147
 
    qhelpfile = '''<?xml version="1.0" encoding="UTF-8"?>
148
 
<QtHelpProject version="1.0">
149
 
    <namespace>org.freecad.usermanual</namespace>
150
 
    <virtualFolder>doc</virtualFolder>
151
 
    <!--
152
 
    <customFilter name="FreeCAD '''+RELEASE+'''">
153
 
        <filterAttribute>FreeCAD</filterAttribute>
154
 
        <filterAttribute>'''+RELEASE+'''</filterAttribute>
155
 
    </customFilter>
156
 
    -->
157
 
    <filterSection>
158
 
        <!--
159
 
        <filterAttribute>FreeCAD</filterAttribute>
160
 
        <filterAttribute>'''+RELEASE+'''</filterAttribute>
161
 
        -->
162
 
        <toc>
163
 
            <inserttoc>
164
 
        </toc>
165
 
        <keywords>
166
 
            <insertkeywords>
167
 
        </keywords>
168
 
        <insertfiles>
169
 
    </filterSection>
170
 
</QtHelpProject>
171
 
'''
172
 
    
173
 
    def getname(line):
174
 
        line = re.compile('<li>').sub('',line)
175
 
        line = re.compile('</li>').sub('',line)
176
 
        title = line.strip()
177
 
        link = ''
178
 
        if "<a" in line:
179
 
            title = re.findall('<a[^>]*>(.*?)</a>',line)[0].strip()
180
 
            link = re.findall('href="(.*?)"',line)[0].strip()
181
 
        if not link: link = 'default.html'
182
 
        return title,link
183
 
 
184
 
    if VERBOSE: print "Building table of contents..."
185
 
    f = open(FOLDER+os.sep+INDEX+'.html')
186
 
    html = ''
187
 
    for line in f: html += line
188
 
    f.close()
189
 
    html = html.replace("\n"," ")
190
 
    html = html.replace("> <","><")
191
 
    html = re.findall("<ul.*/ul>",html)[0]
192
 
    items = re.findall('<li[^>]*>.*?</li>|</ul></li>',html)
193
 
    inserttoc = '<section title="FreeCAD Documentation" ref="Online_Help_Toc.html">\n'
194
 
    insertkeywords = ''
195
 
    for item in items:
196
 
        if not ("<ul>" in item):
197
 
            if ("</ul>" in item):
198
 
                inserttoc += '</section>\n'
199
 
            else:
200
 
                link = ''
201
 
                title,link=getname(item)
202
 
                if link:
203
 
                    link='" ref="'+link
204
 
                    insertkeywords += ('<keyword name="'+title+link+'"/>\n')
205
 
                inserttoc += ('<section title="'+title+link+'"></section>\n')
206
 
        else:
207
 
            subitems = item.split("<ul>")
208
 
            for i in range(len(subitems)):
209
 
                link = ''
210
 
                title,link=getname(subitems[i])
211
 
                if link:
212
 
                    link='" ref="'+link
213
 
                    insertkeywords += ('<keyword name="'+title+link+'"/>\n')
214
 
                trail = ''
215
 
                if i == len(subitems)-1: trail = '</section>'
216
 
                inserttoc += ('<section title="'+title+link+'">'+trail+'\n')
217
 
    inserttoc += '</section>\n'
218
 
 
219
 
    insertfiles = "<files>\n"
220
 
    for fil in os.listdir(FOLDER):
221
 
        insertfiles += ("<file>"+fil+"</file>\n")
222
 
    insertfiles += "</files>\n"
223
 
 
224
 
    qhelpfile = re.compile('<insertkeywords>').sub(insertkeywords,qhelpfile)
225
 
    qhelpfile = re.compile('<inserttoc>').sub(inserttoc,qhelpfile)
226
 
    qhelpfile = re.compile('<insertfiles>').sub(insertfiles,qhelpfile)
227
 
    qfilename = FOLDER + os.sep + "freecad.qhp"
228
 
    f = open(qfilename,'wb')
229
 
    f.write(qhelpfile)
230
 
    f.close()
231
 
    if VERBOSE: print "Done writing qhp file",qfilename
232
 
    return qfilename
233
 
    
234
 
if __name__ == "__main__":
235
 
        crawl()
236