Content deleted Content added
+suppressed script extraction; |
m fix module names; |
||
(9 intermediate revisions by 4 users not shown) | |||
Line 1:
require('
Line 31 ⟶ 30:
before the Comments line.
Records with
]=]
Line 41 ⟶ 40:
local in_comments = false;
if string.find (record, 'Deprecated', 1, true) or string.find (record, 'Preferred
or string.find (record, 'Private use', 1, true) then return 'skip';
end
for line in string.gmatch (record, '([^\n]+)\n') do -- get a \n terminated line of text (without the \n)
if string.find (line, 'Subtag: [%a%d]+') then -- if this line is the subtag line▼
code = string.match (line, 'Subtag: ([%a%d]+)'); -- extract and save to subtag's code▼
descriptions[#descriptions] = string.gsub (descriptions[#descriptions], '\"$', ''); -- remove trailing quote mark from previous description▼
descriptions[#descriptions] = descriptions[#descriptions] .. ' ' .. string.match (line, '^ (.+)') .. '\"'; -- extract and save the continuation with new quote mark▼
elseif
local desc = string.match (line, 'Description: (.+)'); -- extract the description
desc = string.gsub (desc, '"', '\\"'); -- in case description contains quote marks (see 1959acad)
table.insert (descriptions, '\"' .. desc .. '\"'); -- save the description wrapped in quote marks
elseif
table.insert (prefixes, '\"' .. string.match (line, 'Prefix: (.+)'):lower() .. '\"'); -- extract and save the prefix wrapped in quote marks
elseif
in_comments = true;
▲ descriptions[#descriptions] = string.gsub (descriptions[#descriptions], '\"$', ''); -- remove trailing quote mark from previous description
▲ descriptions[#descriptions] = descriptions[#descriptions] .. ' ' .. string.match (line, '^ (.+)') .. '\"'; -- extract and save the continuation with new quote mark
end
end
Line 91 ⟶ 93:
before the Comments line.
Records with
]=]
Line 98 ⟶ 100:
local code;
local suppress; -- Suppress script for this code if specified
local deprecated; -- boolean; true when subtag is deprecated
local descriptions = {};
local in_comments = false;
if
return 'skip';
end
for line in
local label = line:match ('(.-):');
▲
table.insert (descriptions, '\"' ..
elseif string.find (line, 'Suppress%-Script: %S+') then▼
elseif 'Deprecated' == label then
▲ suppress = string.match (line, 'Suppress%-Script: (%S+)');
deprecated = true; -- subtag is deprecated; set our flag
▲ elseif string.find (line, 'Comments: .+') then -- if this line is a comments line
suppress = line:match ('Suppress%-Script: (%S+)');
elseif 'Comments' == label then -- if this line is a comments line
in_comments = true;
elseif
descriptions[#descriptions] =
descriptions[#descriptions] = descriptions[#descriptions] .. ' ' ..
end
end
return code, table.concat (descriptions, ', '), suppress, deprecated;
end
Line 127 ⟶ 133:
read a local copy of the IANA language-subtag-registry file and from it build tables to replace the tables in:
[[Module:
[[Module:
[[Module:
[[Module:Lang/data/iana supressed cripts]]
[[Module:Lang/data/iana variants]]
current language-subtag-registry file can be found at: http://www.iana.org/assignments/language-subtag-registry
Line 136 ⟶ 144:
]=]
local function
local page = mw.title.getCurrentTitle(); -- get a page object for this page
local content = page:getContent(); -- get unparsed content
local lang_table = {}; -- languages go here
local lang_dep_table = {}; -- deprecated languages go here
local script_table = {}; -- scripts go here
local region_table = {}; -- regions go here
local variant_table = {}; -- variants go here
local suppress_table = {}; -- here we collect suppressed scripts and associated language codes
local iso_639_1_table = {}; -- ISO 639-1 languages; not used by Module:Lang but included here to ensure Module:Lang/data/ISO_639-1 gets updated
local file_date; -- first line
Line 150 ⟶ 160:
local prefixes; -- used for language variants only
local suppress; -- a code's suppress script
local deprecated; -- boolean: true when subtag is deprecated
file_date = content:match ('(File%-Date: %d%d%d%d%-%d%d%-%d%d)'); -- get the file date line from this version of the source file
for record in string.gmatch (content, '%%%%([^%%]+)') do -- get a %% delimited 'record' from the file; leave off the delimiters
if record_type == 'language' then -- if a language record
code, descriptions, suppress, deprecated = get_lang_script_region_parts (record);
if code and ('skip' ~= code) then
if deprecated then
table.insert (lang_table, "[\"" .. code .. "\"] = {" .. descriptions .. "}"); -- make table entries▼
table.insert (lang_dep_table, "[\"" .. code .. "\"] = {" .. descriptions .. "}"); -- make table entries
else
▲ table.insert (lang_table, "[\"" .. code .. "\"] = {" .. descriptions .. "}"); -- make table entries
if 2 == code:len() then
table.insert (iso_639_1_table, "[\"" .. code .. "\"] = {" .. descriptions .. "}"); -- make table entries
end
end
elseif not code then
table.insert (lang_table, "[\"error\"] = {" .. record .. "}"); -- code should never be nil, but inserting an error entry in the final output can be helpful
Line 175 ⟶ 194:
end
elseif
code, descriptions = get_lang_script_region_parts (record); -- get the code and description(s)
Line 184 ⟶ 203:
end
elseif
code, descriptions = get_lang_script_region_parts (record); -- get the code and description(s)
Line 193 ⟶ 212:
end
elseif
code, prefixes, descriptions = get_variant_parts (record); -- get the code, prefix(es), and description(s)
Line 217 ⟶ 236:
for script, code_tbl in pairs (suppress_table) do
local LIMIT = 11; -- max number of subtags on a line before a line break
table.insert (pretty_suppressed,▼
local fragment_tbl = {}; -- groups of LIMIT number of subtags collected here
table.concat ({'[\"', script, '\"] = {', table.concat (code_tbl, ', '), '}'})▼
for i=1, #code_tbl, LIMIT do
local stop = ((i+LIMIT-1) > #code_tbl) and #code_tbl or i+LIMIT-1; -- calculate a table.concat stop position
table.insert (fragment_tbl, table.concat (code_tbl, ', ', i, stop)); -- get the fragment and save it
end
▲ table.insert (pretty_suppressed, -- and make all pretty
▲ table.concat ({'[\"', script, '\"] = {', table.concat (
);
end
table.sort (pretty_suppressed);
-- make
return
file_date .. "<br />
'------------------------------< I A N A S C R I P T S >------------------------------------------------------<br />--' ..
file_date .. "<br />return {<br />	" .. table.concat (pretty_suppressed, ',<br />	') .. "<br />	}<br />" .. "</pre>";▼
file_date .. "<br />return {<br />	" .. table.concat (script_table, ',<br />	') .. "<br />	}<br /><br />" ..
'------------------------------< I A N A R E G I O N S >------------------------------------------------------<br />--' ..
file_date .. "<br />return {<br />	" .. table.concat (region_table, ',<br />	') .. "<br />	}<br /><br />" ..
'------------------------------< I A N A V A R I A N T S >----------------------------------------------------<br />--' ..
file_date .. "<br />return {<br />	" .. table.concat (variant_table, ',<br />	') .. "<br />	}<br /><br />" ..
'------------------------------< I A N A S U P P R E S S E D S C R I P T S >--------------------------------<br />--' ..
▲ file_date .. "<br />return {<br />	" .. table.concat (pretty_suppressed, ',<br />	') .. "<br />	}<br /><br />" ..
'------------------------------< I S O 6 3 9 - 1 >------------------------------------------------------------<br />--' ..
file_date .. "<br />return {<br />	" .. table.concat (iso_639_1_table, ',<br />	') .. "<br />	}<br /><br />" .. "</pre>";
end
return p;▼
--[[--------------------------< E X P O R T E D F U N C T I O N >--------------------------------------------
]]
iana_extract = iana_extract,
}
|