-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkpage
executable file
·332 lines (313 loc) · 9.2 KB
/
mkpage
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env lua
--------------------------------------------------------------------------------
--LuaZDF-begin --with append capexec countlen dirfiles dirhas dump invertby keys mint
--+ readfile readlines rmdirtree startswith subdirs writefile
--------------------------------------------------------------------------------
local lfs = require( "lfs" ) --ZREQ-lfs
--ZFUNC-readfile-v1
local function readfile( filename ) --> str, err
local f, err = io.open( filename, "r" )
if err then return nil, err end
local str, err = f:read( "*a" )
if err then return nil, err end
local res, err = f:close()
if err then return nil, err end
return str
end
--ZFUNC-append-v1
local function append( arr, v, ... ) --> arr
table.insert( arr, v )
if ... then
for i, o in ipairs{ ... } do
table.insert( arr, o )
end
end
return arr
end
--ZFUNC-capexec-v1
local function capexec( cmd ) --> exit, signal, stdout, stderr
local outfile = os.tmpname()
local errfile = os.tmpname()
cmd = cmd..[[ >"]]..outfile..[[" 2>"]]..errfile..[["]]
local exit, signal = os.execute( cmd )
local outcnt = readfile( outfile )
local errcnt = readfile( errfile )
os.remove( outfile )
os.remove( errfile )
return exit, signal, outcnt, errcnt
end
--ZFUNC-countlen-v1
local function countlen( tab ) --> len
local len = 0
for _ in pairs( tab ) do
len = len+1
end
return len
end
--ZFUNC-dirfiles-v1
local function dirfiles( path ) --> iter
local function yielddir( path )
for entry in lfs.dir( path ) do
local entrypath = path.."/"..entry
local mode = lfs.attributes( entrypath, "mode" )
if mode == "file" then
coroutine.yield( entry )
end
end
end
return coroutine.wrap( function() yielddir( path ) end )
end
--ZFUNC-dirhas-v1
local function dirhas( path, name, mode ) --> entrypath
local modeval = lfs.attributes( path, "mode" )
if modeval ~= "directory" then return nil end
for entry in lfs.dir( path ) do
if entry == name then
local entrypath = path.."/"..entry
if mode then
local modeval = lfs.attributes( entrypath, "mode" )
if modeval ~= mode then
return nil
end
end
return entrypath
end
end
return nil
end
--ZFUNC-dump-v1
local function dump( tab, format )
format = format or {}
format.indent = format.indent or 3
format.level = format.level or 0
format.file = format.file or io.stdout
local indent = format.level * format.indent
for k,v in pairs( tab ) do
format.file:write( string.rep( " ", indent )..k..":" )
if type( v ) == "table" then
format.file:write( "\n" )
format.level = format.level + 1
dump( v, format )
format.level = format.level - 1
elseif type( v ) == 'boolean' then
format.file:write( " "..tostring( v ).."\n" )
else
format.file:write( " "..v.."\n" )
end
end
end
--ZFUNC-invertby-v1
local function invertby( tab, fv ) --> itab
local itab = {}
for k, v in pairs( tab ) do
local ik = v
if fv then ik = fv( v ) end
itab[ ik ] = itab[ ik ] or {}
table.insert( itab[ ik ], k )
end
return itab
end
--ZFUNC-keys-v1
local function keys( tab ) --> arr
local arr = {}
for k, v in pairs( tab ) do
table.insert( arr, k )
end
return arr
end
--ZFUNC-mint-v0
local function mint( template, ename ) --> ( sandbox ) --> expstr, err
if not ename then ename = '_o' end
local function expr(e) return ' '..ename..'('..e..')' end
local function compat_load( str, env )
local chunkname = 'mint_script'
local func, err
if _VERSION ~= 'Lua 5.1' then
func, err = load( str, chunkname, 't', env )
else
func, err = loadstring( str, chunkname)
if func then setfenv( func, env ) end
end
return func, err
end
-- Generate a script that expands the template
local script = template:gsub( '(.-)@(%b{})([^@]*)',
function( prefix, code, suffix )
prefix = expr( string.format( '%q', prefix ) )
suffix = expr( string.format( '%q', suffix ) )
code = code:sub( 2, #code-1 )
if code:match( '^{.*}$' ) then
return prefix .. code:sub( 2, #code-1 ) .. suffix
else
return prefix .. expr( code ) .. suffix
end
end
)
-- The generator must be run only if at least one @{} was found
local run_generator = ( script ~= template )
-- Return a function that executes the script with a custom environment
return function( sandbox )
if not run_generator then return script end
local expstr = ''
if 'table' ~= type( sandbox ) then
return nil, "mint generator requires a sandbox"
end
local oldout = sandbox[ ename ]
sandbox[ ename ] = function( out ) expstr = expstr..tostring(out) end
local generate, err = compat_load( script, sandbox )
if not generate or err then
sandbox[ ename ] = oldout
return nil, err..'\nTemplate script: [[\n'..script..'\n]]'
end
local ok, err = pcall(generate)
sandbox[ ename ] = oldout
if not ok or err then
return nil, err..'\nTemplate script: [[\n'..script..'\n]]'
end
return expstr
end
end
--ZFUNC-readlines-v1
local function readlines( filename ) --> strlst, err
--ZFUNC-lines-v1
local function lines( str )
if not str:find( "\n$" ) then str = str.."\n" end
return str:gmatch( "([^\n]*)\n" )
end
local f, err = io.open( filename, "r" )
if err then return nil, err end
local str, err = f:read( "*a" )
if err then return nil, err end
local strlst = {}
for line in lines( str ) do
table.insert( strlst, line )
end
local res, err = f:close()
if err then return nil, err end
return strlst
end
--ZFUNC-rmdirtree-v1
local function rmdirtree( path ) --> res, err
--ZFUNC-isdodd-v1
local function isdodd( e )
if e == "." or e == ".." then
return true
end
return false
end
for entry in lfs.dir( path ) do
if not isdodd( entry ) then
local entrypath = path.."/"..entry
local mode = lfs.attributes( entrypath, "mode" )
local res, err = true, nil
if mode == "directory" then
res, err = rmdirtree( entrypath )
else
res, err = os.remove( entrypath )
end
if err then return res, err end
end
end
return os.remove( path )
end
--ZFUNC-startswith-v1
local function startswith( str, prefix ) --> res
return string.sub( str, 1, string.len( prefix ) ) == prefix
end
--ZFUNC-subdirs-v1
local function subdirs( path ) --> iter
--ZFUNC-isdodd-v1
local function isdodd( e )
if e == "." or e == ".." then
return true
end
return false
end
local function yielddir( path )
for entry in lfs.dir( path ) do
local entrypath = path.."/"..entry
local mode = lfs.attributes( entrypath, "mode" )
if mode == "directory" and not isdodd( entry ) then
coroutine.yield( entry )
end
end
end
return coroutine.wrap( function() yielddir( path ) end )
end
--ZFUNC-writefile-v1
local function writefile( filename, ... ) --> res, err
local f, err = io.open( filename, "w" )
if err then return nil, err end
local wres, err = f:write( ... )
if err then return nil, err end
return f:close()
end
--------------------------------------------------------------------------------
--LuaZDF-end
--------------------------------------------------------------------------------
local function logln( str, ... )
return io.stdout:write( str:format( ... ), "\n" )
end
local function errfln( str, ... )
return io.stderr:write( "Error: ", str:format( ... ), "\n" )
end
local function errexit( ... )
if ... then
errfln( ... )
end
os.exit( 1 )
end
local iocachetab = {}
local function _f( path )
if iocachetab[ path ] then
return iocachetab[ path ]
end
local res, err = readfile( path )
if err then
errexit( err )
end
iocachetab[ path ] = res
return res
end
local svgcache = {}
local function svgicon( path )
if svgcache[ path ] then
return svgcache[ path ]
end
local lines, err = readlines( path )
if err then
errexit( err )
end
table.remove( lines, 1 )
local res = table.concat( lines )
svgcache[ path ] = res
return res
end
local function cmdexec( cmdtab )
local cmdline = table.concat( cmdtab, " " )
logln( "Execute: %s", cmdline )
local exit, signal, stdout, stderr = capexec( cmdline )
if not exit then
errfln( "Problems with: %q", cmdline )
errfln( stderr )
errexit()
end
return stdout
end
local function adoctohtml( adoc ) --> html
local cmd = { "asciidoc" }
append( cmd, "--backend", "html5" )
append( cmd, "--no-header-footer" )
append( cmd, "--out-file", "-" )
append( cmd, adoc )
return cmdexec( cmd )
end
--------------------------------------------------------------------------------
local m = mint( _f"page/page.htm" )
local page = m{
css = "page/sbr.css",
title = "mxt",
content = adoctohtml( "README.adoc" )
}
local res, err = writefile( "index.html", page )
if err then errexit( err ) end