Skip to main content

Reference

Every module, and every function it exports.

Reference for each Lua module of quarto-lua-modules: its functions, what they return, and what they do.

Each section below names one module. Load a module with the path in Vendoring, and call its functions through the table it returns.

A module that loads a sibling needs that sibling copied too. The table at the top of Vendoring lists them.

colour.lua

Converts CSS colours to HTML hex, and reads the colour an attribute sets.

Functions of colour.lua.
Function Returns What it does
is_named_colour(value) boolean Tells whether the value is one of the 148 CSS named colours. The match ignores case.
expand_hex_colour(hex) string Expands a three-digit hex colour to six digits, so #123 becomes #112233.
RGB_to_HTML(rgb) string Converts rgb(r, g, b) to an uppercase hex colour.
RGBPercent_to_HTML(rgb) string Converts rgb(r%, g%, b%) to an uppercase hex colour.
HSL_to_HTML(hsl) string Converts hsl(h, s%, l%) to an uppercase hex colour.
HWB_to_HTML(hwb) string Converts hwb(h w% b%) to an uppercase hex colour.
named_to_HTML(name) string Converts a CSS named colour to an uppercase hex colour. The match ignores case.
to_html(code, format) string Converts a colour by its format name: hwb, hsl, rgb, rgb_percent, hex, or named.
get_colour(attrs, default) string or nil Reads colour from the attributes, then color, and returns default when neither is set.
is_custom_colour(colour) boolean Tells whether the value is a custom colour rather than a named one. Use it to choose between a class and an inline style.
hue_to_rgb(p, q, t) number A helper for HSL_to_HTML, which returns one RGB component.

content-extraction.lua

Finds sections, divs, and headers in a list of Pandoc blocks. It needs string.lua and html.lua.

Functions of content-extraction.lua.
Function Returns What it does
extract_section(blocks, section_id, include_header) blocks or nil Returns the blocks of the section with that header identifier, up to the next header of the same or a higher level. The header is included unless include_header is false.
remove_sections(blocks, predicate) kept, removed Removes each section whose header matches predicate. Returns the kept blocks and the removed blocks, in document order. The input is not changed.
extract_div(blocks, div_id, include_div) blocks or nil Searches inside the blocks for a Div with that identifier, and returns its contents. The Div itself is included when include_div is true.
find_block(blocks, predicate) integer or nil Returns the index of the first block for which predicate is true.
protect_headers(blocks, id_prefix, format) blocks Replaces each header with a raw HTML block, so its identifier cannot clash with another. The optional prefix is added to each identifier.
parse_sections(blocks) table Splits content at a horizontal rule into header_text, header_level, body_blocks, and footer_blocks. Useful for modal dialogs.
extract_code_metadata(code_text, pattern) filename or nil, text Reads a line such as python \| filename: script.py, and returns the filename and the code without that line.

embedded.lua

Loads a filter that an extension bundles, and skips it when the page runs the project’s own copy of the same extension.

Functions of embedded.lua.
Function Returns What it does
load(name, file, options) table Loads the bundled filter at file, unless the page runs a filter from the project’s own copy of the extension name. A copy that the page does not name in filters leaves the bundled filter on. Returns the list of filters, which is empty when the bundled one is skipped, or holds only the plugin check. Set options.plugin when the extension also gives a Reveal.js plugin: the page then warns if it names the copy in only one of filters and revealjs-plugins. Set options.shortcode when the extension also gives shortcodes: the page then warns if the project has a copy that filters does not name, because the shortcodes of the copy always replace the bundled ones.

engine.lua

Gives the values that Quarto 1 and Quarto 2 expose through different routes. Each function returns the same value on both engines for the same document. It reads the data that is present, and never the name or the version of the engine. It needs metadata.lua and string.lua.

Functions of engine.lua.
Function Returns What it does
project_repo_url(meta) string or nil, reason Returns website.repo-url, else book.repo-url. Quarto 1 gives it in QUARTO_EXECUTE_INFO, and Quarto 2 in the document metadata meta. The reason is the same as for metadata.get_project_metadata, and is set only when QUARTO_EXECUTE_INFO cannot be read.
project_site_url(meta) string or nil, reason Returns website.site-url, else book.site-url, by the same rule as project_repo_url.
project_kind(meta) 'book', 'website' or nil, reason Returns 'book' when the project has a book map, else 'website' when it has a website map. Returns nil for any other project and for a single file.
document_dir() string or nil Returns the directory of the document being rendered, from quarto.doc.input_file. When that is missing, it reads QUARTO_DOCUMENT_PATH.
slide_level(meta) integer Returns the Reveal.js slide level. Quarto 1 gives it in PANDOC_WRITER_OPTIONS.slide_level, and Quarto 2 in the slide-level metadata. Returns 2 when nothing sets it.
is_output(kind) boolean Returns whether the output is of kind: html, revealjs, typst or docx. It uses quarto.format.is_<kind>_output() when it exists, and the FORMAT test of Quarto 1 otherwise. Any other kind raises an error.

