Jump to content

Module:File link: Difference between revisions

516 bytes removed ,  10 years ago
m
Undid revision 611451330 by Mr. Stradivarius (talk) whoops, that edit was supposed to be to the sandbox...
(allow specifying height and width values as strings ending in px)
m (Undid revision 611451330 by Mr. Stradivarius (talk) whoops, that edit was supposed to be to the sandbox...)
Line 5: Line 5:


local fileLink = {}
local fileLink = {}
local function checkTypeStringOrNum(funcName, pos, arg)
local argType = type(arg)
if argType ~= 'nil' and argType ~= 'string' and argType ~= 'number' then
error(string.format(
"bad argument #%d to '%s' (string or number expected, got %s)",
pos,
funcName,
argType
), 3)
end
end


function fileLink.new(filename)
function fileLink.new(filename)
Line 64: Line 52:
end
end


local function validateSize(method, px)
local function sizeError(methodName)
-- Validate input for size-related functions. The px type is checked
-- Used for formatting duplication errors in size-related methods.
-- using checkTypeStringOrNum, so will be a string, a number, or nil.
error(string.format(
if px and data.isUpright then
"duplicate size argument detected in '%s'"
error(string.format(
.. " ('upright' cannot be used in conjunction with height or width)",
"duplicate size argument detected in '%s' ('upright' cannot be"
methodName
.. ' used in conjunction with height or width)',
), 3)
method
), 3)
end
if type(px) == 'string' then
local origPx = px
px = px:match('^(%d+)px$') or px
px = tonumber(px)
if not px then
error(string.format(
"invalid string '%s' passed to '%s'",
origPx,
method
), 3)
end
end
-- px is now a number or nil.
if px and px < 1 or math.floor(px) ~= px then
error(string.format(
"invalid image size specified in '%s' (size must be a positive"
.. 'integer)',
method
), 3)
end
return px
end
end
function data:width(px)
function data:width(px)
checkSelf(self, 'width')
checkSelf(self, 'width')
checkTypeStringOrNum('fileLink:width', 1, px)
checkType('fileLink:width', 1, px, 'number', true)
px = validateSize('fileLink:width', px)
if px and data.isUpright then
sizeError('fileLink:width')
end
data.theWidth = px
data.theWidth = px
return self
return self
Line 107: Line 73:
function data:height(px)
function data:height(px)
checkSelf(self, 'height')
checkSelf(self, 'height')
checkTypeStringOrNum('fileLink:height', 1, px)
checkType('fileLink:height', 1, px, 'number', true)
px = validateSize('fileLink:height', px)
if px and data.isUpright then
sizeError('fileLink:height')
end
data.theHeight = px
data.theHeight = px
return self
return self
Line 217: Line 185:
data.theLang = s
data.theLang = s
return self
return self
end
local function checkTypeStringOrNum(funcName, pos, arg)
local argType = type(arg)
if argType ~= 'nil' and argType ~= 'string' and argType ~= 'number' then
error(string.format(
"bad argument #%d to '%s' (string or number expected, got %s)",
pos,
funcName,
argType
), 3)
end
end
end
Cookies help us deliver our services. By using our services, you agree to our use of cookies.