scansion/stanzacmp.lua

changeset 153
f83ea6e5c3d8
parent 151
8191f165b9dd
child 170
db73c4c317ce
equal deleted inserted replaced
152:ba8219ac7484 153:f83ea6e5c3d8
9 9
10 local function trim(s) 10 local function trim(s)
11 return (s:gsub("^%s+", ""):gsub("%s+$", "")); 11 return (s:gsub("^%s+", ""):gsub("%s+$", ""));
12 end 12 end
13 13
14 local function wants_strict(tag) 14 local function wants_strict(tag, default)
15 local opt = tag.attr["scansion:strict"] or "yes"; 15 local opt = tag.attr["scansion:strict"] or default or "yes";
16 if opt == "no" or opt == "false" or opt == "0" then 16 if opt == "no" or opt == "false" or opt == "0" then
17 return false; 17 return false;
18 elseif opt == "yes" or opt == "true" or opt == "1" then 18 elseif opt == "yes" or opt == "true" or opt == "1" then
19 return true; 19 return true;
20 end 20 end
70 end 70 end
71 71
72 -- Everything in stanza1 should be present in stanza2 72 -- Everything in stanza1 should be present in stanza2
73 73
74 local function stanzas_match(stanza1, stanza2) 74 local function stanzas_match(stanza1, stanza2)
75 if wants_strict(stanza1, stanza1.attr.xmlns == nil and "no" or "yes") then
76 return stanzas_strict_match(stanza1, stanza2);
77 end
75 if stanza1.name ~= stanza2.name or stanza1.attr.xmlns ~= stanza2.attr.xmlns then 78 if stanza1.name ~= stanza2.name or stanza1.attr.xmlns ~= stanza2.attr.xmlns then
76 return false; 79 return false;
77 end 80 end
78 81
79 for k, v in pairs(stanza1.attr) do 82 for k, v in pairs(stanza1.attr) do
93 local child2 = stanza2[stanza2_pos]; 96 local child2 = stanza2[stanza2_pos];
94 stanza2_pos = stanza2_pos + 1; 97 stanza2_pos = stanza2_pos + 1;
95 98
96 if type(child2) == type(child) then 99 if type(child2) == type(child) then
97 if type(child) == "table" and child2.name == child.name and child2.attr.xmlns == child.attr.xmlns then 100 if type(child) == "table" and child2.name == child.name and child2.attr.xmlns == child.attr.xmlns then
98 if wants_strict(child) then 101 match = stanzas_match(child, child2);
99 -- Strict deep match
100 match = stanzas_strict_match(child, child2);
101 else
102 match = stanzas_match(child, child2);
103 end
104 elseif type(child) == "string" then -- Text nodes, must be equal, ignoring leading/trailing whitespace 102 elseif type(child) == "string" then -- Text nodes, must be equal, ignoring leading/trailing whitespace
105 match = trim(child) == trim(child2); 103 match = trim(child) == trim(child2);
106 end 104 end
107 if match then 105 if match then
108 break; 106 break;

mercurial