aboutsummaryrefslogtreecommitdiff
path: root/data
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2018-10-14 17:28:15 +0200
committerAlbert Krewinkel <albert@zeitkraut.de>2018-10-14 21:23:41 +0200
commit6082caf2331d041739bd2c99d628e2f075b8e130 (patch)
treeb9c872e4c660d880f7b402de54405989a7fda587 /data
parent983277c6ebad990be7df5ad68ded245a30c61ade (diff)
downloadpandoc-6082caf2331d041739bd2c99d628e2f075b8e130.tar.gz
Custom writer: provide PANDOC_DOCUMENT instead of Setup function
Custom writers have access to the global variable `PANDOC_DOCUMENT`. The variable contains a userdata wrapper around the full pandoc AST and exposes two fields, `meta` and `blocks`. The field content is only marshaled on-demand, performance of scripts not accessing the fields remains unaffected.
Diffstat (limited to 'data')
-rw-r--r--data/sample.lua36
1 files changed, 13 insertions, 23 deletions
diff --git a/data/sample.lua b/data/sample.lua
index 019ac13f3..9d6bf0fc7 100644
--- a/data/sample.lua
+++ b/data/sample.lua
@@ -16,30 +16,24 @@
local pipe = pandoc.pipe
local stringify = (require "pandoc.utils").stringify
-local image_format = "png"
-local image_mime_type = "image/png"
-
--- Get the mime type for a given format.
-local function mime_type(img_format)
- local formats = {
+-- 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",
- }
- return formats[img_format]
- or error("unsupported image format `" .. img_format .. "`")
-end
-
--- Set options from document metadata.
-function Setup(doc)
- local meta = doc.meta
- if meta.image_format then
- image_format = stringify(meta.image_format)
- image_mime_type = mime_type(image_format)
- end
-end
+ })[image_format]
+ or error("unsupported image format `" .. img_format .. "`")
-- Character escaping
local function escape(s, in_attribute)
@@ -352,10 +346,6 @@ end
local meta = {}
meta.__index =
function(_, key)
- -- Setup is optional, don't warn if it's not present.
- if key == 'Setup' then
- return
- end
io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
return function() return "" end
end