Module:scripts/templates: Difference between revisions

From Linguifex
Jump to navigation Jump to search
m Chrysophylax moved page Module:Scripts/templates to Module:scripts/templates without leaving a redirect
No edit summary
 
Line 2: Line 2:


function export.exists(frame)
function export.exists(frame)
local args = frame.args
return require("Module:scripts").getByCode(
local sc = args[1] or error("Script code has not been specified. Please pass parameter 1 to the module invocation.")
require("Module:parameters").process(frame.args, {
[1] = {required = true}
sc = require("Module:scripts").getByCode(sc)
})[1]
) and "1" or ""
if sc then
return "1"
else
return ""
end
end
end


function export.getByCode(frame)
function export.getByCode(frame)
local args = frame.args
return require("Module:language-like").templateGetByCode(
local sc = args[1] or error("Script code (parameter 1) has not been specified.")
require("Module:parameters").process(frame.args, {
local itemname = args[2] or error("Function to call (parameter 2) has not been specified.")
[1] = {required = true, type = "script"},
[2] = {required = true},
sc = require("Module:scripts").getByCode(sc) or error("The script code '" .. sc .. "' is not valid.")
[3] = {}
}),
-- The item that the caller wanted to look up
function(itemname)
if itemname == "getCanonicalName" then
if itemname == "countCharacters" then
return sc:getCanonicalName()
local text = args[3] or ""
elseif itemname == "getOtherNames" then
return args[1]:countCharacters(text)
local index = args[3]; if index == "" then index = nil end
end
index = tonumber(index or error("Numeric index of the desired item in the list (parameter 3) has not been specified."))
return sc:getOtherNames()[index] or ""
elseif itemname == "getCategoryName" then
return sc:getCategoryName()
elseif itemname == "countCharacters" then
local text = args[3] or ""
return sc:countCharacters(text)
elseif sc[itemname] then
local ret = sc[itemname](sc)
if type(ret) == "string" then
return ret
else
error("The function \"" .. itemname .. "\" did not return a string value.")
end
end
else
)
error("Requested invalid item name \"" .. itemname .. "\".")
end
end
end


function export.getByCanonicalName(frame)
function export.getByCanonicalName(frame)
local args = frame.args
local sc = require("Module:scripts").getByCanonicalName(
local sc = args[1] or error("Script name (parameter 1) has not been specified.")
require("Module:parameters").process(frame.args, {
[1] = {required = true}
sc = require("Module:scripts").getByCanonicalName(sc)
})[1]
)
if sc then
return sc and sc:getCode() or "None"
return sc:getCode()
else
return "None"
end
end
end


function export.findBestScript(frame)
function export.findBestScriptWithoutLang(frame)
local args = frame.args
local sc = require("Module:scripts").findBestScriptWithoutLang(
local text = args[1] or error("Text to analyse (parameter 1) has not been specified.")
require("Module:parameters").process(frame.args, {
local lang = args[2] or error("Language code (parameter 2) has not been specified.")
[1] = {required = true}
})[1]
lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
)
return sc or "None"
return require("Module:scripts").findBestScript(text, lang):getCode()
end
end


return export
return export

Latest revision as of 11:09, 14 June 2026



local export = {}

function export.exists(frame)
	return require("Module:scripts").getByCode(
		require("Module:parameters").process(frame.args, {
			[1] = {required = true}
		})[1]
	) and "1" or ""
end

function export.getByCode(frame)
	return require("Module:language-like").templateGetByCode(
		require("Module:parameters").process(frame.args, {
			[1] = {required = true, type = "script"},
			[2] = {required = true},
			[3] = {}
		}),
		function(itemname)
			if itemname == "countCharacters" then
				local text = args[3] or ""
				return args[1]:countCharacters(text)
			end
		end
	)
end

function export.getByCanonicalName(frame)
	local sc = require("Module:scripts").getByCanonicalName(
		require("Module:parameters").process(frame.args, {
			[1] = {required = true}
		})[1]
	)
	return sc and sc:getCode() or "None"
end

function export.findBestScriptWithoutLang(frame)
	local sc = require("Module:scripts").findBestScriptWithoutLang(
		require("Module:parameters").process(frame.args, {
			[1] = {required = true}
		})[1]
	)
	return sc or "None"
end

return export