2
# -*- coding: utf-8 -*-
3
# (c) 2007 Juergen Riegel GPL
5
Usage = """wiki2chm - connect to a wiki and spider the docu
8
wiki2chm [Options] WikiBaseUrl TocPageName
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)
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)
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.
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
33
(c) 2007-2008 Juergen Riegel
34
mail@juergen-riegel.net
41
import os,sys,string,re,getopt,codecs,binascii,datetime,urllib,glob,time
54
hhcHeader = """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
57
<meta name="GENERATOR" content="Microsoft® HTML Help Workshop 4.1">
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
73
Language=0x409 Englisch (USA)
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 '
93
result = os.popen(cmdLine).read()
98
global proxies,WikiBaseUrl,TocPageName,BasePath
100
url = WikiBaseUrl + 'index.php?title=' + Name.replace(" ","_") + '&printable=yes'
102
print "GetFile: " + url
104
urlFile = urllib.urlopen(url,proxies=proxies)
105
Article = urlFile.readlines()
106
file = open(BasePath + Name.replace(" ","_") + ".htm","w")
108
i = i.replace("/FreeCAD/Docu/skins/common/commonPrint.css","test.css")
112
def insertTocEntry(file,Indent,points,name,folder=0):
113
Output.write( "TocEntry:" + `Indent` + ' ' + name)
114
name = name.replace("\n","")
116
for i in range(points):IndentStr += " "
119
for i in range(points-Indent):
123
for i in range(Indent-points):
126
file.write(IndentStr + '<LI><OBJECT type="text/sitemap">\n')
127
file.write(IndentStr + ' <param name="Name" value="'+ name +'">\n')
129
file.write(IndentStr + ' <param name="Local" value="index.php@title='+ name.replace(" ","_") + '">\n')
130
file.write(IndentStr + ' </OBJECT>\n')
134
global proxies,WikiBaseUrl,TocPageName,BasePath
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()
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]*)\\]\]")
146
file = open(BasePath + TocPageName +".hhc","w")
147
file.write(hhcHeader)
152
TocMatch = Toc2.search(line);
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))
158
TocMatch = Toc3.search(line);
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))
164
TocMatch = Toc1.search(line);
166
#print "Match1: ", TocMatch.group(1),TocMatch.group(2)
167
ListIndent = insertTocEntry(file,ListIndent,len(TocMatch.group(1)),TocMatch.group(2),1)
169
for i in range(ListIndent-1):
173
file.write(hhcFooter)
176
global proxies,WikiBaseUrl,TocPageName,BasePath
178
file = open(BasePath + TocPageName +".hhp","w")
179
file.write(ProjectFile)
181
for FileEntry in os.listdir(BasePath) :
182
if(not FileEntry == TocPageName +".hhp"):
183
file.write(FileEntry + '\n')
188
paths = glob.glob(BasePath + 'index.php*')
190
Output.write( "Replace: " +file + '\n')
191
input = open(file,'r')
192
output = open(file + '_temp','w')
194
output.write(l.replace('"/FreeCAD/Docu/skins/monobook/main.css?9"','"chm.css"'))
200
os.rename(file + '_temp',file)
203
global proxies,WikiBaseUrl,TocPageName,BasePath, Wget
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)
217
# checking on the options
221
if o in ("-h", "--help"):
222
sys.stderr.write(Usage)
224
if o in ("-w", "--weget-path"):
226
if o in ("-p", "--proxy"):
228
if o in ("-o", "--out-path"):
229
print "Using output path: " + a +"\n"
232
# runing through the files
235
print 'Using no proxy'
237
proxies = {'http':Proxy}
238
print 'Using proxy: ' + Proxy
241
sys.stderr.write("Wrong number of arguments!\n\n")
242
sys.stderr.write(Usage)
245
WikiBaseUrl = args[0]
246
TocPageName = args[1]
253
if __name__ == "__main__":