0 votes
by (160 points)

Harlowe: 2.2.1

For every time the text reads "red", can I have a global enchant that looks for that exact string without needing a variable? Perhaps I need to set up a loop in the header? I don't want to have to go $red[red] for all instances of red, but I need to have an exact match because "red" is enchanting "plasteRED" and "textuRED". What am I doing wrong?

(if: "" is "red")[(enchant: "red", (text-colour: "red"))]
<!--nothing happens, is there a macro for any string?-->
(if: "red" is "red")[(enchant: "red", (text-colour: "red"))]
<!--tried defining exact match string-->
(if: "red")[(enchant: "red", (text-colour: "red"))]]
<!--obviously not since if: needs a 2nd parameter-->
(for: each _red where it is "red")[(enchant: "red", (text-colour: red))]
<!--call _red each time I write it in a string? nope-->

Thank you.

1 Answer

+1 vote
by (154k points)

Assuming your passage text looks something like the following..

Blah blah red blah blah plastered blah blah textured blah blah redistribution blah.

... and that you only want to style the 'red' that is delimited (preceded & followed) by a single space character, then all you need to do is add those space characters to the String value you are searching for like so...

(enchant: " red ", (text-colour: "red"))

You can use a header or footer tagged special passage to apply the above (enchant:) to every (or selected) passage of your story.

warnings:
a. The String search feature of the (enchant:) macro is case-sensitive so passing "red" to it will not find "Red" or "RED".
b. Changes that run in the background (like those created by the (enchant:) macro) constantly monitor the HTML structure of the current page so that they can detect any relevant changes made to that structure. Depending on the number or complexity of the Changes currently active this can effect both the user's ability to interact with the page as well as the visual displaying of it.

by (160 points)
Fabulous. Thank you! I figured it was something easy and appreciate the notes and warnings. Cheers!
by (160 points)
In relation to the warning re: changers, does this simply mean a slowing down page load times or is there a more nefarious outcome for adding too many of them? Is using css with less intensive?
by (154k points)

This (background) Changer scans the HTML Plain Text Nodes of the entire page looking for the relevant String once all of the HTML elements for the current Passage have been generated, it also preforms that scan each time the HTML structure is interactively changed by macros like (append:), (replace:), etc...

This allows you to do things like the following.

Blah blah red Red blah blah plastered blah blah textured blah blah redish.

|more>[]

(enchant: " red ", (text-colour: "red"))

(link: "continue")[(append: ?more)["More blah blah red blah."]]

... the "red" in the text appended by the link is style after it is inserted into the current page, and the styling of the "red" text in the first sentence is re-applied at the same time. (this re-applying can be seen if you use your web-browser's built-in Web Developer tools to Inspect the HTML element structure during the selection of the link)

The main timing difference between using a CSS rule or a Changer  to style content is, the CSS styling is applied by the web-browser engine during the HTML render process, while the Changer styling is applied by the Harlowe engine just after the HTML has been rendered.

The main reasons why CSS rule based styling may be faster are:
a. searching for identifiable HTML elements within the structure is generally faster than searching the content of all of the page's HTML Plain Text Nodes for a value.
b. the CSS rule is styling an existing HTML element, where as the Changer has to replace the found text with a new styled tw-enchantment element (containing the found text), thus it has to modify the HTML structure.

by (160 points)
Amazing. Thank you so much for this detailed explanation. I will do my own testing to see which performs quickest for my purposes. Have an excellent day.
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...