-- License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
-- docbook-to-cde.lua -- DocBook XML (pandoc AST) to the DocBook SGML dialect
-- that MCDE's dtdocbook2infolib reads.
--
-- The CDE information library predates DocBook 4: it knows Book, Chapter,
-- Sect1..Sect4, Para, ItemizedList, OrderedList, VariableList, Screen,
-- Literal, Emphasis, ULink and XRef, and not much else. This writer emits
-- only that subset.
local out = {}
local open = {} -- stack of { level = n, tag = "chapter"|"sect1"|... }
local counter = 0
local function emit(s)
out[#out + 1] = s
end
local function esc(s)
if s == nil then return "" end
s = s:gsub("&", "&")
s = s:gsub("<", "<")
s = s:gsub(">", ">")
return s
end
local function slug(s, fallback)
s = (s or ""):lower()
s = s:gsub("[^%w]+", "-")
s = s:gsub("^%-+", ""):gsub("%-+$", "")
if s == "" then s = fallback end
if not s:match("^[%a]") then s = "id-" .. s end
return s
end
local function attr_id(el)
local id = ""
if el.attr and el.attr.identifier then id = el.attr.identifier end
if id == "" then
counter = counter + 1
id = "MKG." .. tostring(counter)
end
return slug(id, "MKG." .. tostring(counter))
end
local function close_to(level)
while #open > 0 and open[#open].level >= level do
local top = table.remove(open)
emit("" .. top.tag .. ">")
end
end
local render_blocks, render_inlines
render_inlines = function(xs)
for _, x in ipairs(xs) do
local t = x.t
if t == "Str" then
emit(esc(x.text))
elseif t == "Space" then
emit(" ")
elseif t == "SoftBreak" or t == "LineBreak" then
emit("\n")
elseif t == "Emph" then
emit("")
render_inlines(x.content)
emit("")
elseif t == "Strong" then
emit("")
render_inlines(x.content)
emit("")
elseif t == "Code" then
emit("" .. esc(x.text) .. "")
elseif t == "Quoted" or t == "Cite" or t == "Span" or
t == "SmallCaps" or t == "Strikeout" or
t == "Superscript" or t == "Subscript" then
render_inlines(x.content)
elseif t == "Link" then
if x.target:sub(1, 1) == "#" then
emit("")
else
emit("")
render_inlines(x.content)
emit("")
end
elseif t == "Math" then
emit(esc(x.text))
elseif t == "Note" then
emit("")
render_blocks(x.content)
emit("")
elseif t == "Image" then
emit(esc(pandoc.utils.stringify(x.caption)))
elseif t == "RawInline" then
-- nothing from HTML or TeX belongs in the SGML book
else
emit(esc(pandoc.utils.stringify(x)))
end
end
end
render_blocks = function(bs)
for _, b in ipairs(bs) do
local t = b.t
if t == "Header" then
local level = b.level
local tag = "sect4"
if level <= 1 then tag = "chapter"
elseif level == 2 then tag = "sect1"
elseif level == 3 then tag = "sect2"
elseif level == 4 then tag = "sect3"
end
close_to(level)
emit("<" .. tag .. " id=\"" .. attr_id(b) .. "\">")
emit("
")
render_inlines(b.content)
emit("")
open[#open + 1] = { level = level, tag = tag }
elseif t == "Para" or t == "Plain" then
emit("")
render_inlines(b.content)
emit("")
elseif t == "CodeBlock" then
emit("" .. esc(b.text) .. "")
elseif t == "BlockQuote" then
emit("")
render_blocks(b.content)
emit("
")
elseif t == "BulletList" then
emit("")
for _, item in ipairs(b.content) do
emit("")
render_blocks(item)
emit("")
end
emit("")
elseif t == "OrderedList" then
emit("")
for _, item in ipairs(b.content) do
emit("")
render_blocks(item)
emit("")
end
emit("")
elseif t == "DefinitionList" then
emit("")
for _, item in ipairs(b.content) do
emit("")
render_inlines(item[1])
emit("")
for _, def in ipairs(item[2]) do
render_blocks(def)
end
emit("")
end
emit("")
elseif t == "Div" then
local classes = b.attr and b.attr.classes or {}
local note = false
for _, c in ipairs(classes) do
if c == "note" or c == "tip" or c == "warning" or
c == "caution" or c == "important" then
note = true
end
end
if note then
emit("Note: " ..
esc(pandoc.utils.stringify(b.content)) .. "")
else
render_blocks(b.content)
end
elseif t == "LineBlock" then
emit("")
for i, line in ipairs(b.content) do
if i > 1 then emit("") end
render_inlines(line)
end
emit("")
elseif t == "Table" then
emit("" .. esc(pandoc.utils.stringify(b)) .. "")
elseif t == "Figure" then
-- The CDE DTD can carry graphics, but the guide's information library
-- stays text-only: keep the caption, drop the bitmap.
local cap = pandoc.utils.stringify(b.caption)
if cap ~= "" then
emit("Figure: " .. esc(cap) .. "")
end
elseif t == "HorizontalRule" or t == "RawBlock" then
-- no rule in the CDE DTD; raw HTML/TeX is not for the book
else
emit("" .. esc(pandoc.utils.stringify(b)) .. "")
end
end
end
function Writer(doc, opts)
local title = "The Makronix Guide"
if doc.meta and doc.meta.title then
title = pandoc.utils.stringify(doc.meta.title)
end
out = {}
open = {}
counter = 0
render_blocks(doc.blocks)
while #open > 0 do
local top = table.remove(open)
emit("" .. top.tag .. ">")
end
return "\n" ..
"\n" .. esc(title) .. "\n" ..
table.concat(out, "\n") .. "\n\n"
end