Quarto 1 does not set QUARTO_EXECUTE_INFO for a page that it takes from the freeze cache (execute: freeze). For such a page, rendered alone or first, the three project_* functions return nil in Quarto 1 and the project values in Quarto 2. metadata.get_project_metadata has the same limit in Quarto 1.

Pass the document metadata from a Pandoc or Meta handler:

local engine = require(quarto.utils.resolve_path('_vendor/quarto-lua-modules/engine.lua'):gsub('%.lua$', ''))

function Pandoc(doc)
  local repo_url = engine.project_repo_url(doc.meta)
  if repo_url ~= nil then
    quarto.log.output('repository: ' .. repo_url)
  end
end

git.lua

Reads the Git repository the document is in.

Functions of git.lua.
Function Returns What it does
get_repository() string or nil Returns owner/repo, read from the origin remote, or nil when there is none.

html.lua

Builds raw HTML, and adds an HTML dependency to the page once. It needs string.lua.

Functions of html.lua.
Function Returns What it does
raw_header(level, text, id, classes, attributes) string Returns a raw HTML header element, with its identifier, classes, and any extra attributes.
get_extension_version() string Returns the version of the running extension, read from its _extension.yml. Raises an error when that file has no version.
ensure_html_dependency(config) boolean Adds an HTML dependency once per document, and returns true only when it was added. Without version in config, the running extension’s version is used.
reset_dependencies() nothing Forgets the dependencies added so far. Use it when one process renders several documents.

logging.lua

Writes messages in one format, prefixed with the extension name: [extension-name] Message.

Functions of logging.lua.
Function Returns What it does
log_error(extension_name, message) nothing Writes an error message.
log_warning(extension_name, message) nothing Writes a warning message.
log_output(extension_name, message) nothing Writes an informational message.
log_debug(extension_name, message) nothing Writes a debug message.

lookup.lua

Checks membership in a list, and maps keywords to values.

Functions of lookup.lua.
Function Returns What it does
is_valid_value(value, valid_array, formatter) string or nil Returns the value, formatted when formatter is given, if it is in valid_array. Returns nil when it is not. A formatter string is a prefix, and a function receives the value.
in_array(value, valid_array) boolean Tells whether the value is in the array.
keyword_to_value(keyword, mapping, default) string Returns the mapped value of the keyword. Falls back to default, and then to the keyword itself.
size_to_css(size) string Converts a size keyword such as large to font-size: 1.2em;. A value that already has a unit is passed through. Returns an empty string for an empty input.

metadata.lua

Reads the configuration of an extension from the document metadata, and the project metadata from Quarto. It needs string.lua and logging.lua.

Functions of metadata.lua.
Function Returns What it does
get_extension_config(meta, extension_name) any or nil Returns the table under extensions.<extension_name>.
get_metadata_value(meta, extension_name, key) string or nil Returns extensions.<extension_name>.<key> as a string. A boolean false is a value, and is returned as "false".
check_deprecated_config(meta, extension_name, key, deprecation_warning_shown) value or nil, boolean Reads a top-level key that an extension has deprecated, and warns once. Returns the value and the updated warning flag.
get_option_with_fallbacks(spec) any Returns one option, taking the first of the named arguments, the document metadata, and the default.
get_options(spec) table Returns several options at once, by the same rule as get_option_with_fallbacks.
get_project_metadata() table or nil, reason, path Reads the format.metadata table that Quarto writes to QUARTO_EXECUTE_INFO. When that fails, the reason is one of file is unreadable, file is empty, invalid JSON, or no format.metadata table.
get_project_repo_url(format_meta) string or nil Returns repo-url from the website or book metadata, website first.

pandoc-helpers.lua

Builds Pandoc elements, and checks the Lua values Pandoc hands to a filter. It needs string.lua.

