1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-11-25 19:54:52 +00:00

Refactor link handling to use a link macro

Instead of the special case handling for <a> tags
This commit is contained in:
Jeremy Ruston
2012-01-15 13:29:16 +00:00
parent c4f9efd894
commit ca6e32bd5a
5 changed files with 62 additions and 35 deletions

View File

@@ -90,6 +90,7 @@ var App = function() {
// Bit of a hack to set up the macros // Bit of a hack to set up the macros
this.store.installMacro(require("./macros/echo.js").macro); this.store.installMacro(require("./macros/echo.js").macro);
this.store.installMacro(require("./macros/info.js").macro); this.store.installMacro(require("./macros/info.js").macro);
this.store.installMacro(require("./macros/link.js").macro);
this.store.installMacro(require("./macros/list.js").macro); this.store.installMacro(require("./macros/list.js").macro);
this.store.installMacro(require("./macros/story.js").macro); this.store.installMacro(require("./macros/story.js").macro);
this.store.installMacro(require("./macros/tiddler.js").macro); this.store.installMacro(require("./macros/tiddler.js").macro);

View File

@@ -132,15 +132,15 @@ WikiTextParseTree.prototype.compileMacroCall = function(type,name,params) {
value: n value: n
}); });
} }
var wrapperTag = macro.wrapperTag || "div";
if(type === "text/html") { if(type === "text/html") {
this.pushString(utils.stitchElement("div",{ this.pushString(utils.stitchElement(wrapperTag,{
"data-tw-macro": name, "data-tw-macro": name
"data-tw-params": JSON.stringify(params)
})); }));
} }
this.output.push(macroCall); this.output.push(macroCall);
if(type === "text/html") { if(type === "text/html") {
this.pushString("</div>"); this.pushString("</" + wrapperTag + ">");
} }
}; };
@@ -160,11 +160,7 @@ WikiTextParseTree.prototype.compileElementHtml = function(element, options) {
tagBits.push(a + "=\"" + utils.htmlEncode(r) + "\""); tagBits.push(a + "=\"" + utils.htmlEncode(r) + "\"");
} }
} }
this.pushString("<" + tagBits.join(" ") + (options.selfClosing ? " /" : "")); this.pushString("<" + tagBits.join(" ") + (options.selfClosing ? " /" : "") + ">");
if(options.insertAfterAttributes) {
this.output.push(options.insertAfterAttributes);
}
this.pushString(">");
if(!options.selfClosing) { if(!options.selfClosing) {
if(element.children) { if(element.children) {
this.compileSubTreeHtml(element.children); this.compileSubTreeHtml(element.children);
@@ -189,21 +185,6 @@ WikiTextParseTree.prototype.compileSubTreeHtml = function(tree) {
case "macro": case "macro":
this.compileMacroCall("text/html",tree[t].name,tree[t].params); this.compileMacroCall("text/html",tree[t].name,tree[t].params);
break; break;
case "a":
this.compileElementHtml(tree[t],{
insertAfterAttributes: {
"type": "FunctionCall",
"name": {
"type": "PropertyAccess",
"base": {
"type": "Variable",
"name": "store"},
"name": "classesForLink"},
"arguments":[{
"type": "StringLiteral",
"value": tree[t].attributes.href}]}
});
break;
default: default:
this.compileElementHtml(tree[t]); this.compileElementHtml(tree[t]);
break; break;

View File

@@ -462,20 +462,23 @@ var rules = [
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 e = {type: "a", children: []}; var e = {type: "macro", name: "link", params: {
var text = lookaheadMatch[1]; target: {type: "string", value: null},
text: {type: "string", value: null}
}},
text = lookaheadMatch[1];
if(lookaheadMatch[3]) { if(lookaheadMatch[3]) {
// Pretty bracketted link // Pretty bracketted link
var link = lookaheadMatch[3]; var link = lookaheadMatch[3];
setAttr(e,"href",link); e.params.target.value = link;
w.addDependency(link); w.addDependency(link);
} else { } else {
// Simple bracketted link // Simple bracketted link
setAttr(e,"href",text); e.params.target.value = text;
w.addDependency(text); w.addDependency(text);
} }
e.params.text.value = text;
w.output.push(e); w.output.push(e);
e.children.push({type: "text", value: text});
w.nextMatch = this.lookaheadRegExp.lastIndex; w.nextMatch = this.lookaheadRegExp.lastIndex;
} }
} }
@@ -500,11 +503,14 @@ var rules = [
} }
} }
if(w.autoLinkWikiWords) { if(w.autoLinkWikiWords) {
var link = {type: "a", children: []}; var link = {type: "macro", name: "link", params: {
setAttr(link,"href",w.matchText); target: {type: "string", value: null},
text: {type: "string", value: null}
}};
link.params.target.value = w.matchText;
link.params.text.value = w.source.substring(w.matchStart,w.nextMatch);
w.addDependency(w.matchText); w.addDependency(w.matchText);
w.output.push(link); w.output.push(link);
w.outputText(link.children,w.matchStart,w.nextMatch);
} else { } else {
w.outputText(w.output,w.matchStart,w.nextMatch); w.outputText(w.output,w.matchStart,w.nextMatch);
} }
@@ -516,11 +522,14 @@ var rules = [
match: textPrimitives.urlPattern, match: textPrimitives.urlPattern,
handler: function(w) handler: function(w)
{ {
var e = {type: "a", children: []}; var e = {type: "macro", name: "link", params: {
setAttr(e,"href",w.matchText); target: {type: "string", value: null},
text: {type: "string", value: null}
}};
e.params.target.value = w.matchText;
e.params.text.value = w.source.substring(w.matchStart,w.nextMatch);
w.addDependency(w.matchText); w.addDependency(w.matchText);
w.output.push(e); w.output.push(e);
w.outputText(e.children,w.matchStart,w.nextMatch);
} }
}, },

35
js/macros/link.js Normal file
View File

@@ -0,0 +1,35 @@
/*\
title: js/macros/link.js
\*/
(function(){
/*jslint node: true */
"use strict";
var utils = require("../Utils.js");
exports.macro = {
name: "link",
wrapperTag: "span",
types: ["text/html","text/plain"],
params: {
target: {byName: "default", type: "tiddler", optional: false},
text: {byName: true, type: "text", optional: true}
},
handler: function(type,tiddler,store,params) {
var text = params.text || params.target;
if(type === "text/html") {
return utils.stitchElement("a",{
href: params.target
},{
content: utils.htmlEncode(text),
classNames: store.adjustClassesForLink([],params.target)
});
} else if (type === "text/plain") {
return text;
}
}
};
})();

View File

@@ -30,6 +30,7 @@ jsmodule: ../js/App.js
jsmodule: ../js/macros/echo.js jsmodule: ../js/macros/echo.js
jsmodule: ../js/macros/info.js jsmodule: ../js/macros/info.js
jsmodule: ../js/macros/link.js
jsmodule: ../js/macros/list.js jsmodule: ../js/macros/list.js
jsmodule: ../js/macros/story.js jsmodule: ../js/macros/story.js
jsmodule: ../js/macros/tiddler.js jsmodule: ../js/macros/tiddler.js