Ir al contenido

Módulo:scripts

De Wikcionario, el diccionario libre

La documentación para este módulo puede ser creada en Módulo:scripts/doc

--cf https://en.wiktionary.org/wiki/Módulo:scripts

local export = {}

local Script = {}

--[==[Returns the script code of the language. Example: {{code|lua|"Cyrl"}} for Cyrillic.]==]
function Script:getCode()
	return self._code
end

--[==[Returns the canonical name of the script. This is the name used to represent that script on Wiktionary. Example: {{code|lua|"Cyrillic"}} for Cyrillic.]==]
function Script:getCanonicalName()
	return self._rawData[1]
end

--[==[Returns the display form of the script.]==]
function Script:getDisplayForm()
    local name = self._rawData[1]
    return "script "..name
end

function Script:getOtherNames()
	return self._rawData.otherNames
end

function Script:getAliases()
	return self._rawData.aliases or {}
end

function Script:getVarieties(flatten)
	return self._rawData.varieties or {}
end

--[==[Returns the parent of the script. Example: {{code|lua|"Latn"}} for {{code|lua|"Latnx"}} and {{code|lua|"Arab"}} for {{code|lua|"fa-Arab"}}. It returns {{code|lua|"top"}} for scripts without a parent, like {{code|lua|"Latn"}}, {{code|lua|"Grek"}}, etc.]==]
function Script:getParent()
	return self._rawData.parent
end

function Script:getWritingSystems()
	if not self._systemCodes then
        if type(self._rawData[2]) == "string" then
            self._systemCodes = mw.text.split(self._rawData[2], "%s*,%s*")
        elseif type(self._rawData[2]) == "nil" then
            self._systemCodes = {}
        else
            error("mal el tipo de dato")
        end
	end
	return self._systemCodes
end

function Script:makeCategoryLink()
	return "[[:Categoría:" .. self:getDisplayForm() .. "]]"
end

--[==[Returns the regex defining the script's characters from the language's data file.
This can be used to search for words consisting only of this script, but see the warning above.]==]
function Script:getCharacters()
	if self._rawData.characters then
		return self._rawData.characters
	else
		return nil
	end
end

--[==[Returns the number of characters in the text that are part of this script.
'''Note:''' You should never rely on text consisting entirely of the same script. Strings may contain spaces, punctuation and even wiki markup or HTML tags. HTML tags will skew the counts, as they contain Latin-script characters. So it's best to avoid them.]==]
function Script:countCharacters(text)
	if not self._rawData.characters then
		return 0
	-- Due to the number of Chinese characters, a different determination method is used when differentiating between traditional ("Hant") and simplified ("Hans") Chinese.
	elseif self:getCode() == "Hant" or self:getCode() == "Hans" then
		local num, charData = 0, self:getCode() == "Hant" and mw.loadData("Módulo:zh/datos/ts") or mw.loadData("Módulo:zh/datos/st")
		for char in text:gmatch("[\194-\244][\128-\191]*") do
			num = num + (charData[char] and 1 or 0)
		end
		return num
	end
	return select(2, mw.ustring.gsub(text, "[" .. self._rawData.characters .. "]", ""))
end

function Script:hasCapitalization()
	return not not self._rawData.capitalized
end

function Script:hasSpaces()
	return self._rawData.spaces ~= false
end

function Script:isTransliterated()
	return self._rawData.translit ~= false
end

--[==[Returns true if the script is (sometimes) sorted by scraping page content, meaning that it is sensitive to changes in capitalization during sorting.]==]
function Script:sortByScraping()
	return not not self._rawData.sort_by_scraping
end

--[==[Returns the text direction, if any. Currently, left-to-right scripts are unmarked, while most right-to-left scripts have direction specified as {{code|lua|"rtl"}} and Mongolian as {{code|lua|"down"}}.]==]
function Script:getDirection()
	return self._rawData.direction
end

function Script:getRawData()
	return self._rawData
end

--[==[Returns {{code|lua|true}} if the script contains characters that require fixes to Unicode normalization under certain circumstances, {{code|lua|false}} if it doesn't.]==]
function Script:hasNormalizationFixes()
	return not not self._rawData.normalizationFixes
end

Script.__index = Script

function export.makeObject(code, data)
	return data and setmetatable({_rawData = data, _code = code}, Script) or nil
end

--[==[Finds the script whose code matches the one provided. If it exists, it returns a {{code|lua|Script}} object representing the script. Otherwise, it returns {{code|lua|nil}}, unless <span class="n">paramForError</span> is given, in which case an error is generated. If <code class="n">paramForError</code> is {{code|lua|true}}, a generic error message mentioning the bad code is generated; otherwise <code class="n">paramForError</code> should be a string or number specifying the parameter that the code came from, and this parameter will be mentioned in the error message along with the bad code.]==]
function export.getByCode(code, useRequire)
	if code == nil then
		return nil
	end

	local data
	if useRequire then
		data = require("Módulo:scripts/datos")[code]
	else
		data = mw.loadData("Módulo:scripts/datos")[code]
	end

	local retval = export.makeObject(code, data)

	if not retval then
        return nil
	end

	return retval
end

return export