This modules exposes the internal structures of the package.
Its intended to be used in combination with some preprocessing.
The expressions always return a string
Value
- just return the given stringVariable
- look up the key and insert a generated string according to the definition of the key.The definition specifies how the strings gets generated
Choose
- Choose a random sentence out of a list.Let
- Generate the sentence one. Then use the sentence over and over again.With
- Generate the sentence according to the sub-grammar.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"