Files
makronix/docs/guide/tools/docbook-to-cde.lua
T
lotherk b654bf74a2 docs: add The Makronix Guide and its four installed forms
One DocBook source in docs/guide produces the manual pages, one HTML
file, the PDF and an MCDE information library.  The guide follows the
structure of The NetBSD Guide, describes Makronix 0.99.10
(Camouflage), and has its own chapters for MCDE, the Information
Manager and the display.

The forms are installed by the release:

  /usr/share/doc/makronix/makronix-guide.html
  /usr/share/doc/makronix/makronix-guide.pdf
  /usr/share/doc/makronix/infolib/makronix.dti/   (base-sys-share)
  /usr/share/man/man7/makronix-guide*.7           (man-reference-man)
  /etc/dt/config/Xsession.d/0000makronix          (etc-sys-etc)
  /etc/dt/infolib/en_US.UTF-8/makronix.dti        (symlink)

The information library is built with MCDE's dtdocbook2infolib from a
small Lua writer for the CDE DocBook SGML dialect; tools/test-tools.sh
runs docs/guide/check.sh on the installed copies and proves that a
missing manual page falls red.
2026-11-15 21:21:49 +00:00

207 lines
6.0 KiB
Lua

-- 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("<", "&lt;")
s = s:gsub(">", "&gt;")
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("<emphasis>")
render_inlines(x.content)
emit("</emphasis>")
elseif t == "Strong" then
emit("<emphasis>")
render_inlines(x.content)
emit("</emphasis>")
elseif t == "Code" then
emit("<literal>" .. esc(x.text) .. "</literal>")
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("<xref linkend=\"" .. slug(x.target:sub(2), "ref") .. "\"/>")
else
emit("<ulink url=\"" .. esc(x.target) .. "\">")
render_inlines(x.content)
emit("</ulink>")
end
elseif t == "Math" then
emit(esc(x.text))
elseif t == "Note" then
emit("<footnote>")
render_blocks(x.content)
emit("</footnote>")
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("<title>")
render_inlines(b.content)
emit("</title>")
open[#open + 1] = { level = level, tag = tag }
elseif t == "Para" or t == "Plain" then
emit("<para>")
render_inlines(b.content)
emit("</para>")
elseif t == "CodeBlock" then
emit("<screen>" .. esc(b.text) .. "</screen>")
elseif t == "BlockQuote" then
emit("<blockquote>")
render_blocks(b.content)
emit("</blockquote>")
elseif t == "BulletList" then
emit("<itemizedlist>")
for _, item in ipairs(b.content) do
emit("<listitem>")
render_blocks(item)
emit("</listitem>")
end
emit("</itemizedlist>")
elseif t == "OrderedList" then
emit("<orderedlist>")
for _, item in ipairs(b.content) do
emit("<listitem>")
render_blocks(item)
emit("</listitem>")
end
emit("</orderedlist>")
elseif t == "DefinitionList" then
emit("<variablelist>")
for _, item in ipairs(b.content) do
emit("<varlistentry><term>")
render_inlines(item[1])
emit("</term><listitem>")
for _, def in ipairs(item[2]) do
render_blocks(def)
end
emit("</listitem></varlistentry>")
end
emit("</variablelist>")
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("<para><emphasis>Note:</emphasis> " ..
esc(pandoc.utils.stringify(b.content)) .. "</para>")
else
render_blocks(b.content)
end
elseif t == "LineBlock" then
emit("<para>")
for i, line in ipairs(b.content) do
if i > 1 then emit("<literallayout></literallayout>") end
render_inlines(line)
end
emit("</para>")
elseif t == "Table" then
emit("<para>" .. esc(pandoc.utils.stringify(b)) .. "</para>")
elseif t == "Figure" then
render_blocks(b.content)
elseif t == "HorizontalRule" or t == "RawBlock" then
-- no rule in the CDE DTD; raw HTML/TeX is not for the book
else
emit("<para>" .. esc(pandoc.utils.stringify(b)) .. "</para>")
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 "<!DOCTYPE Book PUBLIC \"-//HaL and O'Reilly//DTD DocBook//EN\">\n" ..
"<Book>\n<Title>" .. esc(title) .. "</Title>\n" ..
table.concat(out, "\n") .. "\n</Book>\n"
end