cortav  Artifact [ab8e0fd21b]

Artifact ab8e0fd21b091ae6e302de0398eab6d5ad291584689d6ecbfab8d502241c6c09:


-- [ʞ] render/groff.lua
--  ~ lexi hale <lexi@hale.su>
--  🄯 AGPLv3
--  ? renders cortav to groff source code, for creating pdfs,
--    dvis, manapages, and html files that are grievously
--    inferior compared to our own illustrious direct-html
--    renderer.
--  > cortav -m render:format groff

local ct = require 'cortav'
local ss = require 'sirsem'

local tcat = function(a,b)
	for i,v in ipairs(b) do
		table.insert(a, b)
	end
	return a
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