Module:Sandbox/Erutuon: Difference between revisions

Content deleted Content added
start parse_IETF
finished, I guess
Line 356:
end
 
-- A previous draft, in [[Module:Lang/sandbox]].
-- https://en.wikipedia.org/w/index.php?oldid=812819217
function p.parse_IETF(tag)
local subtags = mw.text.split(tag, "-")
local patternsparsed_subtags = {}
{ '^%a%a%a?$', '^1%a+$', type = 'code' }, -- ll or lll; special case
-- These patterns are checked in order. That is, the 'language' subtag must
{ '^%a%a%a%a$', type = 'script' }, -- Ssss
-- be before the 'script' subtag. However, any of the subtags can be skipped.
{ '^%a%a$', '^%d%d%d$', type = 'region' }, -- rr, DDD
-- 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 = 'codelanguage' }, -- 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
'^[%aw%d][w%aw%d][w%aw%d][%a%d][%a%d][%a%d]w?[%a%d]w?[%a%d]w?$', -- 5-8 alnum characters
type = 'variant'
}
}
local index = 1
local last_matched_subtag_i = 0
for subtag_i, subtag in ipairs(subtags) do
local type
local matched = false
while not matched do
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
break
end
end
if not matched then -- Go to next item in subtag_info.
index = index + 1
end
end
if type then
parsed_subtags[type] = subtag
last_matched_subtag_i = subtag_i
elseif not subtag_info[index] then
break
end
end
if #subtags > require "Module:TableTools".size(parsed_subtags) then
-- Not all subtags were matched. The unmatched tail end of the tag
-- (after the subtag at the index last_matched_subtag_i) may be a
-- private-use subtag.
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
end
end
return parsed_subtags
end