mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-12-24 17:10:29 +00:00
Get rid of the tweakParseTreeNode() hack
It’s an embarrassing hangover from a refactoring of the parsing mechanism last year.
This commit is contained in:
parent
92aa1f24be
commit
c9c1b0fbb4
@ -15,7 +15,7 @@ The CSV text parser processes CSV files into a table wrapped in a scrollable wid
|
||||
var CsvParser = function(type,text,options) {
|
||||
// Table framework
|
||||
this.tree = [{
|
||||
"type": "element", "tag": "$scrollable", "children": [{
|
||||
"type": "scrollable", "children": [{
|
||||
"type": "element", "tag": "table", "children": [{
|
||||
"type": "element", "tag": "tbody", "children": []
|
||||
}], "attributes": {
|
||||
|
@ -14,8 +14,7 @@ The plain text parser processes blocks of source text into a degenerate parse tr
|
||||
|
||||
var TextParser = function(type,text,options) {
|
||||
this.tree = [{
|
||||
type: "element",
|
||||
tag: "$codeblock",
|
||||
type: "codeblock",
|
||||
attributes: {
|
||||
code: {type: "string", value: text},
|
||||
language: {type: "string", value: type}
|
||||
|
@ -46,8 +46,7 @@ exports.parse = function() {
|
||||
}
|
||||
// Return the $codeblock widget
|
||||
return [{
|
||||
type: "element",
|
||||
tag: "$codeblock",
|
||||
type: "codeblock",
|
||||
attributes: {
|
||||
code: {type: "string", value: text},
|
||||
language: {type: "string", value: this.match[1]}
|
||||
|
@ -40,8 +40,7 @@ exports.parse = function() {
|
||||
classes = this.match[5];
|
||||
// Return the list widget
|
||||
var node = {
|
||||
type: "element",
|
||||
tag: "$list",
|
||||
type: "list",
|
||||
attributes: {
|
||||
filter: {type: "string", value: filter}
|
||||
},
|
||||
|
@ -40,8 +40,7 @@ exports.parse = function() {
|
||||
classes = this.match[5];
|
||||
// Return the list widget
|
||||
var node = {
|
||||
type: "element",
|
||||
tag: "$list",
|
||||
type: "list",
|
||||
attributes: {
|
||||
filter: {type: "string", value: filter}
|
||||
}
|
||||
|
@ -97,6 +97,9 @@ exports.parseTag = function(source,pos,options) {
|
||||
return null;
|
||||
}
|
||||
node.tag = token.match[1];
|
||||
if(node.tag.charAt(0) === "$") {
|
||||
node.type = node.tag.substr(1);
|
||||
}
|
||||
pos = token.end;
|
||||
// Process attributes
|
||||
var attribute = $tw.utils.parseAttribute(source,pos);
|
||||
@ -142,7 +145,7 @@ exports.findNextTag = function(source,pos,options) {
|
||||
// Try to parse the candidate as a tag
|
||||
var tag = this.parseTag(source,match.index,options);
|
||||
// Return success
|
||||
if(tag && this.isLegalTag(tag.tag)) {
|
||||
if(tag && this.isLegalTag(tag)) {
|
||||
return tag;
|
||||
}
|
||||
// Look for the next match
|
||||
@ -154,11 +157,11 @@ exports.findNextTag = function(source,pos,options) {
|
||||
};
|
||||
|
||||
exports.isLegalTag = function(tag) {
|
||||
// If it starts with a $ then we'll let anything go
|
||||
if(tag.charAt(0) === "$") {
|
||||
// Widgets are always OK
|
||||
if(tag.type !== "element") {
|
||||
return true;
|
||||
// If it starts with a dash then it's not legal
|
||||
} else if(tag.charAt(0) === "-") {
|
||||
// If it's an HTML tag that starts with a dash then it's not legal
|
||||
} else if(tag.tag.charAt(0) === "-") {
|
||||
return false;
|
||||
} else {
|
||||
// Otherwise it's OK
|
||||
|
@ -40,8 +40,7 @@ exports.parse = function() {
|
||||
// Move past the match
|
||||
this.parser.pos = this.nextImage.end;
|
||||
var node = {
|
||||
type: "element",
|
||||
tag: "$image",
|
||||
type: "image",
|
||||
attributes: this.nextImage.attributes
|
||||
};
|
||||
return [node];
|
||||
@ -72,13 +71,12 @@ exports.findNextImage = function(source,pos) {
|
||||
};
|
||||
|
||||
/*
|
||||
Look for an image at the specified position. Returns null if not found, otherwise returns {type: "element", name: "$image", attributes: [], isSelfClosing:, start:, end:,}
|
||||
Look for an image at the specified position. Returns null if not found, otherwise returns {type: "image", attributes: [], isSelfClosing:, start:, end:,}
|
||||
*/
|
||||
exports.parseImage = function(source,pos) {
|
||||
var token,
|
||||
node = {
|
||||
type: "element",
|
||||
name: "$image",
|
||||
type: "image",
|
||||
start: pos,
|
||||
attributes: {}
|
||||
};
|
||||
|
@ -53,8 +53,7 @@ exports.parse = function() {
|
||||
}];
|
||||
} else {
|
||||
return [{
|
||||
type: "element",
|
||||
tag: "$link",
|
||||
type: "link",
|
||||
attributes: {
|
||||
to: {type: "string", value: link}
|
||||
},
|
||||
|
@ -36,8 +36,7 @@ exports.parse = function() {
|
||||
textRef = $tw.utils.trim(this.match[1]);
|
||||
// Prepare the transclude widget
|
||||
var transcludeNode = {
|
||||
type: "element",
|
||||
tag: "$transclude",
|
||||
type: "transclude",
|
||||
attributes: {},
|
||||
isBlock: true
|
||||
};
|
||||
@ -48,8 +47,7 @@ exports.parse = function() {
|
||||
targetField = tr.field,
|
||||
targetIndex = tr.index,
|
||||
tiddlerNode = {
|
||||
type: "element",
|
||||
tag: "$tiddler",
|
||||
type: "tiddler",
|
||||
attributes: {
|
||||
tiddler: {type: "string", value: targetTitle}
|
||||
},
|
||||
|
@ -34,8 +34,7 @@ exports.parse = function() {
|
||||
textRef = $tw.utils.trim(this.match[1]);
|
||||
// Prepare the transclude widget
|
||||
var transcludeNode = {
|
||||
type: "element",
|
||||
tag: "$transclude",
|
||||
type: "transclude",
|
||||
attributes: {}
|
||||
};
|
||||
// Prepare the tiddler widget
|
||||
@ -45,8 +44,7 @@ exports.parse = function() {
|
||||
targetField = tr.field,
|
||||
targetIndex = tr.index,
|
||||
tiddlerNode = {
|
||||
type: "element",
|
||||
tag: "$tiddler",
|
||||
type: "tiddler",
|
||||
attributes: {
|
||||
tiddler: {type: "string", value: targetTitle}
|
||||
},
|
||||
|
@ -64,8 +64,7 @@ exports.parse = function() {
|
||||
}
|
||||
}
|
||||
return [{
|
||||
type: "element",
|
||||
tag: "$link",
|
||||
type: "link",
|
||||
attributes: {
|
||||
to: {type: "string", value: linkText}
|
||||
},
|
||||
|
@ -13,14 +13,12 @@ Parse tree utility functions.
|
||||
"use strict";
|
||||
|
||||
exports.addAttributeToParseTreeNode = function(node,name,value) {
|
||||
if(node.type === "element") {
|
||||
node.attributes = node.attributes || {};
|
||||
node.attributes[name] = {type: "string", value: value};
|
||||
}
|
||||
node.attributes = node.attributes || {};
|
||||
node.attributes[name] = {type: "string", value: value};
|
||||
};
|
||||
|
||||
exports.getAttributeValueFromParseTreeNode = function(node,name,defaultValue) {
|
||||
if(node.type === "element" && node.attributes && node.attributes[name] && node.attributes[name].value !== undefined) {
|
||||
if(node.attributes && node.attributes[name] && node.attributes[name].value !== undefined) {
|
||||
return node.attributes[name].value;
|
||||
}
|
||||
return defaultValue;
|
||||
@ -28,29 +26,25 @@ exports.getAttributeValueFromParseTreeNode = function(node,name,defaultValue) {
|
||||
|
||||
exports.addClassToParseTreeNode = function(node,classString) {
|
||||
var classes = [];
|
||||
if(node.type === "element") {
|
||||
node.attributes = node.attributes || {};
|
||||
node.attributes["class"] = node.attributes["class"] || {type: "string", value: ""};
|
||||
if(node.attributes["class"].type === "string") {
|
||||
if(node.attributes["class"].value !== "") {
|
||||
classes = node.attributes["class"].value.split(" ");
|
||||
}
|
||||
if(classString !== "") {
|
||||
$tw.utils.pushTop(classes,classString.split(" "));
|
||||
}
|
||||
node.attributes["class"].value = classes.join(" ");
|
||||
node.attributes = node.attributes || {};
|
||||
node.attributes["class"] = node.attributes["class"] || {type: "string", value: ""};
|
||||
if(node.attributes["class"].type === "string") {
|
||||
if(node.attributes["class"].value !== "") {
|
||||
classes = node.attributes["class"].value.split(" ");
|
||||
}
|
||||
if(classString !== "") {
|
||||
$tw.utils.pushTop(classes,classString.split(" "));
|
||||
}
|
||||
node.attributes["class"].value = classes.join(" ");
|
||||
}
|
||||
};
|
||||
|
||||
exports.addStyleToParseTreeNode = function(node,name,value) {
|
||||
if(node.type === "element") {
|
||||
node.attributes = node.attributes || {};
|
||||
node.attributes["style"] = node.attributes["style"] || {type: "string", value: ""};
|
||||
if(node.attributes["style"].type === "string") {
|
||||
node.attributes["style"].value += name + ":" + value + ";";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
exports.findParseTreeNode = function(nodeArray,search) {
|
||||
|
@ -741,19 +741,6 @@ exports.old_parseTiddler = function(title,options) {
|
||||
}) : null;
|
||||
};
|
||||
|
||||
// We need to tweak parse trees generated by the existing parser because of the change from {type:"element",tag:"$tiddler",...} to {type:"tiddler",...}
|
||||
var tweakParseTreeNode = function(node) {
|
||||
if(node.type === "element" && node.tag.charAt(0) === "$") {
|
||||
node.type = node.tag.substr(1);
|
||||
delete node.tag;
|
||||
}
|
||||
tweakParseTreeNodes(node.children);
|
||||
};
|
||||
|
||||
var tweakParseTreeNodes = function(nodeList) {
|
||||
$tw.utils.each(nodeList,tweakParseTreeNode);
|
||||
};
|
||||
|
||||
var tweakMacroDefinition = function(nodeList) {
|
||||
if(nodeList && nodeList[0] && nodeList[0].type === "macrodef") {
|
||||
nodeList[0].type = "set";
|
||||
@ -770,8 +757,6 @@ var tweakMacroDefinition = function(nodeList) {
|
||||
var tweakParser = function(parser) {
|
||||
// Move any macro definitions to contain the body tree
|
||||
tweakMacroDefinition(parser.tree);
|
||||
// Tweak widgets
|
||||
tweakParseTreeNodes(parser.tree);
|
||||
};
|
||||
|
||||
exports.parseText = function(type,text,options) {
|
||||
|
@ -165,22 +165,22 @@ describe("HTML tag new parser tests", function() {
|
||||
{ type : 'element', start : 0, attributes : { attrib1 : { type : 'string', value : 'true', start : 6, name : 'attrib1', end : 14 } }, tag : 'mytag', isSelfClosing : true, end : 16 }
|
||||
);
|
||||
expect(parser.parseTag("<$view field=\"title\" format=\"link\"/>",0)).toEqual(
|
||||
{ type : 'element', start : 0, attributes : { field : { start : 6, name : 'field', type : 'string', value : 'title', end : 20 }, format : { start : 20, name : 'format', type : 'string', value : 'link', end : 34 } }, tag : '$view', isSelfClosing : true, end : 36 }
|
||||
{ type : 'view', start : 0, attributes : { field : { start : 6, name : 'field', type : 'string', value : 'title', end : 20 }, format : { start : 20, name : 'format', type : 'string', value : 'link', end : 34 } }, tag : '$view', isSelfClosing : true, end : 36 }
|
||||
);
|
||||
expect(parser.parseTag("<mytag attrib1='something'>",0)).toEqual(
|
||||
{ type : 'element', start : 0, attributes : { attrib1 : { type : 'string', start : 6, name : 'attrib1', value : 'something', end : 26 } }, tag : 'mytag', end : 27 }
|
||||
);
|
||||
expect(parser.parseTag("<$mytag attrib1='something' attrib2=else thing>",0)).toEqual(
|
||||
{ type : 'element', start : 0, attributes : { attrib1 : { type : 'string', start : 7, name : 'attrib1', value : 'something', end : 27 }, attrib2 : { type : 'string', start : 27, name : 'attrib2', value : 'else', end : 40 }, thing : { type : 'string', start : 40, name : 'thing', value : 'true', end : 46 } }, tag : '$mytag', end : 47 }
|
||||
{ type : 'mytag', start : 0, attributes : { attrib1 : { type : 'string', start : 7, name : 'attrib1', value : 'something', end : 27 }, attrib2 : { type : 'string', start : 27, name : 'attrib2', value : 'else', end : 40 }, thing : { type : 'string', start : 40, name : 'thing', value : 'true', end : 46 } }, tag : '$mytag', end : 47 }
|
||||
);
|
||||
expect(parser.parseTag("< $mytag attrib1='something' attrib2=else thing>",0)).toEqual(
|
||||
null
|
||||
);
|
||||
expect(parser.parseTag("<$mytag attrib3=<<myMacro one:two three:'four and five'>>>",0)).toEqual(
|
||||
{ type : 'element', start : 0, attributes : { attrib3 : { type : 'macro', start : 7, name : 'attrib3', value : { type : 'macrocall', start : 16, params : [ { type : 'macro-parameter', start : 25, value : 'two', name : 'one', end : 33 }, { type : 'macro-parameter', start : 33, value : 'four and five', name : 'three', end : 55 } ], name : 'myMacro', end : 57 }, end : 57 } }, tag : '$mytag', end : 58 }
|
||||
{ type : 'mytag', start : 0, attributes : { attrib3 : { type : 'macro', start : 7, name : 'attrib3', value : { type : 'macrocall', start : 16, params : [ { type : 'macro-parameter', start : 25, value : 'two', name : 'one', end : 33 }, { type : 'macro-parameter', start : 33, value : 'four and five', name : 'three', end : 55 } ], name : 'myMacro', end : 57 }, end : 57 } }, tag : '$mytag', end : 58 }
|
||||
);
|
||||
expect(parser.parseTag("<$mytag attrib1='something' attrib2=else thing attrib3=<<myMacro one:two three:'four and five'>>>",0)).toEqual(
|
||||
{ type : 'element', start : 0, attributes : { attrib1 : { type : 'string', start : 7, name : 'attrib1', value : 'something', end : 27 }, attrib2 : { type : 'string', start : 27, name : 'attrib2', value : 'else', end : 40 }, thing : { type : 'string', start : 40, name : 'thing', value : 'true', end : 47 }, attrib3 : { type : 'macro', start : 47, name : 'attrib3', value : { type : 'macrocall', start : 55, params : [ { type : 'macro-parameter', start : 64, value : 'two', name : 'one', end : 72 }, { type : 'macro-parameter', start : 72, value : 'four and five', name : 'three', end : 94 } ], name : 'myMacro', end : 96 }, end : 96 } }, tag : '$mytag', end : 97 }
|
||||
{ type : 'mytag', start : 0, attributes : { attrib1 : { type : 'string', start : 7, name : 'attrib1', value : 'something', end : 27 }, attrib2 : { type : 'string', start : 27, name : 'attrib2', value : 'else', end : 40 }, thing : { type : 'string', start : 40, name : 'thing', value : 'true', end : 47 }, attrib3 : { type : 'macro', start : 47, name : 'attrib3', value : { type : 'macrocall', start : 55, params : [ { type : 'macro-parameter', start : 64, value : 'two', name : 'one', end : 72 }, { type : 'macro-parameter', start : 72, value : 'four and five', name : 'three', end : 94 } ], name : 'myMacro', end : 96 }, end : 96 } }, tag : '$mytag', end : 97 }
|
||||
);
|
||||
});
|
||||
|
||||
|
@ -70,7 +70,7 @@ describe("WikiText parser tests", function() {
|
||||
);
|
||||
expect(parse("<$reveal state='$:/temp/search' type='nomatch' text=''>")).toEqual(
|
||||
|
||||
[ { type : 'element', tag : 'p', children : [ { type : 'reveal', start : 0, attributes : { state : { start : 8, name : 'state', type : 'string', value : '$:/temp/search', end : 31 }, type : { start : 31, name : 'type', type : 'string', value : 'nomatch', end : 46 }, text : { start : 46, name : 'text', type : 'string', value : '', end : 54 } }, end : 55, isBlock : false, children : [ ] } ] } ]
|
||||
[ { type : 'element', tag : 'p', children : [ { type : 'reveal', tag: '$reveal', start : 0, attributes : { state : { start : 8, name : 'state', type : 'string', value : '$:/temp/search', end : 31 }, type : { start : 31, name : 'type', type : 'string', value : 'nomatch', end : 46 }, text : { start : 46, name : 'text', type : 'string', value : '', end : 54 } }, end : 55, isBlock : false, children : [ ] } ] } ]
|
||||
|
||||
);
|
||||
expect(parse("<div attribute={{TiddlerTitle!!field}}>some text</div>")).toEqual(
|
||||
|
Loading…
Reference in New Issue
Block a user