I’ve (very slowly) been working on a JavaScript API for Interactive Fiction. Last year I released the parser component. I still have a long way to go before I’m done, but I’m happy to report that today I released the text generation component. It’s based on JavaScript’s template literals and has a rich feature set.
If you’ve got a JavaScript project that needs some random text generation, you may want to check it out. Here’s a tutorial if you want to know more. It includes many worked examples that you can play with in JSFiddle.
I welcome all feedback.
Some examples:
var _ = ishml.Template
var example1=_`Carmen has a pet ${_.pick("dog","cat","emu","octopus").tag("pet")}.
Saul has ${_.a.tags.pet}. I saw his ${tags=>tags.pet.text} yesterday. `
var example2=_`Carmen has ${_.a.pick().tag("pet")}, but ${tags=>tags.pet.data.problem}.
Is ${tags=>tags.pet.data.value} right for you?
A pet ${tags=>tags.pet.inner.data.value} is not right for me.`
example2.populate({pet:[{animal:"dog",problem:"they shed a lot"},
{animal:"cat",problem:"I'm allergic"},
{animal:"emu",problem:"they can be temperamental"},
{animal:"octopus",problem:"they get bored easily"}]})
var example3=_`The humane society has ${_.list(_("dog","cat","emu","octopus").tag("pet").s).tag("list")}.
I've always wanted ${_.a.pick.tags.pet.tag("my_pet")}.
I wish I could take home all the ${tags=>tags.list.text},
but I should just get ${tags=>tags.my_pet.text}.`
var demo=function()
{
example1.say().replace("#paragraph1")
example2.say().replace("#paragraph2")
example3.say().replace("#paragraph3")
}