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
This commit is contained in:
Albert Krewinkel 2019-12-23 01:09:30 +01:00 committed by John MacFarlane
parent 8d64cb6954
commit 2cf89ade85

View file

@ -264,32 +264,33 @@ return {
} }
``` ```
## Default metadata file ## Center images in LaTeX and HTML output
This filter causes metadata defined in an external file For LaTeX, wrap an image in LaTeX snippets which cause the image
(`metadata-file.yaml`) to be used as default values in a to be centered horizontally. In HTML, the image element's style
document's metadata: attribute is used to achieve centering.
``` lua ``` lua
-- read metadata file into string -- Filter images with this function if the target format is LaTeX.
local metafile = io.open('metadata-file.yaml', 'r') if FORMAT:match 'latex' then
local content = metafile:read("*a") function Image (elem)
metafile:close() -- Surround all images with image-centering raw LaTeX.
-- get metadata return {
local default_meta = pandoc.read(content, "markdown").meta pandoc.RawInline('latex', '\\hfill\\break{\\centering'),
elem,
pandoc.RawInline('latex', '\\par}')
}
end
end
return { -- Filter images with this function if the target format is HTML
{ if FORMAT:match 'html' then
Meta = function(meta) function Image (elem)
-- use default metadata field if it hasn't been defined yet. -- Use CSS style to center image
for k, v in pairs(default_meta) do elem.attributes.style = 'margin:auto; display: block;'
if meta[k] == nil then return elem
meta[k] = v end
end end
end
return meta
end,
}
``` ```
## Setting the date in the metadata ## Setting the date in the metadata