Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Alter passage text before it is rendered?

@TheMadExile Yes, I know you already could not fathom it, but hear me out.

I'm a lazy bastard who has a list of things I'd like to replace with a styled version.
@aStyle;ADept@@bStyle;BDept@@cStyle;CDept@@>;>
}>>

I may add/delete/change the department names and styles of each of them, and I don't want to hunt through all my Passages for each of them. Really, it's just like Harlowe's replace, instead on a particular String instead of a hook.

So, what's this crazy method you spoke of? Does it use postrender/predisplay?

Also, is there a better way to define the replaceList array?

Comments

  • Apparently, you missed the part where I said it was crazy, as in insane. I was not being hyperbolic. I've said as much as I'm going to about that particular topic.

    If this is simply about replacing text within your raw passages once in a blue moon, then I suggest using Twine 2's Find and Replace feature.

    If this is about having dynamic text at run time, then my answer now remains the same as it was then. Dynamic text is the basic reason why any/all of the following exist: $variables, macros, widgets, etc. If you want dynamic text, look into the usual suspects. For example, using properties on the setup object:
    @aStyle;ADept@@bStyle;BDept@@cStyle;CDept@@>;>
    
    
    :: Some Other Passage
    Hello!  Welcome to <<=setup.ADept>>!
    
    Or via widgets:
    @aStyle;ADept@@bStyle;BDept@@cStyle;CDept@@<;</widget>>
    
    
    :: Some Other Passage
    Hello!  Welcome to <<ADept>>!
    
    Etc, etc, ad nauseam.
  • I didn't. I specifically asked what the crazy method was :tongue:

    But, alright, I get the point. For further reference, though, can you elaborate on why it's crazy/insane?

    Would it introduce hard to debug issues? Or is it something that's just! Isn't! Done!
  • I don't think (I hope I don't) understand exactly what the other thread was about, but I guess instead of using variables to alter passage text and such, the other thread was asking for a system that did...something else.

    Like instead of $passage where $passage could be set up with a variable, he wanted some type of system to crawl through the text before is was displayed and use regular expressions to find and replace specific text. Which is about 100000000000000 times the work and processing requirements of just using a variable, for no discernible benefit.

    It's reinventing the wheel, not the SugarCube wheel, more like the wheel of computer programming in general. So it's not done because why would it be? It'd be like building a new house in order to make your bed.
  • I think, yeah, the original thread was thinking of using regex. Meanwhile, I've found a better solution, which includes downloading Twee2.

    I guess the only question remaining is how does Harlowe do it, then? Harlowe's (replace:) macro targets a hook, but it can also target an arbitrary string. Or maybe I'm just looking at it wrong.
  • I guess the only question remaining is how does Harlowe do it, then? Harlowe's (replace:) macro targets a hook, but it can also target an arbitrary string. Or maybe I'm just looking at it wrong.
    To hazard a guess. I would assume that when the (replace:) macro is passed a regular string, rather than a hook reference, Harlowe searches through all of the appropriate text nodes within the post-rendered DOM originally generated by Harlowe. For all matching text nodes, it probably replaces the text node with a suitable replacement—meaning another text node for simple text replacements or a mix of text and element nodes for replacements containing markup.

    For example, a simple text only replacement might work out like the following:
    → Pre-replacement text node
    "I'd like a slice of cake."
    
    → Execute changer
    (replace: "cake")[pie]
    
    → Post-replacement node
    "I'd like a slice of pie."
    

    A text and markup replacement:
    → Pre-replacement text node
    "I'd like a slice of cake."
    
    → Execute changer
    (replace: "cake")[**pie**]
    
    → Post-replacement text and element nodes
    "I'd like a slice of ", <strong>, "."; where the <strong> node contains the text node: "pie"
    
  • how does Harlowe do it
    The best person to answer that would be it's developer.

    Otherwise I believe to understand how Harlowe renders a Passage you need to first look at the code for the Section.renderInto() method found in the section.js file which describes what is being done (and when it is done) during the rendering process, I think the call to Utils.findAndFilter() may be of some interest. Then you will need to understand what a ChangerCommand is and how they work.
Sign In or Register to comment.