BenziAhamed/Tracery

Get object references after evaluating

Closed this issue · 4 comments

Is there a way to get references to objects after you've evaluated them? Tracery proper uses separate expand() and flatten() (what this framework calls expand()) methods to return a tree and a string, respectively.

What I'd like to do is something like

enum TraceSegment {
  case text(String)
  case object(name: String, result: String)
}

let t = Tracery()
t.add(object: "jack", named: "person")
let segments = t.expandSegments("hi my name is #person#, nice to meet you.")
// [.text("hi my name is "), .object(name: "person", result: "jack"), .text(", nice to meet you.")]

Is that possible?

What do you think about this? ce132ef#diff-7283b8491c4beb07e4e5348f6bf0b77fR21

You could use this to apply custom text styling via an NSAttributedString, for example.

Interesting, could you raise a PR?

So, the default return object for the original Tracery is a Trace object, which holds the segment, and you can call flatten() on it to get a string. How would you feel about altering t.expand() to return a Trace instead of a string? Obviously, it would be a breaking change, but it might be nice to get some consistency with the original framework.

(On the subject of consistency—my Javascript is pretty rusty, and I can't actually tell what the original Trace consists of. Is it a bunch of Nodes?)

I have to check on that JS implementation as well.

To be honest, my intention with this project was not to port the original JS lib, rather take inspiration from it and create something from scratch. Hence the discrepancy in function names and outputs. I also have added a bunch of things I personally had wanted (e.g. WHILE loops) - which were not there in the original Tracery lib.

(Usually I'd try to avoid a breaking change - unless there is a very critical requirement - which I don't feel to be the case here).