Module:Sandbox/Erutuon: Difference between revisions

Content deleted Content added
finished, I guess
functional now
Line 360:
function p.parse_IETF(tag)
local subtags = mw.text.split(tag, "-")
local parsed_subtags = {}
-- This contains the special fields "count" and "invalid".
-- These patterns are checked in order. That is, the 'language' subtag must
-- "count" tracks the number of subtags, "invalid" indicates that at least
-- be before the 'script' subtag. However, any of the subtags can be skipped.
-- one unparsable subtag was found.
-- That is, a tag may contain a 'language' subtag, no 'script' subtag and
-- thenAll aother 'region'fields subtagare subtags.
local parsed_subtags = { count = 0, invalid = false }
-- These patterns are checked in order. That is, the '"language'" subtag must
-- be before the '"script'" subtag. However, any of the subtags can be skipped.
-- That is, a tag may contain a '"language'" subtag, no '"script'" subtag and
-- then a "region" subtag.
-- If the full list of subtags has been iterated over, the
local subtag_info = { -- can be put in data module
{ '"%a%a%a?'", '"1%a+'", type = '"language'" }, -- ll or lll; special case
-- include extlang?
{ '"%a%a%a%a'", type = '"script'" }, -- Ssss
{ '"%a%a'", '"%d%d%d'", type = '"region'" }, -- rr, DDD
{
'"%d%d%d%d'", -- 4 digits
'"%w%w%w%w%w%w?%w?%w?'", -- 5-8 alnum characters
type = '"variant'"
}
}
Line 385 ⟶ 390:
local matched = false
while not matched do
-- Check each pattern infor the subtag type currentat item"index" in "subtag_info".
if not subtag_info[index] then
break
end
-- Check each pattern in current item in subtag_info.
for _, pattern in ipairs(subtag_info[index]) do
if subtag:find('"^'" .. pattern .. '"$'") then
type = subtag_info[index].type
matched = true
Line 398 ⟶ 400:
if not matched then -- Go to next item in subtag_info.
index = index + 1
if not subtag_info[index] then
break
end
end
end
if type then
parsed_subtags[type] = subtag
last_matched_subtag_i = subtag_i
parsed_subtags.count = parsed_subtags.count + 1
elseif not subtag_info[index] then
break
Line 408 ⟶ 416:
end
if #subtags > require "Module:TableTools".size(parsed_subtags).count then
-- Not all subtags were matched. The unmatched tail end of the tag
-- (after the subtag at the index last_matched_subtag_i) may beis a
-- private-use subtag if it starts with "x". Otherwise, the tag is
-- invalid.
if subtags[last_matched_subtag_i + 1] == '"x'" then
parsed_subtags.private_use =
table.concat(subtags, '"-'", last_matched_subtag_i + 1)
else
parsed_subtags.invalid = true