Key Concept
Tracery lets you build a template (called a grammar), in which various rules govern the substitution of symbols in the template. Because you can embed rules within rules, you can quickly build complex templates.
Important Modifiers
- Adding .a to a rule will cause the right “a” or “an” article to appear, for example #noun.a#
- Adding .capitalize to a rule will capitalize the word
- Adding .s to a rule will pluralize it, e.g. #noun.s#
- Adding .ed to a rule will make a verb past tense. Obviously, use #verb.ed# only with verbs!
Saving and Reusing Data
Let’s say you want to choose a random animal at the beginning of your story, and you want that same animal to recur throughout. If you have a rule like this:
“animal” : [“dog”, “cat”, “bear”, “bird”]
Everytime you call #animal#, you’ll call up a different animal. That works in many instances, but not when you want the same animal to reappear later. Tracery lets you define static values. It’s a bit complicated, because it requires creating several more levels of nested grammars, but definitely doable.
In your origin statement you can define static values with this sequence:
“origin” : [“#[permanentAnimal:#animal#]longerOrigin#”]
And then place what you had in your original origin in longerOrigin:
“longerOrigin” : [“The #permanentAnimal# was sick. Then the #permanentAnimal# died.”]
Now, everytime you use #permamentAnimal# it will use the same animal pulled from the #animal# rule.
Validating Your Grammar
Pasting your grammar JSON file into https://jsonlint.com is a good way to quickly find missing or extra commas, brackets, or quotations that will screw up Tracery.
Your grammar is everything between the two curly-que brackets: { }
But in order for index.html to load you grammar you need to make sure your grammar file itself has the line
var grammar =
at the top. Then the curly que brackets and everything in between should appear.
Common Errors
- Missing commas between items in list
- An extra comma after the last item in a list
- Rule names with spaces, like #my noun#