mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-07-13 15:32:51 +00:00
Refactored config.formatterHelpers
This commit is contained in:
parent
28cce8b269
commit
e1f26eb07b
143
js/Formatter.js
143
js/Formatter.js
@ -19,82 +19,73 @@ function Formatter(formatters)
|
|||||||
this.formatterRegExp = new RegExp(pattern.join("|"),"mg");
|
this.formatterRegExp = new RegExp(pattern.join("|"),"mg");
|
||||||
}
|
}
|
||||||
|
|
||||||
config.formatterHelpers = {
|
Formatter.createElementAndWikify = function(w) {
|
||||||
|
w.subWikifyTerm(createTiddlyElement(w.output,this.element),this.termRegExp);
|
||||||
|
};
|
||||||
|
|
||||||
createElementAndWikify: function(w)
|
Formatter.inlineCssHelper = function(w) {
|
||||||
{
|
var styles = [];
|
||||||
w.subWikifyTerm(createTiddlyElement(w.output,this.element),this.termRegExp);
|
config.textPrimitives.cssLookaheadRegExp.lastIndex = w.nextMatch;
|
||||||
},
|
var lookaheadMatch = config.textPrimitives.cssLookaheadRegExp.exec(w.source);
|
||||||
|
while(lookaheadMatch && lookaheadMatch.index == w.nextMatch) {
|
||||||
inlineCssHelper: function(w)
|
var s,v;
|
||||||
{
|
if(lookaheadMatch[1]) {
|
||||||
var styles = [];
|
s = lookaheadMatch[1].unDash();
|
||||||
|
v = lookaheadMatch[2];
|
||||||
|
} else {
|
||||||
|
s = lookaheadMatch[3].unDash();
|
||||||
|
v = lookaheadMatch[4];
|
||||||
|
}
|
||||||
|
if(s=="bgcolor")
|
||||||
|
s = "backgroundColor";
|
||||||
|
if(s=="float")
|
||||||
|
s = "cssFloat";
|
||||||
|
styles.push({style: s, value: v});
|
||||||
|
w.nextMatch = lookaheadMatch.index + lookaheadMatch[0].length;
|
||||||
config.textPrimitives.cssLookaheadRegExp.lastIndex = w.nextMatch;
|
config.textPrimitives.cssLookaheadRegExp.lastIndex = w.nextMatch;
|
||||||
var lookaheadMatch = config.textPrimitives.cssLookaheadRegExp.exec(w.source);
|
lookaheadMatch = config.textPrimitives.cssLookaheadRegExp.exec(w.source);
|
||||||
while(lookaheadMatch && lookaheadMatch.index == w.nextMatch) {
|
}
|
||||||
var s,v;
|
return styles;
|
||||||
if(lookaheadMatch[1]) {
|
};
|
||||||
s = lookaheadMatch[1].unDash();
|
|
||||||
v = lookaheadMatch[2];
|
|
||||||
} else {
|
|
||||||
s = lookaheadMatch[3].unDash();
|
|
||||||
v = lookaheadMatch[4];
|
|
||||||
}
|
|
||||||
if(s=="bgcolor")
|
|
||||||
s = "backgroundColor";
|
|
||||||
if(s=="float")
|
|
||||||
s = "cssFloat";
|
|
||||||
styles.push({style: s, value: v});
|
|
||||||
w.nextMatch = lookaheadMatch.index + lookaheadMatch[0].length;
|
|
||||||
config.textPrimitives.cssLookaheadRegExp.lastIndex = w.nextMatch;
|
|
||||||
lookaheadMatch = config.textPrimitives.cssLookaheadRegExp.exec(w.source);
|
|
||||||
}
|
|
||||||
return styles;
|
|
||||||
},
|
|
||||||
|
|
||||||
applyCssHelper: function(e,styles)
|
Formatter.applyCssHelper = function(e,styles) {
|
||||||
{
|
var t;
|
||||||
var t;
|
for(t=0; t< styles.length; t++) {
|
||||||
for(t=0; t< styles.length; t++) {
|
try {
|
||||||
try {
|
e.style[styles[t].style] = styles[t].value;
|
||||||
e.style[styles[t].style] = styles[t].value;
|
} catch (ex) {
|
||||||
} catch (ex) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
};
|
||||||
|
|
||||||
enclosedTextHelper: function(w)
|
Formatter.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) {
|
var text = lookaheadMatch[1];
|
||||||
var text = lookaheadMatch[1];
|
if(config.browser.isIE)
|
||||||
if(config.browser.isIE)
|
text = text.replace(/\n/g,"\r");
|
||||||
text = text.replace(/\n/g,"\r");
|
createTiddlyElement(w.output,this.element,null,null,text);
|
||||||
createTiddlyElement(w.output,this.element,null,null,text);
|
w.nextMatch = lookaheadMatch.index + lookaheadMatch[0].length;
|
||||||
w.nextMatch = lookaheadMatch.index + lookaheadMatch[0].length;
|
}
|
||||||
}
|
};
|
||||||
},
|
|
||||||
|
|
||||||
isExternalLink: function(link)
|
Formatter.isExternalLink = function(link) {
|
||||||
{
|
if(store.tiddlerExists(link) || store.isShadowTiddler(link)) {
|
||||||
if(store.tiddlerExists(link) || store.isShadowTiddler(link)) {
|
//# Definitely not an external link
|
||||||
//# Definitely not an external link
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
var urlRegExp = new RegExp(config.textPrimitives.urlPattern,"mg");
|
|
||||||
if(urlRegExp.exec(link)) {
|
|
||||||
//# Definitely an external link
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if(link.indexOf(".")!=-1 || link.indexOf("\\")!=-1 || link.indexOf("/")!=-1 || link.indexOf("#")!=-1) {
|
|
||||||
//# Link contains . / \ or # so is probably an external link
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//# Otherwise assume it is not an external link
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
var urlRegExp = new RegExp(config.textPrimitives.urlPattern,"mg");
|
||||||
|
if(urlRegExp.exec(link)) {
|
||||||
|
//# Definitely an external link
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(link.indexOf(".")!=-1 || link.indexOf("\\")!=-1 || link.indexOf("/")!=-1 || link.indexOf("#")!=-1) {
|
||||||
|
//# Link contains . / \ or # so is probably an external link
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//# Otherwise assume it is not an external link
|
||||||
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
config.formatters = [
|
config.formatters = [
|
||||||
@ -185,7 +176,7 @@ config.formatters = [
|
|||||||
} else {
|
} else {
|
||||||
// Cell
|
// Cell
|
||||||
w.nextMatch++;
|
w.nextMatch++;
|
||||||
var styles = config.formatterHelpers.inlineCssHelper(w);
|
var styles = Formatter.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 == " ") {
|
||||||
@ -207,7 +198,7 @@ config.formatters = [
|
|||||||
cell.setAttribute("colSpan",colSpanCount); // Needed for IE
|
cell.setAttribute("colSpan",colSpanCount); // Needed for IE
|
||||||
colSpanCount = 1;
|
colSpanCount = 1;
|
||||||
}
|
}
|
||||||
config.formatterHelpers.applyCssHelper(cell,styles);
|
Formatter.applyCssHelper(cell,styles);
|
||||||
w.subWikifyTerm(cell,this.cellTermRegExp);
|
w.subWikifyTerm(cell,this.cellTermRegExp);
|
||||||
if(w.matchText.substr(w.matchText.length-2,1) == " ") // spaceRight
|
if(w.matchText.substr(w.matchText.length-2,1) == " ") // spaceRight
|
||||||
cell.align = spaceLeft ? "center" : "left";
|
cell.align = spaceLeft ? "center" : "left";
|
||||||
@ -294,7 +285,7 @@ config.formatters = [
|
|||||||
match: "^<<<\\n",
|
match: "^<<<\\n",
|
||||||
termRegExp: /(^<<<(\n|$))/mg,
|
termRegExp: /(^<<<(\n|$))/mg,
|
||||||
element: "blockquote",
|
element: "blockquote",
|
||||||
handler: config.formatterHelpers.createElementAndWikify
|
handler: Formatter.createElementAndWikify
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -362,7 +353,7 @@ config.formatters = [
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
config.formatterHelpers.enclosedTextHelper.call(this,w);
|
Formatter.enclosedTextHelper.call(this,w);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -405,7 +396,7 @@ config.formatters = [
|
|||||||
if(lookaheadMatch[3]) {
|
if(lookaheadMatch[3]) {
|
||||||
// Pretty bracketted link
|
// Pretty bracketted link
|
||||||
var link = lookaheadMatch[3];
|
var link = lookaheadMatch[3];
|
||||||
e = (!lookaheadMatch[2] && config.formatterHelpers.isExternalLink(link)) ?
|
e = (!lookaheadMatch[2] && Formatter.isExternalLink(link)) ?
|
||||||
createExternalLink(w.output,link) : createTiddlyLink(w.output,link,false,null,w.isStatic,w.tiddler);
|
createExternalLink(w.output,link) : createTiddlyLink(w.output,link,false,null,w.isStatic,w.tiddler);
|
||||||
} else {
|
} else {
|
||||||
// Simple bracketted link
|
// Simple bracketted link
|
||||||
@ -466,7 +457,7 @@ config.formatters = [
|
|||||||
var e = w.output;
|
var e = w.output;
|
||||||
if(lookaheadMatch[5]) {
|
if(lookaheadMatch[5]) {
|
||||||
var link = lookaheadMatch[5];
|
var link = lookaheadMatch[5];
|
||||||
e = config.formatterHelpers.isExternalLink(link) ? createExternalLink(w.output,link) : createTiddlyLink(w.output,link,false,null,w.isStatic,w.tiddler);
|
e = Formatter.isExternalLink(link) ? createExternalLink(w.output,link) : createTiddlyLink(w.output,link,false,null,w.isStatic,w.tiddler);
|
||||||
jQuery(e).addClass("imageLink");
|
jQuery(e).addClass("imageLink");
|
||||||
}
|
}
|
||||||
var img = createTiddlyElement(e,"img");
|
var img = createTiddlyElement(e,"img");
|
||||||
@ -557,11 +548,11 @@ config.formatters = [
|
|||||||
switch(w.matchText) {
|
switch(w.matchText) {
|
||||||
case "@@":
|
case "@@":
|
||||||
var e = createTiddlyElement(w.output,"span");
|
var e = createTiddlyElement(w.output,"span");
|
||||||
var styles = config.formatterHelpers.inlineCssHelper(w);
|
var styles = Formatter.inlineCssHelper(w);
|
||||||
if(styles.length == 0)
|
if(styles.length == 0)
|
||||||
e.className = "marked";
|
e.className = "marked";
|
||||||
else
|
else
|
||||||
config.formatterHelpers.applyCssHelper(e,styles);
|
Formatter.applyCssHelper(e,styles);
|
||||||
w.subWikifyTerm(e,/(@@)/mg);
|
w.subWikifyTerm(e,/(@@)/mg);
|
||||||
break;
|
break;
|
||||||
case "{{":
|
case "{{":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user