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
..
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
...
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
...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
local ss = require 'sirsem'
local default_mode = {
['render:format'] = 'html';
['html:gen-styles'] = true;
}
local function
kmap(fn, list)
local new = {}
for k, v in pairs(list) do
local nk,nv = fn(k,v)
new[nk or k] = nv or v
end
return new
end
local function
kfilter(list, fn)
local new = {}
for k, v in pairs(list) do
if fn(k,v) then new[k] = v end
end
return new
end
local function
main(input, output, log, mode, suggestions, vars)
local doc = ct.parse(input.stream, input.src, mode)
input.stream:close()
if mode['parse:show-tree'] then
log:write(dump(doc))
end
................................................................................
error 'what output format should i translate the input to?'
end
if mode['render:format'] == 'none' then return 0 end
if not ct.render[mode['render:format']] then
ct.exns.unimpl('output format ā%sā unsupported', mode['render:format']):throw()
end
local render_opts = kmap(function(k,v)
return k:sub(2+#mode['render:format'])
end, kfilter(mode, function(m)
return ss.str.begins(m, mode['render:format']..':')
end))
doc.vars = vars
-- this is kind of gross but the context object belongs to the parser,
-- not the renderer, so that's not a suitable place for this information
................................................................................
mode = function(key,value) mode[checkmodekey(key)] = value end;
['mode-set'] = function(key) mode[checkmodekey(key)] = true end;
['mode-clear'] = function(key) mode[checkmodekey(key)] = false end;
['mode-weak'] = function(key,value) suggestions[checkmodekey(key)] = value end;
['mode-set-weak'] = function(key) suggestions[checkmodekey(key)] = true end;
['mode-clear-weak'] = function(key) suggestions[checkmodekey(key)] = false end;
}
local args = {}
local keepParsing = true
do local i = 1 while i <= #arg do local v = arg[i]
local execLongOpt = function(longopt)
if not onswitch[longopt] then
ct.exns.cli('switch --%s unrecognized', longopt):throw()
end
local nargs = optnparams(longopt)
if nargs > 1 then
if i + nargs > #arg then
ct.exns.cli('not enough arguments for switch --%s (%u expected)', longopt, nargs):throw()
end
local nt = {}
for j = i+1, i+nargs do
table.insert(nt, arg[j])
end
onswitch[longopt](table.unpack(nt))
elseif nargs == 1 then
onswitch[longopt](arg[i+1])
end
i = i + nargs
end
if v == '--' then
keepParsing = false
else
local longopt = v:match '^%-%-(.+)$'
................................................................................
execLongOpt(longopt)
else
if keepParsing and v:sub(1,1) == '-' then
for c,p in ss.str.enc.utf8.each(v:sub(2)) do
if optmap[c] then
execLongOpt(optmap[c])
else
ct.exns.cli('switch -%i unrecognized', c):throw()
end
end
else
table.insert(args, v)
end
end
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
|
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
..
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
...
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
...
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
local ss = require 'sirsem'
local default_mode = {
['render:format'] = 'html';
['html:gen-styles'] = true;
}
local function
main(input, output, log, mode, suggestions, vars)
local doc = ct.parse(input.stream, input.src, mode)
input.stream:close()
if mode['parse:show-tree'] then
log:write(dump(doc))
end
................................................................................
error 'what output format should i translate the input to?'
end
if mode['render:format'] == 'none' then return 0 end
if not ct.render[mode['render:format']] then
ct.exns.unimpl('output format ā%sā unsupported', mode['render:format']):throw()
end
local render_opts = ss.kmap(function(k,v)
return k:sub(2+#mode['render:format'])
end, ss.kfilter(mode, function(m)
return ss.str.begins(m, mode['render:format']..':')
end))
doc.vars = vars
-- this is kind of gross but the context object belongs to the parser,
-- not the renderer, so that's not a suitable place for this information
................................................................................
mode = function(key,value) mode[checkmodekey(key)] = value end;
['mode-set'] = function(key) mode[checkmodekey(key)] = true end;
['mode-clear'] = function(key) mode[checkmodekey(key)] = false end;
['mode-weak'] = function(key,value) suggestions[checkmodekey(key)] = value end;
['mode-set-weak'] = function(key) suggestions[checkmodekey(key)] = true end;
['mode-clear-weak'] = function(key) suggestions[checkmodekey(key)] = false end;
['version'] = function()
outp:write(ct.info:about())
if next(ct.ext.loaded) then
outp:write('\nactive extensions:\n')
for k,v in pairs(ct.ext.loaded) do
outp:write(string.format(' * %s', v.id ..
(v.version and (' ' .. v.version:string()) or '')))
if v.desc then
outp:write(string.format(': %s', v.desc))
if v.contributors then
outp:write(string.format(' [%s]', table.concat(
ss.map(function(ctr)
return ctr.name or ctr.handle
end, v.contributors), ', ')))
end
else
outp:write'\n'
end
end
end
os.exit(0)
end
}
local args = {}
local keepParsing = true
do local i = 1 while i <= #arg do local v = arg[i]
local execLongOpt = function(longopt)
if not onswitch[longopt] then
ct.exns.cli('switch --%s unrecognized', longopt):throw()
end
local nargs = optnparams(longopt)
if nargs > 1 then
if i + nargs > #arg then
ct.exns.cli('not enough arguments for switch --%s (%s expected)', longopt, nargs):throw()
end
local nt = {}
for j = i+1, i+nargs do
table.insert(nt, arg[j])
end
print('onsw')
elseif nargs == 1 then
onswitch[longopt](arg[i+1])
else
onswitch[longopt]()
end
i = i + nargs
end
if v == '--' then
keepParsing = false
else
local longopt = v:match '^%-%-(.+)$'
................................................................................
execLongOpt(longopt)
else
if keepParsing and v:sub(1,1) == '-' then
for c,p in ss.str.enc.utf8.each(v:sub(2)) do
if optmap[c] then
execLongOpt(optmap[c])
else
ct.exns.cli('switch -%s unrecognized', c):throw()
end
end
else
table.insert(args, v)
end
end
|