arctic/parse

Types

The data accessible while parsing, such as current position or filename.

pub opaque type ParseData(a)

An under-construction parser that you can add rules to. For example,

my_parser
  |> add_inline_rule("_", "_", wrap_inline(html.i))
pub opaque type ParserBuilder(a)

A place in an Arctic markup file

pub type Position {
  Position(line: Int, column: Int)
}

Constructors

  • Position(line: Int, column: Int)

Functions

pub fn add_dynamic_component(
  p: ParserBuilder(a),
  name: String,
) -> ParserBuilder(a)

Add a “dynamic component” to a parser. A dynamic component is a component (imagine a DSL) that needs MVC interactivity. You will need to separately register a Lustre component of the same name; this is just the way that you put it into your site from an Arctic markup file. In your Arctic markup file, you would write

@component_name(an arg, another arg)
A bunch
of content

Arctic will parse the body until the first blank line. Then the produced HTML is

<component_name data-parameters="an arg,another arg" data-body="A bunch\nof content">
</component_name>
pub fn add_inline_rule(
  p: ParserBuilder(a),
  left: String,
  right: String,
  action: fn(Element(Nil), List(String), ParseData(a)) ->
    Result(#(Element(Nil), a), Snag),
) -> ParserBuilder(a)

Add an “inline rule” to a parser. An inline rule rewrites parts of text paragraphs. For example, add_inline_rule("**", "**", wrap_inline(html.strong)) replaces anything wrapped in double-asterisks with a bolded version of the same text. Note that the rule may fail with a snag error, halting the parsing of that paragraph, and that the position in the file is given, so you can produce better snag error messages. The rewrite might also be given parameters, allowing for something like [here](https://example.com) is a link

pub fn add_prefix_rule(
  p: ParserBuilder(a),
  prefix: String,
  action: fn(Element(Nil), ParseData(a)) ->
    Result(#(Element(Nil), a), Snag),
) -> ParserBuilder(a)

Add a “prefix rule” to a parser. A prefix rule rewrites a whole paragraph based on symbols at the beginning. For example, add_prefix_rule("#", wrap_prefix(html.h1)) replaces anything that starts with a hashtag with a header of the same text. Note that the rule may fail with a snag error, halting the parsing of that paragraph, and that the position in the file is given, so you can produce better snag error messages.

pub fn add_static_component(
  p: ParserBuilder(a),
  name: String,
  action: fn(List(String), String, ParseData(a)) ->
    Result(#(Element(Nil), a), Snag),
) -> ParserBuilder(a)

Add a “static component” to a parser. A static component is a component (imagine a DSL) that doesn’t need MVC interactivity. You just specify how it gets turned into HTML, and Arctic turns it into HTML. In your Arctic markup file, you write

@component_name(an arg, another arg)
A bunch
of content

Arctic will parse the body until the first blank line, then apply your given action to the parameters and body. This allows you to embed languages in Arctic markup files, like latex or HTML. Note that the component may fail with a snag error, halting the parsing of that paragraph, and that the position in the file is given, so you can produce better snag error messages.

pub fn get_metadata(data: ParseData(a)) -> Dict(String, String)
pub fn get_pos(data: ParseData(a)) -> Position
pub fn get_state(data: ParseData(a)) -> a
pub fn new(start_state: a) -> ParserBuilder(a)

Create a new parser builder, with no rules or components. It only has an initial state.

pub fn parse(
  p: ParserBuilder(a),
  src_name: String,
  src: String,
) -> Result(Page, Snag)

Apply a given parser to a given string.

pub fn with_state(data: ParseData(a), state: a) -> ParseData(a)
pub fn wrap_inline(
  w: fn(List(Attribute(a)), List(Element(Nil))) -> Element(Nil),
) -> fn(Element(Nil), b, ParseData(c)) ->
  Result(#(Element(Nil), c), d)

A convenience function for inline rules that just put content in an element. For example, wrap_inline(html.i) italicizes.

pub fn wrap_inline_with_attributes(
  w: fn(List(Attribute(a)), List(Element(Nil))) -> Element(Nil),
  attrs: List(Attribute(a)),
) -> fn(Element(Nil), b, ParseData(c)) ->
  Result(#(Element(Nil), c), d)

A convenience function for inline rules that just put content in an element and give the element some parameters. For example, wrap_inline(html.a, [attribute.src("https://arctic-framework.org")]) makes something a link to arctic-framework.org.

pub fn wrap_prefix(
  w: fn(List(Attribute(a)), List(Element(Nil))) -> Element(Nil),
) -> fn(Element(Nil), ParseData(b)) ->
  Result(#(Element(Nil), b), c)

A convenience function for prefix rules that just put content in an element For example, wrap_prefix(html.h1) makes a paragraph a header.

pub fn wrap_prefix_with_attributes(
  w: fn(List(Attribute(a)), List(Element(Nil))) -> Element(Nil),
  attrs: List(Attribute(a)),
) -> fn(Element(Nil), ParseData(b)) ->
  Result(#(Element(Nil), b), c)

A convenience function for prefix rules that just put content in an element and give the element some parameters. For example, wrap_prefix(html.a, [attribute.src("https://arctic-framework.org")]) makes a paragraph a link to arctic-framework.org.

Search Document