Module:File link: Difference between revisions

Created page with " -- This module provides a library for formatting file wikilinks. local libraryUtil = require('libraryUtil') local checkType = libraryUtil.checkType local fileLink = {}..."
Enwikipedia>Mr. Stradivarius
m (Undid revision 611451330 by Mr. Stradivarius (talk) whoops, that edit was supposed to be to the sandbox...)
(Created page with " -- This module provides a library for formatting file wikilinks. local libraryUtil = require('libraryUtil') local checkType = libraryUtil.checkType local fileLink = {}...")
Line 1:
 
 
-- This module provides a library for formatting file wikilinks.
 
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
 
local fileLink = {}
 
function fileLink.new(filename)
checkType('fileLink.new', 1, filename, 'string', true)
local obj, data = {}, {}
local checkSelf = libraryUtil.makeCheckSelfFunction(
'fileLink',
Line 16 ⟶ 18:
'fileLink object'
)
-- Set the filename if we were passed it as an input to fileLink.new.
if filename then
data.theName = filename
end
function data:name(s)
checkSelf(self, 'name')
Line 28 ⟶ 30:
return self
end
function data:format(s, filename)
checkSelf(self, 'format')
Line 51 ⟶ 53:
return self
end
 
local function sizeError(methodName)
-- Used for formatting duplication errors in size-related methods.
Line 60 ⟶ 62:
), 3)
end
function data:width(px)
checkSelf(self, 'width')
Line 70 ⟶ 72:
return self
end
function data:height(px)
checkSelf(self, 'height')
Line 80 ⟶ 82:
return self
end
function data:upright(isUpright, factor)
checkSelf(self, 'upright')
Line 92 ⟶ 94:
return self
end
function data:resetSize()
checkSelf(self, 'resetSize')
Line 100 ⟶ 102:
return self
end
function data:location(s)
checkSelf(self, 'location')
Line 120 ⟶ 122:
return self
end
function data:alignment(s)
checkSelf(self, 'alignment')
Line 144 ⟶ 146:
return self
end
function data:border(hasBorder)
checkSelf(self, 'border')
Line 151 ⟶ 153:
return self
end
function data:link(s)
checkSelf(self, 'link')
Line 158 ⟶ 160:
return self
end
function data:alt(s)
checkSelf(self, 'alt')
Line 165 ⟶ 167:
return self
end
function data:page(num)
checkSelf(self, 'page')
Line 172 ⟶ 174:
return self
end
function data:class(s)
checkSelf(self, 'class')
Line 179 ⟶ 181:
return self
end
function data:lang(s)
checkSelf(self, 'lang')
Line 186 ⟶ 188:
return self
end
 
local function checkTypeStringOrNum(funcName, pos, arg)
local argType = type(arg)
Line 198 ⟶ 200:
end
end
function data:startTime(time)
checkSelf(self, 'startTime')
Line 205 ⟶ 207:
return self
end
function data:endTime(time)
checkSelf(self, 'endTime')
Line 212 ⟶ 214:
return self
end
function data:thumbTime(time)
checkSelf(self, 'thumbTime')
Line 219 ⟶ 221:
return self
end
function data:caption(s)
checkSelf(self, 'caption')
Line 226 ⟶ 228:
return self
end
function data:render()
checkSelf(self, 'render')
local ret = {}
-- Filename
if not data.theName then
Line 236 ⟶ 238:
end
ret[#ret + 1] = 'File:' .. data.theName
-- Format
if data.theFormat and data.theFormatFilename then
Line 243 ⟶ 245:
ret[#ret + 1] = data.theFormat
end
-- Border
if data.hasBorder then
ret[#ret + 1] = 'border'
end
-- Location
ret[#ret + 1] = data.theLocation
 
-- Alignment
ret[#ret + 1] = data.theAlignment
-- Size
if data.isUpright and data.uprightFactor then
Line 267 ⟶ 269:
ret[#ret + 1] = string.format('x%dpx', data.theHeight)
end
-- Render named parameters.
-- That includes link, alt, page, class, lang, start, end, and thumbtime.
Line 289 ⟶ 291:
end
end
 
-- Caption
ret[#ret + 1] = data.theCaption
return string.format('[[%s]]', table.concat(ret, '|'))
end
local privateFields = {
theName = true,
Line 314 ⟶ 316:
theCaption = true
}
local readOnlyFields = {}
for field in pairs(data) do
Line 320 ⟶ 322:
end
readOnlyFields.theName = nil -- This is set if a filename is given to fileLink.new, so remove it.
local function restrictedFieldError(key, restriction)
error(string.format(
Line 328 ⟶ 330:
), 3)
end
setmetatable(obj, {
__index = function (t, key)
Line 359 ⟶ 361:
end
})
return obj
end
 
return fileLink
48

edits