arctic/parse

Types

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 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,
  name: String,
) -> ParserBuilder

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,
  left: String,
  right: String,
  action: fn(Element(Nil), List(String), Position) ->
    Result(Element(Nil), Snag),
) -> ParserBuilder

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,
  prefix: String,
  action: fn(Element(Nil), Position) ->
    Result(Element(Nil), Snag),
) -> ParserBuilder

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,
  name: String,
  action: fn(List(String), String, Position) ->
    Result(Element(Nil), Snag),
) -> ParserBuilder

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 new() -> ParserBuilder

Create a new parser builder, with no rules or components.

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

Apply a given parser to a given string.

pub fn wrap_inline(
  w: fn(List(Attribute(a)), List(Element(Nil))) -> Element(Nil),
) -> fn(Element(Nil), b, c) -> Result(Element(Nil), 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, c) -> Result(Element(Nil), 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), b) -> Result(Element(Nil), 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), b) -> Result(Element(Nil), 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