lsdocs_rst #
A fast reStructuredText parser that builds docutils' document tree.
lsdocs-rst is a Rust port of docutils' parser and transforms. It builds the tree docutils builds (the same node kinds and attributes, printed as the same pseudo-XML) and records where in the source each node came from:
import lsdocs_rst
doc = lsdocs_rst.parse("Title\n=====\n\nHello, *world*.\n", transforms=True)
print(doc.pformat())
for emphasis in doc.findall("emphasis"):
print(emphasis.astext(), emphasis.span) # world (20, 27)Settings are docutils' (tab_width, pep_references, ...), given as
keyword arguments. Directives and roles can be written in Python (see
Directive and Parser.role()), and directives such as
include read files only through a loader you give (root= or
files=). With docutils installed, Element.to_docutils() turns a
tree into a docutils.nodes.document for docutils' writers.
class Text #
, : | None = None)Text in the tree.
method astext #
-> method to_dict #
-> method pformat #
-> class Element #
, : | None = None, : | None = None, : | None = None)An element: a docutils node kind ("paragraph", "section",
...), attributes, children and a span.
Like a docutils node, element[i] is a child and element["ids"]
an attribute.
property tagname #
: The kind, under docutils' name for it.
method from_dict #
-> An element from its dict form (see to_dict()).
method to_dict #
-> The dict form: kind, and attributes, children and
span when present. Text is {"text": ..., "span": ...}. The
same form as the Rust crate's serde feature, so it round-trips
through JSON.
method pformat #
-> Pseudo-XML, exactly as docutils' pformat() prints it.
method to_xml #
-> Docutils XML, as docutils --writer=xml writes it.
method to_docutils #
-> The tree as docutils nodes (needs docutils). A document
element becomes a docutils.nodes.document with settings
(docutils' defaults if None), ready for docutils' writers.
method astext #
-> The text of all descendants, concatenated.
method traverse #
-> This element and its descendant elements, in document order.
method findall #
-> This element and its descendants of kind (all if None),
in document order, as docutils' findall.
method get #
-> variable Node #
= class DirectiveError #
, : = )Raised by a directive or role: reported in the document as a system
message of level (1 info, 2 warning, 3 error, 4 severe), as
docutils reports its own directives' errors.
class DirectiveContext #
)What a directive's Directive.run() gets: what was written, and
access to the parser.
method parse_content #
-> Parses the content as body elements into container.
method parse_inline #
-> Parses text as inline markup: the nodes, and messages to put
after the element holding them.
method message #
-> A system message (1 info to 4 severe) for the directive's line.
field name #
: = field arguments #
: = field options #
: = field content #
: = field lineno #
: = field block_text #
: = field span #
: | None = field in_substitution #
: = class Directive #
A directive: subclass it, set what it accepts, and implement
run():
class Shout(lsdocs_rst.Directive):
required_arguments = 1
final_argument_whitespace = True
def run(self, ctx):
nodes, messages = ctx.parse_inline(ctx.arguments[0].upper())
return [lsdocs_rst.Element("paragraph", children=nodes), *messages]
parser = lsdocs_rst.Parser().directive("shout", Shout)option_spec maps option names to docutils' conversion functions,
by name ("flag", "unchanged", "unchanged_required",
"path", "uri", "int", "nonnegative_int",
"percentage", "length_or_unitless",
"length_or_percentage_or_unitless", "class_option"), by the
functions themselves (docutils.parsers.rst.directives.flag, ...),
or to a list of choices.
field required_arguments #
: = 0field optional_arguments #
: = 0field final_argument_whitespace #
: = Falsefield option_spec #
: = field has_content #
: = Falsemethod run #
-> The directive's nodes; raise DirectiveError to report a
problem instead.
class RoleContext #
)What a role function gets.
field name #
: = field text #
: = field raw_text #
: = field rawsource #
: = field options #
: = field lineno #
: = field span #
: | None = class Parser #
| None = None, : = False, : = False, : | | None = None, : | None = None, : | None = None, **: )A configured parser. Each method returns a new parser, so a parser can be shared and specialized:
parser = Parser(transforms=True, tab_width=4).role("shout", shout)
doc = parser.parse(text)Keyword arguments: source (the document's name, used in messages),
transforms (apply docutils' transforms, as publish_doctree
does), keep_unknown_directives (keep them as directive
elements instead of reporting them), root (let directives read
files below this directory), files and urls (or read them with
these functions, which return bytes or str and raise OSError), and
any docutils setting the parser honors (tab_width,
pep_references, id_prefix, ...; see
default_settings()).
method directive #
-> A parser with directive (a Directive subclass or
instance) under name, before any standard one of that name.
method role #
-> A parser with the role function role under name. It gets
a RoleContext and returns inline nodes (or raises
DirectiveError).
property settings #
: The docutils settings in use.
method parse #
-> Parses text into a document element. Problems are
system_message elements in the tree; parsing itself only fails
if a Python directive, role or loader raises (other than
DirectiveError and OSError).
method parse_with_messages #
-> parse(), plus the messages docutils' transforms report
without putting them in the tree (problematic elements refer to
them by id).
function parse #
-> Parses text; the keyword arguments are Parser's.
function make_id #
-> name as docutils makes ids of names (docutils.nodes.make_id).
function default_settings #
-> The docutils settings the parser honors, with docutils' defaults.