Differences From
Artifact [37b0a0a8ee]:
2 2 local ss = require 'sirsem'
3 3
4 4 local default_mode = {
5 5 ['render:format'] = 'html';
6 6 ['html:gen-styles'] = true;
7 7 }
8 8
9 -local function
10 -kmap(fn, list)
11 - local new = {}
12 - for k, v in pairs(list) do
13 - local nk,nv = fn(k,v)
14 - new[nk or k] = nv or v
15 - end
16 - return new
17 -end
18 -
19 -local function
20 -kfilter(list, fn)
21 - local new = {}
22 - for k, v in pairs(list) do
23 - if fn(k,v) then new[k] = v end
24 - end
25 - return new
26 -end
27 -
28 9 local function
29 10 main(input, output, log, mode, suggestions, vars)
30 11 local doc = ct.parse(input.stream, input.src, mode)
31 12 input.stream:close()
32 13 if mode['parse:show-tree'] then
33 14 log:write(dump(doc))
34 15 end
................................................................................
43 24 error 'what output format should i translate the input to?'
44 25 end
45 26 if mode['render:format'] == 'none' then return 0 end
46 27 if not ct.render[mode['render:format']] then
47 28 ct.exns.unimpl('output format ā%sā unsupported', mode['render:format']):throw()
48 29 end
49 30
50 - local render_opts = kmap(function(k,v)
31 + local render_opts = ss.kmap(function(k,v)
51 32 return k:sub(2+#mode['render:format'])
52 - end, kfilter(mode, function(m)
33 + end, ss.kfilter(mode, function(m)
53 34 return ss.str.begins(m, mode['render:format']..':')
54 35 end))
55 36
56 37 doc.vars = vars
57 38
58 39 -- this is kind of gross but the context object belongs to the parser,
59 40 -- not the renderer, so that's not a suitable place for this information
................................................................................
136 117 mode = function(key,value) mode[checkmodekey(key)] = value end;
137 118 ['mode-set'] = function(key) mode[checkmodekey(key)] = true end;
138 119 ['mode-clear'] = function(key) mode[checkmodekey(key)] = false end;
139 120
140 121 ['mode-weak'] = function(key,value) suggestions[checkmodekey(key)] = value end;
141 122 ['mode-set-weak'] = function(key) suggestions[checkmodekey(key)] = true end;
142 123 ['mode-clear-weak'] = function(key) suggestions[checkmodekey(key)] = false end;
124 +
125 + ['version'] = function()
126 + outp:write(ct.info:about())
127 + if next(ct.ext.loaded) then
128 + outp:write('\nactive extensions:\n')
129 + for k,v in pairs(ct.ext.loaded) do
130 + outp:write(string.format(' * %s', v.id ..
131 + (v.version and (' ' .. v.version:string()) or '')))
132 + if v.desc then
133 + outp:write(string.format(': %s', v.desc))
134 + if v.contributors then
135 + outp:write(string.format(' [%s]', table.concat(
136 + ss.map(function(ctr)
137 + return ctr.name or ctr.handle
138 + end, v.contributors), ', ')))
139 + end
140 + else
141 + outp:write'\n'
142 + end
143 + end
144 + end
145 + os.exit(0)
146 + end
143 147 }
144 148
145 149 local args = {}
146 150 local keepParsing = true
147 151 do local i = 1 while i <= #arg do local v = arg[i]
148 152 local execLongOpt = function(longopt)
149 153 if not onswitch[longopt] then
150 154 ct.exns.cli('switch --%s unrecognized', longopt):throw()
151 155 end
152 156 local nargs = optnparams(longopt)
153 157
154 158 if nargs > 1 then
155 159 if i + nargs > #arg then
156 - ct.exns.cli('not enough arguments for switch --%s (%u expected)', longopt, nargs):throw()
160 + ct.exns.cli('not enough arguments for switch --%s (%s expected)', longopt, nargs):throw()
157 161 end
158 162 local nt = {}
159 163 for j = i+1, i+nargs do
160 164 table.insert(nt, arg[j])
161 165 end
162 - onswitch[longopt](table.unpack(nt))
166 + print('onsw')
163 167 elseif nargs == 1 then
164 168 onswitch[longopt](arg[i+1])
169 + else
170 + onswitch[longopt]()
165 171 end
166 172 i = i + nargs
167 173 end
168 174 if v == '--' then
169 175 keepParsing = false
170 176 else
171 177 local longopt = v:match '^%-%-(.+)$'
................................................................................
173 179 execLongOpt(longopt)
174 180 else
175 181 if keepParsing and v:sub(1,1) == '-' then
176 182 for c,p in ss.str.enc.utf8.each(v:sub(2)) do
177 183 if optmap[c] then
178 184 execLongOpt(optmap[c])
179 185 else
180 - ct.exns.cli('switch -%i unrecognized', c):throw()
186 + ct.exns.cli('switch -%s unrecognized', c):throw()
181 187 end
182 188 end
183 189 else
184 190 table.insert(args, v)
185 191 end
186 192 end
187 193