From 3d1a8ee7033d2ea14ea2676824e2731baaa9a7dd Mon Sep 17 00:00:00 2001 From: buggyj Date: Tue, 15 Apr 2014 12:45:23 +0100 Subject: [PATCH] added updated tw2 parser --- plugins/tiddlywiki/tw2parser/plugin.bundle | 7 - plugins/tiddlywiki/tw2parser/plugin.info | 8 + .../tiddlywiki/tw2parser/wikitextparser.js | 32 +-- plugins/tiddlywiki/tw2parser/wikitextrules.js | 226 ++++++++++++------ 4 files changed, 177 insertions(+), 96 deletions(-) delete mode 100644 plugins/tiddlywiki/tw2parser/plugin.bundle create mode 100644 plugins/tiddlywiki/tw2parser/plugin.info diff --git a/plugins/tiddlywiki/tw2parser/plugin.bundle b/plugins/tiddlywiki/tw2parser/plugin.bundle deleted file mode 100644 index 23ffdfedd..000000000 --- a/plugins/tiddlywiki/tw2parser/plugin.bundle +++ /dev/null @@ -1,7 +0,0 @@ -{ - "title": "$:/plugins/tiddlywiki/tw2parser", - "description": "A parser for TiddlyWiki 2.x.x style wikitext", - "author": "JeremyRuston", - "version": "0.0.0", - "coreVersion": ">=5.0.0" -} diff --git a/plugins/tiddlywiki/tw2parser/plugin.info b/plugins/tiddlywiki/tw2parser/plugin.info new file mode 100644 index 000000000..4d9ec60c5 --- /dev/null +++ b/plugins/tiddlywiki/tw2parser/plugin.info @@ -0,0 +1,8 @@ +{ + "title": "$:/plugins/tiddlywiki/tw2parser", + "description": "legacy parser", + "author": "JeremyRuston, JeffreyWilkinson", + "version": "0.0.1", + "core-version": ">=5.0.0", + "plugin-type": "plugin" +} diff --git a/plugins/tiddlywiki/tw2parser/wikitextparser.js b/plugins/tiddlywiki/tw2parser/wikitextparser.js index a78c22717..f04bdc5ec 100644 --- a/plugins/tiddlywiki/tw2parser/wikitextparser.js +++ b/plugins/tiddlywiki/tw2parser/wikitextparser.js @@ -48,11 +48,26 @@ Planned: extraMacros: An array of additional macro handlers to add */ -var WikiTextParser = function(options) { +var WikiTextParser = function(type,text,options) { this.wiki = options.wiki; this.autoLinkWikiWords = true; + this.installRules(); + text = text || "no text"; + this.source = text; + this.nextMatch = 0; + this.children = []; + //this.children.push({type: "text",text:"hello to the queen"}); + this.tree =[]; + this.output = null; + this.subWikify(this.children); + this.tree = [{ + type: "element", + tag: "div", + children:this.children + }]; }; + WikiTextParser.prototype.installRules = function() { var rules = require("./wikitextrules.js").rules, pattern = []; @@ -63,23 +78,10 @@ WikiTextParser.prototype.installRules = function() { this.rulesRegExp = new RegExp(pattern.join("|"),"mg"); }; -WikiTextParser.prototype.parse = function(type,text) { - text = text || ""; - this.source = text; - this.nextMatch = 0; - this.children = []; - this.dependencies = new $tw.Dependencies(); - this.output = null; - this.subWikify(this.children); - var tree = new $tw.Renderer(this.children,this.dependencies); - this.source = null; - this.children = null; - return tree; -}; WikiTextParser.prototype.outputText = function(place,startPos,endPos) { if(startPos < endPos) { - place.push($tw.Tree.Text(this.source.substring(startPos,endPos))); + place.push({type: "text",text:this.source.substring(startPos,endPos)}); } }; diff --git a/plugins/tiddlywiki/tw2parser/wikitextrules.js b/plugins/tiddlywiki/tw2parser/wikitextrules.js index d32c145fa..aa6e35613 100755 --- a/plugins/tiddlywiki/tw2parser/wikitextrules.js +++ b/plugins/tiddlywiki/tw2parser/wikitextrules.js @@ -51,7 +51,7 @@ var setAttr = function(node,attr,value) { if(!node.attributes) { node.attributes = {}; } - node.attributes[attr] = value; + node.attributes[attr] ={type: "string", value:value} ; }; var inlineCssHelper = function(w) { @@ -80,17 +80,14 @@ var inlineCssHelper = function(w) { }; var applyCssHelper = function(e,styles) { + 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; + $tw.utils.addStyleToParseTreeNode(e,$tw.utils.roundTripPropertyName(styles[t].style),styles[t].value); } } + }; var enclosedTextHelper = function(w) { @@ -98,23 +95,40 @@ var enclosedTextHelper = function(w) { var lookaheadMatch = this.lookaheadRegExp.exec(w.source); if(lookaheadMatch && lookaheadMatch.index == w.matchStart) { var text = lookaheadMatch[1]; - w.output.push($tw.Tree.Element(this.element,null,[$tw.Tree.Text(text)])); + w.output.push({type:"element",tag:this.element, + children:[{type: "text",text: lookaheadMatch[1]}]}); w.nextMatch = lookaheadMatch.index + lookaheadMatch[0].length; } }; -var insertMacroCall = function(w,output,name,params,content) { - if(name in w.wiki.macros) { - var macroNode = $tw.Tree.Macro(name,{ - srcParams: params, - content: content, - wiki: w.wiki - }); - w.dependencies.mergeDependencies(macroNode.dependencies); - output.push(macroNode); +var insertMacroCall = function(w,output,macroName,paramString) { + var params = [], + reParam = /\s*(?:([A-Za-z0-9\-_]+)\s*:)?(?:\s*(?:"([^"]*)"|'([^']*)'|\[\[([^\]]*)\]\]|([^"'\s]+)))/mg, + paramMatch = reParam.exec(paramString); + while(paramMatch) { + // Process this parameter + var paramInfo = { + value: paramMatch[2] || paramMatch[3] || paramMatch[4] || paramMatch[5] + }; + if(paramMatch[1]) { + paramInfo.name = paramMatch[1]; + } + params.push(paramInfo); + // Find the next match + paramMatch = reParam.exec(paramString); } -}; + output.push({ + type: "macrocall", + name: macroName, + params: params + }); +} + +var isLinkExternal = function(to) { + var externalRegExp = /(?:file|http|https|mailto|ftp|irc|news|data|skype):[^\s'"]+(?:\/|\b)/i; + return externalRegExp.test(to); +}; var rules = [ { name: "table", @@ -126,7 +140,9 @@ var rules = [ rowTypes: {"c":"caption", "h":"thead", "":"tbody", "f":"tfoot"}, handler: function(w) { - var table = $tw.Tree.Element("table",{"class": "table"},[]); + var table = {type:"element",tag:"table",attributes: {"class": {type: "string", value:"table"}}, + children: []}; + w.output.push(table); var prevColumns = []; var currRowType = null; @@ -142,7 +158,7 @@ var rules = [ w.nextMatch += lookaheadMatch[0].length+1; } else { if(nextRowType != currRowType) { - rowContainer = $tw.Tree.Element(this.rowTypes[nextRowType],{},[]); + rowContainer = {type:"element",tag:this.rowTypes[nextRowType],children: []}; table.children.push(rowContainer); currRowType = nextRowType; } @@ -154,11 +170,14 @@ var rules = [ table.children.pop(); // Take rowContainer out of the children array table.children.splice(0,0,rowContainer); // Insert it at the bottom } + rowContainer.attributes={}; rowContainer.attributes.align = rowCount === 0 ? "top" : "bottom"; w.subWikifyTerm(rowContainer.children,this.rowTermRegExp); } else { - var theRow = $tw.Tree.Element("tr",{},[]); - theRow.attributes["class"] = rowCount%2 ? "oddRow" : "evenRow"; + var theRow = {type:"element",tag:"tr", + attributes: {"class": {type: "string", value:rowCount%2 ? "oddRow" : "evenRow"}}, + children: []}; + rowContainer.children.push(theRow); this.rowHandler(w,theRow.children,prevColumns); rowCount++; @@ -179,15 +198,16 @@ var rules = [ if(cellMatch[1] == "~") { // Rowspan var last = prevColumns[col]; - if(last) { - last.rowSpanCount++; - last.element.attributes.rowspan = last.rowSpanCount; - last.element.attributes.valign = "center"; - if(colSpanCount > 1) { - last.element.attributes.colspan = colSpanCount; - colSpanCount = 1; - } + if(last) { + last.rowSpanCount++; + $tw.utils.addAttributeToParseTreeNode(last.element,"rowspan",last.rowSpanCount); + var vAlign = $tw.utils.getAttributeValueFromParseTreeNode(last.element,"valign","center"); + $tw.utils.addAttributeToParseTreeNode(last.element,"valign",vAlign); + if(colSpanCount > 1) { + $tw.utils.addAttributeToParseTreeNode(last.element,"colspan",colSpanCount); + colSpanCount = 1; } + } w.nextMatch = this.cellRegExp.lastIndex-1; } else if(cellMatch[1] == ">") { // Colspan @@ -213,25 +233,26 @@ var rules = [ } var cell; if(chr == "!") { - cell = $tw.Tree.Element("th",{},[]); + cell = {type:"element",tag:"th",children: []}; e.push(cell); w.nextMatch++; } else { - cell = $tw.Tree.Element("td",{},[]); + cell = {type:"element",tag:"td",children: []}; e.push(cell); } prevCell = cell; prevColumns[col] = {rowSpanCount:1,element:cell}; if(colSpanCount > 1) { - cell.attributes.colspan = colSpanCount; + $tw.utils.addAttributeToParseTreeNode(cell,"colspan",colSpanCount); colSpanCount = 1; } applyCssHelper(cell,styles); w.subWikifyTerm(cell.children,this.cellTermRegExp); + if (!cell.attributes) cell.attributes ={}; if(w.matchText.substr(w.matchText.length-2,1) == " ") // spaceRight - cell.attributes.align = spaceLeft ? "center" : "left"; + $tw.utils.addAttributeToParseTreeNode(cell,"align",spaceLeft ? "center" : "left"); else if(spaceLeft) - cell.attributes.align = "right"; + $tw.utils.addAttributeToParseTreeNode(cell,"align","right"); w.nextMatch--; } col++; @@ -247,7 +268,7 @@ var rules = [ termRegExp: /(\n)/mg, handler: function(w) { - var e = $tw.Tree.Element("h" + w.matchLength,{},[]); + var e = {type:"element",tag:"h" + w.matchLength,children: []}; w.output.push(e); w.subWikifyTerm(e.children,this.termRegExp); } @@ -291,7 +312,7 @@ var rules = [ if(currLevel !== 0 && target.children) { target = target.children[target.children.length-1]; } - e = $tw.Tree.Element(listType,{},[]); + e = {type:"element",tag:listType,children: []}; target.push(e); stack.push(e.children); } @@ -303,13 +324,13 @@ var rules = [ stack.pop(); } else if(listLevel == currLevel && listType != currType) { stack.pop(); - e = $tw.Tree.Element(listType,{},[]); + e = {type:"element",tag:listType,children: []}; stack[stack.length-1].push(e); stack.push(e.children); } currLevel = listLevel; currType = listType; - e = $tw.Tree.Element(itemType,{},[]); + e = {type:"element",tag:itemType,children: []}; stack[stack.length-1].push(e); w.subWikifyTerm(e.children,this.termRegExp); this.lookaheadRegExp.lastIndex = w.nextMatch; @@ -324,7 +345,7 @@ var rules = [ termRegExp: /(^<<<(\n|$))/mg, element: "blockquote", handler: function(w) { - var e = $tw.Tree.Element(this.element,{},[]); + var e = {type:"element",tag:this.element,children: []}; w.output.push(e); w.subWikifyTerm(e.children,this.termRegExp); } @@ -338,23 +359,32 @@ var rules = [ element: "blockquote", handler: function(w) { - var stack = [w.output]; + var stack = []; var currLevel = 0; var newLevel = w.matchLength; var t,matched,e; do { if(newLevel > currLevel) { for(t=currLevel; tnewLevel; t--) stack.pop(); } currLevel = newLevel; - w.subWikifyTerm(stack[stack.length-1],this.termRegExp); - stack[stack.length-1].push($tw.Tree.Element("br")); + w.subWikifyTerm(stack[stack.length-1].children,this.termRegExp); + stack[stack.length-1].children.push({type:"element",tag:"br"}); + //e.push({type:"element",tag:"br"}); + this.lookaheadRegExp.lastIndex = w.nextMatch; var lookaheadMatch = this.lookaheadRegExp.exec(w.source); matched = lookaheadMatch && lookaheadMatch.index == w.nextMatch; @@ -371,7 +401,7 @@ var rules = [ match: "^----+$\\n?|
\\n?", handler: function(w) { - w.output.push($tw.Tree.Element("hr")); + w.output.push({type:"element",tag:"hr"}); } }, @@ -400,7 +430,7 @@ var rules = [ enclosedTextHelper.call(this,w); } }, - +/* { name: "typedBlock", match: "^\\$\\$\\$(?:.*)\\n", @@ -429,7 +459,7 @@ var rules = [ } } }, - +*/ { name: "wikifyComment", match: "^(?:/\\*\\*\\*|