Difference between revisions of "Module:File link"

From BESA® Wiki
Jump to: navigation, search
m (1 revision)
(beginnings of an image library)
(20 intermediate revisions by one other user not shown)
Line 1: Line 1:
-- This module provides a library for formatting file wikilinks.
+
local image = {}
  
local yesno = require('Module:Yesno')
+
function image.new()
local checkType = require('libraryUtil').checkType
+
local obj, data = {}, {}
 
+
local p = {}
+
function data:name(s)
 
+
self.theName = s
function p._main(args)
+
end
checkType('_main', 1, args, 'table')
+
 
+
function data:format(s, filename)
-- This is basically libraryUtil.checkTypeForNamedArg, but we are rolling our
+
local validFormats = {
-- own function to get the right error level.
+
thumb = true,
local function checkArg(key, val, level)
+
thumbnail = true,
if type(val) ~= 'string' then
+
frame = true,
error(string.format(
+
framed = true,
"type error in '%s' parameter of '_main' (expected string, got %s)",
+
frameless = true
key, type(val)
+
}
), level)
+
if validFormats[s] then
 +
self.theFormat = s
 +
self.theFormatFilename = filename
 +
else
 +
error('invalid format')
 
end
 
end
 
end
 
end
 
+
local ret = {}
+
function data:width(px)
 
+
self.theWidth = px
-- Adds a positional parameter to the buffer.
+
end
local function addPositional(key)
+
local val = args[key]
+
function data:height(px)
if not val then
+
self.theHeight = px
return nil
+
end
 +
 +
function data:upright(factor)
 +
self.isUpright = true
 +
self.uprightFactor = factor
 +
end
 +
 +
function data:resetSize()
 +
for i, field in ipairs{'theWidth', 'theHeight', 'isUpright', 'uprightFactor'} do
 +
self[field] = nil
 
end
 
end
checkArg(key, val, 4)
 
ret[#ret + 1] = val
 
 
end
 
end
 
+
-- Adds a named parameter to the buffer. We assume that the parameter name
+
function data:location(s)
-- is the same as the argument key.
+
local validLocations = {
local function addNamed(key)
+
right = true,
local val = args[key]
+
left = true,
if not val then
+
center = true,
return nil
+
none = true
 +
}
 +
if s and validLocations[s] then
 +
self.theLocation = s
 +
else
 +
error(string.format(
 +
"bad argument #1 to 'image:location'"
 +
.. " (must be one of 'right', 'left', 'center' or 'none'; got '%s').",
 +
tostring(s)
 +
))
 
end
 
end
checkArg(key, val, 4)
 
ret[#ret + 1] = key .. '=' .. val
 
 
end
 
end
 
+
-- Filename
+
function data:alignment(s)
checkArg('file', args.file, 3)
+
local validAlignments = {
ret[#ret + 1] = 'File:' .. args.file
+
baseline = true,
 
+
middle = true,
-- Format
+
sub = true,
if args.format then
+
super = true,
checkArg('format', args.format)
+
['text-top'] = true,
if args.formatfile then
+
['text-bottom'] = true,
checkArg('formatfile', args.formatfile)
+
top = true,
ret[#ret + 1] = args.format .. '=' .. args.formatfile
+
bottom = true
 +
}
 +
if s and validAlignments[s] then
 +
self.theAlignment = s
 
else
 
else
ret[#ret + 1] = args.format
+
error(string.format(
 +
"bad argument #1 to 'data:alignment'"
 +
))
 
end
 
end
 
end
 
end
 
+
-- Border
+
function data:border()
if yesno(args.border) then
+
self.hasBorder = true
ret[#ret + 1] = 'border'
+
 
end
 
end
 
+
addPositional('location')
+
function data:link(s)
addPositional('alignment')
+
self.theLink = s
addPositional('size')
+
addNamed('upright')
+
addNamed('link')
+
addNamed('alt')
+
addNamed('page')
+
addNamed('class')
+
addNamed('lang')
+
addNamed('start')
+
addNamed('end')
+
addNamed('thumbtime')
+
addPositional('caption')
+
 
+
return string.format('[[%s]]', table.concat(ret, '|'))
+
end
+
 
+
function p.main(frame)
+
local origArgs = require('Module:Arguments').getArgs(frame, {
+
wrappers = 'Template:File link'
+
})
+
if not origArgs.file then
+
error("'file' parameter missing from [[Template:File link]]", 0)
+
 
end
 
end
 
+
-- Copy the arguments that were passed to a new table to avoid looking up
+
function data:alt(s)
-- every possible parameter in the frame object.
+
self.theAlt = s
local args = {}
+
end
for k, v in pairs(origArgs) do
+
-- Make _BLANK a special argument to add a blank parameter. For use in
+
function data:caption(s)
-- conditional templates etc. it is useful for blank arguments to be
+
self.theCaption = s
-- ignored, but we still need a way to specify them so that we can do
+
end
-- things like [[File:Example.png|link=]].
+
if v == '_BLANK' then
+
function data:render()
v = ''
+
end
+
args[k] = v
+
 
end
 
end
return p._main(args)
+
 +
return obj
 
end
 
end
  
return p
+
return image

Revision as of 01:45, 30 May 2014

Documentation for this module may be created at Module:File link/doc

Script error: Lua error: Internal error: The interpreter exited with status 126.

local image = {}

function image.new()
	local obj, data = {}, {}
	
	function data:name(s)
		self.theName = s
	end
	
	function data:format(s, filename)
		local validFormats = {
			thumb = true,
			thumbnail = true,
			frame = true,
			framed = true,
			frameless = true
		}
		if validFormats[s] then
			self.theFormat = s
			self.theFormatFilename = filename
		else
			error('invalid format')
		end
	end
	
	function data:width(px)
		self.theWidth = px
	end
	
	function data:height(px)
		self.theHeight = px
	end
	
	function data:upright(factor)
		self.isUpright = true
		self.uprightFactor = factor
	end
	
	function data:resetSize()
		for i, field in ipairs{'theWidth', 'theHeight', 'isUpright', 'uprightFactor'} do
			self[field] = nil
		end
	end
	
	function data:location(s)
		local validLocations = {
			right = true,
			left = true,
			center = true,
			none = true
		}
		if s and validLocations[s] then
			self.theLocation = s
		else
			error(string.format(
				"bad argument #1 to 'image:location'"
					.. " (must be one of 'right', 'left', 'center' or 'none'; got '%s').",
				tostring(s)
			))
		end
	end
	
	function data:alignment(s)
		local validAlignments = {
			baseline = true,
			middle = true,
			sub = true,
			super = true,
			['text-top'] = true,
			['text-bottom'] = true,
			top = true,
			bottom = true
		}
		if s and validAlignments[s] then
			self.theAlignment = s
		else
			error(string.format(
				"bad argument #1 to 'data:alignment'"
			))
		end
	end
	
	function data:border()
		self.hasBorder = true
	end
	
	function data:link(s)
		self.theLink = s
	end
	
	function data:alt(s)
		self.theAlt = s
	end
	
	function data:caption(s)
		self.theCaption = s
	end
	
	function data:render()
	end
	
	return obj
end

return image