Module:File link: Difference between revisions

Content deleted Content added
move duplicate size error checks to the setting functions, fix the alignment error message
enable call-chaining and tweak the error messages to use "image:method" rather than just "method"
Line 2:
 
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
 
local image = {}
 
function image.new(filename)
checkType('image.new', 1, filename, 'string', true)
local obj, data = {}, {}
local checkSelf = libraryUtil.makeCheckSelfFunction(
'image',
'image',
obj,
'image object')
)
local checkType = libraryUtil.checkType
-- Set the filename if we were passed it as an input to image.new.
if filename then
data.theName = filename
end
function data:name(s)
checkSelf(self, 'image:name')
checkType('image:name', 1, s, 'string')
data.theName = s
return self
end
function data:format(s, filename)
checkSelf(self, 'image:format')
checkType('image:format', 1, s, 'string')
checkType('image:format', 2, format, 'string', true)
local validFormats = {
thumb = true,
Line 32 ⟶ 44:
data.theFormatFilename = filename
else
error('invalid string.format')(
"bad argument #1 to 'image:format' ('%s' is not a valid format)",
s
), 2)
end
return self
end
 
Line 46 ⟶ 62:
function data:width(px)
checkSelf(self, 'image:width')
checkType('image:width', 1, px, 'number')
if data.isUpright then
sizeError('image:width')
end
data.theWidth = px
return self
end
function data:height(px)
checkSelf(self, 'image:height')
checkType('image:height', 1, px, 'number')
if data.isUpright then
sizeError('image:height')
end
data.theHeight = px
return self
end
function data:upright(factor)
checkSelf(self, 'image:upright')
checkType('image:upright', 1, factor, 'number', true)
if data.theWidth or data.theHeight then
sizeError('image:upright')
end
data.isUpright = true
data.uprightFactor = factor
return self
end
function data:resetSize()
checkSelf(self, 'image:resetSize')
for i, field in ipairs{'theWidth', 'theHeight', 'isUpright', 'uprightFactor'} do
data[field] = nil
end
return self
end
function data:location(s)
checkSelf(self, 'image:location')
checkType('image:location', 1, s, 'string')
local validLocations = {
right = true,
Line 93 ⟶ 113:
else
error(string.format(
"bad argument #1 to 'image:location' ('%s' is not a valid location)",
s
), 2)
end
return self
end
function data:alignment(s)
checkSelf(self, 'image:alignment')
checkType('image:alignment', 1, s, 'string')
local validAlignments = {
baseline = true,
Line 116 ⟶ 137:
else
error(string.format(
"bad argument #1 to 'image:alignment' ('%s' is not a valid alignment)",
s
), 2)
end
return self
end
function data:border()
checkSelf(self, 'image:border')
data.hasBorder = true
return self
end
function data:link(s)
checkSelf(self, 'image:link')
checkType('image:link', 1, s, 'string')
data.theLink = s
return self
end
function data:alt(s)
checkSelf(self, 'image:alt')
checkType('image:alt', 1, s, 'string')
data.theAlt = s
return self
end
function data:caption(s)
checkSelf(self, 'image:caption')
checkType('image:caption', 1, s, 'string')
data.theCaption = s
return self
end
function data:render()
checkSelf(self, 'image:render')
local ret = {}