1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-21 14:36:52 +00:00

fix: conditional without elseif

This commit is contained in:
lin onetwo 2024-10-14 00:47:37 +08:00
parent f220dacdd8
commit 5fdc9f365e
2 changed files with 43 additions and 19 deletions

View File

@ -118,28 +118,36 @@ exports.parseIfClause = function(filterCondition) {
};
exports.serialize = function(tree,serialize) {
// We always have "if" at the beginning
var filterCondition = tree.attributes.filter.value;
var ifClause = serialize(tree.children[0].children);
var elseClause = tree.children[1].children;
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) {
// Handle elseif clause
var elseifCondition = elseClause[i].attributes.filter.value;
var elseifClause = serialize(elseClause[i].children[0]);
serialized += "<%elseif " + elseifCondition + "%>" + elseifClause;
}
if(elseClause[i].children[1]) {
var elseClauseText = serialize(elseClause[i].children[1]);
serialized += "<%else%>" + elseClauseText;
var ifClauseText = serialize(tree.children[0].children);
var result = "<%if " + filterCondition + "%>" + ifClauseText;
function serializeElseIf(listNode) {
// We receive the only list node inside list-template node
if(!listNode || listNode.type !== "list") {
return "<%else%>" + serialize(listNode);
}
var filter = listNode.attributes.filter.value || "";
var bodyText = serialize(listNode.children[0].children);
var nextConditionResult = "";
// May has an only any node inside list-empty node
if(listNode.children[1] && listNode.children[1].children[0]) {
if(listNode.children[1].children[0].type === "list") {
nextConditionResult = serializeElseIf(listNode.children[1].children[0]);
} else {
nextConditionResult = "<%else%>" + serialize(listNode.children[1]);
}
}
return "<%elseif " + filter + "%>" + bodyText + nextConditionResult;
}
serialized += "<%endif%>";
return serialized += "\n\n";
if(tree.children[1] && tree.children[1].children) {
result += serializeElseIf(tree.children[1].children[0]);
}
result += "<%endif%>";
if(tree.isBlock) {
result += "\n\n";
}
return result;
};
})();

View File

@ -2,4 +2,20 @@ tags: $:/tags/wikitext-serialize-test-spec
title: Serialize/Conditional
type: text/vnd.tiddlywiki
This is a <%if [{something}] %>Elephant<%elseif [{else}] %>Pelican<%else%>Crocodile<%endif%>
This is a <%if [{something}] %>Elephant<%elseif [{else}] %>Pelican<%else%>Crocodile<%endif%> <%if [{something}] %>Elephant<%else%>Crocodile<%endif%>
<%if [{$:/info/url/protocol}match[file:]]%>
Loaded from a file URI
<%elseif [{$:/info/url/protocol}match[https:]]%>
Loaded from an HTTPS URI
<%elseif [{$:/info/url/protocol}match[http:]]%>
Loaded from an HTTP URI
<%else%>
Loaded from an unknown protocol
<%endif%>
Plain text in next paragraph.
<%if [{$:/info/url/protocol}match[file:]]%>
Hidden.
<%endif%>