Caleb
Caleb

Reputation: 5438

Can variables passed to Pandoc be used in a lua-filter?

Does Pandoc expose variables set on the command line (pandoc -V foo=bar) to scripts running inside the built in lua filter environment? In other words if I run:

pandoc -V foo=bar --lua-filter=myfilter.lua

...what can I put in myfilter.lua to access foo?

Upvotes: 6

Views: 1189

Answers (3)

Pascal Rosin
Pascal Rosin

Reputation: 1546

Its a long time but I had the same Question and pandoc changed this with Version 3.0 so it depends on what you want to do with the variable (the existing answers would now only deal with reading the variable value). Also the documentation is a bit hard to put together on this, so I experimented.

This is a Lua filter I found working with pandoc 3.1.9. It is simply printing the variable foo to stdout:

-- file: print_foo_filter.lua

function Pandoc(el)
    local foo = PANDOC_WRITER_OPTIONS["variables"]["foo"]
    print("Variable foo: " .. (foo or "nil"))
end

Usage as filter with the -L option:

$ pandoc -o test.txt -L print_foo_filter.lua test.md -V foo=hello
Variable foo: hello

As far as I understand, it is not possible to pass a modified option table from the filter to the writer but you could create elements based on variables or based on other command line options. Another way to do stuff to a document based on variables or options would be to use a custom writer (where the pandoc documentation states that accessing the PANDOC_WRITER_OPTIONS variable from a custom writer is deprecated and it represents only the default writer options object).

But from a writer the variables are read and write accessible as userdata objects in the WriterOptions.variables table. The following writer is deactivating the TOC and adding a window title prefix "Bar" if the variable foo is set to "bar". If foo is unset or has another value, the title prefix "Foo" is added, the document date is set to the current date if no date is present and the TOC is enabled.

-- file: custom_html_writer.lua

function Writer(doc, opts)
    local foo = opts.variables["foo"] and tostring(opts.variables["foo"])
    if foo == "bar" then
        opts.variables["title-prefix"] = "Bar"
        opts.table_of_contents = false
        print("Creating Bar type document")
    else
        opts.variables["title-prefix"] = "Foo"
        opts.variables["date"] = opts.variables["date"] or os.date('%Y-%m-%d')
        opts.table_of_contents = true
        print("Creating Foo type document")
    end
    return pandoc.write(doc, 'html', opts)
end

Template = pandoc.template.default('html')

Usage as custom writer with the -t option.
(-s, --standalone is required to use the default complete html template):

$ pandoc -o test.html -s -t custom_html_writer.lua test.md -V foo=hello
Variable foo: hello

Upvotes: 2

notinaboat
notinaboat

Reputation: 81

(Since: pandoc 2.17)

PANDOC_WRITER_OPTIONS.variables["foo"]

See https://pandoc.org/lua-filters.html#type-writeroptions

Upvotes: 7

hjpotter92
hjpotter92

Reputation: 80639

This is documented a bit in the description for the --metadata:

Like --variable, --metadata causes template variables to be set. But unlike --variable, --metadata affects the metadata of the underlying document (which is accessible from filters and may be printed in some output formats) and metadata values will be escaped when inserted into the template.

So, I think that using -M to set a variable will give you access to the variable inside your lua filter.

Upvotes: 3

Related Questions