jxxcarlson / elm-tree-builder / Tree.Build

This module provides tools for building a tree from a string or a list of blocks. As noted in the README, a tree is represented in text as an outline:

 > data = "1\n 2\n 3\n 4\n5\n 6\n 7"

To build a tree from it, we apply the function fromString:

fromString :
    node
    -> (Block -> a)
    -> String
    -> Result Error (Tree a)


> fromString "?" .content data
  Tree "0" [
        Tree "1" [Tree "2" [],Tree "3" [], Tree "4" []]
      , Tree "5" [Tree "6" [],Tree "7" []]
  ]

The first argument of fromString is a label for a default node. The second argument tells how to build a node from a Block. In the example, we are building a tree with string labels, so we need a function of type (Block -> String). Recall that

    type alias Block = { indent : Int, content: String }

Therefore

    .content : Block -> String

has the correct type. Here we use the representation of rose trees found in elm/rose-tree.

fromString : a -> (Tree.Blocks.Block -> a) -> String -> Result Error (Tree a)

> Build.fromString "?" .content "1\n 2\n 3"
  Ok (Tree "1" [Tree "2" [],Tree "3" []])

fromBlocks : a -> (Tree.Blocks.Block -> a) -> List Tree.Blocks.Block -> Result Error (Tree a)

forestFromString : a -> (Tree.Blocks.Block -> a) -> (a -> Tree.Blocks.Block) -> String -> Result Error (List (Tree a))

Example:

> Build.forestFromString
      "?"
      .content
      (\str -> {indent = 0, content = str})
      "1\n 2\n 3\na\n b\n c"
  Ok [  Tree "1" [Tree "2" [],Tree "3" []]
       ,Tree "a" [Tree "b" [],Tree "c" []]
     ]

forestFromBlocks : a -> (Tree.Blocks.Block -> a) -> (a -> Tree.Blocks.Block) -> List Tree.Blocks.Block -> Result Error (List (Tree a))


type Error
    = EmptyBlocks
    | BlocksNotWellFormed