Search results

There are no results.

syntax.format.Html

class pub Html

A type that forms a lexer's token stream as HTML.

The generated HTML is compatible with Pygments stylesheets.

Fields

base_class

let pub @base_class: Option[String]

A base/generic class to apply to the surrounding <div> and <pre> elements.

This field defaults to "highlight".

pre_class

let pub @pre_class: Option[String]

An extra class to apply to the <pre> element.

This class can be used to attach a language specific class, such as "bash" when formatting Bash code.

By default, no extra class is added.

Static methods

new

Show source code
Hide source code
fn pub static new -> Html {
  Html(base_class: Option.Some('highlight'), pre_class: Option.None)
}
fn pub static new -> Html

Returns a new HTML formatter.

Instance methods

format

Show source code
Hide source code
fn pub format[I: mut + Lexer](lexer: move I) -> Document {
  let doc = Document.fragment
  let div = doc.div
  let pre = div.pre
  let code = pre.code

  match (@base_class, @pre_class) {
    case (Some(a), Some(b)) -> {
      div.attr('class', a)
      pre.attr('class', '${a} ${b}')
      nil
    }
    case (Some(a), _) -> {
      div.attr('class', a)
      pre.attr('class', a)
    }
    case (_, Some(b)) -> pre.attr('class', b)
    case _ -> {}
  }

  lexer.each(fn (tok) {
    # These classes are taken from
    # https://github.com/pygments/pygments/blob/8f3bec7982ba52915ee95f34f18e188243364600/pygments/token.py.
    let cls = match tok.kind {
      case Comment -> 'c'
      case String -> 's'
      case Int -> 'mi'
      case Float -> 'mf'
      case Keyword -> 'k'
      case Field -> 'vi'
      case Symbol or Macro -> 'ss'
      case Text -> ''
    }

    let el = if cls.size > 0 {
      code.span.attr('class', cls)
    } else {
      mut code
    }

    el.text(tok.value)
  })

  doc
}
fn pub format[I: mut + Lexer](lexer: move I: mut) -> Document

Formats the token stream as an HTML document fragment.

The returned document fragment can be converted into a String by calling to_string on it. See the documentation of inko-builder for more details.