Sugarecube 2: charAt() won't print an exclamation point. Why?

I simplified this to the basics to make sure it wasn’t something I was doing wrong. charAt() refuses to print an exclamation mark, which makes no sense to me.

Here’s the simplified widget:

<<widget "Test">>
	<<for _i to 0; _i lt $args[0].length; _i++>>
		<span><<print $args[0].charAt(_i)>></span>
	<</for>>
<</widget>>

I need the print to be inside of a span in order to apply text effects per letter via the span (which is the whole point of the widget), in case it’s confusing why that span is there. When simplifying I removed everything but the basic span, for loop, and print.

Everything is working properly in the original widget, except for charAt() refusing to print an exclamation mark.

Running the test widget, both of these fail to display an exclamation point:

<<Test "\"Test!\"">> <<Test "\"Test\!\"">>

Even escaping the ! with a \ doesn’t work. Why? I thought \ escaped anything right behind it. Plus \ is working with the quotes, so clearly escaping works with charAt().

I did eventually get around this by putting in an if statement to directly print ! when there’s an exclamation point.

But I would really like to understand why ! wasn’t working with charAt(), and couldn’t be made to work even by escaping it. That makes absolutely no sense to me.

Your problem is that certain symbols are used in the markup language, so they’re getting converted to things like lists and headings. Escaping doesn’t help, because that happens later: the charAt() method correctly returns the string "!", but when that string gets printed the markup converts it to a heading.

To avoid all that, you can use the verbatim text markup, like this:

<<widget "Test">>
	<<for _i to 0; _i lt $args[0].length; _i++>>
		<span><<print '<nowiki>'+$args[0].charAt(_i)+'</nowiki>'>></span>
	<</for>>
<</widget>>