1
2
3
4
5
6
7
8
9
10
11
..
66
67
68
69
70
71
72
73
74
75
76
77
78
79
...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
...
195
196
197
198
199
200
201
202
203
204
205
206
207
208
...
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
...
266
267
268
269
270
271
272
273
274
275
276
277
278
279
...
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
...
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
...
486
487
488
489
490
491
492
493
494
495
496
497
498
499
...
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
...
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
...
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
...
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
...
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
...
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
...
833
834
835
836
837
838
839
840
841
842
843
844
845
846
...
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
...
974
975
976
977
978
979
980
981
982
983
984
985
986
987
....
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
....
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
....
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
....
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
....
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
....
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
....
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
|
-- [ʞ] cortav.lua
-- ~ lexi hale <lexi@hale.su>
-- © AGPLv3
-- ? renderer
local ct = { render = {} }
local function hexdump(s)
local hexlines, charlines = {},{}
for i=1,#s do
local line = math.floor((i-1)/16) + 1
................................................................................
return str
elseif type(o) == "string" then
return string.format('“%s”', o)
else
return tostring(o)
end
end
local function declare(c)
local cls = setmetatable({
__name = c.ident;
}, {
__name = 'class';
__tostring = function() return c.ident or '(class)' 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)
................................................................................
new.generation = 1
end
end;
fns = {
fail = function(self, msg, ...)
ct.exns.tx(msg, self.src.file, self.line or 0, ...):throw()
end;
ref = function(self,id)
if not id:find'%.' then
local rid = self.sec.refs[id]
if self.sec.refs[id] then
return self.sec.refs[id]
else self:fail("no such ref %s in current section", id or '') end
else
................................................................................
fns = {
mksec = function(self, id, depth)
local o = ct.sec(id, depth)
if id then self.sections[id] = o end
table.insert(self.secorder, o)
return o
end;
};
mk = function() return {
sections = {};
secorder = {};
embed = {};
meta = {};
} end;
}
local function map(fn, lst)
local new = {}
for k,v in pairs(lst) do
table.insert(new, fn(v,k))
................................................................................
local function fmtfn(str)
return function(...)
return string.format(str, ...)
end
end
function ct.render.html(doc, opts)
local f = string.format
local ids = {}
local canonicalID = {}
local function getSafeID(obj)
if canonicalID[obj] then
return canonicalID[obj]
elseif obj.id and ids[obj.id] then
................................................................................
lisp = { color = 0x77ff88 };
fortran = { color = 0xff779a };
python = { color = 0xffd277 };
python = { color = 0xcdd6ff };
}
local stylesets = {
code = [[
code {
background: #000;
color: #fff;
font-family: monospace;
font-size: 90%;
padding: 3px 5px;
}
]];
abbr = [[
abbr[title] { cursor: help; }
]];
editors_markup = [[]];
block_code_listing = [[
section > figure.listing {
font-family: monospace;
background: #000;
color: #fff;
padding: 0;
margin: 0.3em 0;
counter-reset: line-number;
position: relative;
}
section > figure.listing>div {
white-space: pre-wrap;
counter-increment: line-number;
text-indent: -2.3em;
margin-left: 2.3em;
}
section > figure.listing>:is(div,hr)::before {
width: 1.0em;
padding: 0.2em 0.4em;
text-align: right;
display: inline-block;
background-color: #333;
border-right: 1px solid #fff;
content: counter(line-number);
margin-right: 0.3em;
}
section > figure.listing>hr::before {
color: #333;
padding-top: 0;
padding-bottom: 0;
}
section > figure.listing>div::before {
color: #fff;
}
section > figure.listing>div:last-child::before {
padding-bottom: 0.5em;
}
section > figure.listing>figcaption:first-child {
border: none;
border-bottom: 1px solid #fff;
}
section > figure.listing>figcaption::after {
display: block;
float: right;
font-weight: normal;
font-style: italic;
font-size: 70%;
padding-top: 0.3em;
}
section > figure.listing>figcaption {
font-family: sans-serif;
font-weight: bold;
font-size: 130%;
padding: 0.2em 0.4em;
border: none;
}
section > figure.listing > hr {
border: none;
margin: 0;
height: 0.7em;
counter-increment: line-number;
}
................................................................................
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
function span_renderers.term(t,b)
local r = b.origin:ref(t.ref)
local name = t.ref
if name:find'%.' then name = name:match '^[^.]*%.(.+)$' end
if type(r) ~= 'string' then
b.origin:fail('%s is an object, not a reference', t.ref)
end
stylesNeeded.abbr = true
return tag('abbr',{title=r},next(t.spans) and htmlSpan(t.spans) or name)
end
function span_renderers.link(sp,b)
local href
if b.origin.doc.sections[sp.ref] then
href = '#' .. sp.ref
else
if sp.addr then href = sp.addr else
local r = b.origin:ref(sp.ref)
if type(r) == 'table' then
href = '#' .. getSafeID(r)
else href = r end
end
end
return tag('a',{href=href},next(sp.spans) and htmlSpan(sp.spans) or href)
end
return {
span_renderers = span_renderers;
htmlSpan = htmlSpan;
htmlDoc = htmlDoc;
}
end
................................................................................
end
return lst
end
local block_renderers = {
paragraph = function(b,s)
return tag('p', nil, sr.htmlSpan(b.spans, b, s), b)
end;
label = function(b,s)
if ct.sec.is(b.captions) then
local h = math.min(6,math.max(1,b.captions.depth))
return tag(f('h%u',h), nil, sr.htmlSpan(b.spans, b, s), b)
else
-- handle other uses of labels here
................................................................................
else
return elt('hr')
end
end, b.lines)
if b.title then
table.insert(nodes,1,tag('figcaption',nil,sr.htmlSpan(b.title)))
end
langsused[b.lang] = true
return tag('figure', {class='listing', lang=b.lang, id=b.id and getSafeID(b)}, catenate(nodes))
end;
['break'] = function() --[[nop]] end;
}
return block_renderers;
end
................................................................................
end
end, attrs)) or '')
end
local tag = function(t,attrs,body)
return f('%s%s</%s>', elt(t,attrs), body, t)
end
local doctitle
local ir = {}
local toc
local dr = getRenderers(tag,elt,table.concat) -- default renderers
local plainr = getRenderers(function(t,a,v) return v end,
function(t,a) return '' end, table.concat)
local irBlockRdrs = getBlockRenderers(
function(t,a,v,o) return {tag = t, attrs = a, nodes = type(v) == 'string' and {v} or v, src = o} end,
................................................................................
for k in pairs(langsused) do
local spec = langpairs[k] or {color=0xaaaaaa}
stylesets.block_code_listing = stylesets.block_code_listing .. string.format(
[[section > figure.listing[lang="%s"]>figcaption::after
{ content: '%s'; color: #%06x }]],
k, spec.name or k, spec.color)
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
................................................................................
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)
return string.sub(str, 1, #pfx) == pfx
end
local function eachcode(str, ascode)
local pos = {
code = 1;
byte = 1;
}
return function()
if pos.byte > #str then return nil end
................................................................................
pos.byte = pos.byte + #utf8.char(thischar)
end
pos.code = pos.code + 1
return thischar, lastpos
end
end
local function formatter(sty)
return function(s,c)
return {
kind = 'format';
style = sty;
spans = ct.parse_span(s, c);
origin = c:clone();
}
end
end
ct.spanctls = {
{seq = '$', parse = formatter 'literal'};
{seq = '!', parse = formatter 'emph'};
{seq = '*', parse = formatter 'strong'};
{seq = '\\', parse = function(s, c) -- raw
return s
end};
{seq = '$\\', parse = function(s, c) -- raw
return {
kind = 'format';
style = 'literal';
spans = {s};
origin = c:clone();
}
end};
{seq = '&', parse = function(s, c)
local r, t = s:match '^([^%s]+)%s*(.-)$'
return {
kind = 'term';
spans = (t and t ~= "") and ct.parse_span(t, c) or {};
ref = r;
origin = c:clone();
}
end};
{seq = '^', parse = function(s, c)
local fn, t = s:match '^([^%s]+)%s*(.-)$'
return {
kind = 'footnote';
spans = (t and t~='') and ct.parse_span(t, c) or {};
ref = fn;
origin = c:clone();
}
end};
{seq = '>', parse = function(s, c)
local to, t = s:match '^([^%s]+)%s*(.-)$'
if not to then c:fail('invalid link syntax >%s', s) end
if t == "" then t = nil end
return {
kind = 'link';
spans = (t and t~='') and ct.parse_span(t, c) or {};
ref = to;
origin = c:clone();
}
end};
}
function ct.parse_span(str,ctx)
local function delimited(start, stop, s)
local depth = 0
if not startswith(s, start) then return nil end
for c,p in eachcode(s) do
if c == '\\' then
................................................................................
return s:sub(1+#start, p.byte - #stop), p.byte -- FIXME
elseif depth < 0 then
ctx:fail('out of place %s', stop)
end
end
end
ctx:fail('%s expected before end of line', stop)
end
local buf = ""
local spans = {}
local function flush()
if buf ~= "" then
table.insert(spans, buf)
buf = ""
................................................................................
local skip = false
for c,p in eachcode(str) do
if skip == true then
skip = false
buf = buf .. c
elseif c == '\\' then
skip = true
elseif c == '[' then
flush()
local substr, following = delimited('[',']',str:sub(p.byte))
p.next.byte = following + p.byte
local found = false
for _,i in pairs(ct.spanctls) do
if startswith(substr, i.seq) then
................................................................................
c.expand_next = 0
end
end;
}
local function insert_table_row(l,c)
local row = {}
for kind, a1, text, a2 in l:gmatch '([+|])(:?)%s*([^:+|]*)%s*(:?)' do
local header = kind == '+'
local align
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
local tbl = c.sec.blocks[#c.sec.blocks]
table.insert(tbl.rows, row)
else
table.insert(c.sec.blocks, {
kind = 'table';
................................................................................
})
end
end
ct.ctlseqs = {
{seq = '.', fn = insert_paragraph};
{seq = '¶', fn = insert_paragraph};
{seq = '#', fn = insert_section};
{seq = '§', fn = insert_section};
{seq = '+', fn = insert_table_row};
{seq = '|', fn = insert_table_row};
{seq = '│', fn = insert_table_row};
{pred = function(s,c) return s:match'^[*:]' end, fn = blockwrap(function(l,c) -- list
local stars = l:match '^([*:]+)'
................................................................................
local words = function(i)
local wds = {}
if i == 0 then return cmdline end
for w,pos in cmdline:gmatch '([^%s]+)()' do
table.insert(wds, w)
i = i - 1
if i == 0 then
return table.unpack(wds), cmdline:sub(pos)
end
end
end
local cmd, rest = words(1)
if ct.directives[cmd] then
ct.directives[cmd](words,c)
elseif crit == '!' then
c:fail('critical directive %s not supported',cmd)
end
end;};
{seq = '~~~', fn = blockwrap(function(l,c)
local extract = function(ptn, str)
local start, stop = str:find(ptn)
................................................................................
return ex, n
end
local lang, id, title
if l:match '^~~~%s*$' then -- no args
elseif l:match '^~~~.*~~~%s*$' then -- CT style
local s = l:match '^~~~%s*(.-)%s*~~~%s*$'
lang, s = extract('%b[]', s)
lang = lang:sub(2,-2)
id, title = extract('#[^%s]+', s)
if id then id = id:sub(2) end
elseif l:match '^~~~' then -- MD shorthand style
lang = l:match '^~~~%s*(.-)%s*$'
end
c.mode = {
kind = 'code';
................................................................................
end
end; fn = blockwrap(function()
return { kind = 'horiz-rule' }
end)};
{fn = insert_paragraph};
}
function ct.parse(file, src)
local function
is_whitespace(cp)
return cp == 0x20
end
local ctx = ct.ctx.mk(src)
ctx.line = 0
ctx.doc = ct.doc.mk()
ctx.sec = ctx.doc:mksec() -- toplevel section
ctx.sec.origin = ctx:clone()
for full_line in file:lines() do ctx.line = ctx.line + 1
local l
for p, c in utf8.codes(full_line) do
if not is_whitespace(c) then
................................................................................
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;
................................................................................
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 = {}
................................................................................
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()
if not ok then
local str = 'translation failure'
local color = false
if log:seek() == nil then
-- this is not a very reliable heuristic for detecting
-- attachment to a tty but it's better than nothing
if os.getenv('COLORTERM') then
color = true
else
................................................................................
local term = os.getenv('TERM')
if term:find 'color' then color = true end
end
end
if color then
str = string.format('\27[1;31m%s\27[m', str)
end
log:write(string.format('[%s] %s\n\t%s\n', os.date(), str, e))
os.exit(1)
end
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
>
|
|
|
|
|
<
|
>
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
<
<
<
>
|
|
|
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
<
<
<
<
<
<
>
>
>
>
|
|
<
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
<
<
|
>
>
|
>
>
>
>
>
>
>
>
>
|
<
>
>
|
<
>
|
|
<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
<
>
>
|
>
>
>
>
>
>
>
|
|
>
>
>
>
>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
|
|
|
>
>
>
|
>
|
1
2
3
4
5
6
7
8
9
10
11
..
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
...
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
...
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
...
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
...
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
...
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
...
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
...
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
...
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
...
669
670
671
672
673
674
675
676
677
678
679
680
681
682
...
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
...
887
888
889
890
891
892
893
894
895
896
897
898
899
900
...
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
....
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
....
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
....
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
....
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
....
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
....
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
....
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
....
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
....
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
....
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
....
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
|
-- [ʞ] cortav.lua
-- ~ lexi hale <lexi@hale.su>
-- © AGPLv3
-- ? reference implementation of the cortav document language
local ct = { render = {} }
local function hexdump(s)
local hexlines, charlines = {},{}
for i=1,#s do
local line = math.floor((i-1)/16) + 1
................................................................................
return str
elseif type(o) == "string" then
return string.format('“%s”', o)
else
return tostring(o)
end
end
local function
lerp(t, a, b)
return (1-t)*a + (t*b)
end
local function
startswith(str, pfx)
return string.sub(str, 1, #pfx) == pfx
end
local function declare(c)
local cls = setmetatable({
__name = c.ident;
}, {
__name = 'class';
__tostring = function() return c.ident or '(class)' 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);
unimpl = ct.exnkind 'feature not implemented';
}
ct.ctx = declare {
mk = function(src) return {src = src} end;
ident = 'context';
cast = {
string = function(me)
................................................................................
new.generation = 1
end
end;
fns = {
fail = function(self, msg, ...)
ct.exns.tx(msg, self.src.file, self.line or 0, ...):throw()
end;
insert = function(self, block)
block.origin = self:clone()
table.insert(self.sec.blocks,block)
end;
ref = function(self,id)
if not id:find'%.' then
local rid = self.sec.refs[id]
if self.sec.refs[id] then
return self.sec.refs[id]
else self:fail("no such ref %s in current section", id or '') end
else
................................................................................
fns = {
mksec = function(self, id, depth)
local o = ct.sec(id, depth)
if id then self.sections[id] = o end
table.insert(self.secorder, o)
return o
end;
context_var = function(self, var, ctx, test)
local fail = function(...)
if test then return false end
ctx:fail(...)
end
if startswith(var, 'cortav.') then
local v = var:sub(8)
if v == 'page' then
if ctx.page then return tostring(ctx.page)
else return '(unpaged)' end
elseif v == 'renderer' then
if not self.stage then
return fail 'document is not being rendererd'
end
return self.stage.format
elseif v == 'datetime' then
return os.date()
elseif v == 'time' then
return os.date '%H:%M:%S'
elseif v == 'date' then
return os.date '%A %d %B %Y'
elseif v == 'id' then
return 'cortav.lua (reference implementation)'
elseif v == 'file' then
return self.src.file
else
return fail('unimplemented predefined variable %s', var)
end
elseif startswith(var, 'env.') then
local v = var:sub(5)
local val = os.getenv(v)
if not val then
return fail('undefined environment variable %s', v)
end
elseif self.stage.kind == 'render' and startswith(var, self.stage.format..'.') then
-- TODO query the renderer somehow
return fail('renderer %s does not implement variable %s', self.stage.format, var)
elseif self.vars[var] then
return self.vars[var]
else
if test then return false end
return '' -- is this desirable behavior?
end
end;
};
mk = function() return {
sections = {};
secorder = {};
embed = {};
meta = {};
vars = {};
} end;
}
local function map(fn, lst)
local new = {}
for k,v in pairs(lst) do
table.insert(new, fn(v,k))
................................................................................
local function fmtfn(str)
return function(...)
return string.format(str, ...)
end
end
function ct.render.html(doc, opts)
local doctitle = opts['title']
local f = string.format
local ids = {}
local canonicalID = {}
local function getSafeID(obj)
if canonicalID[obj] then
return canonicalID[obj]
elseif obj.id and ids[obj.id] then
................................................................................
lisp = { color = 0x77ff88 };
fortran = { color = 0xff779a };
python = { color = 0xffd277 };
python = { color = 0xcdd6ff };
}
local stylesets = {
accent = [[
body { background: @bg; color: @fg }
a[href] {
color: @tone(0.7 30);
text-decoration-color: @tone/0.4(0.7 30);
}
a[href]:hover {
color: @tone(0.9 30);
text-decoration-color: @tone/0.7(0.7 30);
}
h1,h2,h3,h4,h5,h6 {
color: @tone(2);
border-bottom: 1px solid @tone(0.7);
}
]];
code = [[
code {
background: @fg;
color: @bg;
font-family: monospace;
font-size: 90%;
padding: 3px 5px;
}
]];
abbr = [[
abbr[title] { cursor: help; }
]];
editors_markup = [[]];
block_code_listing = [[
section > figure.listing {
font-family: monospace;
background: @tone(0.05);
color: @fg;
padding: 0;
margin: 0.3em 0;
counter-reset: line-number;
position: relative;
border: 1px solid @fg;
}
section > figure.listing>div {
white-space: pre-wrap;
counter-increment: line-number;
text-indent: -2.3em;
margin-left: 2.3em;
}
section > figure.listing>:is(div,hr)::before {
width: 1.0em;
padding: 0.2em 0.4em;
text-align: right;
display: inline-block;
background-color: @tone(0.2);
border-right: 1px solid @fg;
content: counter(line-number);
margin-right: 0.3em;
}
section > figure.listing>hr::before {
color: transparent;
padding-top: 0;
padding-bottom: 0;
}
section > figure.listing>div::before {
color: @fg;
}
section > figure.listing>div:last-child::before {
padding-bottom: 0.5em;
}
section > figure.listing>figcaption:first-child {
border: none;
border-bottom: 1px solid @fg;
}
section > figure.listing>figcaption::after {
display: block;
float: right;
font-weight: normal;
font-style: italic;
font-size: 70%;
padding-top: 0.3em;
}
section > figure.listing>figcaption {
font-family: sans-serif;
font-size: 120%;
padding: 0.2em 0.4em;
border: none;
color: @tone(2);
}
section > figure.listing > hr {
border: none;
margin: 0;
height: 0.7em;
counter-increment: line-number;
}
................................................................................
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
function span_renderers.term(t,b,s)
local r = b.origin:ref(t.ref)
local name = t.ref
if name:find'%.' then name = name:match '^[^.]*%.(.+)$' end
if type(r) ~= 'string' then
b.origin:fail('%s is an object, not a reference', t.ref)
end
stylesNeeded.abbr = true
return tag('abbr',{title=r},next(t.spans) and htmlSpan(t.spans,b,s) or name)
end
function span_renderers.macro(m,b,s)
local r = b.origin:ref(m.macro)
if type(r) ~= 'string' then
b.origin:fail('%s is an object, not a reference', t.ref)
end
local mctx = b.origin:clone()
mctx.invocation = m
return htmlSpan(ct.parse_span(r, mctx),b,s)
end
function span_renderers.var(v,b,s)
local val
if v.pos then
if not v.origin.invocation then
v.origin:fail 'positional arguments can only be used in a macro invocation'
elseif not v.origin.invocation.args[v.pos] then
v.origin:fail('macro invocation %s missing positional argument #%u', v.origin.invocation.macro, v.pos)
end
val = v.origin.invocation.args[v.pos]
else
val = v.origin.doc:context_var(v.var, v.origin)
end
if v.raw then
return val
else
return htmlSpan(ct.parse_span(val, v.origin), b, s)
end
end
function span_renderers.link(sp,b,s)
local href
if b.origin.doc.sections[sp.ref] then
href = '#' .. sp.ref
else
if sp.addr then href = sp.addr else
local r = b.origin:ref(sp.ref)
if type(r) == 'table' then
href = '#' .. getSafeID(r)
else href = r end
end
end
return tag('a',{href=href},next(sp.spans) and htmlSpan(sp.spans,b,s) or href)
end
return {
span_renderers = span_renderers;
htmlSpan = htmlSpan;
htmlDoc = htmlDoc;
}
end
................................................................................
end
return lst
end
local block_renderers = {
paragraph = function(b,s)
return tag('p', nil, sr.htmlSpan(b.spans, b, s), b)
end;
directive = function(b,s)
-- deal with renderer directives
local _, cmd, args = b.words(2)
if cmd == 'page-title' then
if not opts.title then doctitle = args end
elseif b.critical then
b.origin:fail('critical HTML renderer directive “%s” not supported', cmd)
end
end;
label = function(b,s)
if ct.sec.is(b.captions) then
local h = math.min(6,math.max(1,b.captions.depth))
return tag(f('h%u',h), nil, sr.htmlSpan(b.spans, b, s), b)
else
-- handle other uses of labels here
................................................................................
else
return elt('hr')
end
end, b.lines)
if b.title then
table.insert(nodes,1,tag('figcaption',nil,sr.htmlSpan(b.title)))
end
if b.lang then langsused[b.lang] = true end
return tag('figure', {class='listing', lang=b.lang, id=b.id and getSafeID(b)}, catenate(nodes))
end;
['break'] = function() --[[nop]] end;
}
return block_renderers;
end
................................................................................
end
end, attrs)) or '')
end
local tag = function(t,attrs,body)
return f('%s%s</%s>', elt(t,attrs), body, t)
end
local ir = {}
local toc
local dr = getRenderers(tag,elt,table.concat) -- default renderers
local plainr = getRenderers(function(t,a,v) return v end,
function(t,a) return '' end, table.concat)
local irBlockRdrs = getBlockRenderers(
function(t,a,v,o) return {tag = t, attrs = a, nodes = type(v) == 'string' and {v} or v, src = o} end,
................................................................................
for k in pairs(langsused) do
local spec = langpairs[k] or {color=0xaaaaaa}
stylesets.block_code_listing = stylesets.block_code_listing .. string.format(
[[section > figure.listing[lang="%s"]>figcaption::after
{ content: '%s'; color: #%06x }]],
k, spec.name or k, spec.color)
end
local prepcss = function(css)
local tone = function(fac, sat, sep, alpha)
local hsl = function(h,s,l,a)
local v = string.format('%s, %u%%, %u%%', h,s,l)
if a then
return string.format('hsla(%s, %s)', v,a)
else
return string.format('hsl(%s)', v)
end
end
sat = sat or 1
fac = math.max(math.min(fac, 1), 0)
sat = math.max(math.min(sat, 1), 0)
if opts.accent then
local hue = 'var(--accent)'
local hsep = tonumber(opts['hue-spread'])
if hsep and sep and sep ~= 0 then
hue = string.format('calc(%s - %s)', hue, sep * hsep)
end
return hsl(hue, math.floor(100*sat), math.floor(100*fac), alpha)
else
local g = math.floor(0xFF * fac)
return string.format('#' .. string.rep('%02x',alpha and 4 or 3), g,g,g,alpha and math.floor(0xFF*alpha))
end
end
local replace = function(var,alpha,param)
local tonespan = opts.accent and .1 or 0
local tbg = opts['dark-on-light'] and 1.0 - tonespan or tonespan
local tfg = opts['dark-on-light'] and tonespan or 1.0 - tonespan
if var == 'bg' then
return tone(tbg,nil,nil,tonumber(alpha))
elseif var == 'fg' then
return tone(tfg,nil,nil,tonumber(alpha))
elseif var == 'tone' then
local l, sep, sat
for i=1,3 do -- 🙄
l,sep,sat = param:match('^%('..string.rep('([^%s]*)%s*',i)..'%)$')
if l then break end
end
l = lerp(tonumber(l), tbg, tfg)
return tone(l, tonumber(sat), tonumber(sep), tonumber(alpha))
end
end
css = css:gsub('@(%w+)/([0-9.]+)(%b())', replace)
css = css:gsub('@(%w+)(%b())', function(a,b) return replace(a,nil,b) end)
css = css:gsub('@(%w+)/([0-9.]+)', replace)
css = css:gsub('@(%w+)', function(a,b) return replace(a,nil,b) end)
return (css:gsub('%s+',' '))
end
local styles = {}
if opts.width then
table.insert(styles, string.format([[body {padding:0 1em;margin:auto;max-width:%s}]], opts.width))
end
if opts.accent then
table.insert(styles, string.format(':root {--accent:%s}', opts.accent))
end
if opts.accent or (not opts['dark-on-light']) then
stylesNeeded.accent = true
end
for k in pairs(stylesNeeded) do
if not stylesets[k] then ct.exns.unimpl('styleset %s not implemented (!)', k):throw() end
table.insert(styles, prepcss(stylesets[k]))
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
................................................................................
elseif opts.snippet then
return styletag .. body
else
return dr.htmlDoc(doctitle, next(head) and table.concat(head), body)
end
end
local function eachcode(str, ascode)
local pos = {
code = 1;
byte = 1;
}
return function()
if pos.byte > #str then return nil end
................................................................................
pos.byte = pos.byte + #utf8.char(thischar)
end
pos.code = pos.code + 1
return thischar, lastpos
end
end
do -- define span control sequences
local function formatter(sty)
return function(s,c)
return {
kind = 'format';
style = sty;
spans = ct.parse_span(s, c);
origin = c:clone();
}
end
end
local function insert_link(s, c)
local to, t = s:match '^([^%s]+)%s*(.-)$'
if not to then c:fail('invalid link syntax >%s', s) end
if t == "" then t = nil end
return {
kind = 'link';
spans = (t and t~='') and ct.parse_span(t, c) or {};
ref = to;
origin = c:clone();
}
end
local function insert_var_ref(raw)
return function(s, c)
local pos = tonumber(s)
return {
kind = 'var';
pos = pos;
raw = raw;
var = not pos and s or nil;
origin = c:clone();
}
end
end
ct.spanctls = {
{seq = '$', parse = formatter 'literal'};
{seq = '!', parse = formatter 'emph'};
{seq = '*', parse = formatter 'strong'};
{seq = '\\', parse = function(s, c) -- raw
return s
end};
{seq = '$\\', parse = function(s, c) -- raw
return {
kind = 'format';
style = 'literal';
spans = {s};
origin = c:clone();
}
end};
{seq = '&', parse = function(s, c)
local r, t = s:match '^([^%s]+)%s*(.-)$'
return {
kind = 'term';
spans = (t and t ~= "") and ct.parse_span(t, c) or {};
ref = r;
origin = c:clone();
}
end};
{seq = '^', parse = function(s, c)
local fn, t = s:match '^([^%s]+)%s*(.-)$'
return {
kind = 'footnote';
spans = (t and t~='') and ct.parse_span(t, c) or {};
ref = fn;
origin = c:clone();
}
end};
{seq = '>', parse = insert_link};
{seq = '→', parse = insert_link};
{seq = '🔗', parse = insert_link};
{seq = '##', parse = insert_var_ref(true)};
{seq = '#', parse = insert_var_ref(false)};
}
end
function ct.parse_span(str,ctx)
local function delimited(start, stop, s)
local depth = 0
if not startswith(s, start) then return nil end
for c,p in eachcode(s) do
if c == '\\' then
................................................................................
return s:sub(1+#start, p.byte - #stop), p.byte -- FIXME
elseif depth < 0 then
ctx:fail('out of place %s', stop)
end
end
end
ctx:fail('[%s] expected before end of line', stop)
end
local buf = ""
local spans = {}
local function flush()
if buf ~= "" then
table.insert(spans, buf)
buf = ""
................................................................................
local skip = false
for c,p in eachcode(str) do
if skip == true then
skip = false
buf = buf .. c
elseif c == '\\' then
skip = true
elseif c == '{' then
flush()
local substr, following = delimited('{','}',str:sub(p.byte))
local splitstart, splitstop = substr:find'%s+'
local id, argstr
if splitstart then
id, argstr = substr:sub(1,splitstart-1), substr:sub(splitstop+1)
else
id, argstr = substr, ''
end
local o = {
kind = 'macro';
macro = id;
args = {};
origin = ctx:clone();
}
do local start = 1
local i = 1
while i <= #argstr do
while i<=#argstr and (argstr:sub(i,i) ~= '|' or argstr:sub(i-1,i) == '\\|') do
i = i + 1
end
local arg = argstr:sub(start, i == #argstr and i or i-1)
start = i+1
table.insert(o.args, arg)
i = i + 1
end
end
p.next.byte = p.next.byte + following - 1
table.insert(spans,o)
elseif c == '[' then
flush()
local substr, following = delimited('[',']',str:sub(p.byte))
p.next.byte = following + p.byte
local found = false
for _,i in pairs(ct.spanctls) do
if startswith(substr, i.seq) then
................................................................................
c.expand_next = 0
end
end;
}
local function insert_table_row(l,c)
local row = {}
local buf
local flush = function()
if buf then table.insert(row, buf) end
buf = { str = '' }
end
for c,p in eachcode(l) do
if c == '|' or c == '+' and (p.code == 1 or l:sub(p.byte-1,p.byte-1)~='\\') then
flush()
buf.header = c == '+'
elseif c == ':' then
local lst = l:sub(p.byte-#c,p.byte-#c)
local nxt = l:sub(p.next.byte,p.next.byte)
if lst == '|' or lst == '+' and l:sub(p.byte-2,p.byte-2) ~= '\\' then
buf.align = 'left'
elseif nxt == '|' or nxt == '|' then
if buf.align == 'left' then
buf.align = 'center'
else
buf.align = 'right'
end
else
buf.str = buf.str .. c
end
elseif c:match '%s' then
if buf.str ~= '' then buf.str = buf.str .. c end
elseif c == '\\' then
local nxt = l:sub(p.next.byte,p.next.byte)
if nxt == '|' or nxt == '+' or nxt == ':' then
buf.str = buf.str .. nxt
p.next.byte = p.next.byte + #nxt
p.next.code = p.next.code + 1
else
buf.str = buf.str .. c
end
else
buf.str = buf.str .. c
end
end
if buf.str ~= '' then flush() end
for _,v in pairs(row) do
v.spans = ct.parse_span(v.str, c)
end
if #c.sec.blocks > 1 and c.sec.blocks[#c.sec.blocks].kind == 'table' then
local tbl = c.sec.blocks[#c.sec.blocks]
table.insert(tbl.rows, row)
else
table.insert(c.sec.blocks, {
kind = 'table';
................................................................................
})
end
end
ct.ctlseqs = {
{seq = '.', fn = insert_paragraph};
{seq = '¶', fn = insert_paragraph};
{seq = '❡', fn = insert_paragraph};
{seq = '#', fn = insert_section};
{seq = '§', fn = insert_section};
{seq = '+', fn = insert_table_row};
{seq = '|', fn = insert_table_row};
{seq = '│', fn = insert_table_row};
{pred = function(s,c) return s:match'^[*:]' end, fn = blockwrap(function(l,c) -- list
local stars = l:match '^([*:]+)'
................................................................................
local words = function(i)
local wds = {}
if i == 0 then return cmdline end
for w,pos in cmdline:gmatch '([^%s]+)()' do
table.insert(wds, w)
i = i - 1
if i == 0 then
table.insert(wds,cmdline:sub(pos))
return table.unpack(wds)
end
end
end
local cmd, rest = words(1)
if ct.directives[cmd] then
ct.directives[cmd](words,c)
elseif cmd == c.doc.stage.mode['render:format'] then
-- this is a directive for the renderer; insert it into the tree as is
c:insert {
kind = 'directive';
critical = crit == '!';
words = words;
}
elseif crit == '!' then
c:fail('critical directive %s not supported',cmd)
end
end;};
{seq = '~~~', fn = blockwrap(function(l,c)
local extract = function(ptn, str)
local start, stop = str:find(ptn)
................................................................................
return ex, n
end
local lang, id, title
if l:match '^~~~%s*$' then -- no args
elseif l:match '^~~~.*~~~%s*$' then -- CT style
local s = l:match '^~~~%s*(.-)%s*~~~%s*$'
lang, s = extract('%b[]', s)
if lang then lang = lang:sub(2,-2) end
id, title = extract('#[^%s]+', s)
if id then id = id:sub(2) end
elseif l:match '^~~~' then -- MD shorthand style
lang = l:match '^~~~%s*(.-)%s*$'
end
c.mode = {
kind = 'code';
................................................................................
end
end; fn = blockwrap(function()
return { kind = 'horiz-rule' }
end)};
{fn = insert_paragraph};
}
function ct.parse(file, src, mode)
local function
is_whitespace(cp)
return cp == 0x20
end
local ctx = ct.ctx.mk(src)
ctx.line = 0
ctx.doc = ct.doc.mk()
ctx.doc.src = src
ctx.doc.stage = {
kind = 'parse';
mode = mode;
}
ctx.sec = ctx.doc:mksec() -- toplevel section
ctx.sec.origin = ctx:clone()
for full_line in file:lines() do ctx.line = ctx.line + 1
local l
for p, c in utf8.codes(full_line) do
if not is_whitespace(c) then
................................................................................
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, mode)
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 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 startswith(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
doc.stage = {
kind = 'render';
format = mode['render:format'];
mode = mode;
}
output:write(ct.render[mode['render:format']](doc, render_opts))
return 0
end
local inp,outp,log = io.stdin, io.stdout, io.stderr
local function entry_cli()
local mode, vars, input = default_mode, {}, {
stream = inp;
................................................................................
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)
if startswith(key, 'cortav.') or startswith(key, 'env.') then
ct.exns.cli 'cannot define variable in restricted namespace':throw()
end
vars[key] = value
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 = {}
................................................................................
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
return main(input, outp, log, mode, vars)
end
local ok, e = pcall(entry_cli)
-- local ok, e = true, entry_cli()
if not ok then
local str = 'translation failure'
if ct.exn.is(e) then
str = e.kind.desc
end
local color = false
if log:seek() == nil then
-- this is not a very reliable heuristic for detecting
-- attachment to a tty but it's better than nothing
if os.getenv('COLORTERM') then
color = true
else
................................................................................
local term = os.getenv('TERM')
if term:find 'color' then color = true end
end
end
if color then
str = string.format('\27[1;31m%s\27[m', str)
end
log:write(string.format('%s: %s\n', str, e))
os.exit(1)
end
os.exit(e)
|