Skip to content
lsdocs plugin SDK API

lsdocs_plugin #

Write lsdocs plugins in Python.

A plugin is a Plugin with handlers registered by decorator. Each handler receives the request params (a dict shaped like the protocol schema) and a Context, and returns the result:

from lsdocs_plugin import Plugin

plugin = Plugin("shout", "0.1.0")

@plugin.transform(nodes=["text"])
def shout(params, ctx):
    doc = params["document"]
    for node in doc["children"]:
        if node["type"] == "text":
            node["value"] = node["value"].upper()
    return doc  # the bare document is shorthand for {"document": doc}

if __name__ == "__main__":
    plugin.serve()

Shorthand results: parse and transform handlers may return just the document; read may return any value (taken as data); render may return a str or bytes (taken as output, with extension set to the format); render_all may return a list of files.

Handlers may be async def: coroutines run on one event loop shared by all calls, so loop-bound objects (client sessions, locks) can be reused:

@plugin.transform(nodes=["link"])
async def check_links(params, ctx):
    async with session.head(url) as response: ...

Handlers run one at a time unless max_concurrency is raised, which lets async handlers overlap. The protocol itself (framing, handshake, cancellation, shutdown) is handled in Rust.

Submodules

variable Handler #

Handler = Callable[[dict[str, Any], Context], Any]

A handler: (params, ctx) -> result, or an async def returning it.

class Plugin #

class Plugin(name: str, version: str = "0.0.0", *, max_concurrency: int = 1, config_files: Iterable[str] = ())

An lsdocs plugin: a name, a version and its handlers.

method parse #

parse(self, *, extensions: Iterable[str]) -> Callable[[F], F]

Registers the parser for files with these extensions (no dot).

method read #

read(self, func: F | None = None) -> Any

Registers the parse/transform/each handler, run on every document (reading it, not changing it) before transform(). Usable as @plugin.read or @plugin.read().

method transform #

transform(self, *, nodes: Iterable[str] = ()) -> Callable[[F], F]

Registers the transformer (render/transform/each). nodes are the node kinds it looks at (e.g. ["heading", "custom:toc"]); documents without any of them are not sent. Empty means every document.

method render #

render(self, *, formats: Iterable[str]) -> Callable[[F], F]

Registers the renderer for these output formats.

method render_all #

render_all(self, func: F) -> F

Registers the render/transform/all handler, called once after every page is rendered with the list of rendered pages ({"format", "pages": [{"source", "path", "metadata", "data"}]}, data being what the page's render returned as data, if anything). It returns files no single page makes: {"files": [{"path", "output"}]} or just the list, each output a str or bytes. Only used with a renderer (render()).

property capabilities #

property capabilities: dict[str, Any]

The capabilities reported to the host.

method serve #

serve(self) -> None

Runs the plugin until the host is done with it.

Talks to the host over stdin/stdout, or over the pipe in LSDOCS_PIPE. print() output goes to stderr from here on, so it cannot corrupt the protocol stream.