Editing Module:File link

From MINR.ORG WIKI

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
-- This module provides a library for formatting file wikilinks.
+
local image = {}
  
local yesno = require('Module:Yesno')
+
function image.new()
local checkType = require('libraryUtil').checkType
+
local obj, data = {}, {}
 
+
local p = {}
+
function data:name(s)
 
+
self.theName = s
function p._main(args)
+
end
checkType('_main', 1, args, 'table')
+
 
+
function data:format(s, filename)
-- This is basically libraryUtil.checkTypeForNamedArg, but we are rolling our
+
local validFormats = {
-- own function to get the right error level.
+
thumb = true,
local function checkArg(key, val, level)
+
thumbnail = true,
if type(val) ~= 'string' then
+
frame = true,
error(string.format(
+
framed = true,
"type error in '%s' parameter of '_main' (expected string, got %s)",
+
frameless = true
key, type(val)
+
}
), level)
+
if validFormats[s] then
 +
self.theFormat = s
 +
self.theFormatFilename = filename
 +
else
 +
error('invalid format')
 
end
 
end
 
end
 
end
 
+
local ret = {}
+
function data:width(px)
 
+
self.theWidth = px
-- Adds a positional parameter to the buffer.
+
end
local function addPositional(key)
+
local val = args[key]
+
function data:height(px)
if not val then
+
self.theHeight = px
return nil
+
end
 +
 +
function data:upright(factor)
 +
self.isUpright = true
 +
self.uprightFactor = factor
 +
end
 +
 +
function data:resetSize()
 +
for i, field in ipairs{'theWidth', 'theHeight', 'isUpright', 'uprightFactor'} do
 +
self[field] = nil
 
end
 
end
checkArg(key, val, 4)
 
ret[#ret + 1] = val
 
 
end
 
end
 
+
-- Adds a named parameter to the buffer. We assume that the parameter name
+
function data:location(s)
-- is the same as the argument key.
+
local validLocations = {
local function addNamed(key)
+
right = true,
local val = args[key]
+
left = true,
if not val then
+
center = true,
return nil
+
none = true
 +
}
 +
if s and validLocations[s] then
 +
self.theLocation = s
 +
else
 +
error(string.format(
 +
"bad argument #1 to 'image:location'"
 +
.. " (must be one of 'right', 'left', 'center' or 'none'; got '%s').",
 +
tostring(s)
 +
))
 
end
 
end
checkArg(key, val, 4)
 
ret[#ret + 1] = key .. '=' .. val
 
 
end
 
end
 
+
-- Filename
+
function data:alignment(s)
checkArg('file', args.file, 3)
+
local validAlignments = {
ret[#ret + 1] = 'File:' .. args.file
+
baseline = true,
 
+
middle = true,
-- Format
+
sub = true,
if args.format then
+
super = true,
checkArg('format', args.format)
+
['text-top'] = true,
if args.formatfile then
+
['text-bottom'] = true,
checkArg('formatfile', args.formatfile)
+
top = true,
ret[#ret + 1] = args.format .. '=' .. args.formatfile
+
bottom = true
 +
}
 +
if s and validAlignments[s] then
 +
self.theAlignment = s
 
else
 
else
ret[#ret + 1] = args.format
+
error(string.format(
 +
"bad argument #1 to 'data:alignment'"
 +
))
 
end
 
end
 
end
 
end
 
+
-- Border
+
function data:border()
if yesno(args.border) then
+
self.hasBorder = true
ret[#ret + 1] = 'border'
+
end
 +
 +
function data:link(s)
 +
self.theLink = s
 +
end
 +
 +
function data:alt(s)
 +
self.theAlt = s
 
end
 
end
 
+
addPositional('location')
+
function data:caption(s)
addPositional('alignment')
+
self.theCaption = s
addPositional('size')
 
addNamed('upright')
 
addNamed('link')
 
addNamed('alt')
 
addNamed('page')
 
addNamed('class')
 
addNamed('lang')
 
addNamed('start')
 
addNamed('end')
 
addNamed('thumbtime')
 
addPositional('caption')
 
 
 
return string.format('[[%s]]', table.concat(ret, '|'))
 
end
 
 
 
function p.main(frame)
 
local origArgs = require('Module:Arguments').getArgs(frame, {
 
wrappers = 'Template:File link'
 
})
 
if not origArgs.file then
 
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
+
function data:render()
-- every possible parameter in the frame object.
 
local args = {}
 
for k, v in pairs(origArgs) do
 
-- Make _BLANK a special argument to add a blank parameter. For use in
 
-- 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
 
-- things like [[File:Example.png|link=]].
 
if v == '_BLANK' then
 
v = ''
 
end
 
args[k] = v
 
 
end
 
end
return p._main(args)
+
 +
return obj
 
end
 
end
  
return p
+
return image

Please note that all contributions to MINR.ORG WIKI may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see MINR.ORG WIKI:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)