Orasund / elm-tracery / Tracery.Syntax

This modules exposes the internal structures of the package.

Its intended to be used in combination with some preprocessing.


type Expression
    = Value String
    | Variable String

The expressions always return a string


type Definition
    = Choose (List (List Expression))
    | Let (List Expression)
    | With (Dict String Definition)

The definition specifies how the strings gets generated

decoder : Json.Decode.Decoder (Dict String Definition)

Decoder for the Syntax.

fromString : String -> Result Json.Decode.Error (Dict String Definition)

import Dict exposing (Dict)
import Tracery.Command exposing (Command(..))

input : String
input =
   """{ "origin" : [ "Hello \\\\\\\\ World \\\\#", "#statement# and #statement#" ]
   , "statement" :
     { "origin" : "my #myPet# is the #complement#"
     , "myPet": "#pet#"
     , "pet" : ["cat","dog"]
     , "complement" : ["smartest #myPet# in the world","fastest #myPet# that i know of"]
     }
   }"""

output : Dict String Definition
output =
    Dict.fromList
        [ ( "origin"
          , Choose
              [ [ (Value "Hello "),  (Value "\\"),  (Value " World "), (Value "#")]
              , [ (Variable "statement"),  (Value " and "),  (Variable "statement")]
              ]
          )
        , ( "statement"
          , Dict.fromList
              [ ( "origin"
                , [  (Value "my ")
                  ,  (Variable "myPet")
                  ,  (Value " is the ")
                  ,  (Variable "complement")
                  ]
                    |> Let
                )
              , ( "myPet",Let [ (Variable "pet")])
              , ( "pet", Choose [[ (Value "cat")],[ (Value "dog")]])
              , ( "complement"
                , Choose
                    [ [  (Value "smartest "),  (Variable "myPet"),  (Value " in the world")]
                    , [  (Value "fastest "),  (Variable "myPet"),  (Value " that i know of")]
                    ]
                )
              ]
                |> With
            )
        ]

input |> fromString
--> Ok output

originString : String

The origin is the starting point for the generated story.

originString : String
originString =
    "origin"