Extension#
Sphinx extensions#
t-doc is based on Sphinx, which provides many extension mechanisms to add roles and directives or extend the build process. This doesn't require creating a separate Python package, and can be done within a site repository.
Please refer to the Sphinx documentation to extend t-doc via Sphinx.
t-doc extensions#
The roles and directives provided by t-doc are intended to cover the most common use cases. For more specific uses, they provide extension points that can be used from site repositories to augment their capabilities.
Many roles and directies generate custom <tdoc-*> HTML elements that are
rendered asynchronously, so they may not be complete by the time the
DOMContentLoaded fires. This section presents two methods for safely
adding functionality to <tdoc-*> elements.
The following tables shows how roles and directives map to custom elements.
Role / directive |
Element |
Element class |
|---|---|---|
|
||
|
||
|
||
|
||
|
||
|
Lifecycle hooks#
TdocElement.extend() allows
registering a handler object whose methods are called at different stages of the
lifecycle of <tdoc-*> elements. In particular, the ready() method is called
when t-doc has finished rendering the element.
The following example adds a button to exec blocks having the class
extend-inspect that displays the content of the block.
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
Enumeration#
Selected <tdoc-*> elements on a page can be enumerated with
qsaReady(). The function waits for the DOM to be loaded and for
the elements to be ready before yielding them asynchronously. The returned
generator must therefore be iterated with for await.
The following example adds a button to exec blocks having the class
enum-inspect that displays the content of the block.
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
JavaScript libraries#
tdoc/early.js#
This script
(source)
is run before most scripts and modules are loaded, and exposes functionality via
the tdoc object accessible in the global namespace.
Globals
- tdoc#
This object is pre-loaded into the global namespace and provides functionality that is needed outside of JavaScript modules, and therefore cannot be placed into a module.
Functions
- tdoc.import(...modules)#
Import one or more JavaScript modules.
- Arguments:
modules (
string[]) -- The modules to import, as paths relative to the_staticdirectory.
- Returns:
A
Promiseresolving to the module namespace if a single argument was provided, or to anArrayof module namespaces if multiple arguments were provided.
tdoc/core.js#
This module (source) provides functionality used across all of t-doc.
Module globals
- core.domLoaded#
A
Promisethat resolves when the DOM content has been fully loaded, i.e. theDOMContentLoadedevent has been dispatched on the document.
Functions
- core.qs(el, selector)#
An short alias for
el.querySelector(selector). It gracefully handles the case whereelisundefinedornull.- Arguments:
el (
HTMLElement|DocumentFragment) -- The DOM object to query.selector (
string) -- A CSS selector.
- Returns:
The first matching element, or
nullif there are none.
- core.qsa(el, selector)#
An short alias for
el.querySelectorAll(selector). It gracefully handles the case whereelisundefinedornullby returning an emptyNodeList.- Arguments:
el (
HTMLElement|DocumentFragment) -- The DOM object to query.selector (
string) -- A CSS selector.
- Returns:
A
NodeListof elements matching the selector.
- core.qsaReady(el, selector)#
Query DOM elements below
elmatchingselectoronce, and yield them asynchronously. Standard elements are yielded immediately after the DOM has been loaded; custom<tdoc-*>elements are yielded as they become ready.- Arguments:
el (
HTMLElement|DocumentFragment) -- The DOM object to query.selector (
string) -- A CSS selector.
- Returns:
An asynchronous iterator that yields the matching elements.
- core.html(tmpl, ...values)#
A tag function that safely generates a
DocumentFragmentvia a tagged template. Substitutions are HTML-escaped, except forDocumentFragmentandElementinstances, whoseouterHTMLis substituted as-is.const value = "x < y"; const el = html`The variable <code>value</code> contains: ${value}`;
- Returns:
A
DocumentFragment.
- core.elmt(tmpl, ...values)#
A tag function that safely generates an
Elementvia a tagged template. Substitutions are HTML-escaped, except forDocumentFragmentandElementinstances, whoseouterHTMLis substituted as-is.- Returns:
The first
Elementparsed from the string.
- core.htmle(tmpl, ...values)#
A tag function that safely generates an
HtmlErrorvia a tagged template. Substitutions are HTML-escaped, except forDocumentFragmentandElementinstances, whoseouterHTMLis substituted as-is.- Returns:
An
HtmlError.
- core.on(el)#
Return a
Proxyobject that sets event listeners on an element. This is a more compact way to callel.addEventListener(). To set a listener, call the method on the proxy that corresponds to the event, and pass the listener function as an argument. The methods return theProxy, so multiple liseteners can be set with chained calls.on(qs(document, 'button')) .click(() => alert("Clicked")) .mouseenter(() => alert("Mouse entered")) .mouseleave(() => alert("Mouse left"));
- Arguments:
el (
HTMLElement) -- The DOM element on which to set event listeners.
- Returns:
A
Proxyobject.
- core.showAlert(message, kind='success')#
Show an alert at the top of the page. If the message is an
HtmlError,kindis taken from the error and defaults to'danger'.- Arguments:
message (
string|Error) -- The message to display.kind (
string) -- The kind of message to display.
Classes
- class core.HtmlError(html, options)#
An
Errorsubclass that carries its message as an HTML element or fragment. It is useful to generate rich error messages for display e.g. withshowAlert().Properties
- HtmlError.html#
The HTML element or fragment provided to the constructor.
- HtmlError.kind#
The kind of alert to show for this error.
Methods
- HtmlError.[static] of(err)#
Create an
HtmlErrorfrom anErroror a string.- Arguments:
err (
Error|string) -- The error or error message from which to construct theHtmlError. Iferris already anHtmlError, it is returned unchanged.
- HtmlError.as(kind)#
Specify the kind of alert to show for this error.
- Retunrs:
The error itself.
- class core.TdocElement()#
The base class for all custom
<tdoc-*>elements.Properties
- TdocElement.ready#
A
Promisethat resolves to the element when it becomes ready.
Methods
- TdocElement.[static] extend(name, handler)#
Extend a custom
<tdoc-*>element type. This method registers a handler whose methods are called at different stages of the lifecycle of element instances.- Arguments:
name (
string) -- The name of the custom element type.handler (
Object) -- An object of lifecycle methods.
The following methods can be defined on
handler:ready(el): Called when an element becomes ready.
- class core.DynElement()#
The class implementing
<tdoc-dyn>custom elements. It extendsTdocElement.Properties
- DynElement.type#
The type of the directive, contained in the
typeattribute.
- DynElement.name#
The name of the block, contained in the
nameattribute, if the directive was named.
- DynElement.args#
The arguments provided in the directive content, contained in the
argsattribute. This is a serialized JSON object.
- DynElement.controller#
The controller object for the element. The type of the controller is specific to each directive.