smalto/lustre
Lustre element renderer for smalto syntax-highlighted tokens.
Converts a list of Token values into Lustre Element nodes using a
configurable Config that maps each token type to a rendering function.
Use default_config for inline-styled spans matching smalto’s ANSI color
scheme, or build a custom config with the builder functions.
Usage
import smalto
import smalto/languages/python
import smalto/lustre as smalto_lustre
// Render with the default inline-styled config
let tokens = smalto.to_tokens("print('hello')", python.grammar())
let elements = smalto_lustre.to_lustre(tokens, smalto_lustre.default_config())
// Customize specific token renderers
let config =
smalto_lustre.default_config()
|> smalto_lustre.keyword(fn(value) {
html.span([attribute.class("my-keyword")], [element.text(value)])
})
let elements = smalto_lustre.to_lustre(tokens, config)
Types
Configuration for rendering tokens as Lustre elements.
Each field is a function that receives the token’s text value and returns
a Lustre element. The custom field receives both the token name and value.
Whitespace and Other tokens are always rendered as plain text nodes.
pub type Config(msg) {
Config(
keyword: fn(String) -> element.Element(msg),
string: fn(String) -> element.Element(msg),
number: fn(String) -> element.Element(msg),
comment: fn(String) -> element.Element(msg),
function: fn(String) -> element.Element(msg),
operator: fn(String) -> element.Element(msg),
punctuation: fn(String) -> element.Element(msg),
type_: fn(String) -> element.Element(msg),
module: fn(String) -> element.Element(msg),
variable: fn(String) -> element.Element(msg),
constant: fn(String) -> element.Element(msg),
builtin: fn(String) -> element.Element(msg),
tag: fn(String) -> element.Element(msg),
attribute: fn(String) -> element.Element(msg),
selector: fn(String) -> element.Element(msg),
property: fn(String) -> element.Element(msg),
regex: fn(String) -> element.Element(msg),
custom: fn(String, String) -> element.Element(msg),
)
}
Constructors
-
Config( keyword: fn(String) -> element.Element(msg), string: fn(String) -> element.Element(msg), number: fn(String) -> element.Element(msg), comment: fn(String) -> element.Element(msg), function: fn(String) -> element.Element(msg), operator: fn(String) -> element.Element(msg), punctuation: fn(String) -> element.Element(msg), type_: fn(String) -> element.Element(msg), module: fn(String) -> element.Element(msg), variable: fn(String) -> element.Element(msg), constant: fn(String) -> element.Element(msg), builtin: fn(String) -> element.Element(msg), tag: fn(String) -> element.Element(msg), attribute: fn(String) -> element.Element(msg), selector: fn(String) -> element.Element(msg), property: fn(String) -> element.Element(msg), regex: fn(String) -> element.Element(msg), custom: fn(String, String) -> element.Element(msg), )
Values
pub fn attribute(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for attribute tokens.
pub fn builtin(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for builtin tokens.
pub fn comment(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for comment tokens.
pub fn constant(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for constant tokens.
pub fn custom(
config: Config(msg),
render: fn(String, String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for custom tokens. The function receives both the token name and text value.
pub fn default_config() -> Config(msg)
Create the default config with inline-styled <span> elements
matching smalto’s built-in ANSI color scheme.
Each token is rendered as <span style="color: ...">value</span>.
Comments additionally receive font-style: italic.
Punctuation and custom tokens are rendered as plain text.
pub fn function(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for function tokens.
pub fn keyword(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for keyword tokens.
pub fn module(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for module tokens.
pub fn number(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for number tokens.
pub fn operator(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for operator tokens.
pub fn property(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for property tokens.
pub fn punctuation(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for punctuation tokens.
pub fn regex(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for regex tokens.
pub fn selector(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for selector tokens.
pub fn string(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for string tokens.
pub fn tag(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for tag tokens.
pub fn to_lustre(
tokens: List(token.Token),
config: Config(msg),
) -> List(element.Element(msg))
Render a list of tokens as Lustre elements using the given config.
Whitespace and Other tokens are always rendered as plain text nodes
regardless of the config. All other tokens are dispatched to the
corresponding config function.
pub fn type_(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for type tokens.
pub fn variable(
config: Config(msg),
render: fn(String) -> element.Element(msg),
) -> Config(msg)
Set the rendering function for variable tokens.