I'm writing a Lua filter for Pandoc to deduce the HTML pagetitle
from the first h1
heading of the input document. Other than the print()
function, io.stderr.write
does not append a newline. I found in the answer to NewLine(\n) alternative in Lua? that [[
and ]]
can be used to create multi-line string literals, but surprisingly this does not work with strings that contain nothing but a line break.
local pagetitle
function Header(header)
if not pagetitle and header.level == 1 then
pagetitle = pandoc.utils.stringify(header)
end
end
-- capture current line break:
local EOL = [[
]]
function Meta(meta)
if not meta.pagetitle then
if pagetitle then
meta.pagetitle = pagetitle
else
io.stderr:write(
'WARNING: h1 missing, defaulting HTML pagetitle to input filename.', EOL)
meta.pagetitle = PANDOC_STATE.input_files[1]
end
return meta
end
end
Maybe that's just a Lua bug. On the other hand, it didn't look right from the start, although the idea seemed quite clever at first.
What would you suggest instead?
I'm writing a Lua filter for Pandoc to deduce the HTML pagetitle
from the first h1
heading of the input document. Other than the print()
function, io.stderr.write
does not append a newline. I found in the answer to NewLine(\n) alternative in Lua? that [[
and ]]
can be used to create multi-line string literals, but surprisingly this does not work with strings that contain nothing but a line break.
local pagetitle
function Header(header)
if not pagetitle and header.level == 1 then
pagetitle = pandoc.utils.stringify(header)
end
end
-- capture current line break:
local EOL = [[
]]
function Meta(meta)
if not meta.pagetitle then
if pagetitle then
meta.pagetitle = pagetitle
else
io.stderr:write(
'WARNING: h1 missing, defaulting HTML pagetitle to input filename.', EOL)
meta.pagetitle = PANDOC_STATE.input_files[1]
end
return meta
end
end
Maybe that's just a Lua bug. On the other hand, it didn't look right from the start, although the idea seemed quite clever at first.
What would you suggest instead?
Share edited Mar 14 at 15:07 Wolf asked Mar 14 at 14:57 WolfWolf 10.3k8 gold badges68 silver badges112 bronze badges1 Answer
Reset to default 1Use the string.format()
function to add the newline as \n
, also make print warnings a function to be future-safe:
local pagetitle
local function print_warning(msg)
io.stderr:write(string.format('WARNING: %s\n', msg))
end
function Header(header)
if not pagetitle and header.level == 1 then
pagetitle = pandoc.utils.stringify(header)
end
end
function Meta(meta)
if not meta.pagetitle then
if pagetitle then
meta.pagetitle = pagetitle
else
print_warning('h1 missing, defaulting HTML pagetitle to input filename.')
meta.pagetitle = PANDOC_STATE.input_files[1]
end
return meta
end
end
While writing the question, I figured out an answer myself after finding a matching programming idom.