Functions of pandoc-helpers.lua.
Function Returns What it does
create_link(text, uri) pandoc.Link or nil Returns a link, or nil when the text or the URI is empty.
attr(id, classes, attributes) pandoc.Attr Returns the Pandoc attributes built from an identifier, classes, and key-value pairs.
has_class(classes, name) boolean Tells whether the class list holds name.
add_class(classes, name) nothing Adds name to the class list, when it is not already there.
get_quarto_format() string, string Returns the output format, such as html, typst, or pptx, or unknown, and its language.
is_object_empty(obj) boolean Tells whether the value is nil, an empty string, or an empty table.
is_type_simple(obj) boolean Tells whether the value is a string, a number, or a boolean.
is_function_userdata(obj) boolean Tells whether the value is a function or userdata.
get_value(fields, obj) any Reads a nested value, following the field names in order.
attributes_to_table(element) table Converts the attributes of a Div, Span, Table, or Image to a plain table.

paths.lua

Resolves paths against the project, and checks file types. It needs string.lua.

Functions of paths.lua.
Function Returns What it does
resolve_project_path(path) string Resolves a path relative to the project directory. A leading / marks the path as project-relative. Without a project, the leading / is removed.
has_extension(uri, extensions, case_sensitive) boolean Tells whether the URI ends with one of the extensions. The match ignores case unless case_sensitive is true, and the leading dot is optional.
is_markdown(uri) boolean Tells whether the URI ends with .md, .markdown, or .qmd.

schema-check.lua

Checks a document, a shortcode call, an element, a code cell, and an output format against the extension’s _schema.yml. It needs logging.lua and string.lua.

The validator is passed to the module as an argument, not loaded by it. An extension can then vendor the schema validator from one source and this module from another. The validator must provide load_schema, validate, validate_shortcode, extract_meta_options, validate_attributes, validate_format, validate_cells, and validate_element. validate_cells needs Quarto Wizard schema 2.3.0 or newer, and validate_element needs Quarto Wizard schema 2.5.0 or newer. If the validator does not provide validate_attributes, validate_format, validate_cells, or validate_element, the checker reports it once and does not check that section. If the validator does not provide one of the other functions, the checker raises an error.

Functions of schema-check.lua.
Function Returns What it does
new(validator, extension_name, schema_path) checker Reads the schema once, and returns a checker. The schema is _schema.yml beside the entry point. Pass schema_path when the entry point is in a subdirectory, such as '../_schema.yml'.
checker:options(meta) defaults, resolved Checks the document options. Returns a copy of the schema defaults first. Second, returns resolved: provided holds what the document set, merged holds those values with coercion and defaults applied, and defaults holds the schema defaults. resolved is nil when there is no schema. Only the first call reads meta.
checker:option(key) value Returns what the schema resolves one option to, after options has run.
checker:element(el) table Checks the attributes of one Pandoc element against the attributes section, and returns the resolved values. The groups apply in this order: each class of the element, the identifier prefix, the element type (el.t), then _any. The first group that declares an attribute decides it.
checker:attributes(attributes, group) table Checks a table of attributes that is not an element against the attributes section, and returns the resolved values. _any applies first and the given group applies second. If both groups declare an attribute, the _any default decides it and the given group also checks it. element applies the class group first, so the resolved value can be different.
checker:cells(options, language) table Checks the comment-pipe options of a code cell, such as #\|, against the cells section. The cell language picks the group.
checker:format(name) table Checks one output format against the formats section, after options has run.
checker:enabled() boolean Tells whether the extension is on for the document. It is false only when the schema declares an enabled option and that option resolves to false. The value comes from the document, or from the schema default when the document does not set it. Call it after options.
checker:call(name, args, kwargs) boolean Checks one shortcode call against the shortcode’s entry in the schema, and reports what it finds. Returns false when a required argument is missing, and true in all other cases. The checker already reported the missing argument, so the shortcode returns without a second message.

Build the checker at file scope, and not inside a shortcode handler. The schema is then read once for the render, and not once for each call.

local check = require(quarto.utils.resolve_path('_vendor/quarto-lua-modules/schema-check.lua'):gsub('%.lua$', ''))
local checker = check.new(validator, 'iconify')
local defaults, resolved = checker:options(meta)

Switching an extension off

A schema can name optionsKey, a top-level metadata key that holds the options of the extension. It can also declare an enabled option of type boolean. optionsKey needs Quarto Wizard schema 2.6.0 or newer, and the schema must declare the $schema of version 2.

