Diff: Module:File link
Comparing revision #1 (2020-04-01 06:31:54) with revision #2 (2023-02-02 05:25:39).
| Old | New |
|---|---|
-- This module provides a library for formatting file wikilinks. |
-- This module provides a library for formatting file wikilinks. |
local yesno = require('Module:Yesno') |
local yesno = require('Module:Yesno') |
local checkType = require('libraryUtil').checkType |
local checkType = require('libraryUtil').checkType |
local p = {} |
local p = {} |
function p._main(args) |
function p._main(args) |
checkType('_main', 1, args, 'table') |
checkType('_main', 1, args, 'table') |
-- This is basically libraryUtil.checkTypeForNamedArg, but we are rolling our |
-- This is basically libraryUtil.checkTypeForNamedArg, but we are rolling our |
-- own function to get the right error level. |
-- own function to get the right error level. |
local function checkArg(key, val, level) |
local function checkArg(key, val, level) |
if type(val) ~= 'string' then |
if type(val) ~= 'string' then |
error(string.format( |
error(string.format( |
"type error in '%s' parameter of '_main' (expected string, got %s)", |
"type error in '%s' parameter of '_main' (expected string, got %s)", |
key, type(val) |
key, type(val) |
), level) |
), level) |
end |
end |
end |
end |
local ret = {} |
local ret = {} |
-- Adds a positional parameter to the buffer. |
-- Adds a positional parameter to the buffer. |
local function addPositional(key) |
local function addPositional(key) |
local val = args[key] |
local val = args[key] |
if not val then |
if not val then |
return nil |
return nil |
end |
end |
checkArg(key, val, 4) |
checkArg(key, val, 4) |
ret[#ret + 1] = val |
ret[#ret + 1] = val |
end |
end |
-- Adds a named parameter to the buffer. We assume that the parameter name |
-- Adds a named parameter to the buffer. We assume that the parameter name |
-- is the same as the argument key. |
-- is the same as the argument key. |
local function addNamed(key) |
local function addNamed(key) |
local val = args[key] |
local val = args[key] |
if not val then |
if not val then |
return nil |
return nil |
end |
end |
checkArg(key, val, 4) |
checkArg(key, val, 4) |
ret[#ret + 1] = key .. '=' .. val |
ret[#ret + 1] = key .. '=' .. val |
end |
end |
-- Filename |
-- Filename |
checkArg('file', args.file, 3) |
checkArg('file', args.file, 3) |
ret[#ret + 1] = 'File:' .. args.file |
ret[#ret + 1] = 'File:' .. args.file |
-- Format |
-- Format |
if args.format then |
if args.format then |
checkArg('format', args.format) |
checkArg('format', args.format) |
if args.formatfile then |
if args.formatfile then |
checkArg('formatfile', args.formatfile) |
checkArg('formatfile', args.formatfile) |
ret[#ret + 1] = args.format .. '=' .. args.formatfile |
ret[#ret + 1] = args.format .. '=' .. args.formatfile |
else |
else |
ret[#ret + 1] = args.format |
ret[#ret + 1] = args.format |
end |
end |
end |
end |
-- Border |
-- Border |
if yesno(args.border) then |
if yesno(args.border) then |
ret[#ret + 1] = 'border' |
ret[#ret + 1] = 'border' |
end |
end |
addPositional('location') |
addPositional('location') |
addPositional('alignment') |
addPositional('alignment') |
addPositional('size') |
addPositional('size') |
addNamed('upright') |
addNamed('upright') |
addNamed('link') |
addNamed('link') |
addNamed('alt') |
addNamed('alt') |
addNamed('page') |
addNamed('page') |
addNamed('class') |
addNamed('class') |
addNamed('lang') |
addNamed('lang') |
addNamed('start') |
addNamed('start') |
addNamed('end') |
addNamed('end') |
addNamed('thumbtime') |
addNamed('thumbtime') |
addPositional('caption') |
addPositional('caption') |
return string.format('[[%s]]', table.concat(ret, '|')) |
return string.format('[[%s]]', table.concat(ret, '|')) |
end |
end |
function p.main(frame) |
function p.main(frame) |
local origArgs = require('Module:Arguments').getArgs(frame, { |
local origArgs = require('Module:Arguments').getArgs(frame, { |
wrappers = 'Template:File link' |
wrappers = 'Template:File link' |
}) |
}) |
if not origArgs.file then |
if not origArgs.file then |
error("'file' parameter missing from [[Template:File link]]", 0) |
error("'file' parameter missing from [[Template:File link]]", 0) |
end |
end |
-- Copy the arguments that were passed to a new table to avoid looking up |
-- Copy the arguments that were passed to a new table to avoid looking up |
-- every possible parameter in the frame object. |
-- every possible parameter in the frame object. |
local args = {} |
local args = {} |
for k, v in pairs(origArgs) do |
for k, v in pairs(origArgs) do |
-- Make _BLANK a special argument to add a blank parameter. For use in |
-- Make _BLANK a special argument to add a blank parameter. For use in |
-- conditional templates etc. it is useful for blank arguments to be |
-- conditional templates etc. it is useful for blank arguments to be |
-- ignored, but we still need a way to specify them so that we can do |
-- ignored, but we still need a way to specify them so that we can do |
-- things like [[File:Example.png|link=]]. |
-- things like [[File:Example.png|link=]]. |
if v == '_BLANK' then |
if v == '_BLANK' then |
v = '' |
v = '' |
end |
end |
args[k] = v |
args[k] = v |
end |
end |
return p._main(args) |
return p._main(args) |
end |
end |
return p |
return p |