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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
#
#
# Nimrod Website Generator
# (c) Copyright 2012 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
import
os, strutils, times, parseopt, parsecfg, streams, strtabs
type
TKeyValPair = tuple[key, val: string]
TConfigData = object of TObject
tabs, links: seq[TKeyValPair]
doc, srcdoc, webdoc, pdf: seq[string]
authors, projectName, projectTitle, logo, infile, outdir, ticker: string
vars: PStringTable
nimrodArgs: string
proc initConfigData(c: var TConfigData) =
c.tabs = @[]
c.links = @[]
c.doc = @[]
c.srcdoc = @[]
c.webdoc = @[]
c.pdf = @[]
c.infile = ""
c.outdir = ""
c.nimrodArgs = "--hint[Conf]:off "
c.authors = ""
c.projectTitle = ""
c.projectName = ""
c.logo = ""
c.ticker = ""
c.vars = newStringTable(modeStyleInsensitive)
include "sunset.tmpl"
# ------------------------- configuration file -------------------------------
const
Version = "0.7"
Usage = "nimweb - Nimrod Website Generator Version " & version & """
(c) 2012 Andreas Rumpf
Usage:
nimweb [options] ini-file[.ini] [compile_options]
Options:
-o, --output:dir set the output directory (default: same as ini-file)
--var:name=value set the value of a variable
-h, --help shows this help
-v, --version shows the version
Compile_options:
will be passed to the Nimrod compiler
"""
proc parseCmdLine(c: var TConfigData) =
var p = initOptParser()
while true:
next(p)
var kind = p.kind
var key = p.key
var val = p.val
case kind
of cmdArgument:
c.infile = addFileExt(key, "ini")
c.nimrodArgs.add(cmdLineRest(p))
break
of cmdLongOption, cmdShortOption:
case normalize(key)
of "help", "h":
stdout.write(Usage)
quit(0)
of "version", "v":
stdout.write(Version & "\n")
quit(0)
of "o", "output": c.outdir = val
of "var":
var idx = val.find('=')
if idx < 0: quit("invalid command line")
c.vars[substr(val, 0, idx-1)] = substr(val, idx+1)
else: quit(Usage)
of cmdEnd: break
if c.infile.len == 0: quit(Usage)
proc walkDirRecursively(s: var seq[string], root, ext: string) =
for k, f in walkDir(root):
case k
of pcFile, pcLinkToFile:
if cmpIgnoreCase(ext, splitFile(f).ext) == 0:
add(s, f)
of pcDir: walkDirRecursively(s, f, ext)
of pcLinkToDir: nil
proc addFiles(s: var seq[string], dir, ext: string, patterns: seq[string]) =
for p in items(patterns):
if existsDir(dir / p):
walkDirRecursively(s, dir / p, ext)
else:
add(s, dir / addFileExt(p, ext))
proc parseIniFile(c: var TConfigData) =
var
p: TCfgParser
section: string # current section
var input = newFileStream(c.infile, fmRead)
if input != nil:
open(p, input, c.infile)
while true:
var k = next(p)
case k.kind
of cfgEof: break
of cfgSectionStart:
section = normalize(k.section)
case section
of "project", "links", "tabs", "ticker", "documentation", "var": nil
else: echo("[Warning] Skipping unknown section: " & section)
of cfgKeyValuePair:
var v = k.value % c.vars
c.vars[k.key] = v
case section
of "project":
case normalize(k.key)
of "name": c.projectName = v
of "title": c.projectTitle = v
of "logo": c.logo = v
of "authors": c.authors = v
else: quit(errorStr(p, "unknown variable: " & k.key))
of "var": nil
of "links": add(c.links, (k.key, v))
of "tabs": add(c.tabs, (k.key, v))
of "ticker": c.ticker = v
of "documentation":
case normalize(k.key)
of "doc": addFiles(c.doc, "doc", ".txt", split(v, {';'}))
of "pdf": addFiles(c.pdf, "doc", ".txt", split(v, {';'}))
of "srcdoc": addFiles(c.srcdoc, "lib", ".nim", split(v, {';'}))
of "webdoc": addFiles(c.webdoc, "lib", ".nim", split(v, {';'}))
else: quit(errorStr(p, "unknown variable: " & k.key))
else: nil
of cfgOption: quit(errorStr(p, "syntax error"))
of cfgError: quit(errorStr(p, k.msg))
close(p)
if c.projectName.len == 0:
c.projectName = changeFileExt(extractFilename(c.infile), "")
if c.outdir.len == 0:
c.outdir = splitFile(c.infile).dir
else:
quit("cannot open: " & c.infile)
# ------------------- main ----------------------------------------------------
proc Exec(cmd: string) =
echo(cmd)
if os.execShellCmd(cmd) != 0: quit("external program failed")
proc buildDoc(c: var TConfigData, destPath: string) =
# call nim for the documentation:
for d in items(c.doc):
Exec("nimrod rst2html $# -o:$# --index=$#/theindex $#" %
[c.nimrodArgs, destPath / changeFileExt(splitFile(d).name, "html"),
destpath, d])
for d in items(c.srcdoc):
Exec("nimrod doc $# -o:$# --index=$#/theindex $#" %
[c.nimrodArgs, destPath / changeFileExt(splitFile(d).name, "html"),
destpath, d])
Exec("nimrod rst2html $1 -o:$2/theindex.html $2/theindex" %
[c.nimrodArgs, destPath])
proc buildPdfDoc(c: var TConfigData, destPath: string) =
if os.execShellCmd("pdflatex -version") != 0:
echo "pdflatex not found; no PDF documentation generated"
else:
for d in items(c.pdf):
Exec("nimrod rst2tex $# $#" % [c.nimrodArgs, d])
# call LaTeX twice to get cross references right:
Exec("pdflatex " & changeFileExt(d, "tex"))
Exec("pdflatex " & changeFileExt(d, "tex"))
# delete all the crappy temporary files:
var pdf = splitFile(d).name & ".pdf"
moveFile(dest=destPath / pdf, source=pdf)
removeFile(changeFileExt(pdf, "aux"))
if existsFile(changeFileExt(pdf, "toc")):
removeFile(changeFileExt(pdf, "toc"))
removeFile(changeFileExt(pdf, "log"))
removeFile(changeFileExt(pdf, "out"))
removeFile(changeFileExt(d, "tex"))
proc buildAddDoc(c: var TConfigData, destPath: string) =
# build additional documentation (without the index):
for d in items(c.webdoc):
Exec("nimrod doc $# -o:$# $#" %
[c.nimrodArgs, destPath / changeFileExt(splitFile(d).name, "html"), d])
proc main(c: var TConfigData) =
const
cmd = "nimrod rst2html --compileonly $1 -o:web/$2.temp web/$2.txt"
if c.ticker.len > 0:
Exec(cmd % [c.nimrodArgs, c.ticker])
var temp = "web" / changeFileExt(c.ticker, "temp")
try:
c.ticker = readFile(temp)
except EIO:
quit("[Error] cannot open: " & temp)
RemoveFile(temp)
for i in 0..c.tabs.len-1:
var file = c.tabs[i].val
Exec(cmd % [c.nimrodArgs, file])
var temp = "web" / changeFileExt(file, "temp")
var content: string
try:
content = readFile(temp)
except EIO:
quit("[Error] cannot open: " & temp)
var f: TFile
var outfile = "web/upload/$#.html" % file
if open(f, outfile, fmWrite):
writeln(f, generateHTMLPage(c, file, content))
close(f)
else:
quit("[Error] cannot write file: " & outfile)
removeFile(temp)
buildAddDoc(c, "web/upload")
buildDoc(c, "web/upload")
buildDoc(c, "doc")
buildPdfDoc(c, "doc")
var c: TConfigData
initConfigData(c)
parseCmdLine(c)
parseIniFile(c)
main(c)
|