~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Tools/wiki2chm.py

  • Committer: Bazaar Package Importer
  • Author(s): Adam C. Powell, IV
  • Date: 2010-01-11 08:48:33 UTC
  • mfrom: (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100111084833-4g9vgdqbkw8u34zb
Tags: 0.9.2646.5-1
* New upstream version (closes: #561696).
* Added swig to Build-Depends (closes: #563523, #563772).
* Removed python-opencv from Build-Depends and Recommends (closes: #560768).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! python
2
 
# -*- coding: utf-8 -*-
3
 
# (c) 2007 Juergen Riegel GPL
4
 
 
5
 
Usage = """wiki2chm - connect to a wiki and spider the docu
6
 
 
7
 
Usage:
8
 
   wiki2chm [Options] WikiBaseUrl TocPageName
9
 
   
10
 
Options:
11
 
 -p, --proxy=ProxyUrl     specify a proxy
12
 
 -o  --out-path=BASEPATH  use this base path to the HTML project
13
 
 -h, --help               print this help
14
 
 -w, --wget-path=WGETPATH full path to wget (if not in system path)
15
 
 
16
 
Exit:
17
 
 0      No Error or Warning found
18
 
 1      Argument error, wrong or less Arguments given
19
 
 2      Run delivers Warnings (printed on standard error)
20
 
 10     Run stops with an error (printed on standard error)
21
 
 
22
 
This programm reads the wiki and generates all necessary files
23
 
to generate a .chm file.
24
 
The TocPageName is a special page in the Wiki, which is used
25
 
to generate the content for the .chm.
26
 
 
27
 
Examples:
28
 
  
29
 
   wiki2chm  "http://juergen-riegel.net/FreeCAD/Docu/" Online_Help_Toc
30
 
   wiki2chm  -w "C:/Libs/FreeCADLibs/FreeCADLibs6.1/bin/wget.exe" "http://juergen-riegel.net/FreeCAD/Docu/" Online_Help_Toc
31
 
 
32
 
Autor:
33
 
  (c) 2007-2008 Juergen Riegel
34
 
  mail@juergen-riegel.net       
35
 
  Licence: GPL
36
 
 
37
 
Version:
38
 
  0.3
39
 
"""
40
 
 
41
 
import os,sys,string,re,getopt,codecs,binascii,datetime,urllib,glob,time
42
 
 
43
 
 
44
 
# Globals
45
 
Woff = 0
46
 
 
47
 
proxies = {}
48
 
WikiBaseUrl = ""
49
 
TocPageName = ""
50
 
FetchedArticels =[]
51
 
BasePath = ""
52
 
Wget = "wget"
53
 
 
54
 
hhcHeader = """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
55
 
<HTML>
56
 
<HEAD>
57
 
<meta name="GENERATOR" content="Microsoft&reg; HTML Help Workshop 4.1">
58
 
<!-- Sitemap 1.0 -->
59
 
</HEAD><BODY>
60
 
<UL>
61
 
"""
62
 
hhcFooter="""</UL>
63
 
</BODY></HTML>
64
 
"""
65
 
ProjectFile = """
66
 
[OPTIONS]
67
 
Compatibility=1.1 or later
68
 
Compiled file=FreeCAD.chm
69
 
Contents file=Online_Help_Toc.hhc
70
 
Default topic=index.php@title=Online_Help_Startpage
71
 
Display compile progress=Yes
72
 
Full-text search=Yes
73
 
Language=0x409 Englisch (USA)
74
 
[FILES]
75
 
 
76
 
"""
77
 
Output = sys.stdout
78
 
 
79
 
def runWget():
80
 
        global Wget
81
 
        cmdLine = Wget + " "
82
 
        cmdLine += "-k -r "    # convert to local links and do recursive
83
 
        cmdLine += "-P tmp "   # write in this subdir
84
 
        cmdLine += "-nd "      # flat (no subdirs)
85
 
        cmdLine += "-l5 "      # max link follow depth
86
 
        cmdLine += '-R "*action=*" '      # Reject all action links
87
 
        cmdLine += '-R "*title=Special*" '# Reject all special pages
88
 
        cmdLine += '-R "*title=Talk*" '   # Reject all Talk pages
89
 
        cmdLine += '-R "*oldid=*" '       # Reject all history pages
90
 
        cmdLine += '-R "*printable=yes*" '# Reject all print pages
91
 
        cmdLine += 'http://juergen-riegel.net/FreeCAD/Docu/index.php?title=Online_Help_Toc '
92
 
        
93
 
        result = os.popen(cmdLine).read()
94
 
        print result
95
 
        
96
 
 
97
 
def getArticle(Name):
98
 
        global proxies,WikiBaseUrl,TocPageName,BasePath
99
 
        
100
 
        url = WikiBaseUrl + 'index.php?title=' + Name.replace(" ","_") + '&printable=yes'
101
 
        
102
 
        print "GetFile: " + url
103
 
        
104
 
        urlFile = urllib.urlopen(url,proxies=proxies)
105
 
        Article = urlFile.readlines()
106
 
        file = open(BasePath + Name.replace(" ","_") + ".htm","w")
107
 
        for i in Article:
108
 
                i = i.replace("/FreeCAD/Docu/skins/common/commonPrint.css","test.css")
109
 
                file.write(i)
110
 
        file.close()
111
 
 
112
 
def insertTocEntry(file,Indent,points,name,folder=0):
113
 
        Output.write( "TocEntry:" + `Indent` + ' ' + name)
114
 
        name = name.replace("\n","")
115
 
        IndentStr = ""
116
 
        for i in range(points):IndentStr += "   "
117
 
 
118
 
        if(points > Indent):
119
 
                for i in range(points-Indent):
120
 
                        file.write("<UL>")
121
 
                file.write("\n")
122
 
        if(points < Indent):
123
 
                for i in range(Indent-points):
124
 
                        file.write("</UL>")
125
 
                file.write("\n")
126
 
        file.write(IndentStr + '<LI><OBJECT type="text/sitemap">\n')
127
 
        file.write(IndentStr + '      <param name="Name" value="'+ name +'">\n')
128
 
        if(not folder):
129
 
                file.write(IndentStr + '      <param name="Local" value="index.php@title='+ name.replace(" ","_") + '">\n')
130
 
        file.write(IndentStr + '   </OBJECT>\n')
131
 
        return points
132
 
        
133
 
def readToc():
134
 
        global proxies,WikiBaseUrl,TocPageName,BasePath
135
 
        
136
 
        url = WikiBaseUrl + 'index.php?title=Special:Export/' + TocPageName
137
 
        Output.write( 'Open Toc url: ' + url + '\n')
138
 
        urlFile = urllib.urlopen(url,proxies=proxies)
139
 
        Toc = urlFile.readlines()
140
 
        
141
 
        # compile the regexes needed
142
 
        Toc1 = re.compile("^(\*+)\s([\w|\s]*)")
143
 
        Toc2 = re.compile("^(\*+)\s\[\[([\w|\s]*)\|([\w|\s]*)\]\]")
144
 
        Toc3 = re.compile("^(\*+)\s\[\[([\w|\s]*)\\]\]")
145
 
        
146
 
        file = open(BasePath + TocPageName +".hhc","w")
147
 
        file.write(hhcHeader)
148
 
        ListIndent = 1
149
 
        
150
 
        for line in Toc:
151
 
                #print line
152
 
                TocMatch = Toc2.search(line);
153
 
                if TocMatch:
154
 
                        #print "Match2: ", TocMatch.group(1),TocMatch.group(2),TocMatch.group(3)
155
 
                        #getArticle(TocMatch.group(2))
156
 
                        ListIndent = insertTocEntry(file,ListIndent,len(TocMatch.group(1)),TocMatch.group(2))
157
 
                        continue
158
 
                TocMatch = Toc3.search(line);
159
 
                if TocMatch:
160
 
                        #print "Match3: ", TocMatch.group(1),TocMatch.group(2)
161
 
                        #getArticle(TocMatch.group(2))
162
 
                        ListIndent = insertTocEntry(file,ListIndent,len(TocMatch.group(1)),TocMatch.group(2))
163
 
                        continue
164
 
                TocMatch = Toc1.search(line);
165
 
                if TocMatch:
166
 
                        #print "Match1: ", TocMatch.group(1),TocMatch.group(2)
167
 
                        ListIndent = insertTocEntry(file,ListIndent,len(TocMatch.group(1)),TocMatch.group(2),1)
168
 
                        continue
169
 
        for i in range(ListIndent-1):
170
 
                file.write("</UL>")
171
 
        file.write("\n")
172
 
 
173
 
        file.write(hhcFooter)
174
 
 
175
 
def WriteProject():
176
 
        global proxies,WikiBaseUrl,TocPageName,BasePath
177
 
        
178
 
        file = open(BasePath + TocPageName +".hhp","w")
179
 
        file.write(ProjectFile)
180
 
        
181
 
        for FileEntry in os.listdir(BasePath) :
182
 
                if(not FileEntry == TocPageName +".hhp"):
183
 
                        file.write(FileEntry + '\n')
184
 
        
185
 
        file.close()
186
 
        
187
 
def replaceCSS():
188
 
        paths = glob.glob(BasePath + 'index.php*')
189
 
        for file in paths:
190
 
                Output.write( "Replace: " +file + '\n')
191
 
                input = open(file,'r')
192
 
                output = open(file + '_temp','w')
193
 
                for l in input:
194
 
                        output.write(l.replace('"/FreeCAD/Docu/skins/monobook/main.css?9"','"chm.css"'))
195
 
                output.close()
196
 
                input.close()
197
 
                time.sleep(0.2)
198
 
                os.remove(file)
199
 
                time.sleep(0.2)
200
 
                os.rename(file + '_temp',file)
201
 
                
202
 
def main():
203
 
        global proxies,WikiBaseUrl,TocPageName,BasePath, Wget
204
 
        Proxy = ""
205
 
        Qout = None
206
 
        DFQout = None
207
 
        CSVout = None
208
 
        NoOut  = 0
209
 
        
210
 
        try:
211
 
                opts, args = getopt.getopt(sys.argv[1:], "hw:p:o:", ["help", "weget-path=",  "proxy=","out-path="])
212
 
        except getopt.GetoptError:
213
 
                # print help information and exit:
214
 
                sys.stderr.write(Usage)
215
 
                sys.exit(2)
216
 
 
217
 
        # checking on the options
218
 
        for o, a in opts:
219
 
                if o == "-v":
220
 
                        verbose = True
221
 
                if o in ("-h", "--help"):
222
 
                        sys.stderr.write(Usage)
223
 
                        sys.exit()
224
 
                if o in ("-w", "--weget-path"):
225
 
                        Wget = a
226
 
                if o in ("-p", "--proxy"):
227
 
                        Proxy = a
228
 
                if o in ("-o", "--out-path"):
229
 
                        print "Using output path: " + a +"\n"
230
 
                        BasePath = a
231
 
 
232
 
        # runing through the files
233
 
        if(Proxy == ""):
234
 
                proxies = {}
235
 
                print 'Using no proxy'
236
 
        else:
237
 
                proxies = {'http':Proxy}
238
 
                print 'Using proxy: ' + Proxy
239
 
 
240
 
        if (len(args) !=2):
241
 
                sys.stderr.write("Wrong number of arguments!\n\n")
242
 
                sys.stderr.write(Usage)
243
 
                sys.exit(2)
244
 
        else:
245
 
                WikiBaseUrl = args[0]
246
 
                TocPageName = args[1]
247
 
                runWget()
248
 
                readToc()
249
 
                WriteProject()
250
 
                
251
 
 
252
 
 
253
 
if __name__ == "__main__":
254
 
        main()