-- Pandoc filter to simulate stubs (aka "row headers") in the first column of -- tables by formatting simple cell content as bold/strong. -- -- This filter is a heavily edited version of code compiled from MoonScript. -- Please report any problems to -- -- This software is Copyright (c) 2021 by Benct Philip Jonsson. -- This is free software, licensed under: -- The MIT (X11) License -- http://www.opensource.org/licenses/mit-license.php -- Save some typing... local p = assert(pandoc, "Cannot get the pandoc library") local u = assert(p.utils, "Cannot get the pandoc.utils library") local L = assert(p.List, "Cannot get the pandoc.List class") local pt = p.types -- I'm using SimpleTable based on the assumption that most/all -- docx tables fit into a SimpleTable, because I'm not sure -- that I can find my way around the new Table model... local to_simple_table = u.to_simple_table if not to_simple_table then if pt and PANDOC_VERSION then local Version = pt.Version if Version and PANDOC_VERSION >= Version('2.10.0') then error(format("This filter does not work with pandoc %s", tostring(PANDOC_VERSION))) end end end -- More saving on typing local format = string.format local unpack = table.unpack -- Package some boilerplate local function try (what, fun, ...) -- In case fun is a callable object call = function (...) return fun(...) end -- Collect any nimber of return values local res = {pcall(call, ...)} if res[1] then -- on success -- Return the return values return unpack(res, 2) else -- on failure -- Propagate the error message along with the description error(format("Error %s: %s", what, res[2])) end end function Table (tab) if to_simple_table then tab = try( "converting Table to SimpleTable", to_simple_table, tab ) end -- Loop over the indices so that we don't confuse the stateless iterator -- when we modify the list of rows for r=1, #tab.rows do local row = tab.rows[r] local stub = row[1] -- first cell in the row -- Check that we got simple cell contents if 1 == #stub then -- if only one block in the cell... local block = stub[1] local tag = block.tag -- ...and that block is a Para or Plain if ('Para' == tag) or ('Plain' == tag) then -- Wrap the block content in a Strong to simulate a stub block.content = L{ p.Strong(block.content) } -- Is all this rearguard action necessary? stub[1] = block row[1] = stub tab.rows[r] = row end end end if to_simple_table then tab = try( "converting SimpleTable to Table", u.from_simple_table, tab ) end return tab end