texMathToInlines in custom writers #11399
-
|
Is it possible to access Lots of built-in writers call For example, this Markdown document: renders to the below HTML. The first formula is simple enough that it can be rendered with HTML elements like It's a neat feature. Is it possible to access it from a custom writer? Below is a hack to call The output format is a made-up S-expression thing; that's not the important part. The important part is This hack actually seems pretty reasonable, except for the error checking. See, Writer = pandoc.scaffolding.Writer
local function sexp_inlines(tag, content)
local rendered = {}
for i, el in ipairs(content) do
rendered[i] = Writer.Inline(el)
end
return pandoc.layout.parens(tag + pandoc.layout.concat(rendered, pandoc.layout.space))
end
local function sexp_text(tag, text)
return sexp_inlines(tag, {pandoc.Str(text)})
end
local function quote(s) return string.format("%q", s) end
function Writer.Block.Para(el, opts) return sexp_inlines(el.tag, el.content) end
function Writer.Inline.Emph(el, opts) return sexp_inlines(el.tag, el.content) end
function Writer.Inline.Span(el, opts) return sexp_inlines(el.tag, el.content) end
function Writer.Inline.Superscript(el, opts) return sexp_inlines(el.tag, el.content) end
function Writer.Inline.Code(el, opts) return sexp_text(el.tag, el.text) end
function Writer.Inline.Str(el, opts) return quote(el.text) end
function Writer.Inline.Space(el, opts) return quote(" ") end
local function tex_math_to_inlines(mathtype, inp)
-- Create a new pandoc.Pandoc containing just the desired Math.
local math_doc = pandoc.Pandoc({pandoc.Plain({pandoc.Math(mathtype, inp)})})
-- Count the number of log messages. We will check this again after the
-- conversion to see if a warning log was written, indicating that the
-- conversion failed.
local log_len_before = #PANDOC_STATE.log
-- Render the Math doc to HTML, taking advantage of the fact that the
-- HTML writer calls texMathToInlines.
local html = pandoc.write(math_doc, "html", {html_math_method = "plain", wrap_text = "wrap-none"})
-- Count the number of log messages again.
local log_len_after = #PANDOC_STATE.log
local num_logs = log_len_after - log_len_before
if num_logs == 0 then
-- No new log messages were written: the conversion succeeded.
-- Parse the HTML back into a pandoc.Pandoc and extract the
-- inlines containing the rendered formula.
return pandoc.utils.blocks_to_inlines(pandoc.read(html, "html").blocks)
elseif num_logs == 1 then
-- The conversion failed. A log message was written, presumably
-- [WARNING] Could not convert TeX math ..., rendering as TeX
-- Return nil to signal error.
return nil
else
error("unexpected %d new log messages", num_logs)
end
end
function Writer.Inline.Math(el, opts)
local plain = tex_math_to_inlines(el.mathtype, el.text)
if plain then
-- Managed to represent the math as plain inlines.
return Writer.Inlines(plain)
else
-- Couldn't render the math as inlines, output the TeX math.
local delim = assert(({InlineMath = "$", DisplayMath = "$$"})[el.mathtype])
return Writer.Inline(pandoc.Code(delim .. el.text .. delim))
end
end |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
|
To simplify a bit, you could use the plain writer instead of the HTML writer, and check for an initial |
Beta Was this translation helpful? Give feedback.
To simplify a bit, you could use the plain writer instead of the HTML writer, and check for an initial
$for failure.