Twine: <<autolink>>, a macro to automatically make links.

This macro, when surrounding a span of text, will search through the text, finds words that are also names of passages in the story, and converts them to internal links.

* Since Twine passage names are case-sensitive, this search is also case-sensitive.
* It currently only matches single ASCII words (letters and numbers) and hyphens contained within, but currently not apostrophe-containing words.

(function(){version.extensions.autolinkMacro={major:1,minor:0,revision:0};
macros.autolink={handler:function(g,e,f,b){function tagcontents(starttag,endtag,k){var a=b.source.slice(k),l=0,c="";
for(var i=0;i<a.length;i++){var w=endtag.length;if(a.substr(i,w)==endtag){if(l==0){b.nextMatch=k+i+w;
return c}else{l--;c+=a.charAt(i)}}else{if(a.substr(i,starttag.length)==starttag){l++
}c+=a.charAt(i)}}return""}var k=b.source.indexOf(">>",b.matchStart)+2,d=tagcontents("<<"+e,"<<end"+e+">>",k),bs=String.fromCharCode(92),re=new RegExp("(["+bs+"w-]+)","g"),words,len;
do{words=re.exec(d);if(words&&tale.has(words[0])){len=words[0].length;
d=d.slice(0,words.index)+"[["+words[0]+"]]"+d.slice(words.index+len);
re.lastIndex+=(len)}}while(words);new Wikifier(g,d)}};macros.endautolink={handler:function(){}}
}());

Usage example:
* <<autolink>>The most popular pets are dogs and cats<<endautolink>> - Assuming the passage "dogs" exists, that becomes "dogs".

Not too sure of the name - might change it later.

Feel free to report any bugs to @webbedspace.

Comments

I love the way you're

I love the way you're increasing twine's functionality and extending not only its potential for "gameyness" but for purely aesthetic expression as well. I'd love to see you continue with this :)

Please help me with formatting links

I've wanted link that has one word stroken through, so I've wrote:

[ [ You see can of ==beer== soda on the table ] ]

However that didn't work. So I've tried inline html after inspecting how link looks like in code:

<html><a id="SodaBeer" href="javascript:void(0)" class="internalLink">You see can of <s>beer</s> soda on the table</a></html>

Unfortunately now link is unclickable even though appropriate passage is still there, albeit unconnected.

Of course I could do:

[ [ You see can of|SodaBeer ] ] ==[ [ beer|SodaBeer ] ]== [ [ soda on the table|SodaBeer ] ] (spaces added so it won't try to interpret this as wiki links but it isn't elegant and might bring confusion in readers, namely choosing which link to click as those are highlighted separately.

Any suggestions?

The correct way to make a

The correct way to make a HTML link is outlined here.