galaxykate / tracery

Tracery: a story-grammar generation library for javascript
Apache License 2.0
2.11k stars 246 forks source link

Substitutions behave strangely inside bindings #21

Open rocketnia opened 8 years ago

rocketnia commented 8 years ago

My CBDQ bot started acting up a few months ago. Suddenly #[pMysteriousPower:#pMysteriousPower#]treasureOfPower# started generating the text "pMysteriousPower" instead of following the treasureOfPower rules.

To see what was going on, I tried #[a:FOO#[b:c]d#BAR]a# as a more elaborate example. It generated the text "FOOb".

Here's some test code:

console.log(tracery.createGrammar({
    origin: [
        "#[a:FOO#[b:c]d#BAR]a#"
    ]
}).flatten("#origin#"));

console.log(tracery.createGrammar({
    "origin": [
        "#story#"
    ],

    "story": [
        "#[pMysteriousPower:#mysteriousPower#]storyOfParams1#"
    ],
    "storyOfParams1": [
        "#[pMysteriousTechnoTreasure:#[pMysteriousPower:#pMysteriousPower#]treasureOfPower#]storyOfParams2#"
    ],
    "storyOfParams2": [
        "When you need #pMysteriousPower#, look no further than the #pMysteriousTechnoTreasure#!"
    ],

    "mysteriousTechnoTreasure": [
        "#[pMysteriousPower:#mysteriousPower#]treasureOfPower#"
    ],
    "treasureOfPower": [
        "#treasureType# of #pMysteriousPower#"
    ],
    "treasureType": [
        "amulet",
        "talisman"
    ],
    "mysteriousPower": [
        "teleportation",
        "frost"
    ]
}).flatten("#origin#"));

On the main branch, these result in the text "{{a}}" and (for example) "When you need frost, look no further than the amulet of frost!" respectively. Those look like good results to me.

On the tracery2 branch, I get the text "FOOb" and (for example) "When you need frost, look no further than pMysteriousPower!"

Here's an .html file demonstrating the above example code on the tracery2 branch. It doesn't display anything, but it logs to the console.