Skip to content
KB4IT Help

Theme API reference

Reference Themes Development advanced
On this page

A theme is a Theme class in logic/theme.py that subclasses kb4it.theme.BaseTheme. It switches on features by name in features, sets their options as class attributes and fills a hook by defining a method with the same name. The tables below come from the KB4IT source (scripts/devel/theme_hooks_doc.py), so they match the installed version. See Write a theme for a walk through.

kb4it.theme exports BaseTheme, Site, Doc, Table, Feature, FEATURES, Problem, ThemeError, page_name, slugify and q; kb4it.theme.html has the body rewrites enhance_images, enhance_blocks, lazy_images and wrap_code. Anything else is internal.

BaseTheme

The base class of every theme.

Class attribute Default Meaning
features () Features to switch on: names of built-in features or bundles, or Feature subclasses.
config_class None Dataclass of the theme's settings (theme.json and the repo.json block named after the theme).
reserved_pages () Source file names the build refuses because the theme writes a page with that name.
untitled_label '' Title of a page whose document has no Title.
ellipsize_titles True Cut long titles (True) or leave them whole (False, when the CSS cuts them).
log_area 'THEME' Area of the theme's log lines, such as [THEME] HTML_MISSING.
person_pages True The site has a page per person (Author_.html) that the structured data links to; False describes each person in the home page instead.

KB4IT sets self.site (the Site), self.config (the settings), self.log and self.internals (unstable, see Theme internals) before the first hook.

Lifecycle

Called once per build, in this order. Every one is optional.

Hook What it does
setup() -> None Once per build, before anything else: reset the theme's own per-build state.
generate_sources(site: Site) -> None Write documents into the source folder before KB4IT reads it.
prepare(site: Site) -> None After the documents are read, before any page is built: compute what the pages need.
build(site: Site) -> None Write the theme's own pages with site.page(). The features' pages are already written.
after_build(site: Site) -> None After every document page is compiled, before the site is deployed.
after_deploy(site: Site) -> None After the site is in target (site.paths.target): copy or package files there.
checks(site: Site) -> list[Problem] Problems of the built site; default: links to missing pages and anchors.
after_checks(site: Site) -> None After the checks passed: publish, copy, notify.

Document pages

Called for every page, in this order: page_var, page_context, body_transforms, page_template (through render_page), finalize_html. backlinks_html adds the block after the body.

Hook What it does
page_var() -> dict[str, Any] The starting var of a document page; default: base_var().
page_context(doc: Doc, var: dict[str, Any]) -> dict[str, Any] Add the theme's data to var before the body is rewritten and the page rendered.
body_transforms(doc: Doc, var: dict[str, Any]) -> list[Callable[[str], str]] Functions from HTML to HTML applied in order to the compiled body.
page_template(doc: Doc, var: dict[str, Any]) -> str Name of the template that renders the body of the page.
render_page(doc: Doc, var: dict[str, Any]) -> str The whole page: page_template() between HTML_HEADER_COMMON and HTML_FOOTER when they exist.
finalize_html(html: str) -> str Last change to the whole page before it is written.
backlinks_html(doc: Doc) -> str The block after the body that lists the documents linking here (HTML_BACKLINKS); '' for none.

Site-wide

Values that hold for the whole site.

Hook What it does
site_signature() -> str A value that changes when every page must be rebuilt (site-wide navigation...); '' for none.
target_subdir() -> str \| None Folder inside target to build into, or None.
page_lang() -> str Language of the pages (); default: repo.json seo.lang.
structured_defaults() -> dict[str, Any] Schema.org defaults of the theme's documents: {'type': ..., 'map': {property: schema field}}.
search_page() -> str The search page of the site, for the structured data of the home page.

Rendering

Helpers to call from the hooks.

Hook What it does
render(name: str, **values: Any) -> str Render template name with the base variables plus values, all under var.
template(name: str) -> Any The compiled template name; call .render(var=...) on it.
has_template(name: str) -> bool True when a template named name exists (site override, theme, base or common).
base_var() -> dict[str, Any] A new dict of the variables every template gets: repo, theme, page, lang, search_box...

Site

The knowledge base being built, read-only. site.page(), site.keep() and site.table() write through KB4IT.

Member What it is
site.documents The documents of the site (no system pages), newest first, undated ones last.
site.properties The properties used by the documents and their values.
site.people The person profiles of the site.
site.build What this build compiles.
site.paths Folders of the site.
site.features The enabled features, by name: site.features.events, site.features['table']
doc(doc_id: str) -> Doc The Doc of an id, also for a system page.
keep(name: str, unless: str = 'documents') -> bool Reuse name.html of the previous build when nothing it shows changed; True if kept.
page(name: str, title: str, html: str, *, markdown: bool = False, listing: bool = False) -> None Add a page to the site: name.html with this title and content.
repo repo.json, read-only.
table(table: Table) -> str The HTML of a document table.
theme theme.json of the theme being built, read-only.

site.documents

Member What it does
by_date() -> list[Doc] Every document, newest first, undated ones last.
filter(test: Callable[[Doc], bool]) -> list[Doc] The documents for which test(doc) is true, newest first.
get(doc_id: str) -> Doc \| None The document with this id ('note.md'), system pages included, or None.
latest(n: int) -> list[Doc] The n newest documents.
with_value(key: str, value: str) -> list[Doc] Documents that have value among the values of key, newest first.

site.properties

