diff options
Diffstat (limited to 'data/sample.lua')
-rw-r--r-- | data/sample.lua | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/data/sample.lua b/data/sample.lua index 6c09442b5..9d6bf0fc7 100644 --- a/data/sample.lua +++ b/data/sample.lua @@ -2,7 +2,8 @@ -- that is very similar to that of pandoc's HTML writer. -- There is one new feature: code blocks marked with class 'dot' -- are piped through graphviz and images are included in the HTML --- output using 'data:' URLs. +-- output using 'data:' URLs. The image format can be controlled +-- via the `image_format` metadata field. -- -- Invoke with: pandoc -t sample.lua -- @@ -12,6 +13,28 @@ -- produce informative error messages if your code contains -- syntax errors. +local pipe = pandoc.pipe +local stringify = (require "pandoc.utils").stringify + +-- The global variable PANDOC_DOCUMENT contains the full AST of +-- the document which is going to be written. It can be used to +-- configure the writer. +local meta = PANDOC_DOCUMENT.meta + +-- Chose the image format based on the value of the +-- `image_format` meta value. +local image_format = meta.image_format + and stringify(meta.image_format) + or "png" +local image_mime_type = ({ + jpeg = "image/jpeg", + jpg = "image/jpeg", + gif = "image/gif", + png = "image/png", + svg = "image/svg+xml", + })[image_format] + or error("unsupported image format `" .. img_format .. "`") + -- Character escaping local function escape(s, in_attribute) return s:gsub("[<>&\"']", @@ -44,19 +67,6 @@ local function attributes(attr) return table.concat(attr_table) end --- Run cmd on a temporary file containing inp and return result. -local function pipe(cmd, inp) - local tmp = os.tmpname() - local tmph = io.open(tmp, "w") - tmph:write(inp) - tmph:close() - local outh = io.popen(cmd .. " " .. tmp,"r") - local result = outh:read("*all") - outh:close() - os.remove(tmp) - return result -end - -- Table to store footnotes, so they can be included at the end. local notes = {} @@ -217,8 +227,8 @@ function CodeBlock(s, attr) -- If code block has class 'dot', pipe the contents through dot -- and base64, and include the base64-encoded png as a data: URL. if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then - local png = pipe("base64", pipe("dot -Tpng", s)) - return '<img src="data:image/png;base64,' .. png .. '"/>' + local img = pipe("base64", {}, pipe("dot", {"-T" .. image_format}, s)) + return '<img src="data:' .. image_mime_type .. ';base64,' .. img .. '"/>' -- otherwise treat as code (one could pipe through a highlighter) else return "<pre><code" .. attributes(attr) .. ">" .. escape(s) .. |