Jump to content

Module:writing systems

Wiktionary වෙතින්


local export = {}

local si_utilities_module = "Module:si-utilities"
local json_module = "Module:JSON"
local language_like_module = "Module:language-like"
local table_module = "Module:table"
local writing_systems_data_module = "Module:writing systems/data"

local gmatch = string.gmatch
local make_object -- Defined below.
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 deepcopy(...)
		deepcopy = require(table_module).deepcopy
		return deepcopy(...)
	end
	
	local function keys_to_list(...)
		keys_to_list = require(table_module).keysToList
		return keys_to_list(...)
	end
	
	local function pluralize(...)
		pluralize = require(si_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 = mw.loadData(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[1]
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 = {["writing system"] = true}
		local rawtypes = self._data.type
		if rawtypes then
			for t in gmatch(rawtypes, "[^,]+") do
				types[t] = true
			end
		end
		self._types = types
	end
	return types
end

--[==[Given a list of types as strings, returns true if the writing system has all of them.]==]
function WritingSystem:hasType(...)
	local args, types = {...}, self:getTypes()
	for i = 1, #args do
		if not types[args[i]] then
			return false
		end
	end
	return true
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 "[[:Category:" .. self:getCategoryName() .. "|" .. self:getDisplayForm() .. "]]"
end

--[==[Returns the Wikidata item id for the writing system or <code>nil</code>. This corresponds to the the second field in the data modules.]==]
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

--[==[Returns the name of the Wikimedia Commons category page for the writing system.]==]
function WritingSystem:getCommonsCategory()
	WritingSystem.getCommonsCategory = require(language_like_module).getCommonsCategory
	return self:getCommonsCategory()
end

function WritingSystem:getData()
	return self._data
end

function WritingSystem:toJSON(returnTable)
	-- Use `deepcopy` when returning a table, so that there are no editing restrictions imposed by `mw.loadData`.
	return (returnTable and deepcopy or to_json){
		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),
	}
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())[code]
	return data ~= nil and make_object(code, data) or nil
end

export.getByCanonicalName = export.getByCode

return export
"https://si.wiktionary.org/w/index.php?title=Module:writing_systems&oldid=196939" වෙතින් සම්ප්‍රවේශනය කෙරිණි