Write a theme covers the public API, kb4it.theme. This page is for the people who work on KB4IT, or who need more than the public API: how KB4IT runs a theme, what self.internals reaches, and the older API the bundled themes used before.
Public and internal
The public API is what kb4it.theme exports (tests/test_public_names.py guards the list) and the members Theme API reference lists: BaseTheme and its hooks, Site, Doc, Table, Feature, the built-in features with their options and hooks. A change there goes through a deprecation period.
Everything else is internal and may change in any release: Builder, DocumentTheme and the themekit mixins, the Database and Backend services, the runtime dict, kbdict, BuildPlan, distribute_md, distribute_html, add_target, get_theme_var and the template lookup.
When the public API does not cover a need yet, self.internals reaches the services:
| Attribute | What it is |
|---|---|
self.internals.db |
The Database service: get_values(doc, key), get_doc_properties(doc), get_docs_by_key_value(key, value)... |
self.internals.backend |
The Backend service: get_dict("repo"), get_path("tmp"), get_kb_dict(), get_plan()... |
self.internals.builder |
The engine that runs the theme (a Builder): distribute_html(), add_system_page(), inject_head_meta()... |
techdoc uses it for its helper modules, which take a database (logic/docpage.py, logic/records.py, logic/dashboard.py), and apphelp for the front matter of the documents it leaves out of the site. Code that uses it may break with any KB4IT release; tell the KB4IT developers what you needed, so it can become public.
How KB4IT runs a theme
- Load.
Frontend.theme_load()loadslogic/as a package namedkb4it_theme_<id>_<8 hex of the path>(kb4it/core/themeload.py) and imports itsthememodule. A bare import of a sibling module (from helpers import x) still works: a finder limited to the files oflogic/loads it as<package>.helpersand gives the bare name the same module. It logsTHEME_BARE_IMPORT, a warning for themes outside KB4IT. Loading a theme again (terminal interface,kb4it serve) runs its files again. - Assemble the features. For a
BaseThemeclass,resolve()inkb4it/theme/feature.pyexpands bundles, adds what each featurerequiresbefore it, and orders the features so each runs after the ones it requires or uses; ties keep the order offeatures. The features'theme_jsonkeys,metadata_pagesand theirassetsare merged into the runtime copy oftheme.json(apply_theme_json()); a keytheme.jsonsets to another value stops the build. The theme's fingerprint is computed again with the added assets. - Register the engine. Frontend registers
ThemeEngine(Theme)(kb4it/services/themeengine.py) as the theme service. The engine is aBuilder: the pipeline calls it as it always called a theme. It owns the theme instance and one object per feature, copies the options it needs (search_index_var,profile_skeleton...) and setsreserved_pages(the theme's plus the features'reserves) andadd_page(theeditorfeature). On its first start it makes theSite, setstheme.site,theme.logandtheme.internals, logsTHEME_HOOK_UNUSEDfor a theme method named like a hook of a feature that is off, and callstheme.setup(). - Settings. Frontend builds
self.configfromconfig_class,theme.jsonand therepo.jsonblock named after the theme, and sets it on the engine, which hands it to the theme.
The stages of a build, and what the engine calls in each:
| Stage | Core call | Engine |
|---|---|---|
| 2 get_sources | theme.generate_sources() |
theme.generate_sources(site) |
| 3 process_sources, after the analysis | theme.prepare() |
each feature's prepare(site), then theme.prepare(site) |
| 3 process_sources, transformation | build_page_key(), build_page_key_value() |
the property_pages feature; a value page goes through the value_page hook |
| 4 process_theme | theme.build() |
each feature's pages(site), then theme.build(site) |
| 5 compilation | build_page(path) per page |
Builder.build_page(), which calls the page hooks; the engine turns the doc id into a Doc |
| 6 theme_post | theme.post_activities() |
features' after_build(site), then theme.after_build(site) |
| 7 deploy | inject_head_meta() per page |
Builder, with page_lang(), structured_defaults(), search_page() from the theme |
| 8 theme_post_deploy | post_deploy_activities(), write_search_index(), write_sitemap(), write_feed(), run_checks(), after_checks() |
features' and theme's after_deploy(site), the search feature's index, the theme's checks(site), then theme.after_checks(site) |
The property and value pages are built in stage 3, before build(), so a theme that needs per-build data in them must not compute it in build(). Older themes computed it lazily, on first use; prepare() runs before those pages: compute there, never lazily.
Features inside
A built-in feature lives in kb4it/theme/features/. It imports kb4it.theme, kb4it.core and its own helpers, never another feature module (tests/core/test_feature_independence.py); it reaches another feature through requires, uses and self.feature(name). Code that two themes, or a theme and a starter, need becomes a built-in feature when it can be described without one theme's words; otherwise it stays in the theme, as a feature of its own when it has pages, per-build data or work after deployment (techdoc's quality and admin), or as a plain module when it only computes.
A feature reads its options with self.option(name) and calls a hook with self.hook(name), which is the theme's method when the theme defines one and the feature's own method otherwise. Tables take extensions: a feature with table_extension(table, columns, doc_ids) returns HTML to put before the rows and the attributes of each row; table_filter adds its filter bar that way, for tables whose Table.filterable is true.
The API before kb4it.theme
Before kb4it.theme, a theme subclassed kb4it.services.builder.Builder, or kb4it.services.themekit.DocumentTheme for a theme with property pages, and added kits (mixins) to its class line. It read the documents through self.srvdtb, wrote pages with add_system_page() and called the page builders in build(). Theme hooks of the legacy API lists every method.
This API is deprecated. It keeps working until it is removed, not before KB4IT 1.0 and at least two minor releases after the release that deprecated it, and a theme outside KB4IT that uses it logs THEME_API_LEGACY once per build. Inside, the mixins are thin: their code is in the features, and a mixin calls the feature with the legacy theme as both the theme and the engine; the feature then calls the theme's methods with their old arguments (doc ids, not Doc objects).
Move a theme to kb4it.theme
-
Change the class line:
class Theme(BaseTheme), imported fromkb4it.theme, and list the features the old class line andbuild()had:Before After DocumentTheme"documents"(and"stats","bookmarks","events"ifbuild()built those pages)FilterMixin"table_filter"RelatedMixin,NavigationMixin,TocSidebarMixin"related","navigation","toc_sidebar"GalleryMixin,DiagramsMixin"gallery","diagrams"add_page = Trueandcreate_page_add()"editor"create_page_search()and"search": true"search"create_page_about_kb4it(),create_page_about_app()"about"build_page_404()andreserved_pages = ("404.md",)"not_found", in"documents" -
Delete from
build()every call the features now make (build_page_properties(),build_page_index_all(),build_page_stats()...). What is left are your own pages: write them withsite.page()andself.render(). - Give the hooks their new arguments:
doc(aDoc) instead ofdoc_id, andsitefor the stage hooks.page_body_templateispage_template,post_activitiesisafter_build,post_deploy_activitiesisafter_deploy,_initializeissetup(nosuper()call), and the publishing a theme did afterrun_checks()goes intoafter_checks. - Replace
self.srvdtbwithsite.documents,site.propertiesand theDocmethods,self.srvbes.get_dict("repo")withsite.repo, andget_theme_var()withbase_var(). - Move what you computed on first use to
prepare(site). - Remove
"search","metadata_pages"and the features'assetsfromtheme.json. - Make the imports between the modules of
logic/relative, and add an emptylogic/__init__.py. - Run
kb4it theme-infoon the theme: theapiline saysBaseTheme, and noTHEME_HOOK_UNUSEDshows in a build.
The bundled themes moved this way, each with a site built before and after and compared page by page.