1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-04-10 12:46:39 +00:00

refactor: ' -> "

This commit is contained in:
lin onetwo 2024-08-04 00:29:33 +08:00
parent f69795c62e
commit 9df36a2b15
8 changed files with 68 additions and 41 deletions

View File

@ -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;
};

View File

@ -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";
};
})();

View File

@ -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;
};
})();

View File

@ -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;
};
})();

View File

@ -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);
};
})();

View File

@ -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);
};
})();

View File

@ -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: '<<name value value2>>',
text: '<<name "value" "value2">>\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: <<name value value2>>',
text: 'These are macro calls in a line: <<name "value" "value2">> 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',

View File

@ -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() {