pub struct ClassedHTMLGenerator<'a> { /* private fields */ }
Expand description

Output HTML for a line of code with <span> elements using class names

Because this has to keep track of open and closed <span> tags, it is a struct with additional state.

There is a finalize() method that must be called in the end in order to close all open <span> tags.

Note that because CSS classes have slightly different matching semantics than Textmate themes, this may produce somewhat less accurate highlighting than the other highlighting functions which directly use inline colors as opposed to classes and a stylesheet.

§Example

use syntect::html::{ClassedHTMLGenerator, ClassStyle};
use syntect::parsing::SyntaxSet;
use syntect::util::LinesWithEndings;

let current_code = r#"
x <- 5
y <- 6
x + y
"#;

let syntax_set = SyntaxSet::load_defaults_newlines();
let syntax = syntax_set.find_syntax_by_name("R").unwrap();
let mut html_generator = ClassedHTMLGenerator::new_with_class_style(syntax, &syntax_set, ClassStyle::Spaced);
for line in LinesWithEndings::from(current_code) {
    html_generator.parse_html_for_line_which_includes_newline(line);
}
let output_html = html_generator.finalize();

Implementations§

source§

impl<'a> ClassedHTMLGenerator<'a>

source

pub fn new( syntax_reference: &'a SyntaxReference, syntax_set: &'a SyntaxSet ) -> ClassedHTMLGenerator<'a>

👎Deprecated since 4.2.0: Please use new_with_class_style instead
source

pub fn new_with_class_style( syntax_reference: &'a SyntaxReference, syntax_set: &'a SyntaxSet, style: ClassStyle ) -> ClassedHTMLGenerator<'a>

source

pub fn parse_html_for_line_which_includes_newline( &mut self, line: &str ) -> Result<(), Error>

Parse the line of code and update the internal HTML buffer with tagged HTML

Note: This function requires line to include a newline at the end and also use of the load_defaults_newlines version of the syntaxes.

source

pub fn parse_html_for_line(&mut self, line: &str)

👎Deprecated since 4.5.0: Please use parse_html_for_line_which_includes_newline instead

Parse the line of code and update the internal HTML buffer with tagged HTML

§Warning

Due to an unfortunate oversight this function adds a newline after the HTML line, and thus requires lines to be passed without newlines in them, and thus requires usage of the load_defaults_nonewlines version of the default syntaxes.

These versions of the syntaxes can have occasionally incorrect highlighting but this function can’t be changed without breaking compatibility so is deprecated.

source

pub fn finalize(self) -> String

Close all open <span> tags and return the finished HTML string

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.