From 9075b8a020d610fc7a93df70133ca2fb3c7f040a Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Mon, 16 Jan 2012 08:58:51 +0000 Subject: [PATCH] Mapped wiki image markup to image macro --- js/WikiTextRules.js | 48 ++++++++++++++------------- js/macros/image.js | 30 ++++++++++++----- js/macros/link.js | 2 +- tiddlywiki5/tiddlers/Introduction.tid | 4 +++ 4 files changed, 52 insertions(+), 32 deletions(-) diff --git a/js/WikiTextRules.js b/js/WikiTextRules.js index 5a457185a..4d8f861a2 100755 --- a/js/WikiTextRules.js +++ b/js/WikiTextRules.js @@ -507,7 +507,7 @@ var rules = [ } if(w.autoLinkWikiWords) { var link = {type: "macro", name: "link", params: { - target: {type: "string", value: w.matchText}, + target: {type: "string", value: w.matchText} }, children: [{ type: "text", @@ -546,27 +546,29 @@ var rules = [ handler: function(w) { this.lookaheadRegExp.lastIndex = w.matchStart; - var lookaheadMatch = this.lookaheadRegExp.exec(w.source); + var lookaheadMatch = this.lookaheadRegExp.exec(w.source), + image = {type: "macro", name: "image", params: { + src: {type: "string", value: ""} + }}, + link = {type: "macro", name: "link", params: { + target: {type: "string", value: ""} + }, children: [image]}; if(lookaheadMatch && lookaheadMatch.index == w.matchStart) { - var e = w.output; - if(lookaheadMatch[5]) { - var link = lookaheadMatch[5], - t = {type: "a", children: []}; - setAttr(t,"href",link); - w.output.push(t); - e = t.children; + if(lookaheadMatch[1]) { + image.params.alignment = {type: "string", value: "left"}; + } else if(lookaheadMatch[2]) { + image.params.alignment = {type: "string", value: "right"}; } - var img = {type: "img"}; - e.push(img); - if(lookaheadMatch[1]) - setAttr(img,"align","left"); - else if(lookaheadMatch[2]) - setAttr(img,"align","right"); if(lookaheadMatch[3]) { - setAttr(img,"title",lookaheadMatch[3]); - setAttr(img,"alt",lookaheadMatch[3]); + image.params.text = {type: "string", value: lookaheadMatch[3]}; + } + image.params.src.value = lookaheadMatch[4]; + if(lookaheadMatch[5]) { + link.params.target.value = lookaheadMatch[5]; + w.output.push(link); + } else { + w.output.push(image); } - setAttr(img,"src",lookaheadMatch[4]); w.nextMatch = this.lookaheadRegExp.lastIndex; } } @@ -605,7 +607,7 @@ var rules = [ match: "''|//|__|\\^\\^|~~|--(?!\\s|$)|\\{\\{\\{|`", handler: function(w) { - var e; + var e,lookaheadRegExp,lookaheadMatch; switch(w.matchText) { case "''": e = {type: "strong", children: []}; @@ -638,9 +640,9 @@ var rules = [ w.subWikifyTerm(e.children,/(--)/mg); break; case "`": - var lookaheadRegExp = /`((?:.|\n)*?)`/mg; + lookaheadRegExp = /`((?:.|\n)*?)`/mg; lookaheadRegExp.lastIndex = w.matchStart; - var lookaheadMatch = lookaheadRegExp.exec(w.source); + lookaheadMatch = lookaheadRegExp.exec(w.source); if(lookaheadMatch && lookaheadMatch.index == w.matchStart) { w.output.push({type: "code", children: [ {type: "text", value: lookaheadMatch[1]} @@ -649,9 +651,9 @@ var rules = [ } break; case "{{{": - var lookaheadRegExp = /\{\{\{((?:.|\n)*?)\}\}\}/mg; + lookaheadRegExp = /\{\{\{((?:.|\n)*?)\}\}\}/mg; lookaheadRegExp.lastIndex = w.matchStart; - var lookaheadMatch = lookaheadRegExp.exec(w.source); + lookaheadMatch = lookaheadRegExp.exec(w.source); if(lookaheadMatch && lookaheadMatch.index == w.matchStart) { w.output.push({type: "code", children: [ {type: "text", value: lookaheadMatch[1]} diff --git a/js/macros/image.js b/js/macros/image.js index 53874dd88..46f3e88cf 100644 --- a/js/macros/image.js +++ b/js/macros/image.js @@ -14,17 +14,31 @@ exports.macro = { types: ["text/html","text/plain"], params: { src: {byName: "default", type: "tiddler", optional: false}, - text: {byName: true, type: "text", optional: true} + text: {byName: true, type: "text", optional: true}, + alignment: {byName: true, type: "text", optional: true} }, handler: function(type,tiddler,store,params) { if(type === "text/html") { - return utils.stitchElement("img",{ - href: params.src, - alt: params.text, - title: params.text - },{ - selfClosing: true - }); + if(store.tiddlerExists(params.src)) { + if(params.text) { + return utils.stitchElement("div",{ + alt: params.text, + title: params.text + },{ + content: store.renderTiddler(type,params.src) + }); + } else { + return store.renderTiddler(type,params.src); + } + } else { + return utils.stitchElement("img",{ + href: params.src, + alt: params.text, + title: params.text + },{ + selfClosing: true + }); + } } else if (type === "text/plain") { return params.text ? params.text : params.src; } diff --git a/js/macros/link.js b/js/macros/link.js index e8cda00e8..39b3bd26f 100644 --- a/js/macros/link.js +++ b/js/macros/link.js @@ -25,7 +25,7 @@ exports.macro = { classNames: store.adjustClassesForLink([],params.target) }); } else if (type === "text/plain") { - return ""; + return content; } } }; diff --git a/tiddlywiki5/tiddlers/Introduction.tid b/tiddlywiki5/tiddlers/Introduction.tid index 690e21b66..cc944bace 100644 --- a/tiddlywiki5/tiddlers/Introduction.tid +++ b/tiddlywiki5/tiddlers/Introduction.tid @@ -1,6 +1,10 @@ title: Introduction <> +<> +[img[Motovun Jack.jpg]] +[img[tooltip|Motovun Jack.jpg]] +[img[tooltip|Motovun Jack.jpg][http://google.com/]] TiddlyWiki5 gains new capabilities through a [[completely rebuilt architecture|TiddlyWikiArchitecture]] using the latest features of HTML5 and node.js. It runs natively under node.js, and can also use its own components to construct a version of itself that works entirely within the browser, just as TiddlyWiki has always done.