Revision of Twine macro: << replace >> and << endreplace >> from Mon, 01/28/2013 - 01:18

What this macro does is create an internal link that, when clicked, vanishes and is replaced with whatever is between the << replace >> and << endreplace >> tags. This could be useful if (just for starters) you want to have a passage that can be modified by clicking specific details inside it.

version.extensions['replaceMacro'] = {major:1, minor:0, revision:0}; macros['replace'] = { handler: function (g, e, f, b) { var h = insertElement(null, "div", null, "replacement"); var k = b.source.indexOf('>>', b.matchStart) + 2; var a = b.source.slice(k); var d = -1; var c = ''; var l = 0; for(var i = 0; i < a.length; i++) { if(a.substr(i, 14) == '<<endreplace>>') { if(l == 0) { d = k + i + 14; break; } else { l--; c += a.charAt(i); } } else { if(a.substr(i, 9) == '<<replace') { l++; } c += a.charAt(i); } }; if(d != -1) { var m = Wikifier.createInternalLink(g,null); m.className="internalLink replaceLink"; insertText(m,f[0]); insertText(h,c); g.appendChild(h); h.style.display = "none"; m.onclick = function() { var n = this.nextSibling; if (n) { var t = n.firstChild ? (n.firstChild.nodeValue) : ""; removeChildren(n); new Wikifier(n,t); n.style.display = "inline"; fade(n, { fade: "in" }); } this.parentNode.removeChild(this); } b.nextMatch = d; }; else { throwError(g, "can't find matching endreplace"); delete h; return; } } } macros['endreplace']={handler: function () {} };

Here is a usage example: You see <<replace "a half-eaten cake">> a plate of crumbs <<endreplace>>

Some notes:
* For those writing CSS: the < a > tag of the link has the class names "internalLink" and "replaceLink". It is immediately followed with a < span > tag with a class name of "replacement" and the style "display:none". Clicking the link removes it and changes the span's display style to "inline".
* Code inside << replace >> tags is only executed when you click the link.

AttachmentSize
TwineMacro-ReplaceTest.html186.33 KB