Revision of Twine: mix HTML tags and Twine markup syntax from Thu, 06/20/2013 - 02:54

Just now I was thinking to myself, "Why is it that inline HTML must be stuck between the <html> tags in Twine, anyway? Would it be possible to make HTML usable intersperced with Twine syntax?" So I decided to give it a try. The resulting Javascript code is as follows:

(function () {
	var bs = String.fromCharCode(92);
	Wikifier.formatters.unshift({
		name: "htmltag",
		match: "<"+bs+"w+(?:(?:"+bs+"s+"+bs+"w+(?:"+bs+"s*="+bs+"s*(?:"+'"'+".*?"+'"'+"|'.*?'|[^'"+'"'+">"+bs+"s]+))?)+"+bs+"s*|"+bs+"s*)"+bs+"/?>",
		tagname: "<("+bs+"w+)",
		voids: ["br", "hr", "area", "img", "input", "embed", "param", "source", "track"],
		handler: function (a) {
			var re, tn, e;
			re = new RegExp(this.tagname).exec(a.matchText);
			tn = re && re[1];
			if(tn) {
				e = document.createElement(a.output.tagName);
				e.innerHTML = a.matchText;
				e = e.firstChild;
				if(this.voids.indexOf(tn.toLowerCase()) == -1) {
					a.subWikify(e, "<" + bs + "/" + bs + "s*" + tn + bs + "s*>");
				}
				a.output.appendChild(e);
			}
		}
	});
}());

This code will allow you to put HTML tags in Twine passage text, and have them function in the compiled game. Note: it requires all HTML tags to be properly closed, with the exception of the so-called "void tags" that never have a closing pair (br, hr, area, img, input, embed, param, source, track). Unmatched end tags will be ignored!

Feel free to report any bugs to @webbedspace.

pensive-mosquitoes