Module:scripts: Difference between revisions

No edit summary
Tag: Reverted
m 1 revision imported
 
(3 intermediate revisions by 2 users 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 data_module = "Module:scripts/data"
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 scripts_by_name_module = "Module:scripts/by name"
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 scripts_by_name
local scripts_canonical_names
local function get_scripts_by_name()
local function get_scripts_canonical_names()
scripts_by_name, get_scripts_by_name = load_data(scripts_by_name_module), nil
scripts_canonical_names, get_scripts_canonical_names = load_data(scripts_canonical_names_module), nil
return scripts_by_name
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(data_module), nil
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. Example: {{lua|"Arab"}} for {{lua|"fa-Arab"}}. It returns {{lua|"top"}} for scripts without a parent, like {{lua|"Latn"}}, {{lua|"Grek"}}, etc.]==]
--[==[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()
return self._data.parent
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 229: Line 247:
Use {{lua|hasType("script")}} to determine if an object that may be a language, family or script is a script.]==]
Use {{lua|hasType("script")}} to determine if an object that may be a language, family or script is a script.]==]
function Script:hasType(...)
function Script:hasType(...)
local n = select("#", ...)
Script.hasType = require(language_like_module).hasType
if n == 0 then
return self:hasType(...)
error("Must specify at least one type.")
end
local types = self:getTypes()
if not types[...] then
return false
elseif n == 1 then
return true
end
local args = {...}
for i = 2, n do
if not types[args[i]] then
return false
end
end
return true
end
end


Line 334: 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 431: 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 459: Line 467:
return nil
return nil
end
end
local code = (scripts_by_name or get_scripts_by_name())[name]
local code = (scripts_canonical_names or get_scripts_canonical_names())[name]
if code == nil then
if code == nil then
return nil
return nil
Line 479: Line 487:
name,
name,
" script",
" script",
scripts_by_name or get_scripts_by_name(),
scripts_canonical_names or get_scripts_canonical_names(),
scripts_suffixes or get_scripts_suffixes()
scripts_suffixes or get_scripts_suffixes()
)
)