diff --git a/old-examples/divert-inline/main.hank b/examples/divert-inline/main.hank similarity index 79% rename from old-examples/divert-inline/main.hank rename to examples/divert-inline/main.hank index 066d27b..07e3cc5 100644 --- a/old-examples/divert-inline/main.hank +++ b/examples/divert-inline/main.hank @@ -1,7 +1,7 @@ This is a line that diverts -> part2 - (start) -This is a text line {if (seen_once>0) "-> second_route"} with a conditional divert in it. +This is a text line {if (seen_once>0) ",-> second_route"} with a conditional divert in it. - (seen_once) -> start diff --git a/old-examples/divert-inline/test1.hlog b/examples/divert-inline/test1.hlog similarity index 100% rename from old-examples/divert-inline/test1.hlog rename to examples/divert-inline/test1.hlog diff --git a/hank/Output.hx b/hank/Output.hx index 2c95b0a..37ba28a 100644 --- a/hank/Output.hx +++ b/hank/Output.hx @@ -19,6 +19,7 @@ enum OutputType { @:allow(tests.ParserTest) class Output { var parts: Array = []; + var diverted: Bool = false; public function new(?parts: Array) { this.parts = if (parts != null) { @@ -169,15 +170,28 @@ class Output { fullOutput += altInstances[a].next().format(story, hInterface, random, altInstances, scope, displayToggles); case HExpression(h): trace(h); - fullOutput += hInterface.evaluateExpr(h, scope); + var value = hInterface.evaluateExpr(h, scope); + // HExpressions that start with ',' will be parsed and formatted as their own outputs + if (value.startsWith(',')) { + var nestedOutput = Output.parse(HankBuffer.Dummy(value.substr(1))); + value = nestedOutput.format(story, hInterface, random, altInstances, scope, displayToggles); + fullOutput += value; + if (nestedOutput.diverted) break; + } + else { + fullOutput += value; + } case InlineDivert(t): // follow the divert. If the next expression is an output, concatenate the pieces together. Otherwise, terminate formatting story.divertTo(t); + // Track whether the last call to format() resulted in a divert, so nested Outputs can interrupt the flow of their parents + diverted = true; switch (story.nextFrame()) { case HasText(text): fullOutput += text; default: } + break; // Don't format the rest of this Output's parts case ToggleOutput(o, b): if (b == displayToggles) { @@ -186,6 +200,11 @@ class Output { } } + // Remove double spaces from the output + while (fullOutput.indexOf(" ") != -1) { + fullOutput = fullOutput.replace(" ", " "); + } + return fullOutput; } } \ No newline at end of file