Please log in first.
Diff: Module:Parameter names example
Comparing revision #1 (2020-03-26 04:07:47) with revision #2 (2023-02-03 04:18:00).
| Old | New |
|---|---|
-- This module implements {{parameter names example}}. |
-- This module implements {{parameter names example}}. |
local p = {} |
local p = {} |
local function makeParam(s) |
local function makeParam(s) |
local lb = '{' |
local lb = '{' |
local rb = '}' |
local rb = '}' |
return lb:rep(3) .. s .. rb:rep(3) |
return lb:rep(3) .. s .. rb:rep(3) |
end |
end |
local function italicize(s) |
local function italicize(s) |
return "''" .. s .. "''" |
return "''" .. s .. "''" |
end |
end |
local function plain(s) |
local function plain(s) |
return s |
return s |
end |
end |
function p._main(args, frame) |
function p._main(args, frame) |
-- Find how we want to format the arguments to the template. |
-- Find how we want to format the arguments to the template. |
local formatFunc |
local formatFunc |
if args._display == 'italics' or args._display == 'italic' then |
if args._display == 'italics' or args._display == 'italic' then |
formatFunc = italicize |
formatFunc = italicize |
elseif args._display == 'plain' then |
elseif args._display == 'plain' then |
formatFunc = plain |
formatFunc = plain |
else |
else |
formatFunc = makeParam |
formatFunc = makeParam |
end |
end |
-- Build the table of template arguments. |
-- Build the table of template arguments. |
local targs = {} |
local targs = {} |
for k, v in pairs(args) do |
for k, v in pairs(args) do |
if type(k) == 'number' then |
if type(k) == 'number' then |
targs[v] = formatFunc(v) |
targs[v] = formatFunc(v) |
elseif not k:find('^_') then |
elseif not k:find('^_') then |
targs[k] = v |
targs[k] = v |
end |
end |
end |
end |
targs['nocat'] = 'yes'; |
targs['nocat'] = 'yes'; |
targs['categories'] = 'no'; |
targs['categories'] = 'no'; |
targs['demo'] = 'yes'; |
targs['demo'] = 'yes'; |
-- Find the template name. |
-- Find the template name. |
local template |
local template |
if args._template then |
if args._template then |
template = args._template |
template = args._template |
else |
else |
local currentTitle = mw.title.getCurrentTitle() |
local currentTitle = mw.title.getCurrentTitle() |
if currentTitle.prefixedText:find('/sandbox$') then |
if currentTitle.prefixedText:find('/sandbox$') then |
template = currentTitle.prefixedText |
template = currentTitle.prefixedText |
else |
else |
template = currentTitle.basePageTitle.prefixedText |
template = currentTitle.basePageTitle.prefixedText |
end |
end |
end |
end |
-- Call the template with the arguments. |
-- Call the template with the arguments. |
frame = frame or mw.getCurrentFrame() |
frame = frame or mw.getCurrentFrame() |
local success, result = pcall( |
local success, result = pcall( |
frame.expandTemplate, |
frame.expandTemplate, |
frame, |
frame, |
{title = template, args = targs} |
{title = template, args = targs} |
) |
) |
if success then |
if success then |
return result |
return result |
else |
else |
return '' |
return '' |
end |
end |
end |
end |
function p.main(frame) |
function p.main(frame) |
local args = require('Module:Arguments').getArgs(frame, { |
local args = require('Module:Arguments').getArgs(frame, { |
wrappers = 'Template:Parameter names example' |
wrappers = 'Template:Parameter names example' |
}) |
}) |
return p._main(args, frame) |
return p._main(args, frame) |
end |
end |
return p |
return p |