comparison mod_xmllang_check/xmpplang.lib.lua @ 6401:de3dc4297035

mod_xmllang_check: Initial commit
author Waqas Hussain <waqas20@gmail.com>
date Mon, 09 Feb 2026 18:34:40 -0500
parents
children
comparison
equal deleted inserted replaced
6400:f4efb8606b92 6401:de3dc4297035
1
2
3 local irregular = {
4 "en-GB-oed", "i-ami", "i-bnn", "i-default", "i-enochian", "i-hak", "i-klingon", "i-lux", "i-mingo", "i-navajo", "i-pwn", "i-tao", "i-tay", "i-tsu", "sgn-BE-FR", "sgn-BE-NL", "sgn-CH-DE"
5 };
6 local regular = {
7 "art-lojban", "cel-gaulish", "no-bok", "no-nyn", "zh-guoyu", "zh-hakka", "zh-min", "zh-min-nan", "zh-xiang"
8 };
9 local grandfathered = {};
10 for _, tag in ipairs(irregular) do
11 grandfathered[tag:lower()] = true;
12 end
13 for _, tag in ipairs(regular) do
14 grandfathered[tag:lower()] = true;
15 end
16
17 local function split_subtags(lower_s)
18 local subtags = {};
19 for subtag in lower_s:gmatch("[^%-]+") do -- split on hyphens
20 table.insert(subtags, subtag);
21 end
22 return subtags;
23 end
24
25 local function is_privateuse(lower_s)
26 if lower_s:sub(1, 2) ~= "x-" then
27 -- print("privateuse: " .. lower_s .. " does not start with x-");
28 return false;
29 end
30
31 -- only allow alphanum and hyphens from here on out
32 if lower_s:match("[^a-z0-9%-]") then
33 -- print("privateuse: " .. lower_s .. " contains non-alphanum or hyphen");
34 return false;
35 end
36 -- lets not allow any zero-len subtags
37 if lower_s:match("^%-") or lower_s:match("%-$") or lower_s:match("%-%-") then
38 -- print("privateuse: " .. lower_s .. " contains zero-len subtags");
39 return false;
40 end
41
42 local subtags = split_subtags(lower_s);
43 -- print("privateuse subtags: " .. table.concat(subtags, "|"));
44 if #subtags < 2 then return false; end
45 for i = 1, #subtags do
46 if #subtags[i] < 1 or #subtags[i] > 8 or not subtags[i]:match("^[a-z0-9]+$") then return false; end
47 end
48 -- print("privateuse: " .. lower_s .. " is valid");
49 return true;
50 end
51
52 local function is_langtag(lower_s)
53 -- print("is_langtag: " .. lower_s);
54 -- print(" lower_s:sub(-5): ".. lower_s:sub(-5))
55 -- Remove the optional .utf8 suffix if it exists
56 if lower_s:sub(-6) == ".utf-8" then
57 -- print("removing .utf-8 suffix");
58 lower_s = lower_s:sub(1, -7);
59 end
60
61 -- at this point we have a hyphen-separated string of subtags, where subtags are alphanumeric
62
63 -- only allow alphanum and hyphens from here on out
64 if lower_s:match("[^a-z0-9%-]") then
65 return false;
66 end
67 -- lets not allow any zero-len subtags
68 if lower_s:match("^%-") or lower_s:match("%-$") or lower_s:match("%-%-") then
69 return false;
70 end
71
72 -- let's split on hyphens
73 local subtags = split_subtags(lower_s);
74
75 -- we are now dealing with this pattern: language ( "-" script )? ( "-" region )? ( "-" variant )* ( "-" extension )*
76 -- we'll now process in order
77
78 -- Let's process "language"
79 -- print("processing language: " .. table.concat(subtags, "|"));
80 local subtag = table.remove(subtags, 1);
81 if not subtag or not subtag:match("^[a-z]+$") then
82 return false;
83 end
84 if #subtag == 2 or #subtag == 3 then
85 -- we want to check for 0-3 instances of three-letter tags
86 for i = 1, 3 do
87 local subtag = subtags[1];
88 if subtag and #subtag == 3 and subtag:match("^[a-z]+$") then
89 table.remove(subtags, 1);
90 end
91 end
92 elseif #subtag >= 4 and #subtag <= 8 then
93 -- passthrough
94 else
95 return false;
96 end
97
98 -- Let's process optional: script = ALPHA{4};
99 -- print("processing script: " .. table.concat(subtags, "|"));
100 subtag = subtags[1];
101 if subtag and #subtag == 4 and subtag:match("^[a-z]+$") then
102 table.remove(subtags, 1);
103 end
104
105 -- Let's process optional: region = ALPHA{2} | DIGIT{3};
106 -- print("processing region: " .. table.concat(subtags, "|"));
107 subtag = subtags[1];
108 if subtag and ((#subtag == 2 and subtag:match("^[a-z][a-z]$")) or (#subtag == 3 and subtag:match("^[0-9][0-9][0-9]$"))) then
109 table.remove(subtags, 1);
110 end
111
112 -- Let's process zero or more: variant = alphanum{5,8} | ( DIGIT alphanum{3} );
113 while true do
114 local variant = subtags[1];
115 if not variant or (#variant < 4 or #variant > 8) then
116 break;
117 end
118 if #variant == 4 and not variant:match("^[0-9]") then return false; end
119 table.remove(subtags, 1);
120 end
121
122 -- print("processing extensions: " .. table.concat(subtags, "|"));
123 -- Let's process zero or more: extension = singleton ( "-" alphanum{2,8} )+;
124 while true do
125 local extension = subtags[1];
126 if not extension or extension == "x" then break; end
127 if #extension ~= 1 then return false; end
128 table.remove(subtags, 1);
129
130 local part = subtags[1];
131 if not part or (#part < 2 or #part > 8) then return false; end
132 table.remove(subtags, 1);
133
134 while true do
135 part = subtags[1];
136 if not part or (#part < 2 or #part > 8) then break; end
137 table.remove(subtags, 1);
138 end
139 end
140
141 -- print("processing privateuse: " .. table.concat(subtags, "|"));
142 -- Let's process optional: privateuse = "x"i ( "-" alphanum{1,8} )+;
143 local privateuse = table.remove(subtags, 1);
144 -- print("privateuse: " .. privateuse);
145 if privateuse then
146 if privateuse ~= "x" or #subtags < 1 then return false; end
147 for i = 1, #subtags do
148 local part = subtags[i];
149 if #part < 1 or #part > 8 then return false; end
150 end
151 end
152
153 return true;
154 end
155
156 local function is_xmpplang(s)
157 local lower_s = s:lower();
158 if grandfathered[lower_s] then
159 return true;
160 end
161 -- print("checking privateuse: " .. lower_s);
162 if is_privateuse(lower_s) then
163 -- print("privateuse: " .. lower_s .. " is valid");
164 return true;
165 end
166 -- print("checking langtag: " .. lower_s);
167 if is_langtag(lower_s) then
168 return true;
169 end
170 return false;
171 end
172
173 -- print("=== x-abc-def")
174 -- print(is_xmpplang("x-abc-def"));
175 -- print("=== x-a-b-c-d-e")
176 -- print(is_xmpplang("x-a-b-c-d-e"));
177
178 return is_xmpplang;