Module:scripts/templates

This module provides access to Module:scripts from templates, so that they can make use of the information stored there.

Exported functionsExported functions

existsexists

{{mw:Extension:Scribunto:#invoke|exists|script code}}

Check whether a script code exists and is valid. It will return "1" if the script code exists, and the empty string "" if it does not.

This is rarely needed, because a script error will result when someone uses a code that is not valid, so you do not need this just to check for errors. However, in case you need to decide different actions based on whether a certain parameter is a script code or something else, this function can be useful.

getByCodegetByCode

{{mw:Extension:Scribunto:#invoke|getByCode|script code|item to look up|index}}

Queries information about a script code.

For example, to request the default (canonical) name of the script whose code is Latn:

{{mw:Extension:Scribunto:#invoke|getByCode|Latn|getCanonicalName}}

To request its second name, if any:

{{mw:Extension:Scribunto:#invoke|getByCode|Latn|getOtherNames|1}}

See alsoSee also


local export = {}

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

function export.getByCode(frame)
	local args = frame.args
	local sc = args[1] or error("Script code (parameter 1) has not been specified.")
	local itemname = args[2] or error("Function to call (parameter 2) has not been specified.")
	
	sc = require("Module:scripts").getByCode(sc) or error("The script code '" .. sc .. "' is not valid.")
	
	-- The item that the caller wanted to look up
	if itemname == "getCanonicalName" then
		return sc:getCanonicalName()
	elseif itemname == "getOtherNames" then
		local index = args[3]; if index == "" then index = nil 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
	else
		error("Requested invalid item name \"" .. itemname .. "\".")
	end
end

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

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

return export
Kategorya:Template interface modules