Search results

There are no results.

builder.html.Generator

class pub Generator

A type for generating an HTML String from a Document.

Static methods

new

Show source code
Hide source code
fn pub static new(indent: String) -> Generator {
  Generator(buffer: StringBuffer.new, indent: indent)
}
fn pub static new(indent: String) -> Generator

Returns a new Generator that formats the HTML with the given indentation

Examples

Generating HTML without any indentation:

import builder.html.(Document, Generator)

let doc = Document.new

doc.body.with fn (body) { body.p.text('hello') }

Generator
  .new(indent: '')
  .generate(doc) # => '<body><p>hello</p></body>'

Using two spaces for indentation:

import builder.html.(Document, Generator)

let doc = Document.new

doc.body.with fn (body) { body.p.text('hello') }

Generator
  .new(indent: '  ')
  .generate(doc) # => "<body>\n  <p>\n    hello\n  </p>\n</body>"

Instance methods

generate

Show source code
Hide source code
fn pub move generate(document: ref Document) -> String {
  # There's not really a reason to use any other doctype for HTML5, so we just
  # don't bother supporting anything else.
  if document.doctype {
    @buffer.push('<!DOCTYPE html>')

    if indent? { @buffer.push('\n') }
  }

  document.nodes.iter.each(fn (node) { node(node, depth: 0, escape: true) })
  @buffer.into_string
}
fn pub move generate(document: ref Document) -> String

Returns a String containing the HTML representation of the given document.