Module:File link: Difference between revisions

Content deleted Content added
add type checks and write the render function; read-only code not working yet
fix read-only behaviour and remove the test function
Line 14:
checkSelf(self, 'name')
checkType('name', 1, s, 'string')
selfdata.theName = s
end
Line 29:
}
if validFormats[s] then
selfdata.theFormat = s
selfdata.theFormatFilename = filename
else
error('invalid format')
Line 39:
checkSelf(self, 'width')
checkType('width', 1, px, 'number')
selfdata.theWidth = px
end
Line 45:
checkSelf(self, 'height')
checkType('height', 1, px, 'number')
selfdata.theHeight = px
end
Line 51:
checkSelf(self, 'upright')
checkType('upright', 1, factor, 'number', true)
selfdata.isUpright = true
selfdata.uprightFactor = factor
end
Line 58:
checkSelf(self, 'resetSize')
for i, field in ipairs{'theWidth', 'theHeight', 'isUpright', 'uprightFactor'} do
selfdata[field] = nil
end
end
Line 72:
}
if validLocations[s] then
selfdata.theLocation = s
else
error(string.format(
"bad argument #1 to 'image:location' ('%s' is not a valid location)",
s
))
Line 95:
}
if validAlignments[s] then
selfdata.theAlignment = s
else
error(string.format(
"bad argument #1 to 'image:alignment' ('%s' is not a valid alignment)"
))
end
Line 105:
function data:border()
checkSelf(self, 'border')
selfdata.hasBorder = true
end
Line 111:
checkSelf(self, 'link')
checkType('link', 1, s, 'string')
selfdata.theLink = s
end
Line 117:
checkSelf(self, 'alt')
checkType('alt', 1, s, 'string')
selfdata.theAlt = s
end
Line 123:
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, '|'))
Line 225 ⟶ 224:
end
 
-- return image
 
local p = {}
 
function p.test()
local myImage = image.new()
myImage:name('Foo')
return myImage:render()
end
 
return p