Modulis:scripts
Šī moduļa dokumentāciju var izveidot Modulis:scripts/doc lapā
local export = {}
local Script = {}
function Script:getRawData()
return self._rawData
end
function Script:getCode()
return self._code
end
function Script:getCanonicalName()
return self._rawData.names[1]
end
function Script:getAllNames()
return self._rawData.names
end
function Script:countCharacters(text)
if not self._rawData.characters then
return 0
else
local _, num = mw.ustring.gsub(text, "[" .. self._rawData.characters .. "]", "")
return num
end
end
function Script:getCategoryName()
local name = self._rawData.names[1]
-- If the name already has "script" in it, don't add it.
if name:find("[Ss]script$") then
return name
else
return name .. " script"
end
end
Script.__index = Script
-- The object cache implements memoisation, and is used to avoid duplication
-- of objects. If you request the same script code twice, you should also get
-- the same object twice, not two different objects with identical data.
-- It might also speed things up a bit.
local object_cache = {}
function export.getByCode(code)
if object_cache[code] then
return object_cache[code]
end
local rawData = mw.loadData("Module:scripts/data")[code]
if not rawData then
return nil
end
local object = setmetatable({ _rawData = rawData, _code = code }, Script)
object_cache[code] = object
return object
end
function export.getScriptByCode(code)
require("Module:debug").track("getScriptByCode")
return export.getByCode(code)
end
-- Find the best script to use, based on the characters of a string.
function export.findBestScript(text, lang)
if not text or not lang then
return export.getByCode("None")
end
local scripts = lang:getScripts()
-- Try to match every script against the text,
-- and return the one with the most matching characters.
local bestcount = 0
local bestscript = nil
for i, script in ipairs(scripts) do
local count = script:countCharacters(text)
if count > bestcount then
bestcount = count
bestscript = script
end
end
if bestscript then
return bestscript
end
-- No matching script was found. Return "None".
return export.getByCode("None")
end
return export