$schema: https://m.canouil.dev/quarto-wizard/assets/schema/v2/extension-schema.json
optionsKey: revealjs-switch
options:
  enabled:
    type: boolean
    default: true

Two functions turn the extension off when enabled resolves to false, from the document or from the schema default.

Functions of schema-check.lua that read enabled.
Function Returns What it does
guard(checker, filters) table Takes the filter tables of an entry point. Returns a list that starts with a Meta table, which calls checker:options, and then holds a copy of each table, with every handler wrapped. A wrapped handler returns nothing when the extension is off, so the element stays as it is. A single filter table or a value that is not a table is reported as misuse. An entry of the list that is not a table is reported as misuse and skipped. A first argument that is not a checker raises an error.
shortcode(checker, handler) function Wraps a shortcode handler. When the extension is off, it returns nothing and logs nothing. That is empty inlines, or one empty Plain block for a shortcode alone in its own paragraph, so no empty paragraph stays. A handler that is not a function is reported as misuse, and the shortcode then returns nothing. A first argument that is not a checker raises an error.

A filter entry point returns the result of guard.

local check = require(quarto.utils.resolve_path('_vendor/quarto-lua-modules/schema-check.lua'):gsub('%.lua$', ''))
local checker = check.new(validator, 'revealjs-switch')

return check.guard(checker, { { Div = Div } })

guard returns a list of filter tables, so a file that returns it cannot also hold shortcode keys. Put the shortcode handlers, wrapped with shortcode, in their own file. List the filter file under filters: and the shortcode file under shortcodes: in _extension.yml. If one file holds both, the filter does not run, and Quarto gives no message. Each file builds its own checker, so a finding about the options shows once for each file.

A shortcode file wraps each handler.

return { ['switch'] = check.shortcode(checker, switch) }

A page turns the extension off in its own metadata.

---
revealjs-switch:
  enabled: false
---

Strict mode

Strict mode stops the render when a check finds an error. Use it in the continuous integration (CI) of the extension’s own repository only. Authors must not set it.

To turn on strict mode, set the environment variable QUARTO_SCHEMA_STRICT to 1. Any other value, or no value, keeps the default behaviour: the checker reports and the render continues. The checker reads the variable once, in new.

In strict mode, these findings stop the render with exit status 1:

  • An unreadable schema.
  • A fault in the calls that the extension makes to the checker.
  • A missing required shortcode argument.
  • An error about a document option, a shortcode call, an element attribute, an output format, or a code cell.
  • A validator that does not provide validate_element, validate_attributes, validate_format, or validate_cells.

The checker logs each of these findings as an error. It stops the render after the check that found them, such as one element or one call, so the log shows every error of that check. Warnings never stop the render.

string.lua

Tests, splits, trims, and escapes strings. It has no dependencies.

Functions of string.lua.
Function Returns What it does
is_empty(s) boolean Tells whether the value is nil or an empty string.
escape_pattern(s) string Escapes the characters that have a meaning in a Lua pattern.
split(str, sep) table Splits the string at each match of sep.
trim(str) string Removes leading and trailing whitespace.
to_string(val) string or nil Converts any value to a string, including Pandoc objects. Returns nil for an empty value.
strip_surrounding(text) prefix, inner, suffix Removes one balanced pair of brackets or quotes, or trailing punctuation, from the ends of the text.
strip_edges(text) prefix, inner, suffix Removes any run of opening brackets from the start, and any run of closing brackets or punctuation from the end.
find_bracketed_content(text, start_pos) prefix, content, suffix, position Finds the first balanced bracket pair after start_pos, and splits the text around it.
escape_latex(text) string Escapes the characters that LaTeX treats as special.
escape_typst(text) string Escapes the characters that Typst treats as special.
escape_typst_string(text) string Escapes text for a Typst string literal, inside double quotes.
escape_js_string(text) string Escapes text for a JavaScript string literal. It also escapes </, so the text cannot close an inline script.
escape_lua_pattern(text) string Escapes text for use in string.gsub.
escape_html(text) string Escapes &, <, >, ", and '.
escape_attribute(value) string Escapes the characters that would end an HTML attribute value.
escape_text(text, format) string Escapes text for latex, typst, or lua. Any other format name raises an error.
ascii_id(text) string Makes an HTML identifier from text, by lowercasing it and replacing the spaces.
Back to top