Harlowe2: How to show a bare string?

0 votes
asked Aug 6 by boyland (260 points)
Suppose I have a variable with value "[]".  How can I get this to print as []?  Neither print: or text: does the job; each treats the [] characters specially.  The string is from user input and I'd like to be able to faithfully show it.  It may include all sorts of special characters such as * or ` or { and I'd like it to print as is.

I could write a JavaScript function to add appropriate backticks around it, but Harlowe doesn't give me a way to call JavaScript functions (AFAIK).

3 Answers

0 votes
answered Aug 9 by boyland (260 points)
edited Aug 9 by boyland
Best answer

Actually I found that Harlowe does let me call out to JavaScript, except that you can't pass a variable.  Instead you need to use text: to force it into a string.  Then the JavaScript code can do the paranoid checking for backticks and additionally add spaces at the front in back just in case the string starts or ends with a backtick.

(set: $quoted to harloweVerbatim((text: $userInput)))
function harloweVerbatim(str) {
	if (str === undefined || str == "") return "";
	var n = str.length;
	var count = 0;
	var max = 0;
	var delim = "`";
	for (var i = 0; i < n; ++i) {
		var ch = str.charAt(i);
		if (ch == "`") {
			if (++count > max) {
				delim += "`";
		} else {
			count = 0;
	var pre = "";
	var post = "";
	if (str.charAt(0) == "`") pre = " ";
	if (str.charAt(n-1) == "`") post = " ";
	return delim + pre + str + post + delim;
window.harloweVerbatim = harloweVerbatim; 


+1 vote
answered Aug 6 by greyelf (97,410 points)

You can use the grave / back-tick character functionality of Verbatim markup to output the user input as is.

(set: $userInput to "[]")
(set: $escaped to '`' + $userInput + '`')

User Input: $userInput
Escaped: $escaped


commented Aug 7 by boyland (260 points)
I'm worried this will fail if the user input has a grave character in it.
0 votes
answered Aug 9 by Deadshot (3,560 points)

Do this:

(set: $thing to (prompt: 'Input something, user!',''))

(if: $thing is '[]')[`[]`](else:)[$thing]

If you find any other characters it doesn't want to print just make more if statements with backticks.

commented Aug 9 by boyland (260 points)
That's not practical: the user could type: [`]{``}
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.