From 9df36a2b15a5f1c66a647819e96dea4b273d0b1c Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Sun, 4 Aug 2024 00:29:33 +0800 Subject: [PATCH] refactor: ' -> " --- .../parsers/wikiparser/rules/conditional.js | 10 +++---- core/modules/parsers/wikiparser/rules/list.js | 27 ++++++++++------- .../wikiparser/rules/macrocallblock.js | 30 ++++++++++++------- .../wikiparser/rules/macrocallinline.js | 28 +++++++++++------ .../parsers/wikiparser/rules/macrodef.js | 2 +- .../parsers/wikiparser/rules/parameters.js | 2 +- .../tiddlers/tests/test-wikitext-serialize.js | 8 ++--- plugins/tiddlywiki/jasmine/jasmine-plugin.js | 2 +- 8 files changed, 68 insertions(+), 41 deletions(-) diff --git a/core/modules/parsers/wikiparser/rules/conditional.js b/core/modules/parsers/wikiparser/rules/conditional.js index 88d243d0e..3a4486cf2 100644 --- a/core/modules/parsers/wikiparser/rules/conditional.js +++ b/core/modules/parsers/wikiparser/rules/conditional.js @@ -121,24 +121,24 @@ exports.serialize = function(tree, serialize) { var filterCondition = tree.attributes.filter.value; var ifClause = serialize(tree.children[0].children); var elseClause = tree.children[1].children; - var serialized = '<% if ' + filterCondition + '%>' + ifClause; + var serialized = "<% if " + filterCondition + "%>" + ifClause; if(elseClause && elseClause.length > 0) { for(var i = 0; i < elseClause.length; i++) { - if(elseClause[i].type === 'list' && elseClause[i].attributes.filter) { + if(elseClause[i].type === "list" && elseClause[i].attributes.filter) { // Handle elseif clause var elseifCondition = elseClause[i].attributes.filter.value; var elseifClause = serialize(elseClause[i].children[0]); - serialized += '<% elseif ' + elseifCondition + '%>' + elseifClause; + serialized += "<% elseif " + elseifCondition + "%>" + elseifClause; } if(elseClause[i].children[1]) { var elseClauseText = serialize(elseClause[i].children[1]); - serialized += '<% else %>' + elseClauseText; + serialized += "<% else %>" + elseClauseText; } } } - serialized += '<% endif %>'; + serialized += "<% endif %>"; return serialized; }; diff --git a/core/modules/parsers/wikiparser/rules/list.js b/core/modules/parsers/wikiparser/rules/list.js index 704ad60ae..71a7cab5e 100644 --- a/core/modules/parsers/wikiparser/rules/list.js +++ b/core/modules/parsers/wikiparser/rules/list.js @@ -63,6 +63,13 @@ var listTypes = { ">": {listTag: "blockquote", itemTag: "div"} }; +var listTags = Object.values(listTypes).map(function(type) { + return type.listTag; +}); +var itemTags = Object.values(listTypes).map(function(type) { + return type.itemTag; +}); + /* Parse the most recent match */ @@ -161,46 +168,46 @@ exports.serialize = function (tree, serialize) { return key; // Return the marker associated with the list tag and item tag } } - return ''; // Return empty string if no matching marker is found + return ""; // Return empty string if no matching marker is found } // Recursive function to serialize list nodes, handling nested lists and formatting output function serializeList(node, markerPrefix) { var result = []; - if(node.type === 'element' && ['ul', 'ol', 'dl', 'blockquote'].includes(node.tag)) { + if(node.type === "element" && listTags.includes(node.tag)) { node.children.forEach(function (child) { - if(['li', 'dt', 'dd', 'div'].includes(child.tag)) { + if(itemTags.includes(child.tag)) { var currentMarker = findMarker(node.tag, child.tag); // Handle class attributes - var classAttr = child.attributes && child.attributes.class ? '.' + child.attributes.class.value : ''; + var classAttr = child.attributes && child.attributes.class ? "." + child.attributes.class.value : ""; // same level text nodes may be split into multiple children, and separated by deeper list sub-tree. We collect same level text nodes into this list, and concat then submit them before enter deeper list. var content = []; $tw.utils.each(child.children,function (subNode) { // Check if the child is a nested list or a simple line of list item - if(['ul', 'ol', 'dl', 'blockquote'].includes(subNode.tag)) { + if(listTags.includes(subNode.tag)) { // Recursive call for nested lists if(content.length > 0) { - result.push(markerPrefix + currentMarker + classAttr + ' ' + content.join('').trim()); + result.push(markerPrefix + currentMarker + classAttr + " " + content.join("").trim()); content = [] } result.push(serializeList(subNode, markerPrefix + currentMarker).trim()) } else { content.push(serialize(subNode)) ; } - return ''; // Default return for unhandled node types + return ""; // Default return for unhandled node types }); if(content.length > 0) { - result.push(markerPrefix + currentMarker + classAttr + ' ' + content.join('').trim()); + result.push(markerPrefix + currentMarker + classAttr + " " + content.join("").trim()); content = [] } } }); } - return result.join('\n'); + return result.join("\n"); } // Begin serialization from the root node, with an empty string as the initial marker prefix - return serializeList(tree, '') + '\n\n'; + return serializeList(tree, "") + "\n\n"; }; })(); diff --git a/core/modules/parsers/wikiparser/rules/macrocallblock.js b/core/modules/parsers/wikiparser/rules/macrocallblock.js index c3b0e92e9..923484bae 100644 --- a/core/modules/parsers/wikiparser/rules/macrocallblock.js +++ b/core/modules/parsers/wikiparser/rules/macrocallblock.js @@ -32,7 +32,7 @@ exports.findNextMatch = function(startPos) { var c = this.parser.source.charAt(nextCall.end); // Ensure EOL after parsed macro // If we didn't need to support IE, we'd just use /(?:\r?\n|$)/ym - if ((c === "") || (c === "\n") || ((c === "\r") && this.parser.source.charAt(nextCall.end+1) === "\n")) { + if((c === "") || (c === "\n") || ((c === "\r") && this.parser.source.charAt(nextCall.end+1) === "\n")) { this.nextCall = nextCall; return nextStart; } @@ -53,15 +53,25 @@ exports.parse = function() { return [call]; }; -exports.serialize = function(tree) { - // Macro name - var name = tree.name; - // Macro parameters - var params = tree.params.map(function(param) { - return param.value; - }).join(" "); - // Construct the serialized string - return "<<" + name + " " + params + ">>"; +/* +Serialize a macro call node to wikitext +*/ +exports.serialize = function (node) { + // Start with macro opener + var result = "<<"; + if(node.attributes && node.attributes["$variable"]) { + result += node.attributes["$variable"].value; // Add macro name + } + // Append ordered arguments if any + if(node.orderedAttributes) { + node.orderedAttributes.forEach(function (attr) { + if(attr.name !== "$variable") { + result += " " + '"' + attr.value + '"'; // Add each additional value + } + }); + } + result += ">>\n\n"; + return result; }; })(); diff --git a/core/modules/parsers/wikiparser/rules/macrocallinline.js b/core/modules/parsers/wikiparser/rules/macrocallinline.js index a62596414..bc0ac7f22 100644 --- a/core/modules/parsers/wikiparser/rules/macrocallinline.js +++ b/core/modules/parsers/wikiparser/rules/macrocallinline.js @@ -46,15 +46,25 @@ exports.parse = function() { return [call]; }; -exports.serialize = function(tree) { - // Macro name - var name = tree.name; - // Macro parameters - var params = tree.params.map(function(param) { - return param.value; - }).join(" "); - // Construct the serialized string - return "<<" + name + " " + params + ">>"; +/* +Same as macrocallblock but without \n\n +*/ +exports.serialize = function (node) { + // Start with macro opener + var result = "<<"; + if(node.attributes && node.attributes["$variable"]) { + result += node.attributes["$variable"].value; // Add macro name + } + // Append ordered arguments if any + if(node.orderedAttributes) { + node.orderedAttributes.forEach(function (attr) { + if(attr.name !== "$variable") { + result += " " + '"' + attr.value + '"'; // Add each additional value + } + }); + } + result += ">>"; + return result; }; })(); diff --git a/core/modules/parsers/wikiparser/rules/macrodef.js b/core/modules/parsers/wikiparser/rules/macrodef.js index 31a93f27b..735c7958a 100644 --- a/core/modules/parsers/wikiparser/rules/macrodef.js +++ b/core/modules/parsers/wikiparser/rules/macrodef.js @@ -99,7 +99,7 @@ exports.serialize = function(tree) { // Definition text var definition = tree.attributes.value.value; // Construct the serialized string - return "\\define " + name + "(" + params + ")\n" + definition + "\n\\end"; + return "\\define " + name + "(" + params + ")\n" + definition + "\n\\end\n\n" + serialize(tree.children); }; })(); diff --git a/core/modules/parsers/wikiparser/rules/parameters.js b/core/modules/parsers/wikiparser/rules/parameters.js index 692f03ea7..ddb3a65ab 100644 --- a/core/modules/parsers/wikiparser/rules/parameters.js +++ b/core/modules/parsers/wikiparser/rules/parameters.js @@ -63,7 +63,7 @@ exports.serialize = function(tree) { return param.name + (param.value ? ":" + param.value : ""); }).join(","); // Construct the serialized string - return "\\parameters(" + params + ")"; + return "\\parameters(" + params + ")\n\n" + serialize(tree.children); }; })(); diff --git a/editions/test/tiddlers/tests/test-wikitext-serialize.js b/editions/test/tiddlers/tests/test-wikitext-serialize.js index e19a2d922..032417549 100644 --- a/editions/test/tiddlers/tests/test-wikitext-serialize.js +++ b/editions/test/tiddlers/tests/test-wikitext-serialize.js @@ -231,11 +231,10 @@ describe('WikiAST serialization unit tests', function () { var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler('ListTest').tree).trimEnd(); expect(serialized).toBe(wiki.getTiddlerText('ListTest').trimEnd()); }); - return; wiki.addTiddler({ title: 'MacroCallBlockTest', - text: '<>', + text: '<>\n\n<<.def "macro calls">>', }); it('should serialize block macro calls correctly', function () { var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler('MacroCallBlockTest').tree).trimEnd(); @@ -244,7 +243,7 @@ describe('WikiAST serialization unit tests', function () { wiki.addTiddler({ title: 'MacroCallInlineTest', - text: 'This is a macro call: <>', + text: 'These are macro calls in a line: <> and <<.def "macro calls">>', }); it('should serialize inline macro calls correctly', function () { var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler('MacroCallInlineTest').tree).trimEnd(); @@ -271,12 +270,13 @@ describe('WikiAST serialization unit tests', function () { wiki.addTiddler({ title: 'ParserModeTest', - text: '\\parsermode block\n\\parsermode inline', + text: '\\parsermode block\n\n\\parsermode inline\n\nTest.', }); it('should serialize parser mode specifications correctly', function () { var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler('ParserModeTest').tree).trimEnd(); expect(serialized).toBe(wiki.getTiddlerText('ParserModeTest').trimEnd()); }); + return; wiki.addTiddler({ title: 'PrettyExtLinkTest', diff --git a/plugins/tiddlywiki/jasmine/jasmine-plugin.js b/plugins/tiddlywiki/jasmine/jasmine-plugin.js index 73d5b55d5..488377b44 100644 --- a/plugins/tiddlywiki/jasmine/jasmine-plugin.js +++ b/plugins/tiddlywiki/jasmine/jasmine-plugin.js @@ -13,7 +13,7 @@ The main module of the Jasmine test plugin for TiddlyWiki5 "use strict"; // DEBUG: only run my tests for development, remove before PR merge -var TEST_TIDDLER_FILTER = "[[test-wikitext-serialize.js]] [[test-html-parser.js]]"; +var TEST_TIDDLER_FILTER = "[[test-wikitext-serialize.js]]"; var TESTS_DONE = false; exports.testsWereRun = function() {