Open main menu

Changes

Module:File link

620 bytes added, 07:46, 30 May 2014
enable call-chaining and tweak the error messages to use "image:method" rather than just "method"
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 -- Set the filename if we were passed it as an input to image.new. if filename then data.theName = libraryUtil.checkTypefilename 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,
data.theFormatFilename = filename
else
error(string.format( "bad argument #1 to 'invalid image:format'('%s' is not a valid format)", s ), 2)
end
return self
end
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,
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,
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 = {}
Anonymous user