Module:File link: Difference between revisions
Content added Content deleted
Enwikipedia>Mr. Stradivarius m (Protected Module:File link: High-risk Lua module ([Edit=Allow only template editors and admins] (indefinite) [Move=Allow only template editors and admins] (indefinite))) |
Enwikipedia>Mr. Stradivarius (allow specifying height and width values as strings ending in px) |
||
Line 5: | Line 5: | ||
local fileLink = {} |
local fileLink = {} |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
function fileLink.new(filename) |
function fileLink.new(filename) |
||
Line 52: | Line 64: | ||
end |
end |
||
local function |
local function validateSize(method, px) |
||
-- |
-- Validate input for size-related functions. The px type is checked |
||
-- using checkTypeStringOrNum, so will be a string, a number, or nil. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
methodName |
|||
⚫ | |||
⚫ | |||
method |
|||
⚫ | |||
⚫ | |||
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 |
|||
-- 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) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
data.theWidth = px |
data.theWidth = px |
||
return self |
return self |
||
Line 73: | Line 107: | ||
function data:height(px) |
function data:height(px) |
||
checkSelf(self, 'height') |
checkSelf(self, 'height') |
||
checkTypeStringOrNum('fileLink:height', 1, px) |
|||
⚫ | |||
if px and data.isUpright then |
|||
⚫ | |||
⚫ | |||
data.theHeight = px |
data.theHeight = px |
||
return self |
return self |
||
Line 185: | Line 217: | ||
data.theLang = s |
data.theLang = s |
||
return self |
return self |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||