Module:scripts: Difference between revisions
No edit summary Tag: Manual revert |
m 1 revision imported |
||
| (One intermediate revision by one other user not shown) | |||
| Line 2: | Line 2: | ||
local combining_classes_module = "Module:Unicode data/combining classes" | local combining_classes_module = "Module:Unicode data/combining classes" | ||
local json_module = "Module:JSON" | local json_module = "Module:JSON" | ||
local language_like_module = "Module:language-like" | local language_like_module = "Module:language-like" | ||
local load_module = "Module:load" | local load_module = "Module:load" | ||
local | local scripts_canonical_names_module = "Module:scripts/canonical names" | ||
local scripts_chartoscript_module = "Module:scripts/charToScript" | local scripts_chartoscript_module = "Module:scripts/charToScript" | ||
local scripts_data_module = "Module:scripts/data" | |||
local string_utilities_module = "Module:string utilities" | local string_utilities_module = "Module:string utilities" | ||
local table_module = "Module:table" | local table_module = "Module:table" | ||
| Line 87: | Line 87: | ||
--[==[ | --[==[ | ||
Loaders for objects, which load data (or some other object) into some variable, which can then be accessed as "foo or get_foo()", where the function get_foo sets the object to "foo" and then returns it. This ensures they are only loaded when needed, and avoids the need to check for the existence of the object each time, since once "foo" has been set, "get_foo" will not be called again.]==] | Loaders for objects, which load data (or some other object) into some variable, which can then be accessed as "foo or get_foo()", where the function get_foo sets the object to "foo" and then returns it. This ensures they are only loaded when needed, and avoids the need to check for the existence of the object each time, since once "foo" has been set, "get_foo" will not be called again.]==] | ||
local | local scripts_canonical_names | ||
local function | local function get_scripts_canonical_names() | ||
scripts_canonical_names, get_scripts_canonical_names = load_data(scripts_canonical_names_module), nil | |||
return | return scripts_canonical_names | ||
end | end | ||
local scripts_data | local scripts_data | ||
local function get_scripts_data() | local function get_scripts_data() | ||
scripts_data, get_scripts_data = load_data( | scripts_data, get_scripts_data = load_data(scripts_data_module), nil | ||
return scripts_data | return scripts_data | ||
end | end | ||
| Line 163: | Line 163: | ||
end | end | ||
--[==[Returns the parent of the script | --[==[Returns a script object for the parent of the script, such as {"Arab"} for {"fa-Arab"}. It returns {nil} for scripts without a parent, like {"Latn"}, {"Grek"}, etc.]==] | ||
function Script:getParent() | |||
local parent = self._parentObject | |||
if parent == nil then | |||
parent = self:getParentCode() | |||
-- If the value is nil, it's cached as false. | |||
parent = parent and get_by_code(parent) or false | |||
self._parentObject = parent | |||
end | |||
return parent or nil | |||
end | |||
--[==[Returns the script code of the parent of the script, such as {"Arab"} for {"fa-Arab"}. It returns {nil} for scripts without a parent, like {"Latn"}, {"Grek"}, etc.]==] | |||
function Script:getParentCode() | function Script:getParentCode() | ||
local parent = self._parentCode | |||
if parent == nil then | |||
-- If the value is nil, it's cached as false. | |||
parent = self._data.parent or false | |||
self._parentCode = parent | |||
end | |||
return parent or nil | |||
end | end | ||
| Line 319: | Line 337: | ||
function Script:getData() | function Script:getData() | ||
return self._data | return self._data | ||
end | |||
--[==[Returns the name of the module containing the script's data. Currently, this is always [[Module:scripts/data]].]==] | |||
function Script:getDataModuleName() | |||
return scripts_data_module | |||
end | end | ||
| Line 416: | Line 439: | ||
return opts and opts.lua_table and deep_copy(ret) or to_json(ret, opts) | return opts and opts.lua_table and deep_copy(ret) or to_json(ret, opts) | ||
end | end | ||
function export.makeObject(code, data) | function export.makeObject(code, data) | ||
local data_type = type(data) | local data_type = type(data) | ||
| Line 444: | Line 467: | ||
return nil | return nil | ||
end | end | ||
local code = ( | local code = (scripts_canonical_names or get_scripts_canonical_names())[name] | ||
if code == nil then | if code == nil then | ||
return nil | return nil | ||
| Line 464: | Line 487: | ||
name, | name, | ||
" script", | " script", | ||
scripts_canonical_names or get_scripts_canonical_names(), | |||
scripts_suffixes or get_scripts_suffixes() | scripts_suffixes or get_scripts_suffixes() | ||
) | ) | ||