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 #
= A handler: (params, ctx) -> result, or an async def returning it.
class Plugin #
, : = , *, : = 1, : = )An lsdocs plugin: a name, a version and its handlers.
method parse #
-> Registers the parser for files with these extensions (no dot).
method read #
-> 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 #
-> 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 #
-> Registers the renderer for these output formats.
method render_all #
-> 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 #
: The capabilities reported to the host.
method serve #
-> NoneRuns 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.