Module:scripts: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 8: Line 8:
function Script:getCanonicalName()
function Script:getCanonicalName()
return self._rawData.canonicalName
return self._rawData.canonicalName
end
function Script:getDisplayForm()
return self:getCategoryName("nocap")
end
end


Line 49: Line 53:




function Script:getCategoryName()
function Script:getCategoryName(nocap)
local name = self._rawData.canonicalName
local name = self._rawData.canonicalName
-- If the name already has "code" or "semaphore" in it, don't add it.
-- If the name already has "code" or "semaphore" in it, don't add it.
-- No names contain "script".
-- No names contain "script".
if name:find("[Cc]ode$") or name:find("[Ss]emaphore$") then
if not name:find("[Cc]ode$") and not name:find("[Ss]emaphore$") then
return name
name = name .. " script"
else
end
return name .. " script"
if not nocap then
name = mw.getContentLanguage():ucfirst(name)
end
end
return name
end
function Script:makeCategoryLink()
return "[[:Category:" .. self:getCategoryName() .. "|" .. self:getDisplayForm() .. "]]"
end
end


Line 103: Line 114:
local ret = {
local ret = {
canonicalName = self:getCanonicalName(),
canonicalName = self:getCanonicalName(),
categoryName = self:getCategoryName(),
categoryName = self:getCategoryName("nocap"),
code = self._code,
code = self._code,
otherNames = self:getOtherNames(true),
otherNames = self:getOtherNames(true),
Line 128: Line 139:




function export.getByCode(code, paramForError)
function export.getByCode(code, paramForError, disallowNil)
if type(code) ~= "string" and paramForError then
if code == nil and not disallowNil then
error("The function getByCode expects a string as its first argument, but received " .. (code == nil and "nil" or "a " .. type(code)) .. ".")
return nil
end
end
local retval = export.makeObject(code, mw.loadData("Module:scripts/data")[code])
local retval = export.makeObject(code, mw.loadData("Module:scripts/data")[code])
if not retval and paramForError then
if not retval and paramForError then
if paramForError == true then
require("Module:languages").err(code, paramForError, "script code", nil, "not real lang")
error("The script code \"" .. code .. "\" is not valid.")
else
require("Module:languages").err(code, paramForError, "script code", nil, "not real lang")
end
end
end
return retval
return retval
Line 154: Line 161:


-- Find the best script to use, based on the characters of a string.
-- Find the best script to use, based on the characters of a string.
function export.findBestScript(text, lang)
-- If forceDetect is set, run the detection algorithm even if there's only one
-- possible script; in that case, if the text isn't in the script, the return
-- value will be None.
function export.findBestScript(text, lang, forceDetect)
if not text or not lang or not lang.getScripts then
if not text or not lang or not lang.getScripts then
return export.getByCode("None")
return export.getByCode("None")
Line 161: Line 171:
local scripts = lang:getScripts()
local scripts = lang:getScripts()
if not scripts[2] then
if not scripts[2] and not forceDetect then
return scripts[1]
return scripts[1]
end
end