Module:File link/doc

From the Croc Wiki, the Croc encyclopedia
Revision as of 09:21, June 1, 2014 by wikipedia>Mr. Stradivarius (add size-related methods)
Jump to navigationJump to search

This is the documentation page for Module:File link

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

Creating the object

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

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

Call-chaining

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

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

Name

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

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

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

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

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

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.