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) {
|
var CsvParser = function(type,text,options) {
|
||||||
// Table framework
|
// Table framework
|
||||||
this.tree = [{
|
this.tree = [{
|
||||||
"type": "element", "tag": "$scrollable", "children": [{
|
"type": "scrollable", "children": [{
|
||||||
"type": "element", "tag": "table", "children": [{
|
"type": "element", "tag": "table", "children": [{
|
||||||
"type": "element", "tag": "tbody", "children": []
|
"type": "element", "tag": "tbody", "children": []
|
||||||
}], "attributes": {
|
}], "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) {
|
var TextParser = function(type,text,options) {
|
||||||
this.tree = [{
|
this.tree = [{
|
||||||
type: "element",
|
type: "codeblock",
|
||||||
tag: "$codeblock",
|
|
||||||
attributes: {
|
attributes: {
|
||||||
code: {type: "string", value: text},
|
code: {type: "string", value: text},
|
||||||
language: {type: "string", value: type}
|
language: {type: "string", value: type}
|
||||||
|
@ -46,8 +46,7 @@ exports.parse = function() {
|
|||||||
}
|
}
|
||||||
// Return the $codeblock widget
|
// Return the $codeblock widget
|
||||||
return [{
|
return [{
|
||||||
type: "element",
|
type: "codeblock",
|
||||||
tag: "$codeblock",
|
|
||||||
attributes: {
|
attributes: {
|
||||||
code: {type: "string", value: text},
|
code: {type: "string", value: text},
|
||||||
language: {type: "string", value: this.match[1]}
|
language: {type: "string", value: this.match[1]}
|
||||||
|
@ -40,8 +40,7 @@ exports.parse = function() {
|
|||||||
classes = this.match[5];
|
classes = this.match[5];
|
||||||
// Return the list widget
|
// Return the list widget
|
||||||
var node = {
|
var node = {
|
||||||
type: "element",
|
type: "list",
|
||||||
tag: "$list",
|
|
||||||
attributes: {
|
attributes: {
|
||||||
filter: {type: "string", value: filter}
|
filter: {type: "string", value: filter}
|
||||||
},
|
},
|
||||||
|
@ -40,8 +40,7 @@ exports.parse = function() {
|
|||||||
classes = this.match[5];
|
classes = this.match[5];
|
||||||
// Return the list widget
|
// Return the list widget
|
||||||
var node = {
|
var node = {
|
||||||
type: "element",
|
type: "list",
|
||||||
tag: "$list",
|
|
||||||
attributes: {
|
attributes: {
|
||||||
filter: {type: "string", value: filter}
|
filter: {type: "string", value: filter}
|
||||||
}
|
}
|
||||||
|
@ -97,6 +97,9 @@ exports.parseTag = function(source,pos,options) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
node.tag = token.match[1];
|
node.tag = token.match[1];
|
||||||
|
if(node.tag.charAt(0) === "$") {
|
||||||
|
node.type = node.tag.substr(1);
|
||||||
|
}
|
||||||
pos = token.end;
|
pos = token.end;
|
||||||
// Process attributes
|
// Process attributes
|
||||||
var attribute = $tw.utils.parseAttribute(source,pos);
|
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
|
// Try to parse the candidate as a tag
|
||||||
var tag = this.parseTag(source,match.index,options);
|
var tag = this.parseTag(source,match.index,options);
|
||||||
// Return success
|
// Return success
|
||||||
if(tag && this.isLegalTag(tag.tag)) {
|
if(tag && this.isLegalTag(tag)) {
|
||||||
return tag;
|
return tag;
|
||||||
}
|
}
|
||||||
// Look for the next match
|
// Look for the next match
|
||||||
@ -154,11 +157,11 @@ exports.findNextTag = function(source,pos,options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.isLegalTag = function(tag) {
|
exports.isLegalTag = function(tag) {
|
||||||
// If it starts with a $ then we'll let anything go
|
// Widgets are always OK
|
||||||
if(tag.charAt(0) === "$") {
|
if(tag.type !== "element") {
|
||||||
return true;
|
return true;
|
||||||
// If it starts with a dash then it's not legal
|
// If it's an HTML tag that starts with a dash then it's not legal
|
||||||
} else if(tag.charAt(0) === "-") {
|
} else if(tag.tag.charAt(0) === "-") {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
// Otherwise it's OK
|
// Otherwise it's OK
|
||||||
|
@ -40,8 +40,7 @@ exports.parse = function() {
|
|||||||
// Move past the match
|
// Move past the match
|
||||||
this.parser.pos = this.nextImage.end;
|
this.parser.pos = this.nextImage.end;
|
||||||
var node = {
|
var node = {
|
||||||
type: "element",
|
type: "image",
|
||||||
tag: "$image",
|
|
||||||
attributes: this.nextImage.attributes
|
attributes: this.nextImage.attributes
|
||||||
};
|
};
|
||||||
return [node];
|
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) {
|
exports.parseImage = function(source,pos) {
|
||||||
var token,
|
var token,
|
||||||
node = {
|
node = {
|
||||||
type: "element",
|
type: "image",
|
||||||
name: "$image",
|
|
||||||
start: pos,
|
start: pos,
|
||||||
attributes: {}
|
attributes: {}
|
||||||
};
|
};
|
||||||
|
@ -53,8 +53,7 @@ exports.parse = function() {
|
|||||||
}];
|
}];
|
||||||
} else {
|
} else {
|
||||||
return [{
|
return [{
|
||||||
type: "element",
|
type: "link",
|
||||||
tag: "$link",
|
|
||||||
attributes: {
|
attributes: {
|
||||||
to: {type: "string", value: link}
|
to: {type: "string", value: link}
|
||||||
},
|
},
|
||||||
|
@ -36,8 +36,7 @@ exports.parse = function() {
|
|||||||
textRef = $tw.utils.trim(this.match[1]);
|
textRef = $tw.utils.trim(this.match[1]);
|
||||||
// Prepare the transclude widget
|
// Prepare the transclude widget
|
||||||
var transcludeNode = {
|
var transcludeNode = {
|
||||||
type: "element",
|
type: "transclude",
|
||||||
tag: "$transclude",
|
|
||||||
attributes: {},
|
attributes: {},
|
||||||
isBlock: true
|
isBlock: true
|
||||||
};
|
};
|
||||||
@ -48,8 +47,7 @@ exports.parse = function() {
|
|||||||
targetField = tr.field,
|
targetField = tr.field,
|
||||||
targetIndex = tr.index,
|
targetIndex = tr.index,
|
||||||
tiddlerNode = {
|
tiddlerNode = {
|
||||||
type: "element",
|
type: "tiddler",
|
||||||
tag: "$tiddler",
|
|
||||||
attributes: {
|
attributes: {
|
||||||
tiddler: {type: "string", value: targetTitle}
|
tiddler: {type: "string", value: targetTitle}
|
||||||
},
|
},
|
||||||
|
@ -34,8 +34,7 @@ exports.parse = function() {
|
|||||||
textRef = $tw.utils.trim(this.match[1]);
|
textRef = $tw.utils.trim(this.match[1]);
|
||||||
// Prepare the transclude widget
|
// Prepare the transclude widget
|
||||||
var transcludeNode = {
|
var transcludeNode = {
|
||||||
type: "element",
|
type: "transclude",
|
||||||
tag: "$transclude",
|
|
||||||
attributes: {}
|
attributes: {}
|
||||||
};
|
};
|
||||||
// Prepare the tiddler widget
|
// Prepare the tiddler widget
|
||||||
@ -45,8 +44,7 @@ exports.parse = function() {
|
|||||||
targetField = tr.field,
|
targetField = tr.field,
|
||||||
targetIndex = tr.index,
|
targetIndex = tr.index,
|
||||||
tiddlerNode = {
|
tiddlerNode = {
|
||||||
type: "element",
|
type: "tiddler",
|
||||||
tag: "$tiddler",
|
|
||||||
attributes: {
|
attributes: {
|
||||||
tiddler: {type: "string", value: targetTitle}
|
tiddler: {type: "string", value: targetTitle}
|
||||||
},
|
},
|
||||||
|
@ -64,8 +64,7 @@ exports.parse = function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return [{
|
return [{
|
||||||
type: "element",
|
type: "link",
|
||||||
tag: "$link",
|
|
||||||
attributes: {
|
attributes: {
|
||||||
to: {type: "string", value: linkText}
|
to: {type: "string", value: linkText}
|
||||||
},
|
},
|
||||||
|
@ -13,14 +13,12 @@ Parse tree utility functions.
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
exports.addAttributeToParseTreeNode = function(node,name,value) {
|
exports.addAttributeToParseTreeNode = function(node,name,value) {
|
||||||
if(node.type === "element") {
|
node.attributes = node.attributes || {};
|
||||||
node.attributes = node.attributes || {};
|
node.attributes[name] = {type: "string", value: value};
|
||||||
node.attributes[name] = {type: "string", value: value};
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getAttributeValueFromParseTreeNode = function(node,name,defaultValue) {
|
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 node.attributes[name].value;
|
||||||
}
|
}
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
@ -28,29 +26,25 @@ exports.getAttributeValueFromParseTreeNode = function(node,name,defaultValue) {
|
|||||||
|
|
||||||
exports.addClassToParseTreeNode = function(node,classString) {
|
exports.addClassToParseTreeNode = function(node,classString) {
|
||||||
var classes = [];
|
var classes = [];
|
||||||
if(node.type === "element") {
|
node.attributes = node.attributes || {};
|
||||||
node.attributes = node.attributes || {};
|
node.attributes["class"] = node.attributes["class"] || {type: "string", value: ""};
|
||||||
node.attributes["class"] = node.attributes["class"] || {type: "string", value: ""};
|
if(node.attributes["class"].type === "string") {
|
||||||
if(node.attributes["class"].type === "string") {
|
if(node.attributes["class"].value !== "") {
|
||||||
if(node.attributes["class"].value !== "") {
|
classes = node.attributes["class"].value.split(" ");
|
||||||
classes = node.attributes["class"].value.split(" ");
|
|
||||||
}
|
|
||||||
if(classString !== "") {
|
|
||||||
$tw.utils.pushTop(classes,classString.split(" "));
|
|
||||||
}
|
|
||||||
node.attributes["class"].value = classes.join(" ");
|
|
||||||
}
|
}
|
||||||
|
if(classString !== "") {
|
||||||
|
$tw.utils.pushTop(classes,classString.split(" "));
|
||||||
|
}
|
||||||
|
node.attributes["class"].value = classes.join(" ");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.addStyleToParseTreeNode = function(node,name,value) {
|
exports.addStyleToParseTreeNode = function(node,name,value) {
|
||||||
if(node.type === "element") {
|
|
||||||
node.attributes = node.attributes || {};
|
node.attributes = node.attributes || {};
|
||||||
node.attributes["style"] = node.attributes["style"] || {type: "string", value: ""};
|
node.attributes["style"] = node.attributes["style"] || {type: "string", value: ""};
|
||||||
if(node.attributes["style"].type === "string") {
|
if(node.attributes["style"].type === "string") {
|
||||||
node.attributes["style"].value += name + ":" + value + ";";
|
node.attributes["style"].value += name + ":" + value + ";";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.findParseTreeNode = function(nodeArray,search) {
|
exports.findParseTreeNode = function(nodeArray,search) {
|
||||||
|
@ -741,19 +741,6 @@ exports.old_parseTiddler = function(title,options) {
|
|||||||
}) : null;
|
}) : 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) {
|
var tweakMacroDefinition = function(nodeList) {
|
||||||
if(nodeList && nodeList[0] && nodeList[0].type === "macrodef") {
|
if(nodeList && nodeList[0] && nodeList[0].type === "macrodef") {
|
||||||
nodeList[0].type = "set";
|
nodeList[0].type = "set";
|
||||||
@ -770,8 +757,6 @@ var tweakMacroDefinition = function(nodeList) {
|
|||||||
var tweakParser = function(parser) {
|
var tweakParser = function(parser) {
|
||||||
// Move any macro definitions to contain the body tree
|
// Move any macro definitions to contain the body tree
|
||||||
tweakMacroDefinition(parser.tree);
|
tweakMacroDefinition(parser.tree);
|
||||||
// Tweak widgets
|
|
||||||
tweakParseTreeNodes(parser.tree);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.parseText = function(type,text,options) {
|
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 }
|
{ 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(
|
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(
|
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 }
|
{ 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(
|
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(
|
expect(parser.parseTag("< $mytag attrib1='something' attrib2=else thing>",0)).toEqual(
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
expect(parser.parseTag("<$mytag attrib3=<<myMacro one:two three:'four and five'>>>",0)).toEqual(
|
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(
|
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(
|
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(
|
expect(parse("<div attribute={{TiddlerTitle!!field}}>some text</div>")).toEqual(
|
||||||
|
Loading…
Reference in New Issue
Block a user