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
...
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
...
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
...
929
930
931
932
933
934
935
936
937
938
939
940
941
942
....
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
|
fns = {
throw = function(me) error(me) end;
}
}
ct.exnkind = declare {
ident = 'exn-kind';
mk = function(desc, report)
return { desc = desc, report = report }
end;
call = function(me, ...)
return ct.exn(me, ...)
end;
}
ct.exns = {
tx = ct.exnkind('translation error', function(msg,...)
return string.format("(%s:%u) "..msg, ...)
end)
}
ct.ctx = declare {
mk = function(src) return {src = src} end;
ident = 'context';
cast = {
string = function(me)
................................................................................
end
local span_renderers = {}
local function htmlSpan(spans, block, sec)
local text = {}
for k,v in pairs(spans) do
if type(v) == 'string' then
table.insert(text,(v:gsub('[<>&]',
function(x)
return string.format('&#%02u', string.byte(x))
end)))
else
table.insert(text, span_renderers[v.kind](v, block, sec))
end
end
return table.concat(text)
end
function span_renderers.format(sp)
local tags = { strong = 'strong', emph = 'em', strike = 'del', insert = 'ins', literal = 'code' }
if sp.style == 'literal' then
stylesNeeded.code = true
end
if sp.style == 'del' or sp.style == 'ins' then
stylesNeeded.editors_markup = true
end
return tag(tags[sp.style],nil,htmlSpan(sp.spans))
end
................................................................................
local styles = {}
for k in pairs(stylesNeeded) do
table.insert(styles, (stylesets[k]:gsub('%s+',' ')))
end
local head = {}
if next(styles) then
table.insert(head, tag('style',{type='text/css'},table.concat(styles)))
end
if opts.snippet then
return body
else
return dr.htmlDoc(doctitle, next(head) and table.concat(head), body)
end
end
local function
startswith(str, pfx)
................................................................................
if a1 == ':' and a2 ~= ':' then
align = 'left'
elseif a1 == ':' and a2 == ':' then
align = 'center'
elseif a1 ~= ':' and a2 == ':' then
align = 'right'
end
table.insert(row, {
spans = ct.parse_span(text, c);
align = align;
header = header;
})
end
if #c.sec.blocks > 1 and c.sec.blocks[#c.sec.blocks].kind == 'table' then
................................................................................
end
end
return ctx.doc
end
local default_mode = {
format = 'html';
}
local function main(input, output, log, mode, vars)
local doc = ct.parse(input.stream, input.src)
input.stream:close()
if mode['show-tree'] then
log:write(dump(doc))
end
if not mode.format then
error 'what output format should i translate the input to?'
end
if not ct.render[mode.format] then
error(string.format('output format “%s” unsupported', mode.format))
end
output:write(ct.render[mode.format](doc, {}))
end
local inp,outp,log = io.stdin, io.stdout, io.stderr
local function entry_cli()
local mode, vars, input = default_mode, {}, {
stream = inp;
src = {
file = '(stdin)';
}
}
if arg[1] and arg[1] ~= '' then
local file = io.open(arg[1], "rb")
if not file then error('unable to load file ' .. arg[1]) end
input.stream = file
input.src.file = arg[1]
end
main(input, outp, log, mode, vars)
end
-- local ok, e = pcall(entry_cli)
local ok, e = true, entry_cli()
|
>
|
>
>
>
>
|
>
>
>
>
>
>
>
|
|
|
>
>
>
>
>
>
>
|
|
>
>
>
>
|
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
>
>
>
>
>
>
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
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
...
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
...
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
...
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
....
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
|
fns = {
throw = function(me) error(me) end;
}
}
ct.exnkind = declare {
ident = 'exn-kind';
mk = function(desc, report)
return {
desc = desc;
report = report or function(msg,...)
return string.format(msg,...)
end;
}
end;
call = function(me, ...)
return ct.exn(me, ...)
end;
}
ct.exns = {
tx = ct.exnkind('translation error', function(msg,...)
return string.format("(%s:%u) "..msg, ...)
end);
io = ct.exnkind('IO error', function(msg, ...)
return string.format("<%s %s> "..msg, ...)
end);
cli = ct.exnkind 'command line parse error';
mode = ct.exnkind('bad mode', function(msg, ...)
return string.format("mode “%s” "..msg, ...)
end);
}
ct.ctx = declare {
mk = function(src) return {src = src} end;
ident = 'context';
cast = {
string = function(me)
................................................................................
end
local span_renderers = {}
local function htmlSpan(spans, block, sec)
local text = {}
for k,v in pairs(spans) do
if type(v) == 'string' then
table.insert(text,(v:gsub('[<>&"]',
function(x)
return string.format('&#%02u;', string.byte(x))
end)))
else
table.insert(text, span_renderers[v.kind](v, block, sec))
end
end
return table.concat(text)
end
function span_renderers.format(sp)
local tags = { strong = 'strong', emph = 'em', strike = 'del', insert = 'ins', literal = 'code' }
if sp.style == 'literal' and not opts['fossil-uv'] then
stylesNeeded.code = true
end
if sp.style == 'del' or sp.style == 'ins' then
stylesNeeded.editors_markup = true
end
return tag(tags[sp.style],nil,htmlSpan(sp.spans))
end
................................................................................
local styles = {}
for k in pairs(stylesNeeded) do
table.insert(styles, (stylesets[k]:gsub('%s+',' ')))
end
local head = {}
local styletag = ''
if opts['link-css'] then
local css = opts['link-css']
if type(css) ~= 'string' then ct.exns.mode('must be a string', 'html:link-css'):throw() end
styletag = styletag .. elt('link',{rel='stylesheet',type='text/css',href=opts['link-css']})
end
if next(styles) then
if opts['gen-styles'] then
styletag = styletag .. tag('style',{type='text/css'},table.concat(styles))
end
table.insert(head, styletag)
end
if opts['fossil-uv'] then
return tag('div',{class='fossil-doc',['data-title']=doctitle},styletag .. body)
elseif opts.snippet then
return styletag .. body
else
return dr.htmlDoc(doctitle, next(head) and table.concat(head), body)
end
end
local function
startswith(str, pfx)
................................................................................
if a1 == ':' and a2 ~= ':' then
align = 'left'
elseif a1 == ':' and a2 == ':' then
align = 'center'
elseif a1 ~= ':' and a2 == ':' then
align = 'right'
end
text = text:match '^%s*(.-)%s*$'
table.insert(row, {
spans = ct.parse_span(text, c);
align = align;
header = header;
})
end
if #c.sec.blocks > 1 and c.sec.blocks[#c.sec.blocks].kind == 'table' then
................................................................................
end
end
return ctx.doc
end
local default_mode = {
['render:format'] = 'html';
['html:gen-styles'] = true;
}
local function filter(list, fn)
local new = {}
for i, v in ipairs(list) do
if fn(v,i) then table.insert(new, v) end
end
return new
end
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, vars)
local doc = ct.parse(input.stream, input.src)
input.stream:close()
if mode['parse:show-tree'] then
log:write(dump(doc))
end
if not mode['render:format'] then
error 'what output format should i translate the input to?'
end
if not ct.render[mode['render:format']] then
error(string.format('output format “%s” unsupported', mode['render:format']))
end
local render_opts = kmap(function(k,v)
return k:sub(2+#mode['render:format'])
end, kfilter(mode, function(m)
return startswith(m, mode['render:format']..':')
end))
output:write(ct.render[mode['render:format']](doc, render_opts))
end
local inp,outp,log = io.stdin, io.stdout, io.stderr
local function entry_cli()
local mode, vars, input = default_mode, {}, {
stream = inp;
src = {
file = '(stdin)';
}
}
local optnparams = function(o)
local param_opts = {
out = 1;
log = 1;
define = 2; -- key value
['mode-set'] = 1;
['mode-clear'] = 1;
mode = 2;
}
return param_opts[o] or 0
end
local optmap = {
o = 'out';
l = 'log';
d = 'define';
V = 'version';
h = 'help';
y = 'mode-set', n = 'mode-clear';
m = 'mode';
}
local checkmodekey = function(key)
if not key:match '[^:]+:.+' then
ct.exns.cli('invalid mode key %s', key):throw()
end
return key
end
local onswitch = {
out = function(file)
local nf = io.open(file,'wb')
if nf then outp:close() outp = nf else
ct.exns.io('could not open output file for writing', 'open',file):throw()
end
end;
log = function(file)
local nf = io.open(file,'wb')
if nf then log:close() log = nf else
ct.exns.io('could not open log file for writing', 'open',file):throw()
end
end;
define = function(key,value)
-- set context key
end;
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;
}
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 '^%-%-(.+)$'
if keepParsing and longopt then
execLongOpt(longopt)
else
if keepParsing and v:sub(1,1) == '-' then
for c,p in eachcode(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
end
i = i + 1 end end
if args[1] and args[1] ~= '' then
local file = io.open(arg[1], "rb")
if not file then error('unable to load file ' .. args[1]) end
input.stream = file
input.src.file = args[1]
end
main(input, outp, log, mode, vars)
end
-- local ok, e = pcall(entry_cli)
local ok, e = true, entry_cli()
|