Module:scripts/templates: difference between revisions
No edit summary |
m remove no-longer-need 'etym_lang = true' setting (manually assisted) Tag: Manual revert |
||
(16 intermediate revisions by 5 users not shown) | |||
Line 2: | Line 2: | ||
function export.exists(frame) |
function export.exists(frame) |
||
⚫ | |||
local args = frame.args |
|||
require("Module:parameters").process(frame.args, { |
|||
local sc = args[1] or error("Script code has not been specified. Please pass parameter 1 to the module invocation.") |
|||
[1] = {required = true} |
|||
⚫ | |||
})[1] |
|||
⚫ | |||
) and "1" or "" |
|||
if sc then |
|||
return "1" |
|||
⚫ | |||
return "" |
|||
⚫ | |||
end |
end |
||
function export.getByCode(frame) |
function export.getByCode(frame) |
||
return require("Module:language-like").templateGetByCode( |
|||
local args = frame.args |
|||
require("Module:parameters").process(frame.args, { |
|||
local sc = args[1] or error("Script code (parameter 1) has not been specified.") |
|||
[1] = {required = true, type = "script"}, |
|||
local itemname = args[2] or error("Function to call (parameter 2) has not been specified.") |
|||
[2] = {required = true}, |
|||
[3] = {} |
|||
sc = require("Module:scripts").getByCode(sc) or error("The script code '" .. sc .. "' is not valid.") |
|||
}), |
|||
function(itemname) |
|||
-- The item that the caller wanted to look up |
|||
if itemname == "countCharacters" then |
|||
⚫ | |||
return sc:getCanonicalName() |
|||
⚫ | |||
elseif itemname == "getOtherNames" then |
|||
end |
|||
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 |
|||
⚫ | |||
⚫ | |||
else |
|||
error("Requested invalid item name \"" .. itemname .. "\".") |
|||
⚫ | |||
end |
end |
||
function export.getByCanonicalName(frame) |
function export.getByCanonicalName(frame) |
||
⚫ | |||
local args = frame.args |
|||
require("Module:parameters").process(frame.args, { |
|||
local sc = args[1] or error("Script name (parameter 1) has not been specified.") |
|||
[1] = {required = true} |
|||
})[1] |
|||
⚫ | |||
) |
|||
⚫ | |||
if sc then |
|||
⚫ | |||
else |
|||
return "None" |
|||
⚫ | |||
end |
end |
||
function export.findBestScript(frame) |
function export.findBestScript(frame) |
||
local args = frame.args |
local args = require("Module:parameters").process(frame.args, { |
||
[1] = {required = true}, |
|||
local text = args[1] or error("Text to analyse (parameter 1) has not been specified.") |
|||
[2] = {required = true, type = "language"}, |
|||
local lang = args[2] or error("Language code (parameter 2) has not been specified.") |
|||
[3] = {} |
|||
}) |
|||
lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.") |
|||
if args[3] == "getCanonicalName" then |
|||
return args[2]:findBestScript(args[1]):getCanonicalName() |
|||
⚫ | |||
return args[2]:findBestScript(args[1]):getCode() |
|||
⚫ | |||
end |
end |
||
Latest revision as of 00:57, 26 June 2024
- The following documentation is located at Module:scripts/templates/documentation. [edit]
- Useful links: root page • root page’s subpages • links • transclusions • testcases • sandbox
This module provides access to Module:scripts from templates, so that they can make use of the information stored there.
Exported functions
exists
{{#invoke:scripts/templates|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.
getByCode
{{#invoke:scripts/templates|getByCode|script code|item to look up|index}}
Queries information about a script code.
- The script code should be one of the codes that is defined in Module:scripts data. If it is missing or does not exist, the result will be a script error.
- The item is the name of one of the pieces of data that is stored for a script, such as
getCanonicalName
orgetCategoryName
. If no item has been provided, the result will be a script error. - The index is optional, and is used for items that are lists, such as
getOtherNames
. It selects which item in the list to return. On items that are single strings, likegetCanonicalName
, it has no effect. If no index is given, the default will be 1 (the first subitem). If an index is given that is higher than the number of items in the list, the result will be an empty string.
For example, to request the default (canonical) name of the script whose code is Latn
:
{{#invoke:scripts/templates|getByCode|Latn|getCanonicalName}}
- Result:
Latin
To request its second name, if any:
{{#invoke:scripts/templates|getByCode|Latn|getOtherNames|1}}
- Result:
Roman
See also
- Module:JSON data — for exporting all the data at once
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.findBestScript(frame)
local args = require("Module:parameters").process(frame.args, {
[1] = {required = true},
[2] = {required = true, type = "language"},
[3] = {}
})
if args[3] == "getCanonicalName" then
return args[2]:findBestScript(args[1]):getCanonicalName()
else
return args[2]:findBestScript(args[1]):getCode()
end
end
return export