Crate marksonnet

Crate marksonnet 

Source
Expand description

This crate provides a Parser which wraps a pulldown_cmark::Parser, while evaluating embedded Jsonnet code blocks. These code blocks are identified using the marksonnet language tag, and they are substituted in the resulting Iterator for the result of their evaluation.

marksonnet blocks can be used to run any Jsonnet program and parse Markdown as though the result of that program were included in the text.

  use indoc;           // multiline string literals
  use pulldown_cmark;  // illustrate equivalent sources

  let input: &str = indoc::indoc! {r#"
    The first five fibonacci numbers are:

    ```marksonnet
    local fib(n) = if (n <= 2) then 1 else (fib(n-1) + fib(n-2));
    [fib(n) for n in std.range(1, 5)]
    ```
  "#};

  let expected: &str = indoc::indoc! {r#"
    The first five fibonacci numbers are:

    ```json
    [
        1,
        1,
        2,
        3,
        5
    ]
    ```
  "#};

  assert_eq!(
    marksonnet::Parser::new(input).collect::<Vec<_>>(),
    pulldown_cmark::Parser::new(expected).collect::<Vec<_>>()
  )

If the Jsonnet evaluates to a string, then it is substituted inline, rather than being JSON encoded and placed inside of a codeblock.

  use indoc;           // multiline string literals
  use pulldown_cmark;  // illustrate equivalent sources

  let input: &str = indoc::indoc! {r#"
    My favorite greeting is:
    ```marksonnet
    std.format('> %s, %s!', ['Hello', 'World'])
    ```
  "#};

  let expected: &str = indoc::indoc! {r#"
    My favorite greeting is:

     > Hello, World!"#};


  assert_eq!(
    marksonnet::Parser::new(input).collect::<Vec<_>>(),
    pulldown_cmark::Parser::new(expected).collect::<Vec<_>>()
  )

Marksonnet also supports imports relative to the file directory.

  use indoc;           // multiline string literals
  use pulldown_cmark;  // illustrate equivalent sources

  let input: &str = indoc::indoc! {r#"
    This is the content of `example/sample.json`:

    ```marksonnet
    import 'example/sample.json'
    ```
  "#};

  let expected: &str = indoc::indoc! {r#"
    This is the content of `example/sample.json`:

    ```json
    {
        "bar": "baz",
        "foo": "bar"
    }
    ```
    "#};

  assert_eq!(
    marksonnet::Parser::new(input).collect::<Vec<_>>(),
    pulldown_cmark::Parser::new(expected).collect::<Vec<_>>()
  )

Note that in this case, example/sample.json was imported by Jsonnet and parsed, then re-serialized.

Structsยง

Parser