Modiwl:File link

Oddi ar Wicipedia
Jump to navigation Jump to search

This module is used to construct wikitext links to files, using a fluent Lua interface. This is done by creating a fileLink object, which has various methods corresponding to different file link parameters. The module is used from other Lua modules, and cannot be used directly from wiki pages.

Usage[golygu cod y dudalen]

Creating the object[golygu cod y dudalen]

First, you need to import the module.

local fileLink = require('Module:File link')

Then, create the object using the fileLink.new function. The first parameter is the filename, and is optional.

local obj = fileLink.new('Example.png')

Basic usage[golygu cod y dudalen]

You can add parameters to the file link using the fileLink object's methods. (See the Methods section below for the full list.)

obj:width(220)
obj:alt('The alt text')
obj:caption('The caption.')

You can then produce the link wikitext using the object's render method.

obj:render()

This will produce the following wikitext:

[[File:Example.png|220px|alt=The alt text|The caption.]]

Call-chaining[golygu cod y dudalen]

All the object's methods apart from the render method return the object itself, so can be used to call-chain.

obj:width(220):alt('The alt text'):caption('The caption.'):render()

Apart from the name method, all of the object's methods support nil as an input, so call-chaining can be performed with variables whose value is unknown. However, an error will be raised if the input is of an unsupported type for that method. Please see the Methods section for supported input types for each method. Passing nil to a method will overwrite any existing values.

Use with tostring[golygu cod y dudalen]

Instead of using the render method, you can call tostring on the object to create the link wikitext.

obj:width(220):alt('The alt text'):caption('The caption.')
tostring(obj)

Methods[golygu cod y dudalen]

Name[golygu cod y dudalen]

obj:name(s)

Sets the filename. s must be a string. Nil values are not accepted, as every file link requires a filename. The filename can also be set in the first parameter to fileLink.new, although in that case it is optional.

Format[golygu cod y dudalen]

obj:format(s, filename)

Sets the display format. s must either be nil, or one of the strings 'thumb', 'thumbnail', 'frame', 'framed', or 'frameless'. To see the effect of each of these values, see the images help page on mediawiki.org. Note that the border format is not set with this method, but with the border method. (This is because the border option can be set independently of the values that can be set with this method, for example in the code [[File:Example.png|frameless|border|caption]].)

The filename variable is an optional variable that can be used to set the filename for thumbnails. The filename specified will be used instead of the automatically generated thumbnail. This option doesn't have any effect on the other format options.

Border[golygu cod y dudalen]

obj:border(hasBorder)

Sets a border for images. hasBorder must either be nil, or a boolean value. A border will be set if hasBorder is true. To see the effect of this option, see the images help page on mediawiki.org.

Width[golygu cod y dudalen]

obj:width(px)

Sets a width for the file in pixels. px must either be nil or a number value. Width can be used in conjunction with the height method, but will produce an error if used when the upright option has been set by the upright method.

Height[golygu cod y dudalen]

obj:height(px)

Sets a height for the file in pixels. px must either be nil or a number value. Height can be used in conjunction with the width method, but will produce an error if used when the upright option has been set by the upright method.

Upright[golygu cod y dudalen]

obj:upright(isUpright, factor)

Sets the upright option, used for images that are taller than they are wide. isUpright must either be nil, or a boolean value. The upright option will be set if isUpright is true. factor provides an optional conversion factor for the upright option; the factor is the image's width divided by its height. If no factor is specified, the MediaWiki software uses the default of 0.75. factor must be either nil or a number value. If a width or height has been set with the width or height methods, then setting the upright value will result in an error.

ResetSize[golygu cod y dudalen]

obj:resetSize()

This method clears any size-related data the fileLink object is holding. This includes width, height, the upright option, and any upright factor.

Location[golygu cod y dudalen]

obj:location(s)

Sets the location option. This is also known as the horizontal alignment option. s must either be nil, or one of the strings 'right', 'left', 'center', or 'none'. To see the effect of each of these values, see the images help page on mediawiki.org.

Alignment[golygu cod y dudalen]

obj:alignment(s)

Sets the alignment option. This is also known as the vertical alignment option. s must either be nil, or one of the strings 'baseline', 'middle', 'sub', 'super', 'text-top', 'text-bottom', 'top', or 'bottom'. To see the effect of each of these values, see the images help page on mediawiki.org.

Link[golygu cod y dudalen]

obj:link(s)

Sets a link target for the file, instead of the default target of the file description page. s must either be nil or a string value. The link can be a wiki page or a URL, although the exact formatting of the string is not checked by this module. To specify no link, use the blank string: obj:link('').

Alt[golygu cod y dudalen]

obj:alt(s)

Sets alt text for the file. s must either be nil or a string value.

Page[golygu cod y dudalen]

obj:page(num)

Sets a page number for multi-paged files such as PDFs. num must either be nil or a number.

Class[golygu cod y dudalen]

obj:class(s)

Adds a class parameter to image links. The MediaWiki software adds this parameter to the class="..." attribute of the image's <img /> element when the page is rendered into HTML. s must either be nil or a string value.

Lang[golygu cod y dudalen]

obj:lang(s)

Adds a language attribute to specify what language to render the file in. s must either be nil or a string value. This module does not check whether the language tag is valid or not.

StartTime[golygu cod y dudalen]

obj:startTime(time)

Specifies a start time for audio and video files. time must be nil, a number, or a string value. Numbers are interpreted as the number of seconds since the start of the file. Strings can be in the format "mm:ss" to specify the number of minutes and seconds of a file, or "ss" to specify the number of seconds since the start of the file.

EndTime[golygu cod y dudalen]

obj:endTime(time)

Specifies an end time for audio and video files. time must be nil, a number, or a string value. Numbers are interpreted as the number of seconds since the start of the file. Strings can be in the format "mm:ss" to specify the number of minutes and seconds of a file, or "ss" to specify the number of seconds since the start of the file.

ThumbTime[golygu cod y dudalen]

obj:thumbTime(time)

Specifies the time to use to generate the thumbnail image for video files. time must be nil, a number, or a string value. Numbers are interpreted as the number of seconds since the start of the file. Strings can be in the format "mm:ss" to specify the number of minutes and seconds of a file, or "ss" to specify the number of seconds since the start of the file.

Caption[golygu cod y dudalen]

obj:caption(s)

Sets a caption for the file. s must either be nil or a string value.

Render[golygu cod y dudalen]

obj:render()

Renders the link wikitext.



-- 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