guidedoc_rst¶
The reStructuredText reader of Guidedoc: lines, block structure, explicit markup, and the standard Docutils directives, implemented independently from the Docutils markup specification. Inline markup and reference resolution live in guidedoc_rst_inline, which this crate drives.
use guidedoc::{Registry, Workspace, default_request, dump_to_string, read_document};
let mut ws = Workspace::new();
let mut request = default_request("x.rst", "Hello *world*.\n", "rst", "html");
request.read.promote_title = false;
let doc = read_document(&mut ws, &guidedoc_rst::reader(), &request).unwrap();
assert!(dump_to_string(doc).starts_with("document\n paragraph\n"));
Dialects (the Sphinx layer) add directives and roles through Extension and read with read_with; another markup (MyST) runs the directives through Host, embed_begin, and Parser::run_hosted.
Re-exports
- pub use settings::SmartQuotes
- pub use guidedoc_rst_inline::Role
- pub use guidedoc_rst_inline::RoleBase
- pub use guidedoc_rst_inline::RoleCall
- pub use guidedoc_rst_inline::standard_role
- pub use guidedoc_rst_inline::standard_roles
Modules
guidedoc_rst::messages– The reader’s messages.
Structs
- pub struct guidedoc_rst::DirOption<'d>¶
-
One option a directive was given: its specification, its value lines (the rest of the marker line, then the continuation lines), and where it was written.
Fields
- pub span: SourceSpan¶
-
The option’s lines in the source.
- pub struct guidedoc_rst::Directive<'d>¶
-
A directive split into its parts and checked against its rules. Handlers read it; the lines are views of the current source (
Parser::src).Fields
- pub name: &'d str¶
-
The directive name as written.
- pub span: SourceSpan¶
-
The whole directive block.
- pub name_span: SourceSpan¶
-
The name on the marker line.
- pub sections: bool¶
-
The directive stands where sections are allowed.
- pub subst: bool¶
-
The directive is inside a substitution definition.
Implementations
- impl<'d> Directive<'d>
-
- pub fn handler(&self) -> Option<&DirectiveHandler<'d>>¶
-
Returns the dialect’s handler that runs the directive;
Nonefor a standard one.
- pub fn option(&self, name: &str) -> Option<&DirOption<'d>>¶
-
Returns the option called
namethe directive was given.
- pub fn has_option(&self, name: &str) -> bool¶
-
Tells whether the directive was given option
name, flags included.
- pub fn option_lines(&self, name: &str) -> Option<&[Line<'d>]>¶
-
Returns the value lines of option
name.
- pub fn option_value(&self, name: &str) -> Option<Cow<'d, str>>¶
-
Returns option
name’s value lines joined by spaces and trimmed;Nonewhen the option was not given.
- pub fn option_text(&self, name: &str) -> Option<Cow<'d, str>>¶
-
Returns option
name’s value lines joined by line breaks and trimmed, as text options such as:start-after:match text that spans lines.
- pub fn argument(&self) -> Cow<'d, str>¶
-
The argument lines joined by spaces.
- pub struct guidedoc_rst::DirectiveHandler<'h>¶
-
A dialect’s directive: its rules, checked by the reader before it runs, and a tag the dialect chooses to find its own code again in
Extension::run_directive.Fields
- pub name: &'h str¶
-
The directive’s canonical name (“py:function”), compared case-insensitively.
- pub final_ws: bool¶
-
The last argument may contain white space.
- pub content: ContentRule¶
-
Whether it takes content.
- pub tag: usize¶
-
The dialect’s own identification of the handler, such as a table index.
- pub struct guidedoc_rst::DirectiveShape<'h>¶
-
How a directive takes its parts, for a host that checks them itself.
Fields
- pub final_ws: bool¶
-
The last argument may contain white space.
- pub content: ContentRule¶
-
Whether it takes content.
- pub struct guidedoc_rst::HostDirective<'s>¶
-
A directive the host has split into its parts.
Fields
- pub name: &'s str¶
-
The directive name.
- pub name_span: SourceSpan¶
-
Where the name was written.
- pub span: SourceSpan¶
-
The whole directive.
- pub options: Vec<HostOption<'s>>¶
-
The options, in order.
- pub sections: bool¶
-
The directive stands where sections are allowed.
- pub struct guidedoc_rst::HostItem<'s>¶
-
One item of a host’s bullet list: its content lines and the index in the given lines of the line it starts on.
Fields
- pub start: usize¶
-
Where the item starts in the lines
Host::itemswas given.
- pub struct guidedoc_rst::HostOption<'s>¶
-
A directive option as the host wrote it: its name and value lines.
Fields
- pub name: &'s str¶
-
The option name.
- pub span: SourceSpan¶
-
Where it was written.
- pub struct guidedoc_rst::Line<'s>¶
-
One input line in columns: tabs expanded, trailing white space stripped. Lines are
Copyviews; cutting one never copies text.Fields
- pub text: &'s str¶
-
The line’s text from its current column.
Implementations
- impl<'s> Line<'s>
-
- pub fn from_view(view: LineView<'s>) -> Line<'s>¶
-
Makes a line of source text that needs no expansion (a host’s view of a line whose tabs it handles itself, or text without tabs).
- pub fn is_blank(&self) -> bool¶
-
Tells whether the line has no text (white space is stripped when lines are made).
- pub fn is_indented(&self) -> bool¶
-
Tells whether the line starts with a space.
- pub fn start(&self, tab: usize) -> u32¶
-
Returns the source offset where the line’s text starts, with tab stops
tab.
- pub fn end(&self, tab: usize) -> u32¶
-
Returns the source offset where the line’s text ends, with tab stops
tab.
- pub struct guidedoc_rst::Parser<'p, 'a>¶
-
The block parser: a read context, the reader’s state, and the dialect’s handlers and host. Handlers receive it;
ctx.bbuilds nodes.Fields
- pub ctx: &'p mut ReadContext<'a>¶
-
The read context: builder, options, sources, reports.
Implementations
- impl<'p, 'a> Parser<'p, 'a>
-
- pub fn add_common(&mut self, id: NodeId, d: &Directive<'_>, class_opt: &str) -> Result<(), Fail>¶
-
Adds the
:class:(orclass_opt) and:name:options as attributes. Directive nodes keep their Name attribute for the directive name, so:name:becomes Custom “name” there.
- impl<'p, 'a> Parser<'p, 'a>
-
- pub fn directive_shape(&self, name: &str) -> Option<DirectiveShape<'p>>¶
-
Looks directive
nameup as the reader would, the dialect’s handlers first.
- pub fn run_hosted(&mut self, h: &HostDirective<'_>) -> Result<HostedResult, Fail>¶
-
Checks and runs a directive written in the host markup. Options are checked as in reStructuredText; a host with other rules checks them first (
directive_shape,option_problem) and passes only those it keeps. Fails only when the builder fails.
- pub fn host_lines<'s>(&self, views: &[LineView<'s>], buf: &'s mut String) -> Vec<Line<'s>>¶
-
Makes lines of a host’s views of source text: tabs are expanded to the parser’s tab stops into
buf, as the reader’s own lines are.
- pub fn host_views<'s>(&self, lines: &[Line<'s>]) -> Vec<LineView<'s>>¶
-
Gives the host the source text of lines, tabs unexpanded.
- pub fn lend<R>(&mut self, f: impl FnOnce(&mut ReadContext<'a>, &mut ReaderState) -> R) -> R¶
-
Lends the read context and the reader’s state to
f. A host that parses nested content with a reader of its own, whose directives run through this reader again, uses it to move the state where those handlers find it (and back) while the context is with its reader.
- impl Parser<'_, '_>
-
- pub fn pool_classes(&mut self, s: &str) -> Result<Option<Text>, Fail>¶
-
Writes white-space separated class names as identifiers, one space apart, to the text pool;
Nonewhen a name has no letters or there is none.
- pub fn pool_lower(&mut self, s: &str) -> Result<Text, Fail>¶
-
Writes
sin lower case (ASCII) to the text pool.
- pub fn push_lines(&mut self, lines: &[Line<'_>], sep: &str) -> Result<(), Fail>¶
-
Pushes
linesjoined byseponto the text run being built (betweenBuilder::text_openandtext_close).
- impl<'p, 'a> Parser<'p, 'a>
-
- pub fn tab(&self) -> usize¶
-
The tab stops of the current source.
- pub fn is_hosted(&self) -> bool¶
-
Tells whether the running directive was written in the host markup.
- pub fn inline_state(&mut self) -> &mut InlineState¶
-
The inline state: custom roles and the default role.
- pub fn stack_guard(&self) -> StackGuard¶
-
The stack guard of the read, for a handler’s own recursion (a C++ parser).
- pub fn apply_host_role(&mut self, name: &str, lines: &[LineView<'_>], content: std::ops::Range<usize>) -> Result<(), Fail>¶
-
Runs role
nameon interpreted text written in the host markup (MyST’s{name}roles), with the dialect’s roles first; seeguidedoc_rst_inline::apply_host_role. Fails only when the builder fails.
- pub fn take_pending_classes(&mut self) -> Option<Text>¶
-
Takes the classes a class directive left for the next element, if any.
- pub fn report(&mut self, m: guidedoc::Message, span: SourceSpan, a: Arg, b: Arg)¶
-
Reports message
matspanwith its arguments. Reports never fail the read.
- pub fn open(&mut self, kind: NodeKind, span: SourceSpan) -> Result<NodeId, Fail>¶
-
Begins an element and gives it the classes of a preceding class directive.
- pub fn span_of(&self, first: &Line<'_>, last: &Line<'_>) -> SourceSpan¶
-
The source span from the start of
firstto the end oflast, in the current source, clamped to its text.
- pub fn line_span(&self, l: &Line<'_>) -> SourceSpan¶
-
The source span of one line.
- pub fn lines_span(&self, lines: &[Line<'_>]) -> SourceSpan¶
-
The source span of
lineswithout trailing blank lines; empty lines give an empty span at the start of the source.
- pub fn inline(&mut self, lines: &[Line<'_>]) -> Result<(), Fail>¶
-
Parses
linesas inline content of the open node, in the markup of the running directive (seeHost).
- pub fn inline_views(&mut self, views: &[LineView<'_>]) -> Result<(), Fail>¶
-
Parses source views as reStructuredText inline content of the open node.
- pub fn inline_node(&mut self, kind: NodeKind, lines: &[Line<'_>]) -> Result<(), Fail>¶
-
Adds a node of
kindholdinglinesparsed as inline content; nothing for no lines.
- impl Parser<'_, '_>
-
- pub fn enter_source(&mut self, id: SourceId, tab: usize) -> SourceFrame¶
-
Makes the lines of source
idcurrent, with tab stopstab, for spans and inline parsing;leave_sourcerestores the previous source.
- pub fn leave_source(&mut self, frame: SourceFrame)¶
-
Makes the source
enter_sourcereturnedframefor current again.
- impl Parser<'_, '_>
- impl Parser<'_, '_>
-
- pub fn place_titled(&mut self, lines: &[Line<'_>]) -> Result<(), Fail>¶
-
Prepares for titled content (
parse_titled) as Sphinx’s only directive does: when the content’s first title has an adornment the document already uses, the content stands where a title of that level would, so deeper sections close first.
- pub fn parse_titled(&mut self, lines: &[Line<'_>]) -> Result<(), Fail>¶
-
Parses content in which titles begin sections, in a title context of its own (Sphinx’s nested parse with titles): adornments take levels afresh, the first nesting in the innermost open section, and the sections close when the content ends. The surrounding adornments are kept for what follows.
- impl Parser<'_, '_>
-
- pub fn take_table_extra(&mut self) -> Option<TableExtra>¶
-
Takes the title and options of the hosted table directive whose content is being parsed, for the first table the host emits;
Noneoutside one or once taken.
- pub fn table_decorate(&mut self, id: NodeId, span: SourceSpan, cols: usize, widths: &[usize], extra: Option<&mut TableExtra>) -> Result<(), Fail>¶
-
Gives an open Table, before its rows, its widths and a table directive’s attributes and title. A host calls it for the table it emits inside a table directive (
take_table_extra).
Trait Implementations
- impl fmt::Debug for Parser<'_, '_>
-
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
- pub struct guidedoc_rst::ReaderState¶
-
The reader’s state of one document: the current source, tab stops, section levels, pending classes, the inline state, and nesting depth.
readkeeps one for the call; a host markup that embeds the reader (embed_begin) keeps it between calls and views it as aParserwithReaderState::parser.Implementations
- impl ReaderState
-
- pub fn new(ctx: &mut ReadContext<'_>, settings: &Settings) -> ReaderState¶
-
Prepares the state for the main source of
ctxwithsettings, with the storage an earlier read left in the workspace (endgives it back).
- pub fn end(&mut self, ctx: &mut ReadContext<'_>)¶
-
Gives the state’s storage back to the workspace for the next read; call it once when the read ends (
Parser::embed_endandread_inlinedo). The state stays usable, with empty buffers.
Trait Implementations
- impl fmt::Debug for ReaderState
-
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
- pub struct guidedoc_rst::SourceFrame¶
-
Where the parser stood before
enter_source.
- pub struct guidedoc_rst::Settings¶
-
The reader’s settings. They reach
readthroughReadOptions::extension, made withguidedoc::read_extension(&settings); settings of another type are refused. Start fromSettings::default()(Docutils’ defaults): the bibliographic fields are on.Directive and role handlers (
Extension) and a host markup (Host) are not settings: a dialect passes them toread_withandembed_begin.Fields
- pub pep_references: bool¶
-
Implicit “PEP nnn” references in running text become links.
- pub rfc_references: bool¶
-
Implicit “RFC nnn” references in running text become links.
- pub trim_footnote_reference_space: bool¶
-
White space before a footnote reference is removed.
- pub smart_quotes: SmartQuotes¶
-
Typographic quotation marks, apostrophes, dashes, and ellipses in text.
- pub smart_quotes_locales: String¶
-
Quotation marks per language, overriding the built-in ones, as
"tag:“”‘’,tag2:«»‹›": primary open and close, then secondary open and close.
- pub language: String¶
-
BCP 47 tag of the document language: bibliographic field names, generated titles, and quotation marks. “” means “en”.
- pub docinfo: bool¶
-
A field list first in the document becomes bibliographic data (docinfo).
- pub sectsubtitle: bool¶
-
A lone subsection title becomes the section’s subtitle.
- pub tab_width: usize¶
-
Tab stops; zero means
ReadOptions::tab_width, which zero makes 8.
- pub character_level_inline_markup: bool¶
-
Inline markup is recognized inside words (Docutils’
character_level_inline_markup).
Trait Implementations
- pub struct guidedoc_rst::OptSpec¶
-
One option a directive accepts.
Fields
- pub name: &'static str¶
-
The option name, compared exactly.
- pub choices: &'static str¶
-
Space-separated choices, for
OptType::Choice.
- pub struct guidedoc_rst::TableExtra¶
-
What a table directive adds to its table: a title and attributes. A host markup that emits the table of a hosted table directive takes it with
Parser::take_table_extraand passes it toParser::table_decorate.Fields
- pub width_class: &'static str¶
-
“colwidths-auto” or “colwidths-given” from
:widths:; a host may set it.
Enums
- pub enum guidedoc_rst::HostedResult¶
-
The outcome of
run_hosted.Variants
- Ran¶
-
The directive ran.
- Unknown¶
-
No directive has that name; nothing was reported.
- Failed¶
-
The arguments, options, or content were refused and reported.
- pub enum guidedoc_rst::ArgRule¶
-
Whether a directive takes an argument.
Variants
- None¶
-
No argument; words on the directive line are content.
- Optional¶
-
An argument may be given.
- Required¶
-
An argument must be given.
- pub enum guidedoc_rst::ContentRule¶
-
Whether a directive takes content.
Variants
- None¶
-
Content is refused.
- Optional¶
-
Content may be given.
- Required¶
-
Content must be given.
- pub enum guidedoc_rst::OptType¶
-
The type of an option’s value, checked before the directive runs.
Variants
- Flag¶
-
No value.
- Text¶
-
Any text, possibly empty.
- TextRequired¶
-
Non-empty text.
- Int¶
-
A signed integer.
- NonnegInt¶
-
A nonnegative integer.
- PosInt¶
-
A positive integer.
- OptInt¶
-
A nonnegative integer or nothing.
- Length¶
-
A CSS length (
12px,3em, a bare number).
- LengthPercent¶
-
A length or a percentage.
- Figwidth¶
-
A length, a percentage, or
image.
- Percentage¶
-
A percentage (with or without “%”).
- Class¶
-
Class names.
- Name¶
-
A reference name (non-empty).
- Choice¶
-
One of
OptSpec::choices.
- Encoding¶
-
An encoding name.
- Char¶
-
One character,
tab,space, or a character code.
- Widths¶
-
auto,grid, or positive integers.
- Path¶
-
A path (non-empty).
Traits
- pub trait guidedoc_rst::Extension¶
-
A dialect’s directives, roles, and hooks. Methods take
&self: a dialect keeps its per-document state in cells, because handlers run re-entrantly (a directive’s content holds more directives).- fn directive(&self, name: &str) -> Option<DirectiveHandler<'_>>¶
-
Returns the handler of directive
name, orNonefor one the dialect does not define (the standard directives are consulted next).
- fn run_directive(&self, p: &mut Parser<'_, '_>, d: &Directive<'_>) -> Result<(), Fail>¶
-
Runs a checked directive whose handler
directivereturned (d.handler()). Problems are reported; only a builder failure is anErr.
- fn role(&self, _ctx: &mut ReadContext<'_>, _call: &RoleCall<'_>) -> bool¶
-
Handles interpreted text before the document’s and the standard roles: emits nodes through
ctx.band returns true, or returns false for a role it does not know.
- fn has_prologue(&self) -> bool¶
-
Tells whether the dialect has a prologue (
prologue). With one, the main source’s leading field lines are read first, on their own, as Sphinx inserts rst_prolog after a document’s bibliographic fields.
- fn prologue(&self, _p: &mut Parser<'_, '_>) -> Result<(), Fail>¶
-
Runs after the main source’s leading field lines (see
has_prologue).
- pub trait guidedoc_rst::Host¶
-
The host markup’s side of hosted directives. Lines are views of the current source (
Parser::src), as the block parser’s are, so every node keeps exact spans. Methods take&self: a host keeps its parser state in cells, as they run re-entrantly.- fn body(&self, p: &mut Parser<'_, '_>, lines: &[Line<'_>], sections: bool, each: Option<Text>) -> Result<(), Fail>¶
-
Parses nested body content of the open node;
sectionsallows headings to make sections, andeach, when set, gives these classes to every top-level element (the class directive).
Functions
- pub fn guidedoc_rst::find_handler<'h>(table: &'h [DirectiveHandler<'h>], name: &str) -> Option<&'h DirectiveHandler<'h>>¶
-
Returns the handler of
tablenamedname, compared case-insensitively.
- pub fn guidedoc_rst::embed_begin(ctx: &mut ReadContext<'_>, settings: &Settings, ext: Option<&dyn Extension>) -> Result<ReaderState, Fail>¶
-
Prepares the reader for a host that reads the main source itself: the state for
ctxwithsettings, after the dialect’s begin hook ran. The host views the state as a parser (ReaderState::parser) for every call and ends withParser::embed_end. Fails only when the builder fails.
- pub fn guidedoc_rst::path_of(lines: &[Line<'_>]) -> String¶
-
Joins path lines without white space, as the specification treats paths.
- pub fn guidedoc_rst::dedent(lines: &mut [Line<'_>])¶
-
Strips the least indentation of the non-blank lines, in place.
- pub fn guidedoc_rst::expand_views<'s>(views: &[LineView<'s>], tab: usize, buf: &'s mut String) -> Vec<Line<'s>>¶
-
Makes lines of a host’s views of source text, expanding tabs into
bufassplit_linesdoes.
- pub fn guidedoc_rst::indent_of(s: &str) -> usize¶
-
Counts the spaces
sstarts with.
- pub fn guidedoc_rst::join_lines<'s>(lines: &[Line<'s>], sep: &str) -> Cow<'s, str>¶
-
Joins the lines’ text with
sep: borrowed for a single line, else a copy.
- pub fn guidedoc_rst::trim_blank_end<'l, 's>(lines: &'l [Line<'s>]) -> &'l [Line<'s>]¶
-
Returns
lineswithout trailing blank lines.
- pub fn guidedoc_rst::trim_blank_start<'l, 's>(lines: &'l [Line<'s>]) -> &'l [Line<'s>]¶
-
Returns
lineswithout leading blank lines.
- pub fn guidedoc_rst::bullet_width(t: &str) -> usize¶
-
Returns the byte length of a bullet that starts
t, or 0.
- pub fn guidedoc_rst::option_problem(s: &OptSpec, v: &str) -> Option<&'static str>¶
-
Checks value
vagainst the option’s type:Nonewhen it is valid, else why not.
- pub fn guidedoc_rst::read(ctx: &mut ReadContext<'_>) -> Result<(), Fail>¶
-
Reads
ctx.sourceas reStructuredText intoctx.b(guidedoc::ReadFn). Problems go to the reports; it fails when the builder fails (a limit, capacity) or reference resolution does.
- pub fn guidedoc_rst::read_inline(ctx: &mut ReadContext<'_>, span: SourceSpan) -> Result<(), Fail>¶
-
Parses a span of
ctx.sourceas inline content of the builder’s open node, as Sphinx parses a translated message (guidedoc::ReadInlineFn). Each line’s leading white space is dropped. References are left for the caller to resolve.
- pub fn guidedoc_rst::read_inline_with(ctx: &mut ReadContext<'_>, span: SourceSpan, settings: &Settings, ext: Option<&dyn Extension>) -> Result<(), Fail>¶
-
read_inlinewith explicit settings and a dialect’s roles.
- pub fn guidedoc_rst::read_with(ctx: &mut ReadContext<'_>, settings: &Settings, ext: Option<&dyn Extension>) -> Result<(), Fail>¶
-
Reads
ctx.sourcewith explicit settings and a dialect’s handlers (the Sphinx layer’s entry point). The extension’s hooks run at their points of the read: begin, prologue (after the leading field lines), end, then resolution with its default substitutions.
- pub const fn guidedoc_rst::reader() -> Reader¶
-
Returns the “rst” reader descriptor, for a
guidedoc::Registry.
- pub fn guidedoc_rst::default_settings() -> Settings¶
-
Returns Docutils’ defaults: bibliographic fields on, everything else off or empty (
Settings::default()).
- pub const fn guidedoc_rst::choice(name: &'static str, choices: &'static str) -> OptSpec¶
-
Shorthand for a choice option.
- pub fn guidedoc_rst::equal_fold(a: &str, b: &str) -> bool¶
-
Compares ASCII case-insensitively, as directive and role names are compared.
- pub fn guidedoc_rst::find_standard_directive(name: &str) -> Option<DirectiveHandler<'static>>¶
-
Returns the standard directive called
name, compared case-insensitively, as the handler the reader checks it with;Nonefor any other name.
- pub const fn guidedoc_rst::opt(name: &'static str, kind: OptType) -> OptSpec¶
-
Shorthand for an option specification without choices.
- pub fn guidedoc_rst::standard_directives() -> impl Iterator<Item = DirectiveHandler<'static>>¶
-
Returns the standard (Docutils) directives in the order the reader lists them, as the handlers the reader checks them with. The
tagof each is its index in that order. A dialect such as the Sphinx layer may redefine some of them.
- pub fn guidedoc_rst::column_width(s: &str) -> usize¶
-
Returns the display width of
sin columns, as Docutils measures a section title against its underline: East Asian wide and fullwidth characters count two columns, combining marks none, every other character one.
Constants
- pub const guidedoc_rst::CONFORMANCE: Conformance = Conformance::Partial¶
-
How complete the reader is.
- pub const guidedoc_rst::VERSION: &str = env!("CARGO_PKG_VERSION")¶
-
The reader’s version: the project version.