1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 18:17:20 +00:00

Refactored helper methods to be module private

This commit is contained in:
Jeremy Ruston 2011-12-09 19:39:19 +00:00
parent 32514df9f3
commit b526f8bfaf

View File

@ -47,20 +47,20 @@ function WikiTextRules()
this.rulesRegExp = new RegExp(pattern.join("|"),"mg"); this.rulesRegExp = new RegExp(pattern.join("|"),"mg");
} }
WikiTextRules.createElementAndWikify = function(w) { var createElementAndWikify = function(w) {
var e = {type: this.element, children: []}; var e = {type: this.element, children: []};
w.output.push(e); w.output.push(e);
w.subWikifyTerm(e.children,this.termRegExp); w.subWikifyTerm(e.children,this.termRegExp);
}; };
WikiTextRules.setAttr = function(e,attr,value) { var setAttr = function(e,attr,value) {
if(!e.attributes) { if(!e.attributes) {
e.attributes = {}; e.attributes = {};
} }
e.attributes[attr] = value; e.attributes[attr] = value;
}; };
WikiTextRules.inlineCssHelper = function(w) { var inlineCssHelper = function(w) {
var styles = []; var styles = [];
textPrimitives.cssLookaheadRegExp.lastIndex = w.nextMatch; textPrimitives.cssLookaheadRegExp.lastIndex = w.nextMatch;
var lookaheadMatch = textPrimitives.cssLookaheadRegExp.exec(w.source); var lookaheadMatch = textPrimitives.cssLookaheadRegExp.exec(w.source);
@ -85,7 +85,7 @@ WikiTextRules.inlineCssHelper = function(w) {
return styles; return styles;
}; };
WikiTextRules.applyCssHelper = function(e,styles) { var applyCssHelper = function(e,styles) {
if(styles.length > 0) { if(styles.length > 0) {
if(!e.attributes) { if(!e.attributes) {
e.attributes = {}; e.attributes = {};
@ -99,7 +99,7 @@ WikiTextRules.applyCssHelper = function(e,styles) {
} }
}; };
WikiTextRules.enclosedTextHelper = function(w) { var enclosedTextHelper = function(w) {
this.lookaheadRegExp.lastIndex = w.matchStart; this.lookaheadRegExp.lastIndex = w.matchStart;
var lookaheadMatch = this.lookaheadRegExp.exec(w.source); var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
if(lookaheadMatch && lookaheadMatch.index == w.matchStart) { if(lookaheadMatch && lookaheadMatch.index == w.matchStart) {
@ -154,7 +154,7 @@ WikiTextRules.rules = [
w.subWikifyTerm(rowContainer.children,this.rowTermRegExp); w.subWikifyTerm(rowContainer.children,this.rowTermRegExp);
} else { } else {
var theRow = {type: "tr", children: []}; var theRow = {type: "tr", children: []};
WikiTextRules.setAttr(theRow,"className",rowCount%2 ? "oddRow" : "evenRow"); setAttr(theRow,"className",rowCount%2 ? "oddRow" : "evenRow");
rowContainer.children.push(theRow); rowContainer.children.push(theRow);
this.rowHandler(w,theRow.children,prevColumns); this.rowHandler(w,theRow.children,prevColumns);
rowCount++; rowCount++;
@ -177,10 +177,10 @@ WikiTextRules.rules = [
var last = prevColumns[col]; var last = prevColumns[col];
if(last) { if(last) {
last.rowSpanCount++; last.rowSpanCount++;
WikiTextRules.setAttr(last.element,"rowspan",last.rowSpanCount); setAttr(last.element,"rowspan",last.rowSpanCount);
WikiTextRules.setAttr(last.element,"valign","center"); setAttr(last.element,"valign","center");
if(colSpanCount > 1) { if(colSpanCount > 1) {
WikiTextRules.setAttr(last.element,"colspan",colSpanCount); setAttr(last.element,"colspan",colSpanCount);
colSpanCount = 1; colSpanCount = 1;
} }
} }
@ -192,14 +192,14 @@ WikiTextRules.rules = [
} else if(cellMatch[2]) { } else if(cellMatch[2]) {
// End of row // End of row
if(prevCell && colSpanCount > 1) { if(prevCell && colSpanCount > 1) {
WikiTextRules.setAttr(prevCell,"colspan",colSpanCount); setAttr(prevCell,"colspan",colSpanCount);
} }
w.nextMatch = this.cellRegExp.lastIndex; w.nextMatch = this.cellRegExp.lastIndex;
break; break;
} else { } else {
// Cell // Cell
w.nextMatch++; w.nextMatch++;
var styles = WikiTextRules.inlineCssHelper(w); var styles = inlineCssHelper(w);
var spaceLeft = false; var spaceLeft = false;
var chr = w.source.substr(w.nextMatch,1); var chr = w.source.substr(w.nextMatch,1);
while(chr == " ") { while(chr == " ") {
@ -219,15 +219,15 @@ WikiTextRules.rules = [
prevCell = cell; prevCell = cell;
prevColumns[col] = {rowSpanCount:1,element:cell}; prevColumns[col] = {rowSpanCount:1,element:cell};
if(colSpanCount > 1) { if(colSpanCount > 1) {
WikiTextRules.setAttr(cell,"colspan",colSpanCount); setAttr(cell,"colspan",colSpanCount);
colSpanCount = 1; colSpanCount = 1;
} }
WikiTextRules.applyCssHelper(cell,styles); applyCssHelper(cell,styles);
w.subWikifyTerm(cell.children,this.cellTermRegExp); w.subWikifyTerm(cell.children,this.cellTermRegExp);
if(w.matchText.substr(w.matchText.length-2,1) == " ") // spaceRight if(w.matchText.substr(w.matchText.length-2,1) == " ") // spaceRight
WikiTextRules.setAttr(cell,"align",spaceLeft ? "center" : "left"); setAttr(cell,"align",spaceLeft ? "center" : "left");
else if(spaceLeft) else if(spaceLeft)
WikiTextRules.setAttr(cell,"align","right"); setAttr(cell,"align","right");
w.nextMatch--; w.nextMatch--;
} }
col++; col++;
@ -319,7 +319,7 @@ WikiTextRules.rules = [
match: "^<<<\\n", match: "^<<<\\n",
termRegExp: /(^<<<(\n|$))/mg, termRegExp: /(^<<<(\n|$))/mg,
element: "blockquote", element: "blockquote",
handler: WikiTextRules.createElementAndWikify handler: createElementAndWikify
}, },
{ {
@ -390,7 +390,7 @@ WikiTextRules.rules = [
default: default:
break; break;
} }
WikiTextRules.enclosedTextHelper.call(this,w); enclosedTextHelper.call(this,w);
} }
}, },
@ -433,10 +433,10 @@ WikiTextRules.rules = [
if(lookaheadMatch[3]) { if(lookaheadMatch[3]) {
// Pretty bracketted link // Pretty bracketted link
var link = lookaheadMatch[3]; var link = lookaheadMatch[3];
WikiTextRules.setAttr(e,"href",link); setAttr(e,"href",link);
} else { } else {
// Simple bracketted link // Simple bracketted link
WikiTextRules.setAttr(e,"href",text); setAttr(e,"href",text);
} }
w.output.push(e); w.output.push(e);
e.children.push({type: "text", value: text}); e.children.push({type: "text", value: text});
@ -465,7 +465,7 @@ WikiTextRules.rules = [
} }
if(w.autoLinkWikiWords) { if(w.autoLinkWikiWords) {
var link = {type: "a", children: []}; var link = {type: "a", children: []};
WikiTextRules.setAttr(link,"href",w.matchText); setAttr(link,"href",w.matchText);
w.output.push(link); w.output.push(link);
w.outputText(link.children,w.matchStart,w.nextMatch); w.outputText(link.children,w.matchStart,w.nextMatch);
} else { } else {
@ -480,7 +480,7 @@ WikiTextRules.rules = [
handler: function(w) handler: function(w)
{ {
var e = {type: "a", children: []}; var e = {type: "a", children: []};
WikiTextRules.setAttr(e,"href",w.matchText); setAttr(e,"href",w.matchText);
w.output.push(e); w.output.push(e);
w.outputText(e.children,w.matchStart,w.nextMatch); w.outputText(e.children,w.matchStart,w.nextMatch);
} }
@ -500,21 +500,21 @@ WikiTextRules.rules = [
if(lookaheadMatch[5]) { if(lookaheadMatch[5]) {
var link = lookaheadMatch[5], var link = lookaheadMatch[5],
t = {type: "a", children: []}; t = {type: "a", children: []};
WikiTextRules.setAttr(t,"href",link); setAttr(t,"href",link);
w.output.push(t); w.output.push(t);
e = t.children; e = t.children;
} }
var img = {type: "img"}; var img = {type: "img"};
e.push(img); e.push(img);
if(lookaheadMatch[1]) if(lookaheadMatch[1])
WikiTextRules.setAttr(img,"align","left"); setAttr(img,"align","left");
else if(lookaheadMatch[2]) else if(lookaheadMatch[2])
WikiTextRules.setAttr(img,"align","right"); setAttr(img,"align","right");
if(lookaheadMatch[3]) { if(lookaheadMatch[3]) {
WikiTextRules.setAttr(img,"title",lookaheadMatch[3]); setAttr(img,"title",lookaheadMatch[3]);
WikiTextRules.setAttr(img,"alt",lookaheadMatch[3]); setAttr(img,"alt",lookaheadMatch[3]);
} }
WikiTextRules.setAttr(img,"src",lookaheadMatch[4]); setAttr(img,"src",lookaheadMatch[4]);
w.nextMatch = this.lookaheadRegExp.lastIndex; w.nextMatch = this.lookaheadRegExp.lastIndex;
} }
} }
@ -609,11 +609,11 @@ WikiTextRules.rules = [
case "@@": case "@@":
var e = {type: "span", children: []}; var e = {type: "span", children: []};
w.output.push(e); w.output.push(e);
var styles = WikiTextRules.inlineCssHelper(w); var styles = inlineCssHelper(w);
if(styles.length === 0) if(styles.length === 0)
WikiTextRules.setAttr(e,"className","marked"); setAttr(e,"className","marked");
else else
WikiTextRules.applyCssHelper(e,styles); applyCssHelper(e,styles);
w.subWikifyTerm(e.children,/(@@)/mg); w.subWikifyTerm(e.children,/(@@)/mg);
break; break;
case "{{": case "{{":
@ -623,7 +623,7 @@ WikiTextRules.rules = [
if(lookaheadMatch) { if(lookaheadMatch) {
w.nextMatch = lookaheadRegExp.lastIndex; w.nextMatch = lookaheadRegExp.lastIndex;
e = {type: lookaheadMatch[2] == "\n" ? "div" : "span", children: []}; e = {type: lookaheadMatch[2] == "\n" ? "div" : "span", children: []};
WikiTextRules.setAttr(e,"className",lookaheadMatch[1]); setAttr(e,"className",lookaheadMatch[1]);
w.output.push(e); w.output.push(e);
w.subWikifyTerm(e.children,/(\}\}\})/mg); w.subWikifyTerm(e.children,/(\}\}\})/mg);
} }