Member What it does
all_keys() -> tuple[str, ...] Every key of the documents except the blocked ones (Title, SystemPage, dates).
blocked Keys that never get a page: Title, SystemPage and the date keys.
documents(key: str, value: str) -> list[Doc] The documents with a value of a property, newest first.
hidden Keys left out of the properties page (repo.json hidden_properties).
ignored Keys left out of every page (repo.json ignored_keys).
keys() -> tuple[str, ...] The properties that get pages: every key but the blocked and the ignored ones.
values(key: str) -> tuple[str, ...] Every value of a property in the site, sorted without case.

site.people

Member What it does
keys The properties that name people (Author by default).
lookup(name: str) -> Any The profile of a person by name or alias, or None.
status(name: str) -> str 'ok', 'missing' or 'no_contact': whether the person has a profile with contact details.
tip(name: str) -> str Role and team of a person, for a tooltip; '' without a profile.

site.build

Member What it does
changed(doc: Doc \| str) -> bool True when the document is compiled in this build (new or changed, or a full build).
documents_changed How many documents this build compiles.
force True for a full build (--force, or nothing cached).
invalid (source file, reason) of each document KB4IT could not read; they are not in the site.
keys_changed The properties whose pages this build rebuilds.

site.paths

Member What it does
source The folder of the Markdown documents.
target The published site; only complete after deployment (after_deploy).

Doc

One document of the knowledge base, read-only.

Member What it does
id The file name in source/, such as note.md
backlinks {title, url} of the documents that link to this one.
date The Date property as written, or None.
datetime The Date property as a datetime, or None when missing or unreadable.
is_system True for a page KB4IT or the theme generates (property pages, index, about...).
keys() -> tuple[str, ...] The properties of the document, sorted, without the ones KB4IT ignores (Title...).
links(key: str) -> list[dict[str, str]] {name, url} of each value of a property, url being the value's page.
modified When the document last changed (Modified property, git or file time), or None.
name The file name without .md.
properties Every property of the document and its values.
source() -> str The Markdown text of the document ('' for a page without a source file).
template_keys() -> dict[str, Any] The var['keys'] of the document's page: {key: [values]}, 'Title' as a string, and the
title The Title (the first heading of the document), or ''.
url The page of the document, relative to the site root.
value(key: str, default: str = '') -> str The first value of a property, or default.
values(key: str) -> tuple[str, ...] The values of a property, () when the document does not have it.

Table

A table of documents, as site.table() renders it. columns: property names; 'Date' and 'Title' get their own cells. None: the repo.json "datatable" setting, else the theme's default. id: the HTML id of the table. filterable: the table filter bar may attach to it (feature table_filter). dates: {doc id: 'YYYY-MM-DD'}, the event day of each row, for a calendar panel that filters the table.

Fields: documents, columns, id, filterable, dates.

Features

Switch a feature on by listing its name in features; a feature that another one requires comes with it, and a feature runs after the ones it requires or uses. Set an option as a class attribute of the theme. Fill a hook by defining a method with its name; to call the default from it, use the feature: self.site.features.table.datatable_date_cell(doc).

Bundle Features
documents toc, metadata, property_pages, all_documents, not_found

about

about_kb4it.html, and about_app.html unless the site has its own about_app.md.

  • Templates: PAGE_ABOUT_KB4IT, PAGE_ABOUT_APP
Hook Default
about_pages() -> bool Whether this build writes the About pages; a theme may leave them out by a setting.

all_documents

all.html.

  • Requires: table
  • Templates: PAGE_ALL
Option Default
reuse_unchanged_pages False

What site.features.all_documents offers:

Method What it does
write() -> str \| None Write all.html; the page, or None when the cached one is kept.

bookmarks

bookmarks.html.

  • Requires: table
  • Templates: PAGE_BOOKMARKS
Option Default
reuse_unchanged_pages False

What site.features.bookmarks offers:

Method What it does
write() -> str \| None Write bookmarks.html; the page, or None when the cached one is kept.

diagrams

diagrams.html: the Gallery page, with the Mermaid diagrams of the documents instead of their images.

  • Templates: PAGE_GALLERY
  • Assets: gallery
  • Reserves: diagrams.md

What site.features.diagrams offers:

Method What it does
html_of(doc_id: str) -> str The HTML of a document body with a mermaid fence, as the compiler makes it, so the heading ids match; else ''.
items(doc_id: str) -> list[dict[str, Any]] The diagrams of one document, each linked to its section.
write() -> None diagrams.html: no diagram at first; the browser draws the ones the filters choose.

editor

add.html when the editor setting is on (repo.json "editor", default theme.json "editor").

  • Templates: PAGE_ADD, EDITOR_DIALOGS
Option Default
profile_skeleton True
Hook Default
editor_keys() -> dict[str, Any] Completion data {Key: [{"v": value, "c": count}]} for the properties pane.
register_add_page(html: str) -> None Write the Add page and register it as a system page.

What site.features.editor offers:

Method What it does
enabled() -> bool True when this build has the Add page (the editor setting is on).

events

events_YYYYMMDD.html, events_YYYYMM.html and events_YYYY.html of the event documents.

  • Requires: table
  • Templates: EVENTCAL_PAGE_EVENTS_DAYS, EVENTCAL_PAGE_EVENTS_MONTHS, EVENTCAL_PAGE_EVENTS_YEARS
Hook Default
event_documents() -> list[Any] Default: every dated document. A theme returns the ones that are events (Docs or ids).

What site.features.events offers:

Method What it does
build_pages(doclist: list[str], *, collect: bool = True) -> set[str] Create the day, month and year pages of the dated documents in doclist.
calendar(days: dict[str, str] \| None = None) -> str The JSON a calendar panel reads, {"days": {"YYYY-MM-DD": count}}, safe inside