Orasund/elm-tracery/1.0.0

NOTE: the latest version is 5.0.0

Syntax

This modules exposes the internal structures of the package.

Its intended to be used in combination with some preprocessing.

type alias Syntax =
Dict String Definition

The Syntax is just a dictionary of keys and their definition.

= Print String
| Insert String

The expressions always return a string

  • Print - just return the given string
  • Insert - look up the key and insert a generated string according to the definition of the key.
= Choose (List (List Expression))
| Let (List Expression)
| With Syntax

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 : Decoder Syntax

Decoder for the Syntax.

fromString : String -> Result Error Syntax
import Dict


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 : Syntax
output =
    Dict.fromList
        [ ( "origin"
          , Choose
              [ [Print "Hello ", Print "\\", Print " World ",Print "#"]
              , [Insert "statement", Print " and ", Insert "statement"]
              ]
          )
        , ( "statement"
          , Dict.fromList
              [ ( "origin"
                , Let [ Print "my ", Insert "myPet", Print " is the ", Insert "complement"]
                )
              , ( "myPet",Let [Insert "pet"])
              , ( "pet", Choose [[Print "cat"],[Print "dog"]])
              , ( "complement"
                , Choose
                    [ [ Print "smartest ", Insert "myPet", Print " in the world"]
                    , [ Print "fastest ", Insert "myPet", Print " 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"