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
|
end
local lines = function(...)
local s = ss.strac()
for _, v in pairs{...} do s(v) end
return s
end
function ct.render.groff(doc, opts)
-- rs contains state specific to this render job
-- that modules will need access to
local rs = {};
rs.macsets = {
strike = {
'.de ST';
[[.nr ww \w'\\$1']];
[[\Z@\v'-.25m'\l'\\n[ww]u'@\\$1']];
'..';
};
}
rs.macsNeeded = {
order = {};
count = 0;
}
function rs.macAdd(id)
if rs.macsets[id] then
rs.macsNeeded.count = macsNeeded.count + 1
rs.macsNeeded.order[rs.macsNeeded.count] = id
return true
else return false end
end
local job = doc:job('render_groff',nil,rs)
-- the way this module works is we build up a table for each block
-- of individual strings paired with attributes that say how they
-- should be rendered. we then iterate over the table, applying
-- formats as need be, and inserting blanks after each block
local spanRenderers = {}
function spanRenderers.format(rc, s, b, sec)
local rcc = rc:clone()
if s.style == 'strong' then
rcc.prop.bold = true
elseif s.style == 'emph' then
rcc.prop.emph = true
elseif s.style == 'strike' then
rcc.prop.strike = true
rs.macAdd 'strike'
elseif s.style == 'insert' then
end
rs.renderSpans(rcc, s.spans, b, sec)
end;
function rs.renderSpans(rc, sp, b, sec)
for i, v in ipairs(sp) do
if type(v) == 'string' then
rc:add(v)
elseif spanRenderers[v.kind] then
spanRenderers[v.kind](rc, v, b, sec)
end
end
end
local blockRenderers = {}
function blockRenderers.paragraph(rc, b, sec)
rs.renderSpans(rc, b.spans, b, sec)
end
function rs.renderBlock(b, sec)
local rc = {
clone = function(self)
return {
clone = self.clone;
lines = self.lines;
prop = ss.clone(self.prop);
mk = self.mk;
add = self.add;
}
end;
lines = {};
prop = {};
mk = function(self, ln)
local p = ss.clone(self.prop)
p.txt = ln
return p
end;
add = function(self, ln)
table.insert(self.lines, self:mk(ln))
end;
}
if blockRenderers[b.kind] then
blockRenderers[b.kind](rc, b, sec)
end
return rc.lines
end
function rs.emitLine(ln)
local q = ss.strac()
if ln.dsz then
q('\\ps +' .. tostring(ln.dsz))
elseif ln.sz then
q('\\ps ' .. tostring(ln.dsz))
end
if ln.bold and ln.emph then
q '\\f(BI'
elseif ln.bold then
q '\\fB'
elseif ln.emph then
q '\\fI'
end
q(ln.txt)
if ln.bold or ln.emph then
q'\\f[]'
end
if ln.dsz then
q('.ps -' .. tostring(ln.dsz))
elseif ln.sz then
q '.ps'
end
return q
end
local ir = {}
for i, sec in ipairs(doc.secorder) do
if sec.kind == 'ordinary' then
local blks = {}
for j, b in ipairs(sec.blocks) do
local r = rs.renderBlock(b, sec)
if r then table.insert(blks, r) end
end
table.insert(ir, blks)
end
end
local rd = ss.strac()
for i, s in ipairs(ir) do
for j, b in ipairs(s) do
for z, l in ipairs(b) do
rd(rs.emitLine(l))
end
rd'\n'
end
end
local macs = ss.strac()
for _, m in pairs(rs.macsNeeded.order) do
for _, ln in pairs(m) do macs(ln) end
end
return macs:compile'\n' .. rd:compile''
end
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
|
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
<
>
|
|
|
|
|
|
|
|
|
|
|
<
|
>
>
>
>
>
|
<
<
<
<
|
|
>
|
<
|
>
>
|
<
>
>
|
<
>
>
>
>
>
>
>
>
>
|
<
<
<
>
>
>
>
|
<
>
|
>
>
>
>
|
>
>
>
|
|
|
<
>
|
<
>
|
>
>
<
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
<
<
>
>
>
>
|
>
|
|
<
>
>
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
<
|
|
|
<
|
<
<
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
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
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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
end
local lines = function(...)
local s = ss.strac()
for _, v in pairs{...} do s(v) end
return s
end
local function gsan(str)
local tocodepoint = function(ch)
return string.format('\\[u%04X]', utf8.codepoint(ch))
end
str = str:gsub('(["\'\\])',tocodepoint)
return str
end
local gtxt = ss.declare {
ident = 'groff-text';
mk = function() return {
lines = {};
} end;
fns = {
raw = function(me, text)
if me.linbuf == nil then
me.linbuf = ss.strac()
end
me.linbuf(text)
end;
txt = function(me, str, ...)
if str == nil then return end
me:raw(gsan(str))
-- WARN this will cause problems if str is ever allowed to
-- include a line break. we can sanitize by converting
-- every line break into a new entry in the table, but i
-- don't think it should be possible for a \n to reach us
-- at this point, so i'm omitting the safety check as it
-- would involve an excessive hit to performance
me:txt(...)
end;
brk = function(me)
me:flush()
table.insert(me.lines, '')
end;
line = function(me, ...)
me:flush()
me:txt(...)
end;
req = function(me, r)
me:flush()
table.insert(me.lines, '.'..r)
end;
esc = function(me, e)
me:raw('\\' .. e)
end;
flush = function(me)
if me.linbuf ~= nil then
local line = me.linbuf:compile()
local first = line:sub(1,1)
-- make sure our lines aren't accidentally interpreted
-- as groff requests. groff is kinda hostile to script
-- generation, huh?
if first == '.' or first == "'" then
line = '\\&' ..line
end
table.insert(me.lines, line)
me.linbuf = nil
end
end;
compile = function(me)
me:flush()
return table.concat(me.lines, '\n')
end;
}
}
local function mkColorDef(name, color)
return '.defcolor '..name..' rgb ' ..
table.concat({color:rgb_t()}, ' ', 1, 3)
end
local function addAccentTones(rs,hue,spread)
local base = ss.color(hue, 1, .5)
local right = spread > 0 and ss.color(hue + spread, 1, .5)
or ss.color(hue, 0.4, 0.6)
local left = spread > 0 and ss.color(hue - spread, 1, .5)
or ss.color(hue, 1, 0.3)
local steps = 6
for i=-3,3 do
local nc, nm
local o if i > 0
then o = right nm = 'R'
else o = left nm = 'L'
end
nc = base + o:alt('alpha', math.abs(i) / 3)
rs.addColor('accent'..nm..tostring(math.abs(i)),nc)
end
end
local function mkrc()
return {
clone = function(self, origin)
return {
origin = origin;
clone = self.clone;
prop = ss.clone(self.prop);
mk = self.mk;
add = self.add;
block = self.block;
blocks = self.blocks;
span = self.span;
spans = self.spans;
}
end;
blocks = {};
prop = {};
block = function(self)
local sub = self:clone()
sub.spans = {}
sub.blocks = nil
sub.span = function(me, ln)
local p = ss.clone(me.prop)
p.txt = ln
p.block = sub
p.origin = me.origin
table.insert(me.spans, p)
return p
end;
table.insert(self.blocks, sub)
return sub
end;
}
end
function ct.render.groff(doc, opts, setup)
-- rs contains state specific to this render job
-- that modules will need access to
local fail = function(msg, ...)
ct.exns.rdr(msg, 'groff', ...):throw()
end
local rs = {};
rs.macsets = {
strike = {
'.de ST';
[[.nr ww \w'\\$1']];
[[\Z@\v'-.25m'\l'\\n[ww]u'@\\$1]];
'..';
};
color = {'.color'};
insert = {};
footnote = {
'.de footnote-blank';
'. sp 0.25m';
'..';
'.ev footnote-env';
'. ps 8p';
'. in 0.5c';
'. blm footnote-blank';
'.ev';
'.de footnote-print';
-- '. sp |\\\\n[.p]u-\\\\n[footnote-pos]u';
'. sp 0.5c';
'. ev footnote-env';
'. fn';
'. ev';
'. rm fn';
'. nr footnote-pos 0';
-- move the trap past the bottom of the page so it's not
-- invoked again until more footnotes have been assembled
'. ch footnote-print |\\\\n[.p]u+10';
'. bp';
'..';
'.wh |\\n[.p]u footnote-print';
};
root = {
-- these are macros included in all documents
-- page offset is hideously broken and unusable; we
-- zero it out so we can use .in to control indents
-- instead. note that the upshot of this is we need
-- to manually specify the indent in every other
-- environment from now on, .evc doesn't seem to cut it
-- set up the page title environment & trap
"'in 2c";
"'ll 18c";
"'po 0";
"'ps 13p";
"'vs 15p";
".ev pgti";
". evc 0";
". fam H";
". ps 10pt";
".ev";
'.de ph';
'. sp 0.6c';
'. ev pgti';
'. po 1c';
'. lt 19c';
". tl '\\\\*[doctitle]'\\fB\\\\*[title]\\f[]'%'";
'. po 0';
". br";
'. ev';
'. sp 1.2c';
'..';
'.wh 0 ph';
'.de np';
'. sp 0.2c';
'..';
'.blm np'
};
}
rs.macsNeeded = {
order = {};
map = {};
count = 0;
deps = {
insert = {'color'};
strike = {'color'};
};
}
rs.linkctr = 0
function rs.macAdd(id)
if rs.macsets[id] and not rs.macsNeeded.map[id] then
rs.macsNeeded.count = rs.macsNeeded.count + 1
rs.macsNeeded.order[rs.macsNeeded.count] = id
rs.macsNeeded.map[id] = true
if not rs.macsNeeded.deps[id] then
return true
end
for k,v in pairs(rs.macsNeeded.deps[id]) do
if not rs.macsNeeded.map[v] then
rs.macAdd(v)
end
end
return true
else return false end
end
rs.macAdd 'root'
rs.colors = {}
rs.addColor = function(name,color)
if not ss.color.is(color) then
ss.bug('%s is not a color value', color):throw()
end
rs.colors[name] = color
end
if opts.accent then
addAccentTones(rs, tonumber(opts.accent), tonumber(opts['hue-spread']) or 0)
rs.addColor('new', rs.colors.accentR3)
rs.addColor('del', rs.colors.accentL3)
else
rs.addColor('new', ss.color(80, 1, .3))
rs.addColor('del', ss.color(0, 1, .3))
end
doc.stage = {
type = 'render';
format = 'groff';
groff_render_state = rs;
}
setup(doc.stage)
local job = doc:job('render_groff',nil,rs)
local function collect(rc, spans, b, s)
local rcc = rc:clone()
rcc.spans = {}
rs.renderSpans(rcc, spans, b, s)
return rcc.spans
end
local function collectText(...)
local text = collect(...)
local s = ss.strac()
for i, l in ipairs(text) do
s(l.txt)
end
return s
end
-- the way this module works is we build up a table for each block
-- of individual strings paired with attributes that say how they
-- should be rendered. we then iterate over the table, applying
-- formats as need be, and inserting blanks after each block
local spanRenderers = {}
function spanRenderers.format(rc, s, b, sec)
local rcc = rc:clone()
if s.style == 'strong' then
rcc.prop.bold = true
elseif s.style == 'emph' then
rcc.prop.emph = true
elseif s.style == 'strike' then
rcc.prop.strike = true
rs.macAdd 'strike'
rcc.prop.color = 'del'
elseif s.style == 'insert' then
rs.macAdd 'insert'
rcc.prop.color = 'new'
end
rs.renderSpans(rcc, s.spans, b, sec)
end;
function spanRenderers.link(rc, l, b, sec)
rs.renderSpans(rc, l.spans, b, sec)
rs.linkctr = rs.linkctr + 1
rs.macAdd 'footnote'
local p = rc:span(string.format('[%u]', rs.linkctr))
if type(l.ref) == 'string' then
local t = ''
if b.origin.doc.sections[l.ref] then
local hn = b.origin.doc.sections[l.ref].heading_node
if hn then
t = collectText(rc, hn.spans, b, sec):compile()
end
else
local obj = l.origin:ref(l.ref)
if type(obj) == 'string' then
t = l.origin:ref(l.ref)
end
end
p.div = { fn = tostring(rs.linkctr) .. ') ' .. t }
end
end;
function spanRenderers.raw(rc, s, b, sec)
rs.renderSpans(rc, s.spans, b, sec)
end;
function spanRenderers.var(rc,v,b,s)
local t, raw = ct.expand_var(v)
if raw then rc:span(t) else
rs.renderSpans(rc,t,b,s)
end
end
function spanRenderers.macro(rc, m,b,s)
local macroname = collectText(rc,
ct.parse_span(m.macro, b.origin),
b, s):compile()
local r = b.origin:ref(macroname)
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
rs.renderSpans(rc, ct.parse_span(r, mctx))
end
function rs.renderSpans(rc, sp, b, sec)
rc = rc or mkrc(b.origin)
for i, v in ipairs(sp) do
if type(v) == 'string' then
rc:span(v)
elseif spanRenderers[v.kind] then
spanRenderers[v.kind](rc, v, b, sec)
end
end
end
local blockRenderers = {}
function blockRenderers.label(rc, b, sec)
if ct.sec.is(b.captions) then
local sizes = {36,24,12,8,4,2}
local margins = {0,5,2,1,0.5}
local dedents = {2.5,1.3,0.8,0.4}
rc.prop.dsz = sizes[b.captions.depth] or 10
rc.prop.underline = b.captions.depth < 4
rc.prop.bold = b.captions.depth > 3
rc.prop.margin = {
top = margins[b.captions.depth] or 0;
bottom = 0.1;
}
rc.prop.indent = -(dedents[b.captions.depth] or 0)
rc.prop.underline = true
rc.prop.chtitle = collectText(rc, b.spans, b.spec):compile()
if b.captions.depth == 1 then
rc.prop.breakBefore = true
end
rs.renderSpans(rc, b.spans, b, sec)
else
ss.bug 'tried to render label for an unknown object type':throw()
end
end
function blockRenderers.paragraph(rc, b, sec)
rs.renderSpans(rc, b.spans, b, sec)
end
function rs.renderBlock(rc, b, sec, outerBlockRenderContext)
if blockRenderers[b.kind] then
local rcc = rc:block()
blockRenderers[b.kind](rcc, b, sec)
end
end
rs.sanitize = gsan
local skippedFirstPagebreak = doc.secorder[1]:visible()
local deferrer = ss.declare {
ident = 'groff-deferrer';
mk = function(buf) return {ops={}, tgt=buf} end;
fns = {
esc = function(me, str) table.insert(me.ops, {0, str}) end;
req = function(me, str) table.insert(me.ops, {1, str}) end;
flush = function(me)
for i=#me.ops,1,-1 do
local d = me.ops[i]
if d[1] == 0 then
me.tgt:esc(d[2])
elseif d[1] == 1 then
me.tgt:req(d[2])
end
end
me.ops = {}
end;
};
}
function rs.emitSpan(gtxt, s)
local defer = deferrer(gtxt)
if s.bold or s.emph then
if s.bold and s.emph then
gtxt:esc 'f(BI'
elseif s.bold then
gtxt:esc 'fB'
elseif s.emph then
gtxt:esc 'fI'
end
defer:esc'f[]'
end
if s.color and opts.color then
gtxt:esc('m[' .. s.color .. ']')
defer:esc('m[]')
end
if s.strike then
gtxt:req('ST "'..s.txt..'"')
else
gtxt:txt(s.txt)
end
defer:flush()
if s.div then
for div, body in pairs(s.div) do
if div == 'fn' then
gtxt:req 'ev footnote-env'
end
gtxt:req('boxa '..div)
gtxt:txt(body)
gtxt:raw '\n'
gtxt:req 'boxa'
if div == 'fn' then
gtxt:req 'ev'
gtxt:req 'nr footnote-pos (\\n[footnote-pos]u+\\n[dn]u)'
gtxt:req 'ch footnote-print -(\\n[footnote-pos]u+1c)'
end
end
end
end
function rs.emitBlock(gtxt, b)
local didfinalbreak = false
local defer = deferrer(gtxt)
local ln = b.prop
if ln.chtitle then
gtxt:req('ds title '..ln.chtitle)
end
if ln.breakBefore then
if skippedFirstPagebreak then
gtxt:req 'bp'
else
skippedFirstPagebreak = true
end
end
if ln.indent then
if ln.indent < 0 then
gtxt:req('in '..tostring(ln.indent)..'m')
defer:req 'in'
gtxt:req('ll +'..tostring(-ln.indent)..'m')
defer:req 'll'
else
gtxt:req('in +'..tostring(ln.indent)..'m')
defer:req 'in'
end
defer:req 'br'
end
if ln.margin then
if ln.margin.top then
gtxt:req(string.format('sp %sm', ln.margin.top))
end
end
if ln.underline then
defer:esc("D'l \\n[.ll]u-\\n[.in]u 0'")
defer:esc"v'-0.5'"
defer:req'br'
end
if ln.dsz and ln.dsz > 0 then
gtxt:req('ps +' .. tostring(ln.dsz) .. 'p')
defer:req('ps -' .. tostring(ln.dsz) .. 'p')
elseif ln.sz or ln.dsz then
if ln.sz and ln.sz <= 0 then
ln.origin:fail 'font sizes must be greater than 0'
end
gtxt:req('ps ' .. tostring(ln.sz or ln.dsz) ..'p')
if ln.dsz then
defer:req('ps +' .. tostring(0 - ln.dsz) .. 'p')
else
defer:req'ps'
end
end
for i,s in pairs(b.spans) do
rs.emitSpan(gtxt, s)
end
if ln.margin then
if ln.margin.bottom then
gtxt:req(string.format('sp %sm', ln.margin.bottom))
end
end
defer:flush()
if not ln.margin then gtxt:brk() end
end
local ir = {}
for i, sec in ipairs(doc.secorder) do
if sec.kind == 'ordinary' then
local rc = mkrc()
for j, b in ipairs(sec.blocks) do
rs.renderBlock(rc, b, sec)
end
table.insert(ir, {blocks = rc.blocks, src = sec})
end
end
local gd = gtxt()
for i, s in ipairs(ir) do
for j, b in ipairs(s.blocks) do
rs.emitBlock(gd,b)
end
end
local macs = ss.strac()
for _, m in pairs(rs.macsNeeded.order) do
for _,ln in pairs(rs.macsets[m]) do macs(ln) end
end
if rs.macsNeeded.map.color and opts.color then
for k,v in pairs(rs.colors) do
macs(mkColorDef(k,v))
end
end
local doctitle = '' if opts.title then
doctitle = opts.title
else
local top = math.huge
for i,s in ipairs(doc.secorder) do
if s.heading_node and s.depth < top then
top = s.depth
doctitle = collectText(mkrc():block(), s.heading_node.spans, s.heading_node, s):compile()
end
end
end
macs('.ds doctitle '..doctitle)
return macs:compile'\n' .. '\n' .. gd:compile()
end
|