Skip to content

Instantly share code, notes, and snippets.

@tarleb
Last active March 21, 2023 07:43
Show Gist options
  • Save tarleb/c34c396b21208b91164a8722b7aca2c2 to your computer and use it in GitHub Desktop.
Save tarleb/c34c396b21208b91164a8722b7aca2c2 to your computer and use it in GitHub Desktop.
Lua filter to clean-up the Lua manual markup
-- There is no glyph for pi in the default font, but there is one in the
-- math font.
function Emph (e)
local s = e.content[1]
if #e.content == 1 and s.tag == 'Str' and s.text == 'π' then
return pandoc.RawInline('tex', '$\\pi$')
end
end
-- No glyph available for `≤` in the monospaced font.
function Code (c)
c.text = c.text:gsub('≤', '<=')
return c
end
function Header (h)
-- The header is wrapped in a span containing the id. Use that id
-- instead of the one automatically assigned by pandoc. Some headers
-- start with the section number and an em-dash, so the span is the
-- fifth element (Lua multipass).
local span
if h.content[1].tag == 'Str' and h.content[1].text:match '[%d%.]+' then
span = h.content[5]
else
span = h.content[1]
h.classes:insert('unnumbered')
end
h.identifier = span.identifier
h.content = span.content
return h
end
-- Remove menubar
function Div (d)
if d.classes:includes 'menubar' then
return {}
end
end
-- Cleanup metadata
function Pandoc (doc)
-- comma separated authors
local authors = doc.blocks[2]
authors.content:remove(1) -- remove 'by'
doc.meta.author = pandoc.List()
for author in pandoc.utils.stringify(authors):gmatch '[^,]+' do
doc.meta.author:insert(author)
end
-- remove title, authors
doc.blocks:remove(2)
doc.blocks:remove(1)
-- add subtitle image
doc.meta.subtitle = pandoc.MetaInlines{
pandoc.RawInline('tex', '\\vspace{1em}'),
pandoc.Image("Lua logo", "https://www.lua.org/images/lua-logo.gif")
}
return doc
end
docker run --rm --volume "$PWD":/data -u $(id -u):$(id -g) pandoc/latex \
--toc \
--toc-depth=2 \
--metadata=documentclass=report \
--pdf-engine=xelatex \
--lua-filter=lua-manual-cleanup.lua \
--number-sections \
--top-level-division=chapter \
--output=lua-5.4-manual.pdf \
"https://lua.org/manual/5.4/manual.html"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment