On this page:
syntax
doc
from_  syntax
to_  syntax
read_  syntax
write_  syntax
syntax_  to_  string
string_  to_  syntax
syntax_  to_  bytes
bytes_  to_  syntax
0.45+9.1

3 HTML Syntax Objects🔗ℹ

An HTML syntax object encodes an HTML document as a human-readable shrubbery form represented by a Rhombus syntax object. The html.from_syntax function converts an HTML syntax object to a html.Document, and html.to_syntax goes in the other direction. HTML syntax objects can be produced from HTML directly with html.read_syntax, html.string_to_syntax, and html.bytes_to_syntax, and they can converted to XML directly with html.write_syntax, html.syntax_to_string, and html.syntax_to_bytes. HTML syntax objects are similar to XML syntax objects as supported by the xml library.

The html.syntax form is essentially an alias of '' (see #%quotes), but it checks that the result syntax object conforms to HTML syntax object constraints.

def my_doc:

  html.syntax:

    head:

      title:

        "Greeting"

    body:

      h1:

        "Hello, World!"

      div:

        ~class: "main"

        "Today is sometime after "

        $(to_string(date.Date.from_seconds(0, ~local: #false)))

> html.write_syntax(my_doc)

<head><title>Greeting</title></head

><body><h1>Hello, World!</h1

><div class="main">Today is sometime after 1970-01-01</div

></body

>

expression

html.syntax:

  content

  ...

 

element

 = 

id:

  attribute

  ...

  content

  ...

 

attribute

 = 

keyword: value_string

 

content

 = 

element

 | 

string

 | 

content ...

 | 

(content, ...)

 | 

[content, ...]

 | 

other

Returns the same syntax object as 'content; ...', but with the constraint that the result conforms to the grammar of an HTML syntax object. Although not shown above in the grammar for element, an escape using $ can appear anywhere in the block after html.syntax, and it works the same as in 'content; ...' (see #%quotes). Conformance is checked on the result after substituting escapes.

An element corresponds to an html.Element. It always starts with an identifier, and it is followed by a block (possibly empty) for the element’s attributes and content. The attributes must appear first, where each attribute starts with a keyword and is followed by a block for the attribute’s value, together corresponding to html.Attribute.

Each content can be one the following:

  • element: Corresponds to a nested html.Element.

  • string: Used directly as string content.

  • content ..., (content, ...), or [content, ...]: Content can be spliced from multiple content forms with a group, within parentheses, or within square brackets. Note that this splicing accommodates @ forms, as in the example below.

  • other: Any value that is satisfies html.Content is allowed. For example, a html.Comment value can appear here. Constructing such objects will require a $ escape with Syntax.inject.

> html.write_syntax(

    html.syntax:

      a: "hello <name>"

  )

<a>hello &lt;name&gt;</a>

> html.write_syntax(

    html.syntax:

      a: ("b", "c", [["d"], "e"])

  )

<a>bcde</a>

> html.write_syntax(

    html.syntax:

      a: @{say this: "hello <name>"}

  )

<a>say this: "hello &lt;name&gt;"</a>

> html.write_syntax(

    html.syntax:

      a: $(Syntax.inject(html.Comment(~data: "123")))

  )

<a><!-- 123 --></a>

expression

html.doc:

  content

  ...

Like html.syntax, but converts the HTML syntax object to a html.Document using html.from_syntax.

function

fun html.from_syntax(

  html_stx :: Syntax,

) :: html.Document

 

function

fun html.to_syntax(

  doc :: html.Document || html.Content,

) :: Syntax

Converts to and from the HTML syntax object representation.

function

fun html.read_syntax(

  ~in: inp :: Port.Input = Port.Input.current()

) :: Syntax

 

function

fun html.write_syntax(

  html_stx :: Syntax,

  ~out: outp :: Port.Output = Port.Output.current(),

  ~newlines: add_newlines :: NewlineMode = #'safe

) :: Void

Reads or prints HTML directly from the HTML syntax object representation.

function

fun html.syntax_to_string(

  html_stx :: Syntax,

  ~newlines: add_newlines :: NewlineMode = #'safe

) :: String

 

function

fun html.string_to_syntax(

  html_str :: String,

) :: Syntax

 

function

fun html.syntax_to_bytes(

  html_stx :: Syntax,

  ~newlines: add_newlines :: NewlineMode = #'safe

) :: Bytes

 

function

fun html.bytes_to_syntax(

  html_bstr :: Bytes,

) :: Syntax

Converts directly between HTML in a string or byte string and a HTML syntax object representation.