Difference between revisions of "Module:File link"

From annadreambrush.com/wiki
Jump to navigation Jump to search
imported>Mr. Stradivarius
(beginnings of an image library)
 
m (1 revision imported)
 
(20 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local image = {}
+
-- This module provides a library for formatting file wikilinks.
  
function image.new()
+
local yesno = require('Module:Yesno')
local obj, data = {}, {}
+
local checkType = require('libraryUtil').checkType
+
 
function data:name(s)
+
local p = {}
self.theName = s
+
 
end
+
function p._main(args)
+
checkType('_main', 1, args, 'table')
function data:format(s, filename)
+
 
local validFormats = {
+
-- This is basically libraryUtil.checkTypeForNamedArg, but we are rolling our
thumb = true,
+
-- own function to get the right error level.
thumbnail = true,
+
local function checkArg(key, val, level)
frame = true,
+
if type(val) ~= 'string' then
framed = true,
+
error(string.format(
frameless = true
+
"type error in '%s' parameter of '_main' (expected string, got %s)",
}
+
key, type(val)
if validFormats[s] then
+
), level)
self.theFormat = s
 
self.theFormatFilename = filename
 
else
 
error('invalid format')
 
 
end
 
end
 
end
 
end
+
 
function data:width(px)
+
local ret = {}
self.theWidth = px
+
 
end
+
-- Adds a positional parameter to the buffer.
+
local function addPositional(key)
function data:height(px)
+
local val = args[key]
self.theHeight = px
+
if not val then
end
+
return nil
 
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
+
 
function data:location(s)
+
-- Adds a named parameter to the buffer. We assume that the parameter name
local validLocations = {
+
-- is the same as the argument key.
right = true,
+
local function addNamed(key)
left = true,
+
local val = args[key]
center = true,
+
if not val then
none = true
+
return nil
}
 
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
+
 
function data:alignment(s)
+
-- Filename
local validAlignments = {
+
checkArg('file', args.file, 3)
baseline = true,
+
ret[#ret + 1] = 'File:' .. args.file
middle = true,
+
 
sub = true,
+
-- Format
super = true,
+
if args.format then
['text-top'] = true,
+
checkArg('format', args.format)
['text-bottom'] = true,
+
if args.formatfile then
top = true,
+
checkArg('formatfile', args.formatfile)
bottom = true
+
ret[#ret + 1] = args.format .. '=' .. args.formatfile
}
 
if s and validAlignments[s] then
 
self.theAlignment = s
 
 
else
 
else
error(string.format(
+
ret[#ret + 1] = args.format
"bad argument #1 to 'data:alignment'"
 
))
 
 
end
 
end
 
end
 
end
+
 
function data:border()
+
-- Border
self.hasBorder = true
+
if yesno(args.border) then
 +
ret[#ret + 1] = 'border'
 
end
 
end
+
 
function data:link(s)
+
addPositional('location')
self.theLink = s
+
addPositional('alignment')
 +
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
+
 
function data:alt(s)
+
-- Copy the arguments that were passed to a new table to avoid looking up
self.theAlt = s
+
-- every possible parameter in the frame object.
end
+
local args = {}
+
for k, v in pairs(origArgs) do
function data:caption(s)
+
-- Make _BLANK a special argument to add a blank parameter. For use in
self.theCaption = s
+
-- conditional templates etc. it is useful for blank arguments to be
end
+
-- ignored, but we still need a way to specify them so that we can do
+
-- things like [[File:Example.png|link=]].
function data:render()
+
if v == '_BLANK' then
 +
v = ''
 +
end
 +
args[k] = v
 
end
 
end
+
return p._main(args)
return obj
 
 
end
 
end
  
return image
+
return p

Latest revision as of 14:33, 9 March 2020

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

-- This module provides a library for formatting file wikilinks.

local yesno = require('Module:Yesno')
local checkType = require('libraryUtil').checkType

local p = {}

function p._main(args)
	checkType('_main', 1, args, 'table')

	-- This is basically libraryUtil.checkTypeForNamedArg, but we are rolling our
	-- own function to get the right error level.
	local function checkArg(key, val, level)
		if type(val) ~= 'string' then
			error(string.format(
				"type error in '%s' parameter of '_main' (expected string, got %s)",
				key, type(val)
			), level)
		end
	end

	local ret = {}

	-- Adds a positional parameter to the buffer.
	local function addPositional(key)
		local val = args[key]
		if not val then
			return nil
		end
		checkArg(key, val, 4)
		ret[#ret + 1] = val
	end

	-- Adds a named parameter to the buffer. We assume that the parameter name
	-- is the same as the argument key.
	local function addNamed(key)
		local val = args[key]
		if not val then
			return nil
		end
		checkArg(key, val, 4)
		ret[#ret + 1] = key .. '=' .. val
	end

	-- Filename
	checkArg('file', args.file, 3)
	ret[#ret + 1] = 'File:' .. args.file

	-- Format
	if args.format then
		checkArg('format', args.format)
		if args.formatfile then
			checkArg('formatfile', args.formatfile)
			ret[#ret + 1] = args.format .. '=' .. args.formatfile
		else
			ret[#ret + 1] = args.format
		end
	end

	-- Border
	if yesno(args.border) then
		ret[#ret + 1] = 'border'
	end

	addPositional('location')
	addPositional('alignment')
	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

	-- Copy the arguments that were passed to a new table to avoid looking up
	-- every possible parameter in the frame object.
	local args = {}
	for k, v in pairs(origArgs) do
		-- Make _BLANK a special argument to add a blank parameter. For use in
		-- conditional templates etc. it is useful for blank arguments to be
		-- ignored, but we still need a way to specify them so that we can do
		-- things like [[File:Example.png|link=]].
		if v == '_BLANK' then
			v = ''
		end
		args[k] = v
	end
	return p._main(args)
end

return p