Module:File link: Difference between revisions

rename the image table to a fileLink table, seeing as this code is applicable to all file links
m (Mr. Stradivarius moved page Module:Image to Module:File link without leaving a redirect: this is applicable to all file links, not just images)
(rename the image table to a fileLink table, seeing as this code is applicable to all file links)
Line 1:
-- This module provides a library for formatting imagefile wikilinks.
 
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
 
local imagefileLink = {}
 
function imagefileLink.new(filename)
checkType('imagefileLink.new', 1, filename, 'string', true)
local obj, data = {}, {}
local checkSelf = libraryUtil.makeCheckSelfFunction(
'imagefileLink',
'imagefileLink',
obj,
'imagefileLink object'
)
-- Set the filename if we were passed it as an input to imagefileLink.new.
if filename then
data.theName = filename
Line 23:
function data:name(s)
checkSelf(self, 'imagefileLink:name')
checkType('imagefileLink:name', 1, s, 'string')
data.theName = s
return self
Line 30:
function data:format(s, filename)
checkSelf(self, 'imagefileLink:format')
checkType('imagefileLink:format', 1, s, 'string', true)
checkType('imagefileLink:format', 2, format, 'string', true)
local validFormats = {
thumb = true,
Line 45:
else
error(string.format(
"bad argument #1 to 'imagefileLink:format' ('%s' is not a valid format)",
s
), 2)
Line 62:
function data:width(px)
checkSelf(self, 'imagefileLink:width')
checkType('imagefileLink:width', 1, px, 'number', true)
if px and data.isUpright then
sizeError('imagefileLink:width')
end
data.theWidth = px
Line 72:
function data:height(px)
checkSelf(self, 'imagefileLink:height')
checkType('imagefileLink:height', 1, px, 'number', true)
if px and data.isUpright then
sizeError('imagefileLink:height')
end
data.theHeight = px
Line 82:
function data:upright(isUpright, factor)
checkSelf(self, 'imagefileLink:upright')
checkType('imagefileLink:upright', 1, isUpright, 'boolean', true)
checkType('imagefileLink:upright', 2, factor, 'number', true)
if isUpright and (data.theWidth or data.theHeight) then
sizeError('imagefileLink:upright')
end
data.isUpright = isUpright
Line 94:
function data:resetSize()
checkSelf(self, 'imagefileLink:resetSize')
for i, field in ipairs{'theWidth', 'theHeight', 'isUpright', 'uprightFactor'} do
data[field] = nil
Line 102:
function data:location(s)
checkSelf(self, 'imagefileLink:location')
checkType('imagefileLink:location', 1, s, 'string', true)
local validLocations = {
right = true,
Line 114:
else
error(string.format(
"bad argument #1 to 'imagefileLink:location' ('%s' is not a valid location)",
s
), 2)
Line 122:
function data:alignment(s)
checkSelf(self, 'imagefileLink:alignment')
checkType('imagefileLink:alignment', 1, s, 'string', true)
local validAlignments = {
baseline = true,
Line 138:
else
error(string.format(
"bad argument #1 to 'imagefileLink:alignment' ('%s' is not a valid alignment)",
s
), 2)
Line 146:
function data:border(hasBorder)
checkSelf(self, 'imagefileLink:border')
checkType('imagefileLink:border', 1, hasBorder, 'boolean', true)
data.hasBorder = hasBorder
return self
Line 153:
function data:link(s)
checkSelf(self, 'imagefileLink:link')
checkType('imagefileLink:link', 1, s, 'string', true)
data.theLink = s
return self
Line 160:
function data:alt(s)
checkSelf(self, 'imagefileLink:alt')
checkType('imagefileLink:alt', 1, s, 'string', true)
data.theAlt = s
return self
Line 167:
function data:page(num)
checkSelf(self, 'imagefileLink:page')
checkType('imagefileLink:page', 1, num, 'number', true)
data.thePage = s
return self
Line 174:
function data:class(s)
checkSelf(self, 'imagefileLink:class')
checkType('imagefileLink:class', 1, s, 'string', true)
data.theClass = s
return self
Line 181:
function data:lang(s)
checkSelf(self, 'imagefileLink:lang')
checkType('imagefileLink:lang', 1, s, 'string', true)
data.theLang = s
return self
Line 188:
function data:caption(s)
checkSelf(self, 'imagefileLink:caption')
checkType('imagefileLink:caption', 1, s, 'string', true)
data.theCaption = s
return self
Line 195:
function data:render()
checkSelf(self, 'imagefileLink:render')
local ret = {}
-- Image name.Filename
if not data.theName then
error('imagefileLink:render: no image namefilename was found')
end
ret[#ret + 1] = 'File:' .. data.theName
-- Image formatFormat
if data.theFormat and data.theFormatFilename then
ret[#ret + 1] = data.theFormat .. '=' .. data.theFormatFilename
Line 277:
readOnlyFields[field] = true
end
readOnlyFields.theName = nil -- This is set if a filename is given to imagefileLink.new, so remove it.
local function restrictedFieldError(key, restriction)
error(string.format(
"imagefileLink object field '%s' is %s",
tostring(key),
restriction
Line 321:
end
 
return imagefileLink