2011-12-13 12:30:09 +00:00
|
|
|
/*\
|
|
|
|
title: js/WikiTextRules.js
|
|
|
|
|
|
|
|
\*/
|
2011-12-12 10:52:04 +00:00
|
|
|
(function(){
|
|
|
|
|
2011-12-09 16:34:02 +00:00
|
|
|
/*jslint node: true */
|
2011-12-03 16:13:35 +00:00
|
|
|
"use strict";
|
|
|
|
|
2012-01-06 18:43:36 +00:00
|
|
|
var ArgParser = require("./ArgParser.js").ArgParser,
|
2012-02-16 20:38:10 +00:00
|
|
|
Renderer = require("./Renderer.js").Renderer,
|
2012-02-17 14:11:25 +00:00
|
|
|
Dependencies = require("./Dependencies.js").Dependencies,
|
2012-01-06 18:43:36 +00:00
|
|
|
util = require("util");
|
2011-12-03 16:13:35 +00:00
|
|
|
|
2011-12-05 10:34:43 +00:00
|
|
|
var textPrimitives = {
|
2011-12-03 18:19:13 +00:00
|
|
|
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]",
|
|
|
|
sliceSeparator: "::",
|
|
|
|
sectionSeparator: "##",
|
|
|
|
urlPattern: "(?:file|http|https|mailto|ftp|irc|news|data):[^\\s'\"]+(?:/|\\b)",
|
|
|
|
unWikiLink: "~",
|
|
|
|
brackettedLink: "\\[\\[([^\\]]+)\\]\\]",
|
|
|
|
titledBrackettedLink: "\\[\\[([^\\[\\]\\|]+)\\|([^\\[\\]\\|]+)\\]\\]"
|
|
|
|
};
|
|
|
|
|
|
|
|
textPrimitives.wikiLink = "(?:(?:" + textPrimitives.upperLetter + "+" +
|
|
|
|
textPrimitives.lowerLetter + "+" +
|
|
|
|
textPrimitives.upperLetter +
|
|
|
|
textPrimitives.anyLetter + "*)|(?:" +
|
|
|
|
textPrimitives.upperLetter + "{2,}" +
|
|
|
|
textPrimitives.lowerLetter + "+))";
|
|
|
|
|
|
|
|
textPrimitives.cssLookahead = "(?:(" + textPrimitives.anyLetter +
|
|
|
|
"+)\\(([^\\)\\|\\n]+)(?:\\):))|(?:(" + textPrimitives.anyLetter + "+):([^;\\|\\n]+);)";
|
|
|
|
|
|
|
|
textPrimitives.cssLookaheadRegExp = new RegExp(textPrimitives.cssLookahead,"mg");
|
|
|
|
|
|
|
|
textPrimitives.tiddlerForcedLinkRegExp = new RegExp("(?:" + textPrimitives.titledBrackettedLink + ")|(?:" +
|
|
|
|
textPrimitives.brackettedLink + ")|(?:" +
|
|
|
|
textPrimitives.urlPattern + ")","mg");
|
|
|
|
|
|
|
|
textPrimitives.tiddlerAnyLinkRegExp = new RegExp("("+ textPrimitives.wikiLink + ")|(?:" +
|
|
|
|
textPrimitives.titledBrackettedLink + ")|(?:" +
|
|
|
|
textPrimitives.brackettedLink + ")|(?:" +
|
|
|
|
textPrimitives.urlPattern + ")","mg");
|
2011-12-03 16:13:35 +00:00
|
|
|
|
2012-01-17 16:44:59 +00:00
|
|
|
// Helper to add an attribute to an HTML node
|
|
|
|
var setAttr = function(node,attr,value) {
|
|
|
|
if(!node.attributes) {
|
|
|
|
node.attributes = {};
|
2011-12-05 13:15:06 +00:00
|
|
|
}
|
2012-01-17 16:44:59 +00:00
|
|
|
node.attributes[attr] = value;
|
2011-12-05 16:50:25 +00:00
|
|
|
};
|
2011-12-05 13:15:06 +00:00
|
|
|
|
2011-12-09 19:39:19 +00:00
|
|
|
var inlineCssHelper = function(w) {
|
2011-12-03 16:18:17 +00:00
|
|
|
var styles = [];
|
2011-12-03 18:19:13 +00:00
|
|
|
textPrimitives.cssLookaheadRegExp.lastIndex = w.nextMatch;
|
|
|
|
var lookaheadMatch = textPrimitives.cssLookaheadRegExp.exec(w.source);
|
2011-12-03 16:18:17 +00:00
|
|
|
while(lookaheadMatch && lookaheadMatch.index == w.nextMatch) {
|
|
|
|
var s,v;
|
|
|
|
if(lookaheadMatch[1]) {
|
2011-12-06 10:40:49 +00:00
|
|
|
s = lookaheadMatch[1];
|
2011-12-03 16:18:17 +00:00
|
|
|
v = lookaheadMatch[2];
|
|
|
|
} else {
|
2011-12-06 10:40:49 +00:00
|
|
|
s = lookaheadMatch[3];
|
2011-12-03 16:18:17 +00:00
|
|
|
v = lookaheadMatch[4];
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
2011-12-03 16:18:17 +00:00
|
|
|
if(s=="bgcolor")
|
|
|
|
s = "backgroundColor";
|
|
|
|
if(s=="float")
|
|
|
|
s = "cssFloat";
|
|
|
|
styles.push({style: s, value: v});
|
|
|
|
w.nextMatch = lookaheadMatch.index + lookaheadMatch[0].length;
|
2011-12-03 18:19:13 +00:00
|
|
|
textPrimitives.cssLookaheadRegExp.lastIndex = w.nextMatch;
|
|
|
|
lookaheadMatch = textPrimitives.cssLookaheadRegExp.exec(w.source);
|
2011-12-03 16:18:17 +00:00
|
|
|
}
|
|
|
|
return styles;
|
|
|
|
};
|
2011-12-03 16:13:35 +00:00
|
|
|
|
2011-12-09 19:39:19 +00:00
|
|
|
var applyCssHelper = function(e,styles) {
|
2011-12-06 18:04:40 +00:00
|
|
|
if(styles.length > 0) {
|
|
|
|
if(!e.attributes) {
|
|
|
|
e.attributes = {};
|
|
|
|
}
|
|
|
|
if(!e.attributes.style) {
|
|
|
|
e.attributes.style = {};
|
|
|
|
}
|
|
|
|
for(var t=0; t< styles.length; t++) {
|
|
|
|
e.attributes.style[styles[t].style] = styles[t].value;
|
|
|
|
}
|
2011-12-03 16:18:17 +00:00
|
|
|
}
|
|
|
|
};
|
2011-12-03 16:13:35 +00:00
|
|
|
|
2011-12-09 19:39:19 +00:00
|
|
|
var enclosedTextHelper = function(w) {
|
2011-12-03 16:18:17 +00:00
|
|
|
this.lookaheadRegExp.lastIndex = w.matchStart;
|
|
|
|
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
|
|
|
|
if(lookaheadMatch && lookaheadMatch.index == w.matchStart) {
|
|
|
|
var text = lookaheadMatch[1];
|
2012-02-16 20:38:10 +00:00
|
|
|
w.output.push(Renderer.ElementNode(this.element,null,[Renderer.TextNode(text)]));
|
2011-12-03 16:18:17 +00:00
|
|
|
w.nextMatch = lookaheadMatch.index + lookaheadMatch[0].length;
|
|
|
|
}
|
|
|
|
};
|
2011-12-03 16:13:35 +00:00
|
|
|
|
2012-02-06 17:16:45 +00:00
|
|
|
var insertMacroCall = function(w,output,name,params,children) {
|
2012-02-20 19:27:37 +00:00
|
|
|
if(name in w.store.macros) {
|
|
|
|
var macroNode = Renderer.MacroNode(name,params,children,w.store);
|
|
|
|
w.dependencies.mergeDependencies(macroNode.dependencies);
|
|
|
|
output.push(macroNode);
|
2012-02-06 17:16:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-11 18:28:09 +00:00
|
|
|
var rules = [
|
2011-12-03 16:13:35 +00:00
|
|
|
{
|
|
|
|
name: "table",
|
|
|
|
match: "^\\|(?:[^\\n]*)\\|(?:[fhck]?)$",
|
|
|
|
lookaheadRegExp: /^\|([^\n]*)\|([fhck]?)$/mg,
|
|
|
|
rowTermRegExp: /(\|(?:[fhck]?)$\n?)/mg,
|
|
|
|
cellRegExp: /(?:\|([^\n\|]*)\|)|(\|[fhck]?$\n?)/mg,
|
|
|
|
cellTermRegExp: /((?:\x20*)\|)/mg,
|
|
|
|
rowTypes: {"c":"caption", "h":"thead", "":"tbody", "f":"tfoot"},
|
|
|
|
handler: function(w)
|
|
|
|
{
|
2012-03-05 15:13:52 +00:00
|
|
|
var table = Renderer.ElementNode("table",{"class": "table"},[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
w.output.push(table);
|
2011-12-03 16:13:35 +00:00
|
|
|
var prevColumns = [];
|
|
|
|
var currRowType = null;
|
|
|
|
var rowContainer;
|
|
|
|
var rowCount = 0;
|
|
|
|
w.nextMatch = w.matchStart;
|
|
|
|
this.lookaheadRegExp.lastIndex = w.nextMatch;
|
|
|
|
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
|
|
|
|
while(lookaheadMatch && lookaheadMatch.index == w.nextMatch) {
|
|
|
|
var nextRowType = lookaheadMatch[2];
|
|
|
|
if(nextRowType == "k") {
|
2011-12-14 15:45:42 +00:00
|
|
|
table.attributes["class"] = lookaheadMatch[1];
|
2011-12-03 16:13:35 +00:00
|
|
|
w.nextMatch += lookaheadMatch[0].length+1;
|
|
|
|
} else {
|
|
|
|
if(nextRowType != currRowType) {
|
2012-02-16 20:38:10 +00:00
|
|
|
rowContainer = Renderer.ElementNode(this.rowTypes[nextRowType],{},[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
table.children.push(rowContainer);
|
2011-12-03 16:13:35 +00:00
|
|
|
currRowType = nextRowType;
|
|
|
|
}
|
|
|
|
if(currRowType == "c") {
|
|
|
|
// Caption
|
|
|
|
w.nextMatch++;
|
2011-12-05 10:34:43 +00:00
|
|
|
// Move the caption to the first row if it isn't already
|
|
|
|
if(table.children.length !== 1) {
|
|
|
|
table.children.pop(); // Take rowContainer out of the children array
|
2011-12-09 12:26:36 +00:00
|
|
|
table.children.splice(0,0,rowContainer); // Insert it at the bottom
|
2011-12-05 10:34:43 +00:00
|
|
|
}
|
|
|
|
rowContainer.attributes.align = rowCount === 0 ? "top" : "bottom";
|
|
|
|
w.subWikifyTerm(rowContainer.children,this.rowTermRegExp);
|
2011-12-03 16:13:35 +00:00
|
|
|
} else {
|
2012-02-16 20:38:10 +00:00
|
|
|
var theRow = Renderer.ElementNode("tr",{},[]);
|
2012-02-06 12:15:16 +00:00
|
|
|
theRow.attributes["class"] = rowCount%2 ? "oddRow" : "evenRow";
|
2011-12-05 10:34:43 +00:00
|
|
|
rowContainer.children.push(theRow);
|
2011-12-06 18:04:40 +00:00
|
|
|
this.rowHandler(w,theRow.children,prevColumns);
|
2011-12-03 16:13:35 +00:00
|
|
|
rowCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.lookaheadRegExp.lastIndex = w.nextMatch;
|
|
|
|
lookaheadMatch = this.lookaheadRegExp.exec(w.source);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
rowHandler: function(w,e,prevColumns)
|
|
|
|
{
|
|
|
|
var col = 0;
|
|
|
|
var colSpanCount = 1;
|
|
|
|
var prevCell = null;
|
|
|
|
this.cellRegExp.lastIndex = w.nextMatch;
|
|
|
|
var cellMatch = this.cellRegExp.exec(w.source);
|
|
|
|
while(cellMatch && cellMatch.index == w.nextMatch) {
|
|
|
|
if(cellMatch[1] == "~") {
|
|
|
|
// Rowspan
|
|
|
|
var last = prevColumns[col];
|
|
|
|
if(last) {
|
|
|
|
last.rowSpanCount++;
|
2012-02-06 12:15:16 +00:00
|
|
|
last.element.attributes.rowspan = last.rowSpanCount;
|
|
|
|
last.element.attributes.valign = "center";
|
2011-12-03 16:13:35 +00:00
|
|
|
if(colSpanCount > 1) {
|
2012-02-06 12:15:16 +00:00
|
|
|
last.element.attributes.colspan = colSpanCount;
|
2011-12-03 16:13:35 +00:00
|
|
|
colSpanCount = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.nextMatch = this.cellRegExp.lastIndex-1;
|
|
|
|
} else if(cellMatch[1] == ">") {
|
|
|
|
// Colspan
|
|
|
|
colSpanCount++;
|
|
|
|
w.nextMatch = this.cellRegExp.lastIndex-1;
|
|
|
|
} else if(cellMatch[2]) {
|
|
|
|
// End of row
|
|
|
|
if(prevCell && colSpanCount > 1) {
|
2012-02-06 12:15:16 +00:00
|
|
|
prevCell.attributes.colspan = colSpanCount;
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
|
|
|
w.nextMatch = this.cellRegExp.lastIndex;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// Cell
|
|
|
|
w.nextMatch++;
|
2011-12-09 19:39:19 +00:00
|
|
|
var styles = inlineCssHelper(w);
|
2011-12-03 16:13:35 +00:00
|
|
|
var spaceLeft = false;
|
|
|
|
var chr = w.source.substr(w.nextMatch,1);
|
|
|
|
while(chr == " ") {
|
|
|
|
spaceLeft = true;
|
|
|
|
w.nextMatch++;
|
|
|
|
chr = w.source.substr(w.nextMatch,1);
|
|
|
|
}
|
|
|
|
var cell;
|
|
|
|
if(chr == "!") {
|
2012-02-16 20:38:10 +00:00
|
|
|
cell = Renderer.ElementNode("th",{},[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
e.push(cell);
|
2011-12-03 16:13:35 +00:00
|
|
|
w.nextMatch++;
|
|
|
|
} else {
|
2012-02-16 20:38:10 +00:00
|
|
|
cell = Renderer.ElementNode("td",{},[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
e.push(cell);
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
|
|
|
prevCell = cell;
|
|
|
|
prevColumns[col] = {rowSpanCount:1,element:cell};
|
|
|
|
if(colSpanCount > 1) {
|
2012-02-06 12:15:16 +00:00
|
|
|
cell.attributes.colspan = colSpanCount;
|
2011-12-03 16:13:35 +00:00
|
|
|
colSpanCount = 1;
|
|
|
|
}
|
2011-12-09 19:39:19 +00:00
|
|
|
applyCssHelper(cell,styles);
|
2011-12-06 18:04:40 +00:00
|
|
|
w.subWikifyTerm(cell.children,this.cellTermRegExp);
|
2011-12-03 16:13:35 +00:00
|
|
|
if(w.matchText.substr(w.matchText.length-2,1) == " ") // spaceRight
|
2012-02-06 12:15:16 +00:00
|
|
|
cell.attributes.align = spaceLeft ? "center" : "left";
|
2011-12-03 16:13:35 +00:00
|
|
|
else if(spaceLeft)
|
2012-02-06 12:15:16 +00:00
|
|
|
cell.attributes.align = "right";
|
2011-12-03 16:13:35 +00:00
|
|
|
w.nextMatch--;
|
|
|
|
}
|
|
|
|
col++;
|
|
|
|
this.cellRegExp.lastIndex = w.nextMatch;
|
|
|
|
cellMatch = this.cellRegExp.exec(w.source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "heading",
|
|
|
|
match: "^!{1,6}",
|
|
|
|
termRegExp: /(\n)/mg,
|
|
|
|
handler: function(w)
|
|
|
|
{
|
2012-02-16 20:38:10 +00:00
|
|
|
var e = Renderer.ElementNode("h" + w.matchLength,{},[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
w.output.push(e);
|
|
|
|
w.subWikifyTerm(e.children,this.termRegExp);
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "list",
|
|
|
|
match: "^(?:[\\*#;:]+)",
|
|
|
|
lookaheadRegExp: /^(?:(?:(\*)|(#)|(;)|(:))+)/mg,
|
|
|
|
termRegExp: /(\n)/mg,
|
|
|
|
handler: function(w)
|
|
|
|
{
|
|
|
|
var stack = [w.output];
|
|
|
|
var currLevel = 0, currType = null;
|
|
|
|
var listLevel, listType, itemType, baseType;
|
|
|
|
w.nextMatch = w.matchStart;
|
|
|
|
this.lookaheadRegExp.lastIndex = w.nextMatch;
|
|
|
|
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
|
|
|
|
while(lookaheadMatch && lookaheadMatch.index == w.nextMatch) {
|
|
|
|
if(lookaheadMatch[1]) {
|
|
|
|
listType = "ul";
|
|
|
|
itemType = "li";
|
|
|
|
} else if(lookaheadMatch[2]) {
|
|
|
|
listType = "ol";
|
|
|
|
itemType = "li";
|
|
|
|
} else if(lookaheadMatch[3]) {
|
|
|
|
listType = "dl";
|
|
|
|
itemType = "dt";
|
|
|
|
} else if(lookaheadMatch[4]) {
|
|
|
|
listType = "dl";
|
|
|
|
itemType = "dd";
|
|
|
|
}
|
|
|
|
if(!baseType)
|
|
|
|
baseType = listType;
|
|
|
|
listLevel = lookaheadMatch[0].length;
|
|
|
|
w.nextMatch += lookaheadMatch[0].length;
|
2011-12-05 10:34:43 +00:00
|
|
|
var t,e;
|
2011-12-03 16:13:35 +00:00
|
|
|
if(listLevel > currLevel) {
|
|
|
|
for(t=currLevel; t<listLevel; t++) {
|
2011-12-08 16:19:02 +00:00
|
|
|
var target = stack[stack.length-1];
|
|
|
|
if(currLevel !== 0 && target.children) {
|
|
|
|
target = target.children[target.children.length-1];
|
|
|
|
}
|
2012-02-16 20:38:10 +00:00
|
|
|
e = Renderer.ElementNode(listType,{},[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
target.push(e);
|
|
|
|
stack.push(e.children);
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
|
|
|
} else if(listType!=baseType && listLevel==1) {
|
|
|
|
w.nextMatch -= lookaheadMatch[0].length;
|
|
|
|
return;
|
|
|
|
} else if(listLevel < currLevel) {
|
|
|
|
for(t=currLevel; t>listLevel; t--)
|
|
|
|
stack.pop();
|
|
|
|
} else if(listLevel == currLevel && listType != currType) {
|
|
|
|
stack.pop();
|
2012-02-16 20:38:10 +00:00
|
|
|
e = Renderer.ElementNode(listType,{},[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
stack[stack.length-1].push(e);
|
|
|
|
stack.push(e.children);
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
|
|
|
currLevel = listLevel;
|
|
|
|
currType = listType;
|
2012-02-16 20:38:10 +00:00
|
|
|
e = Renderer.ElementNode(itemType,{},[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
stack[stack.length-1].push(e);
|
|
|
|
w.subWikifyTerm(e.children,this.termRegExp);
|
2011-12-03 16:13:35 +00:00
|
|
|
this.lookaheadRegExp.lastIndex = w.nextMatch;
|
|
|
|
lookaheadMatch = this.lookaheadRegExp.exec(w.source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "quoteByBlock",
|
|
|
|
match: "^<<<\\n",
|
|
|
|
termRegExp: /(^<<<(\n|$))/mg,
|
|
|
|
element: "blockquote",
|
2012-01-17 16:44:59 +00:00
|
|
|
handler: function(w) {
|
2012-02-16 20:38:10 +00:00
|
|
|
var e = Renderer.ElementNode(this.element,{},[]);
|
2012-01-17 16:44:59 +00:00
|
|
|
w.output.push(e);
|
|
|
|
w.subWikifyTerm(e.children,this.termRegExp);
|
|
|
|
}
|
2011-12-03 16:13:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "quoteByLine",
|
|
|
|
match: "^>+",
|
|
|
|
lookaheadRegExp: /^>+/mg,
|
|
|
|
termRegExp: /(\n)/mg,
|
|
|
|
element: "blockquote",
|
|
|
|
handler: function(w)
|
|
|
|
{
|
|
|
|
var stack = [w.output];
|
|
|
|
var currLevel = 0;
|
|
|
|
var newLevel = w.matchLength;
|
2011-12-05 10:34:43 +00:00
|
|
|
var t,matched,e;
|
2011-12-03 16:13:35 +00:00
|
|
|
do {
|
|
|
|
if(newLevel > currLevel) {
|
2011-12-05 10:34:43 +00:00
|
|
|
for(t=currLevel; t<newLevel; t++) {
|
2012-02-16 20:38:10 +00:00
|
|
|
e = Renderer.ElementNode(this.element,{},[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
stack[stack.length-1].push(e);
|
|
|
|
}
|
2011-12-03 16:13:35 +00:00
|
|
|
} else if(newLevel < currLevel) {
|
|
|
|
for(t=currLevel; t>newLevel; t--)
|
|
|
|
stack.pop();
|
|
|
|
}
|
|
|
|
currLevel = newLevel;
|
|
|
|
w.subWikifyTerm(stack[stack.length-1],this.termRegExp);
|
2012-02-16 20:38:10 +00:00
|
|
|
stack[stack.length-1].push(Renderer.ElementNode("br"));
|
2011-12-03 16:13:35 +00:00
|
|
|
this.lookaheadRegExp.lastIndex = w.nextMatch;
|
|
|
|
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
|
|
|
|
matched = lookaheadMatch && lookaheadMatch.index == w.nextMatch;
|
|
|
|
if(matched) {
|
|
|
|
newLevel = lookaheadMatch[0].length;
|
|
|
|
w.nextMatch += lookaheadMatch[0].length;
|
|
|
|
}
|
|
|
|
} while(matched);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "rule",
|
|
|
|
match: "^----+$\\n?|<hr ?/?>\\n?",
|
|
|
|
handler: function(w)
|
|
|
|
{
|
2012-02-16 20:38:10 +00:00
|
|
|
w.output.push(Renderer.ElementNode("hr"));
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "monospacedByLine",
|
|
|
|
match: "^(?:/\\*\\{\\{\\{\\*/|\\{\\{\\{|//\\{\\{\\{|<!--\\{\\{\\{-->)\\n",
|
|
|
|
element: "pre",
|
|
|
|
handler: function(w)
|
|
|
|
{
|
|
|
|
switch(w.matchText) {
|
|
|
|
case "/*{{{*/\n": // CSS
|
|
|
|
this.lookaheadRegExp = /\/\*\{\{\{\*\/\n*((?:^[^\n]*\n)+?)(\n*^\f*\/\*\}\}\}\*\/$\n?)/mg;
|
|
|
|
break;
|
|
|
|
case "{{{\n": // monospaced block
|
|
|
|
this.lookaheadRegExp = /^\{\{\{\n((?:^[^\n]*\n)+?)(^\f*\}\}\}$\n?)/mg;
|
|
|
|
break;
|
|
|
|
case "//{{{\n": // plugin
|
|
|
|
this.lookaheadRegExp = /^\/\/\{\{\{\n\n*((?:^[^\n]*\n)+?)(\n*^\f*\/\/\}\}\}$\n?)/mg;
|
|
|
|
break;
|
|
|
|
case "<!--{{{-->\n": //template
|
|
|
|
this.lookaheadRegExp = /<!--\{\{\{-->\n*((?:^[^\n]*\n)+?)(\n*^\f*<!--\}\}\}-->$\n?)/mg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-12-09 19:39:19 +00:00
|
|
|
enclosedTextHelper.call(this,w);
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-03-03 12:29:13 +00:00
|
|
|
{
|
|
|
|
name: "typedBlock",
|
|
|
|
match: "^\\$\\$\\$(?:.*)\\n",
|
2012-03-03 13:35:51 +00:00
|
|
|
lookaheadRegExp: /^\$\$\$(.*)\n((?:^[^\n]*\n)+?)(^\f*\$\$\$$\n?)/mg,
|
2012-03-03 12:29:13 +00:00
|
|
|
handler: function(w)
|
|
|
|
{
|
2012-03-03 13:35:51 +00:00
|
|
|
this.lookaheadRegExp.lastIndex = w.matchStart;
|
2012-03-03 12:29:13 +00:00
|
|
|
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
|
|
|
|
if(lookaheadMatch && lookaheadMatch.index == w.matchStart) {
|
|
|
|
// The wikitext parsing infrastructure is horribly unre-entrant
|
|
|
|
var mimeType = lookaheadMatch[1],
|
|
|
|
content = lookaheadMatch[2],
|
|
|
|
oldOutput = w.output,
|
|
|
|
oldSource = w.source,
|
|
|
|
oldNextMatch = w.nextMatch,
|
|
|
|
oldChildren = w.children,
|
|
|
|
oldDependencies = w.dependencies,
|
2012-03-03 18:07:47 +00:00
|
|
|
parseTree = w.store.parseText(mimeType,content,{defaultType: "text/plain"});
|
2012-03-03 12:29:13 +00:00
|
|
|
w.output = oldOutput;
|
|
|
|
w.source = oldSource;
|
|
|
|
w.nextMatch = oldNextMatch;
|
|
|
|
w.children = oldChildren;
|
|
|
|
w.dependencies = oldDependencies;
|
2012-03-03 18:39:13 +00:00
|
|
|
w.output.push.apply(w.output,parseTree.nodes);
|
2012-03-03 12:29:13 +00:00
|
|
|
w.nextMatch = this.lookaheadRegExp.lastIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-12-03 16:13:35 +00:00
|
|
|
{
|
|
|
|
name: "wikifyComment",
|
|
|
|
match: "^(?:/\\*\\*\\*|<!---)\\n",
|
|
|
|
handler: function(w)
|
|
|
|
{
|
|
|
|
var termRegExp = (w.matchText == "/***\n") ? (/(^\*\*\*\/\n)/mg) : (/(^--->\n)/mg);
|
|
|
|
w.subWikifyTerm(w.output,termRegExp);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "macro",
|
|
|
|
match: "<<",
|
|
|
|
lookaheadRegExp: /<<([^>\s]+)(?:\s*)((?:[^>]|(?:>(?!>)))*)>>/mg,
|
|
|
|
handler: function(w)
|
|
|
|
{
|
|
|
|
this.lookaheadRegExp.lastIndex = w.matchStart;
|
|
|
|
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
|
|
|
|
if(lookaheadMatch && lookaheadMatch.index == w.matchStart && lookaheadMatch[1]) {
|
|
|
|
w.nextMatch = this.lookaheadRegExp.lastIndex;
|
2012-02-06 17:16:45 +00:00
|
|
|
var name = lookaheadMatch[1];
|
2012-02-22 23:27:38 +00:00
|
|
|
insertMacroCall(w,w.output,name,lookaheadMatch[2]);
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "prettyLink",
|
|
|
|
match: "\\[\\[",
|
|
|
|
lookaheadRegExp: /\[\[(.*?)(?:\|(~)?(.*?))?\]\]/mg,
|
|
|
|
handler: function(w)
|
|
|
|
{
|
|
|
|
this.lookaheadRegExp.lastIndex = w.matchStart;
|
|
|
|
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
|
|
|
|
if(lookaheadMatch && lookaheadMatch.index == w.matchStart) {
|
2012-02-06 17:16:45 +00:00
|
|
|
var text = lookaheadMatch[1],
|
|
|
|
link = text;
|
2011-12-03 16:13:35 +00:00
|
|
|
if(lookaheadMatch[3]) {
|
|
|
|
// Pretty bracketted link
|
2012-02-06 17:16:45 +00:00
|
|
|
link = lookaheadMatch[3];
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
2012-04-07 10:32:41 +00:00
|
|
|
insertMacroCall(w,w.output,"link",{to: link},[Renderer.TextNode(text)]);
|
2011-12-03 16:13:35 +00:00
|
|
|
w.nextMatch = this.lookaheadRegExp.lastIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "wikiLink",
|
2011-12-03 18:19:13 +00:00
|
|
|
match: textPrimitives.unWikiLink+"?"+textPrimitives.wikiLink,
|
2011-12-03 16:13:35 +00:00
|
|
|
handler: function(w)
|
|
|
|
{
|
2011-12-03 18:19:13 +00:00
|
|
|
if(w.matchText.substr(0,1) == textPrimitives.unWikiLink) {
|
2011-12-03 16:13:35 +00:00
|
|
|
w.outputText(w.output,w.matchStart+1,w.nextMatch);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(w.matchStart > 0) {
|
2011-12-03 18:19:13 +00:00
|
|
|
var preRegExp = new RegExp(textPrimitives.anyLetterStrict,"mg");
|
2011-12-03 16:13:35 +00:00
|
|
|
preRegExp.lastIndex = w.matchStart-1;
|
|
|
|
var preMatch = preRegExp.exec(w.source);
|
|
|
|
if(preMatch.index == w.matchStart-1) {
|
|
|
|
w.outputText(w.output,w.matchStart,w.nextMatch);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-12-05 16:50:25 +00:00
|
|
|
if(w.autoLinkWikiWords) {
|
2012-04-07 10:32:41 +00:00
|
|
|
insertMacroCall(w,w.output,"link",{to: w.matchText},[Renderer.TextNode(w.source.substring(w.matchStart,w.nextMatch))]);
|
2011-12-03 16:13:35 +00:00
|
|
|
} else {
|
|
|
|
w.outputText(w.output,w.matchStart,w.nextMatch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "urlLink",
|
2011-12-03 18:19:13 +00:00
|
|
|
match: textPrimitives.urlPattern,
|
2011-12-03 16:13:35 +00:00
|
|
|
handler: function(w)
|
|
|
|
{
|
2012-04-07 10:32:41 +00:00
|
|
|
insertMacroCall(w,w.output,"link",{to: w.matchText},[Renderer.TextNode(w.source.substring(w.matchStart,w.nextMatch))]);
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "image",
|
|
|
|
match: "\\[[<>]?[Ii][Mm][Gg]\\[",
|
2011-12-03 16:36:11 +00:00
|
|
|
// [<] sequence below is to avoid lessThan-questionMark sequence so TiddlyWikis can be included in PHP files
|
2011-12-03 16:13:35 +00:00
|
|
|
lookaheadRegExp: /\[([<]?)(>?)[Ii][Mm][Gg]\[(?:([^\|\]]+)\|)?([^\[\]\|]+)\](?:\[([^\]]*)\])?\]/mg,
|
|
|
|
handler: function(w)
|
|
|
|
{
|
|
|
|
this.lookaheadRegExp.lastIndex = w.matchStart;
|
2012-01-16 08:58:51 +00:00
|
|
|
var lookaheadMatch = this.lookaheadRegExp.exec(w.source),
|
2012-02-20 19:27:37 +00:00
|
|
|
imageParams = {},
|
|
|
|
linkParams = {};
|
2011-12-03 16:13:35 +00:00
|
|
|
if(lookaheadMatch && lookaheadMatch.index == w.matchStart) {
|
2012-01-16 08:58:51 +00:00
|
|
|
if(lookaheadMatch[1]) {
|
2012-02-20 19:27:37 +00:00
|
|
|
imageParams.alignment = "left";
|
2012-01-16 08:58:51 +00:00
|
|
|
} else if(lookaheadMatch[2]) {
|
2012-02-20 19:27:37 +00:00
|
|
|
imageParams.alignment = "right";
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
|
|
|
if(lookaheadMatch[3]) {
|
2012-02-20 19:27:37 +00:00
|
|
|
imageParams.text = lookaheadMatch[3];
|
2012-01-16 08:58:51 +00:00
|
|
|
}
|
2012-02-20 19:27:37 +00:00
|
|
|
imageParams.src = lookaheadMatch[4];
|
2012-01-16 08:58:51 +00:00
|
|
|
if(lookaheadMatch[5]) {
|
2012-02-20 19:27:37 +00:00
|
|
|
linkParams.target = lookaheadMatch[5];
|
2012-02-06 17:16:45 +00:00
|
|
|
var linkChildren = [];
|
|
|
|
insertMacroCall(w,w.output,"link",linkParams,linkChildren);
|
|
|
|
insertMacroCall(w,linkChildren,"image",imageParams);
|
2012-01-16 08:58:51 +00:00
|
|
|
} else {
|
2012-02-06 17:16:45 +00:00
|
|
|
insertMacroCall(w,w.output,"image",imageParams);
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
|
|
|
w.nextMatch = this.lookaheadRegExp.lastIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "html",
|
|
|
|
match: "<[Hh][Tt][Mm][Ll]>",
|
|
|
|
lookaheadRegExp: /<[Hh][Tt][Mm][Ll]>((?:.|\n)*?)<\/[Hh][Tt][Mm][Ll]>/mg,
|
|
|
|
handler: function(w)
|
|
|
|
{
|
|
|
|
this.lookaheadRegExp.lastIndex = w.matchStart;
|
|
|
|
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
|
|
|
|
if(lookaheadMatch && lookaheadMatch.index == w.matchStart) {
|
2012-02-16 20:38:10 +00:00
|
|
|
w.output.push(Renderer.ElementNode("html",{},[Renderer.RawNode(lookaheadMatch[1])]));
|
2011-12-03 16:13:35 +00:00
|
|
|
w.nextMatch = this.lookaheadRegExp.lastIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "commentByBlock",
|
|
|
|
match: "/%",
|
|
|
|
lookaheadRegExp: /\/%((?:.|\n)*?)%\//mg,
|
|
|
|
handler: function(w)
|
|
|
|
{
|
|
|
|
this.lookaheadRegExp.lastIndex = w.matchStart;
|
|
|
|
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
|
|
|
|
if(lookaheadMatch && lookaheadMatch.index == w.matchStart)
|
|
|
|
w.nextMatch = this.lookaheadRegExp.lastIndex;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "characterFormat",
|
2012-01-08 11:33:24 +00:00
|
|
|
match: "''|//|__|\\^\\^|~~|--(?!\\s|$)|\\{\\{\\{|`",
|
2011-12-03 16:13:35 +00:00
|
|
|
handler: function(w)
|
|
|
|
{
|
2012-01-16 08:58:51 +00:00
|
|
|
var e,lookaheadRegExp,lookaheadMatch;
|
2011-12-03 16:13:35 +00:00
|
|
|
switch(w.matchText) {
|
|
|
|
case "''":
|
2012-02-16 20:38:10 +00:00
|
|
|
e = Renderer.ElementNode("strong",null,[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
w.output.push(e);
|
|
|
|
w.subWikifyTerm(e.children,/('')/mg);
|
2011-12-03 16:13:35 +00:00
|
|
|
break;
|
|
|
|
case "//":
|
2012-02-16 20:38:10 +00:00
|
|
|
e = Renderer.ElementNode("em",null,[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
w.output.push(e);
|
|
|
|
w.subWikifyTerm(e.children,/(\/\/)/mg);
|
2011-12-03 16:13:35 +00:00
|
|
|
break;
|
|
|
|
case "__":
|
2012-02-16 20:38:10 +00:00
|
|
|
e = Renderer.ElementNode("u",null,[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
w.output.push(e);
|
|
|
|
w.subWikifyTerm(e.children,/(__)/mg);
|
2011-12-03 16:13:35 +00:00
|
|
|
break;
|
|
|
|
case "^^":
|
2012-02-16 20:38:10 +00:00
|
|
|
e = Renderer.ElementNode("sup",null,[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
w.output.push(e);
|
|
|
|
w.subWikifyTerm(e.children,/(\^\^)/mg);
|
2011-12-03 16:13:35 +00:00
|
|
|
break;
|
|
|
|
case "~~":
|
2012-02-16 20:38:10 +00:00
|
|
|
e = Renderer.ElementNode("sub",null,[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
w.output.push(e);
|
|
|
|
w.subWikifyTerm(e.children,/(~~)/mg);
|
2011-12-03 16:13:35 +00:00
|
|
|
break;
|
|
|
|
case "--":
|
2012-02-16 20:38:10 +00:00
|
|
|
e = Renderer.ElementNode("strike",null,[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
w.output.push(e);
|
|
|
|
w.subWikifyTerm(e.children,/(--)/mg);
|
2011-12-03 16:13:35 +00:00
|
|
|
break;
|
2012-01-08 11:33:24 +00:00
|
|
|
case "`":
|
2012-01-16 08:58:51 +00:00
|
|
|
lookaheadRegExp = /`((?:.|\n)*?)`/mg;
|
2012-01-15 11:41:54 +00:00
|
|
|
lookaheadRegExp.lastIndex = w.matchStart;
|
2012-01-16 08:58:51 +00:00
|
|
|
lookaheadMatch = lookaheadRegExp.exec(w.source);
|
2012-01-15 11:41:54 +00:00
|
|
|
if(lookaheadMatch && lookaheadMatch.index == w.matchStart) {
|
2012-02-16 20:38:10 +00:00
|
|
|
w.output.push(Renderer.ElementNode("code",null,[Renderer.TextNode(lookaheadMatch[1])]));
|
2012-01-15 11:41:54 +00:00
|
|
|
w.nextMatch = lookaheadRegExp.lastIndex;
|
|
|
|
}
|
2012-01-08 11:33:24 +00:00
|
|
|
break;
|
2011-12-03 16:13:35 +00:00
|
|
|
case "{{{":
|
2012-01-16 08:58:51 +00:00
|
|
|
lookaheadRegExp = /\{\{\{((?:.|\n)*?)\}\}\}/mg;
|
2011-12-03 16:13:35 +00:00
|
|
|
lookaheadRegExp.lastIndex = w.matchStart;
|
2012-01-16 08:58:51 +00:00
|
|
|
lookaheadMatch = lookaheadRegExp.exec(w.source);
|
2011-12-03 16:13:35 +00:00
|
|
|
if(lookaheadMatch && lookaheadMatch.index == w.matchStart) {
|
2012-02-16 20:38:10 +00:00
|
|
|
w.output.push(Renderer.ElementNode("code",null,[Renderer.TextNode(lookaheadMatch[1])]));
|
2011-12-03 16:13:35 +00:00
|
|
|
w.nextMatch = lookaheadRegExp.lastIndex;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "customFormat",
|
|
|
|
match: "@@|\\{\\{",
|
|
|
|
handler: function(w)
|
|
|
|
{
|
|
|
|
switch(w.matchText) {
|
|
|
|
case "@@":
|
2012-02-16 20:38:10 +00:00
|
|
|
var e = Renderer.ElementNode("span",null,[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
w.output.push(e);
|
2011-12-09 19:39:19 +00:00
|
|
|
var styles = inlineCssHelper(w);
|
2011-12-05 10:34:43 +00:00
|
|
|
if(styles.length === 0)
|
2011-12-14 15:45:42 +00:00
|
|
|
setAttr(e,"class","marked");
|
2011-12-03 16:13:35 +00:00
|
|
|
else
|
2011-12-09 19:39:19 +00:00
|
|
|
applyCssHelper(e,styles);
|
2011-12-05 10:34:43 +00:00
|
|
|
w.subWikifyTerm(e.children,/(@@)/mg);
|
2011-12-03 16:13:35 +00:00
|
|
|
break;
|
|
|
|
case "{{":
|
2012-03-05 15:13:52 +00:00
|
|
|
var lookaheadRegExp = /\{\{[\s]*([\-\w]+[\-\s\w]*)[\s]*\{(\n?)/mg;
|
2011-12-03 16:13:35 +00:00
|
|
|
lookaheadRegExp.lastIndex = w.matchStart;
|
|
|
|
var lookaheadMatch = lookaheadRegExp.exec(w.source);
|
|
|
|
if(lookaheadMatch) {
|
|
|
|
w.nextMatch = lookaheadRegExp.lastIndex;
|
2012-02-16 20:38:10 +00:00
|
|
|
e = Renderer.ElementNode(lookaheadMatch[2] == "\n" ? "div" : "span",{
|
2012-02-06 12:43:05 +00:00
|
|
|
"class": lookaheadMatch[1]
|
|
|
|
},[]);
|
2011-12-05 10:34:43 +00:00
|
|
|
w.output.push(e);
|
|
|
|
w.subWikifyTerm(e.children,/(\}\}\})/mg);
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "mdash",
|
|
|
|
match: "--",
|
|
|
|
handler: function(w)
|
|
|
|
{
|
2012-02-16 20:38:10 +00:00
|
|
|
w.output.push(Renderer.EntityNode("—"));
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "lineBreak",
|
|
|
|
match: "\\n|<br ?/?>",
|
|
|
|
handler: function(w)
|
|
|
|
{
|
2012-02-16 20:38:10 +00:00
|
|
|
w.output.push(Renderer.ElementNode("br"));
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "rawText",
|
|
|
|
match: "\"{3}|<nowiki>",
|
|
|
|
lookaheadRegExp: /(?:\"{3}|<nowiki>)((?:.|\n)*?)(?:\"{3}|<\/nowiki>)/mg,
|
|
|
|
handler: function(w)
|
|
|
|
{
|
|
|
|
this.lookaheadRegExp.lastIndex = w.matchStart;
|
|
|
|
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
|
|
|
|
if(lookaheadMatch && lookaheadMatch.index == w.matchStart) {
|
2012-02-16 20:38:10 +00:00
|
|
|
w.output.push(Renderer.TextNode(lookaheadMatch[1]));
|
2011-12-03 16:13:35 +00:00
|
|
|
w.nextMatch = this.lookaheadRegExp.lastIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "htmlEntitiesEncoding",
|
2011-12-08 12:38:08 +00:00
|
|
|
match: "&#?[a-zA-Z0-9]{2,8};",
|
2011-12-03 16:13:35 +00:00
|
|
|
handler: function(w)
|
|
|
|
{
|
2012-02-16 20:38:10 +00:00
|
|
|
w.output.push(Renderer.EntityNode(w.matchText));
|
2011-12-03 16:13:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
];
|
|
|
|
|
2011-12-11 18:28:09 +00:00
|
|
|
exports.rules = rules;
|
2011-12-12 10:52:04 +00:00
|
|
|
|
|
|
|
})();
|