Open main menu

Changes

Module:File link

140 bytes removed, 02:01, 30 May 2014
fix read-only behaviour and remove the test function
checkSelf(self, 'name')
checkType('name', 1, s, 'string')
selfdata.theName = s
end
}
if validFormats[s] then
selfdata.theFormat = s selfdata.theFormatFilename = filename
else
error('invalid format')
checkSelf(self, 'width')
checkType('width', 1, px, 'number')
selfdata.theWidth = px
end
checkSelf(self, 'height')
checkType('height', 1, px, 'number')
selfdata.theHeight = px
end
checkSelf(self, 'upright')
checkType('upright', 1, factor, 'number', true)
selfdata.isUpright = true selfdata.uprightFactor = factor
end
checkSelf(self, 'resetSize')
for i, field in ipairs{'theWidth', 'theHeight', 'isUpright', 'uprightFactor'} do
selfdata[field] = nil
end
end
}
if validLocations[s] then
selfdata.theLocation = s
else
error(string.format(
"bad argument #1 to 'image:location' ('%s' is not a valid location)",
s
))
}
if validAlignments[s] then
selfdata.theAlignment = s
else
error(string.format(
"bad argument #1 to 'image:alignment' ('%s' is not a valid alignment)"
))
end
function data:border()
checkSelf(self, 'border')
selfdata.hasBorder = true
end
checkSelf(self, 'link')
checkType('link', 1, s, 'string')
selfdata.theLink = s
end
checkSelf(self, 'alt')
checkType('alt', 1, s, 'string')
selfdata.theAlt = s
end
checkSelf(self, 'caption')
checkType('caption', 1, s, 'string')
selfdata.theCaption = s
end
function data:render()
checkSelf(self, 'render')
local ret = {}
-- Image name.
if not selfdata.theName then
error('image:render: no image name was found')
end
ret[#ret + 1] = 'File:' .. selfdata.theName
-- Image format
if selfdata.theFormat and selfdata.theFormatFilename then ret[#ret + 1] = selfdata.theFormat .. '=' .. selfdata.theFormatFilename elseif selfdata.theFormat then ret[#ret + 1] = selfdata.theFormat
end
-- Border
if selfdata.hasBorder then
ret[#ret + 1] = 'border'
end
-- Location
ret[#ret + 1] = selfdata.theLocation
-- Alignment
ret[#ret + 1] = selfdata.theAlignment
-- Size
if selfdata.isUpright and (selfdata.theWidth or selfdata.theHeight) then
error("duplicate size value detected in 'render' (height/width cannot be used at the same time as 'upright')")
elseif selfdata.isUpright and selfdata.uprightFactor then ret[#ret + 1] = 'upright=' .. tostring(selfdata.uprightFactor) elseif selfdata.isUpright then
ret[#ret + 1] = 'upright'
elseif selfdata.theWidth and selfdata.theHeight then ret[#ret + 1] = string.format('%dx%dpx', selfdata.theWidth, selfdata.theHeight) elseif selfdata.theWidth then ret[#ret + 1] = tostring(selfdata.theWidth) .. 'px' elseif selfdata.theHeight then ret[#ret + 1] = string.format('x%dpx', selfdata.theHeight)
end
-- Link
if selfdata.theLink then ret[#ret + 1] = 'link=' .. selfdata.theLink
end
-- Alt
if selfdata.theAlt then ret[#ret + 1] = 'alt=' .. selfdata.theAlt
end
-- Caption
ret[#ret + 1] = selfdata.theCaption
return string.format('[[%s]]', table.concat(ret, '|'))
end
-- return image local p = {} function p.test() local myImage = image.new() myImage:name('Foo') return myImage:render()end return p
Anonymous user