local export = {}
local en_utilities_module = "Module:en-utilities"
local json_module = "Module:JSON"
local language_like_module = "Module:language-like"
local load_module = "Module:load"
local table_module = "Module:table"
local writing_systems_data_module = "Module:writing systems/data"
local gmatch = string.gmatch
local make_object -- Defined below.
local require = require
local setmetatable = setmetatable
local type = type
--[==[
Loaders for functions in other modules, which overwrite themselves with the target function when called. This ensures modules are only loaded when needed, retains the speed/convenience of locally-declared pre-loaded functions, and has no overhead after the first call, since the target functions are called directly in any subsequent calls.]==]
local function deep_copy(...)
deep_copy = require(table_module).deepCopy
return deep_copy(...)
end
local function keys_to_list(...)
keys_to_list = require(table_module).keysToList
return keys_to_list(...)
end
local function load_data(...)
load_data = require(load_module).load_data
return load_data(...)
end
local function pluralize(...)
pluralize = require(en_utilities_module).pluralize
return pluralize(...)
end
local function to_json(...)
to_json = require(json_module).toJSON
return to_json(...)
end
--[==[
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 writing_systems_data
local function get_writing_systems_data()
writing_systems_data, get_writing_systems_data = load_data(writing_systems_data_module), nil
return writing_systems_data
end
local WritingSystem = {}
WritingSystem.__index = WritingSystem
function WritingSystem:getCode()
return self._code
end
function WritingSystem:getCanonicalName()
return self._data
end
function WritingSystem:getDisplayForm(singular)
return singular and (self._data.category or self:getCanonicalName()) or self:getCategoryName("nocap")
end
function WritingSystem:getAliases()
WritingSystem.getAliases = require(language_like_module).getAliases
return self:getAliases()
end
function WritingSystem:getVarieties(flatten)
WritingSystem.getVarieties = require(language_like_module).getVarieties
return self:getVarieties(flatten)
end
function WritingSystem:getOtherNames()
WritingSystem.getOtherNames = require(language_like_module).getOtherNames
return self:getOtherNames()
end
function WritingSystem:getAllNames()
WritingSystem.getAllNames = require(language_like_module).getAllNames
return self:getAllNames()
end
--[==[Returns a table of types as a lookup table (with the types as keys).
Currently, the only possible type is {writing system}.]==]
function WritingSystem:getTypes()
local types = self._types
if types == nil then
types = { = true}
local rawtypes = self._data.type
if rawtypes then
for t in gmatch(rawtypes, "+") do
types = true
end
end
self._types = types
end
return types
end
--==]
function WritingSystem:hasType(...)
WritingSystem.hasType = require(language_like_module).hasType
return self:hasType(...)
end
function WritingSystem:getCategoryName(nocap)
local name = pluralize(self._data.category or self:getCanonicalName())
if not nocap then
name = mw.getContentLanguage():ucfirst(name)
end
return name
end
function WritingSystem:makeCategoryLink()
return "]"
end
--==]
function WritingSystem:getWikidataItem()
WritingSystem.getWikidataItem = require(language_like_module).getWikidataItem
return self:getWikidataItem()
end
function WritingSystem:getWikipediaArticle(noCategoryFallback, project)
WritingSystem.getWikipediaArticle = require(language_like_module).getWikipediaArticle
return self:getWikipediaArticle(noCategoryFallback, project)
end
--==]
function WritingSystem:getCommonsCategory()
WritingSystem.getCommonsCategory = require(language_like_module).getCommonsCategory
return self:getCommonsCategory()
end
function WritingSystem:getData()
return self._data
end
function WritingSystem:toJSON(opts)
local ret = {
canonicalName = self:getCanonicalName(),
categoryName = self:getCategoryName("nocap"),
code = self:getCode(),
aliases = self:getAliases(),
varieties = self:getVarieties(),
otherNames = self:getOtherNames(),
type = keys_to_list(self:getTypes()),
wikidataItem = self:getWikidataItem(),
wikipediaArticle = self:getWikipediaArticle(true),
}
-- Use `deep_copy` when returning a table, so that there are no editing restrictions imposed by `mw.loadData`.
return opts and opts.lua_table and deep_copy(ret) or to_json(ret, opts)
end
function export.makeObject(code, data)
local data_type = type(data)
if data_type ~= "table" then
error(("bad argument #2 to 'makeObject' (table expected, got %s)"):format(data_type))
end
return setmetatable({_data = data, _code = code}, WritingSystem)
end
make_object = export.makeObject
function export.getByCode(code)
local data = (writing_systems_data or get_writing_systems_data())
return data ~= nil and make_object(code, data) or nil
end
export.getByCanonicalName = export.getByCode
return export