git.iko.soy/typeable/pandoc
on master help sign in
doc/lua-filters.md: replace metadata example with image centering (#6004)

Metadata defaults can be given via the command line `--metadata-file`. Adding raw format snippets is a common use case for Lua filters, so it seems sensible to provide an example. Thanks to @efx for proposing this filter. Closes: pandoc/lua-filters#70

commit2cf89ade856203bdf794d0b108019cbaa11673ad authorAlbert Krewinkel <albert@zeitkraut.de> dateMon 23 Dec 2019, 01:09 (7 years ago) parent8d64cb6
1 file changed+23−22
doc/lua-filters.md+23−22
doc/lua-filters.md+23−22
@@ -264,32 +264,33 @@ return {
264264}
265265```
266266
267−## Default metadata file
267+## Center images in LaTeX and HTML output
268268
269−This filter causes metadata defined in an external file
270−(`metadata-file.yaml`) to be used as default values in a
271−document's metadata:
269+For LaTeX, wrap an image in LaTeX snippets which cause the image
270+to be centered horizontally. In HTML, the image element's style
271+attribute is used to achieve centering.
272272
273273``` lua
274−-- read metadata file into string
275−local metafile = io.open('metadata-file.yaml', 'r')
276−local content = metafile:read("*a")
277−metafile:close()
278−-- get metadata
279−local default_meta = pandoc.read(content, "markdown").meta
274+-- Filter images with this function if the target format is LaTeX.
275+if FORMAT:match 'latex' then
276+ function Image (elem)
277+ -- Surround all images with image-centering raw LaTeX.
278+ return {
279+ pandoc.RawInline('latex', '\\hfill\\break{\\centering'),
280+ elem,
281+ pandoc.RawInline('latex', '\\par}')
282+ }
283+ end
284+end
280285
281−return {
282− {
283− Meta = function(meta)
284− -- use default metadata field if it hasn't been defined yet.
285− for k, v in pairs(default_meta) do
286− if meta[k] == nil then
287− meta[k] = v
288− end
289− end
290− return meta
291− end,
292− }
286+-- Filter images with this function if the target format is HTML
287+if FORMAT:match 'html' then
288+ function Image (elem)
289+ -- Use CSS style to center image
290+ elem.attributes.style = 'margin:auto; display: block;'
291+ return elem
292+ end
293+end
293294```
294295
295296## Setting the date in the metadata