Extend a site with a plugin

This guide writes a plugin: a Tcl file of the site that changes the pages at build time.

A plugin that adds a line

Make the file plugins/footer.tcl:

# Each page gets a line at its end.
proc html {page body} {
    return "$body<p class=\"edit\">Page: [dict get $page src]</p>"
}

Name it in the site file:

plugins  plugins/footer.tcl

Build the site. Each page now ends with the name of its Markdown file.

The hooks

A plugin defines a proc for each hook that it uses. Flight Manual calls the hooks of each plugin in the order of the key plugins.

Hook What it changes
pages LIST The list of pages. A plugin can add a page with its own HTML.
source PAGE TEXT The Markdown of a page, before the build reads it.
fence PAGE INFO TEXT A fenced code block: the plugin gives its HTML, or "".
link PAGE HREF A link: the plugin gives the URL, or "".
html PAGE BODY The HTML of a page.
head PAGE More lines for the <head> of a page.
files More files of the output.

Plugins has the arguments and the result of each hook.

The sandbox

A plugin runs in a safe child interpreter of Tcl. It has no file, no exec, no socket, and no open. To read the files of the source folder, the site file must give it the capability:

allow  file

Then the plugin has fm::read PATH, fm::files DIR, and fm::kind PATH, for the paths below the source folder.

An error of a plugin stops the build, with the name of the plugin and of the hook.

A larger example

The ztcl docs site has a plugin of about 700 lines: it adds the pages of the command reference from JSON files, runs code blocks in the browser, and writes redirects. The ztcl docs site describes it.

Next: Write good documentation.