gd_highlight
============
.. rust:crate:: gd_highlight
:synopsis: Syntax highlighting with Pygments' token classes (k, nf, s2, c1) and stylesheets, for 35 languages, without a Python runtime.
Syntax highlighting with Pygments' token classes (``k``, ``nf``, ``s2``, ``c1``) and stylesheets, for 35 languages, without a Python runtime.
The common path is one call:
.. code-block:: rust
let html = gd_highlight::highlight_html("def f(x):\n return x\n", "python");
assert!(html.starts_with("def"));
The staged path finds a lexer (:rust:fn:`find_lexer `), resolves Sphinx's pseudo languages (:rust:fn:`resolve_lexer `), scans tokens (:rust:fn:`scan_tokens `, :rust:fn:`tokenize_into `) and writes them (:rust:fn:`write_html `, :rust:fn:`write_typst `) into a caller's ``String``, which can be reused. Styles (:rust:enum:`Style `, :rust:fn:`find_style `, :rust:fn:`write_css `, :rust:fn:`write_typst_theme `) and problems (:rust:fn:`check_code `, :rust:fn:`write_problem `) complete the surface. Nothing here keeps global state; the same input always gives the same output.
.. rubric:: Structs
.. rust:struct:: pub struct HtmlLines<'a>
Numbers and emphasizes lines as Pygments does for Sphinx's ``:linenos:`` and ``:emphasize-lines:`` (``linenos="inline"``, ``hl_lines``).
Each line starts with `` 7``, and an emphasized line, number included, is wrapped in ``...\n``.
.. rubric:: Fields
.. rust:field:: pub first: usize
The first line's number; 0 leaves lines unnumbered.
.. rust:field:: pub emphasized: &'a [usize]
The 1-based numbers of the lines to emphasize, in any order.
.. rust:struct:: pub struct LexerInfo
A lexer's static description.
.. rubric:: Fields
.. rust:field:: pub name: &'static str
Pygments' lexer name: ``"Python"``.
.. rust:field:: pub aliases: &'static [&'static str]
The names code blocks use; the first is the canonical one.
.. rust:field:: pub filenames: &'static [&'static str]
Glob patterns of file names: ``"*.py"``, ``"Makefile"``.
.. rust:field:: pub mimetypes: &'static [&'static str]
MIME types.
.. rust:struct:: pub struct Problem<'a>
A problem with a code block or a style name. It borrows the name and the code it was found in.
.. rubric:: Fields
.. rust:field:: pub kind: ProblemKind
What is wrong.
.. rust:field:: pub name: &'a str
The language or style as written.
.. rust:field:: pub lexer: Lexer
The lexer the code was lexed with (``LexingError``).
.. rust:field:: pub suggestion: Option<&'static str>
A close name, for "Did you mean".
.. rust:field:: pub line: usize
``LexingError``: the 1-based line of the first Error token.
.. rust:field:: pub column: usize
``LexingError``: its 1-based column, in characters.
.. rust:field:: pub excerpt: &'a str
``LexingError``: the line it is on.
.. rust:field:: pub near: &'a str
``LexingError``: its text, up to the line's end.
.. rubric:: Implementations
.. rust:impl:: impl<'a> Problem<'a>
:noindex:
:for: Problem<'a>
.. rust:method:: pub fn title(&self) -> &'static str
The report's title: ``"UNKNOWN LANGUAGE"``.
.. rubric:: Trait Implementations
.. rust:impl:: impl fmt::Display for Problem<'_>
:noindex:
:for: Problem<'_>
:trait: fmt::Display
.. rust:method:: fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
:noindex:
The report without a location.
.. rust:impl:: impl std::error::Error for Problem<'_>
:noindex:
:for: Problem<'_>
:trait: std::error::Error
.. rust:struct:: pub struct Scanner<'a>
Tokenizes one input, yielding merged tokens as an iterator.
Create it with :rust:fn:`scan_tokens `; it borrows the code and allocates nothing.
.. rubric:: Trait Implementations
.. rust:impl:: impl Iterator for Scanner<'_>
:noindex:
:for: Scanner<'_>
:trait: Iterator
.. rust:associatedtype:: type Item = Token
:noindex:
.. rust:method:: fn next(&mut self) -> Option
:noindex:
The next token; neighbours of one kind come back merged.
.. rust:struct:: pub struct Color(pub [u8; 6])
A CSS colour as Pygments normalizes it: six hex digits, case kept.
.. rubric:: Implementations
.. rust:impl:: impl Color
:noindex:
:for: Color
.. rust:method:: pub fn as_str(&self) -> &str
The six hex digits, without ``#``.
.. rust:struct:: pub struct Look
A token kind's resolved style.
.. rubric:: Fields
.. rust:field:: pub color: Option
The text colour.
.. rust:field:: pub background: Option
The background colour.
.. rust:field:: pub border: Option
The border colour.
.. rust:field:: pub bold: bool
Bold text.
.. rust:field:: pub italic: bool
Italic text.
.. rust:field:: pub underline: bool
Underlined text.
.. rubric:: Implementations
.. rust:impl:: impl Look
:noindex:
:for: Look
.. rust:method:: pub fn is_styled(&self) -> bool
Whether the look sets anything.
.. rust:struct:: pub struct StyleInfo
A style's definition as Pygments' style class holds it.
.. rubric:: Fields
.. rust:field:: pub name: &'static str
The ``pygments_style`` name: ``"github-dark"``.
.. rust:field:: pub dark: bool
Whether the background is dark.
.. rust:field:: pub background: &'static str
The ``.highlight`` background.
.. rust:field:: pub highlight: &'static str
The background of emphasized lines (``.hll``).
.. rust:field:: pub line_number: &'static str
The colour of line numbers.
.. rust:field:: pub line_number_background: &'static str
The background of line numbers.
.. rust:field:: pub special_line_number: &'static str
The colour of special line numbers.
.. rust:field:: pub special_background: &'static str
The background of special line numbers.
.. rust:field:: pub base: &'static str
The root token's definition, which every kind inherits.
.. rust:field:: pub rules: &'static [StyleRule]
The style's dict.
.. rust:struct:: pub struct StyleLooks
Every kind's look in one style, from :rust:fn:`resolve_style `.
.. rubric:: Fields
.. rust:field:: pub base: Look
The root token's look, which kinds inherit.
.. rust:field:: pub kinds: [Look; TokenKind::COUNT]
The looks, indexed by ``kind as usize``; read them with :rust:fn:`StyleLooks::look `.
.. rubric:: Implementations
.. rust:impl:: impl StyleLooks
:noindex:
:for: StyleLooks
.. rust:method:: pub fn look(&self, kind: TokenKind) -> Look
The resolved look of ``kind``.
.. rust:struct:: pub struct Token
A run of the input: ``code[start..end]`` has ``kind``.
The tokens a scanner yields are contiguous, cover every byte exactly once, never split a UTF-8 character, and never put two neighbours of one kind side by side.
.. rubric:: Fields
.. rust:field:: pub start: u32
Byte offset of the first byte.
.. rust:field:: pub end: u32
Byte offset just past the last byte.
.. rust:field:: pub kind: TokenKind
What the run is.
.. rubric:: Enums
.. rust:enum:: pub enum Lexer
A language. :rust:variant:`Lexer::Default ` and :rust:variant:`Lexer::Guess ` are Sphinx's pseudo languages, which scanning resolves against the code (see :rust:fn:`resolve_lexer `).
.. rubric:: Variants
.. rust:variant:: Text
Plain text: ``text``, ``none``.
.. rust:variant:: Python
Python.
.. rust:variant:: Pycon
Python console sessions (``pycon``).
.. rust:variant:: Pytb
Python tracebacks (``pytb``).
.. rust:variant:: C
C.
.. rust:variant:: Cpp
C++.
.. rust:variant:: Rust
Rust.
.. rust:variant:: Go
Go.
.. rust:variant:: Java
Java.
.. rust:variant:: Kotlin
Kotlin.
.. rust:variant:: Csharp
C#.
.. rust:variant:: Swift
Swift.
.. rust:variant:: Zig
Zig.
.. rust:variant:: Odin
Odin.
.. rust:variant:: Javascript
JavaScript.
.. rust:variant:: Typescript
TypeScript.
.. rust:variant:: Json
JSON.
.. rust:variant:: Yaml
YAML.
.. rust:variant:: Toml
TOML.
.. rust:variant:: Ini
INI and similar configuration files.
.. rust:variant:: Xml
XML.
.. rust:variant:: Html
HTML, with ``