2012-05-26 17:30:32 +00:00
|
|
|
/*\
|
2012-11-15 12:53:10 +00:00
|
|
|
title: $:/core/modules/parsers/wikitextparser/rules/wikilink.js
|
2012-05-26 17:30:32 +00:00
|
|
|
type: application/javascript
|
2012-05-27 08:19:37 +00:00
|
|
|
module-type: wikitextrule
|
2012-05-26 17:30:32 +00:00
|
|
|
|
2012-06-05 21:54:36 +00:00
|
|
|
Wiki text run rule for wiki links. For example:
|
|
|
|
|
|
|
|
{{{
|
|
|
|
AWikiLink
|
|
|
|
AnotherLink
|
|
|
|
~SuppressedLink
|
|
|
|
}}}
|
|
|
|
|
|
|
|
Precede a camel case word with `~` to prevent it from being recognised as a link.
|
2012-05-26 17:30:32 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
exports.name = "wikilink";
|
|
|
|
|
2012-05-26 23:22:58 +00:00
|
|
|
exports.runParser = true;
|
|
|
|
|
2012-05-26 17:30:32 +00:00
|
|
|
var textPrimitives = {
|
|
|
|
upperLetter: "[A-Z\u00c0-\u00de\u0150\u0170]",
|
|
|
|
lowerLetter: "[a-z0-9_\\-\u00df-\u00ff\u0151\u0171]",
|
|
|
|
anyLetter: "[A-Za-z0-9_\\-\u00c0-\u00de\u00df-\u00ff\u0150\u0170\u0151\u0171]",
|
|
|
|
anyLetterStrict: "[A-Za-z0-9\u00c0-\u00de\u00df-\u00ff\u0150\u0170\u0151\u0171]"
|
|
|
|
};
|
|
|
|
|
|
|
|
textPrimitives.unWikiLink = "~";
|
2012-06-05 14:19:27 +00:00
|
|
|
textPrimitives.wikiLink = textPrimitives.upperLetter + "+" +
|
2012-05-26 17:30:32 +00:00
|
|
|
textPrimitives.lowerLetter + "+" +
|
|
|
|
textPrimitives.upperLetter +
|
2012-06-05 14:19:27 +00:00
|
|
|
textPrimitives.anyLetter + "*";
|
2012-05-26 17:30:32 +00:00
|
|
|
|
2012-05-26 22:37:24 +00:00
|
|
|
exports.regExpString = textPrimitives.unWikiLink + "?" + textPrimitives.wikiLink;
|
2012-05-26 17:30:32 +00:00
|
|
|
|
2012-05-28 14:51:52 +00:00
|
|
|
exports.parse = function(match,isBlock) {
|
2012-05-26 17:30:32 +00:00
|
|
|
this.pos = match.index + match[0].length;
|
|
|
|
// If the link starts with the unwikilink character then just output it as plain text
|
|
|
|
if(match[0].substr(0,1) === textPrimitives.unWikiLink) {
|
|
|
|
return [$tw.Tree.Text(match[0].substr(1))];
|
|
|
|
}
|
|
|
|
// If the link has been preceded with a letter then don't treat it as a link
|
|
|
|
if(match.index > 0) {
|
|
|
|
var preRegExp = new RegExp(textPrimitives.anyLetterStrict,"mg");
|
|
|
|
preRegExp.lastIndex = match.index-1;
|
|
|
|
var preMatch = preRegExp.exec(this.source);
|
|
|
|
if(preMatch && preMatch.index === match.index-1) {
|
|
|
|
return [$tw.Tree.Text(match[0])];
|
|
|
|
}
|
|
|
|
}
|
2012-05-28 14:51:52 +00:00
|
|
|
var macroNode = $tw.Tree.Macro("link",{
|
|
|
|
srcParams: {to: match[0]},
|
|
|
|
content: [$tw.Tree.Text(match[0])],
|
|
|
|
wiki: this.wiki
|
|
|
|
});
|
2012-05-26 17:30:32 +00:00
|
|
|
this.dependencies.mergeDependencies(macroNode.dependencies);
|
|
|
|
return [macroNode];
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|