scansion/stanzacmp.lua

changeset 99
dc56d434e406
parent 92
9a58c8ee0757
child 101
91a9e557e0e5
--- a/scansion/stanzacmp.lua	Thu Sep 06 18:23:56 2018 +0100
+++ b/scansion/stanzacmp.lua	Thu Sep 06 18:26:01 2018 +0100
@@ -11,19 +11,29 @@
 	return (s:gsub("^%s+", ""):gsub("%s+$", ""));
 end
 
+local function wants_strict(tag)
+	local opt = tag.attr["scansion:strict"] or "yes";
+	if opt == "no" or opt == "false" or opt == "0" then
+		return false;
+	elseif opt == "yes" or opt == "true" or opt == "1" then
+		return true;
+	end
+	error("Unexpected scansion:strict value: "..opt);
+end
+
 local function stanzas_strict_match(stanza1, stanza2)
 	if stanza1.name ~= stanza2.name or stanza1.attr.xmlns ~= stanza2.attr.xmlns then
 		return false;
 	end
 	
 	for k, v in pairs(stanza1.attr) do
-		if v ~= "{scansion:any}" and stanza2.attr[k] ~= v then
+		if not k:match("^scansion:") and v ~= "{scansion:any}" and stanza2.attr[k] ~= v then
 			return false;
 		end
 	end
 	
 	for k, v in pairs(stanza2.attr) do
-		if stanza1.attr[k] ~= "{scansion:any}" and stanza1.attr[k] ~= v then
+		if not k:match("^scansion:") and stanza1.attr[k] ~= "{scansion:any}" and stanza1.attr[k] ~= v then
 			return false;
 		end
 	end
@@ -66,7 +76,7 @@
 	end
 	
 	for k, v in pairs(stanza1.attr) do
-		if stanza2.attr[k] ~= v then
+		if not k:match("^scansion:") and stanza2.attr[k] ~= v then
 			return false;
 		end
 	end
@@ -84,8 +94,12 @@
 
 					if type(child2) == type(child) then
 						if type(child) == "table" and child2.name == child.name and child2.attr.xmlns == child.attr.xmlns then
-							-- Strict deep match
-							match = stanzas_strict_match(child, child2);
+							if wants_strict(child) then
+								-- Strict deep match
+								match = stanzas_strict_match(child, child2);
+							else
+								match = stanzas_match(child, child2);
+							end
 						elseif type(child) == "string" then -- Text nodes, must be equal, ignoring leading/trailing whitespace
 							match = trim(child) == trim(child2);
 						end

mercurial