Module:File link/doc: Difference between revisions
m →Lang: +lang |
change docs to reflect the new module code |
||
Line 1: | Line 1: | ||
This module is used to construct wikitext links to files |
This module is used to construct wikitext links to files. It is primarily useful for templates and modules that use complicated logic to make file links. Simple file links should be made with wikitext markup directly, as it uses less resources than calling this module. |
||
== Usage == |
== Usage from wikitext == |
||
From wikitext, this module should be called from a template, usually {{tl|file link}}. Please see the template page for documentation. However, it can also be called using the syntax <code><nowiki>{{#invoke:File link|main|</nowiki>''arguments''<nowiki>}}</nowiki></code>. |
|||
=== Creating the object === |
|||
== Usage from Lua == |
|||
First, you need to import the module. |
First, you need to import the module. |
||
<source lang="lua"> |
<source lang="lua"> |
||
local |
local mFileLink = require('Module:File link') |
||
</source> |
</source> |
||
Then |
Then you can make file links using the <code>_main</code> function. |
||
<source lang="lua"> |
<source lang="lua"> |
||
mFileLink._main(args) |
|||
local obj = fileLink.new('Example.png') |
|||
</source> |
</source> |
||
<var>args</var> is a table of arguments that can have the following keys: |
|||
=== Basic usage === |
|||
* <code>file</code> - the filename. (required) |
|||
* <code>format</code> - the file format, e.g. 'thumb', 'thumbnail', 'frame', 'framed', or 'frameless'. |
|||
* <code>formatfile</code> - a filename to specify with the 'thumbnail' format option. The filename specified will be used instead of the automatically generated thumbnail. |
|||
* <code>border</code> - set this to true or "yes" (or any other value recognized as true by [[Module:Yesno]]) to set a border for the image. |
|||
* <code>location</code> - the horizontal alignment of the file, e.g. 'right', 'left', 'center', or 'none'. |
|||
* <code>alignment</code> - the vertical alignment of the file, e.g. 'baseline', 'middle', 'sub', 'super', 'text-top', 'text-bottom', 'top', or 'bottom'. |
|||
* <code>size</code> - the size of the image, e.g. '100px', 'x100px' or '100x100px'. |
|||
* <code>upright</code> - the 'upright' parameter, used for setting the size of tall and thin images. |
|||
* <code>link</code> - the page that the file should link to. Use the blank string '' to suppress the default link to the file description page. |
|||
* <code>alt</code> - the alt text. Use the blank string '' to suppress the default alt text. |
|||
* <code>caption</code> - a caption for the file. |
|||
* <code>page</code> - sets a page number for multi-paged files such as PDFs. |
|||
* <code>class</code> - adds a <code>class</code> parameter to image links. The MediaWiki software adds this parameter to the <code>class="..."</code> attribute of the image's <code><nowiki><img /></nowiki></code> element when the page is rendered into HTML. |
|||
* <code>lang</code> - adds a language attribute to specify what language to render the file in. |
|||
* <code>start</code> - specifies a start time for audio and video files. |
|||
* <code>end</code> - specifies an end time for audio and video files. |
|||
* <code>thumbtime</code> - specifies the time to use to generate the thumbnail image for video files. |
|||
To see the effect of each of these parameters, see the [[mw:Help:Images#Format|images help page on mediawiki.org]]. |
|||
You can add parameters to the file link using the fileLink object's methods. (See [[#Methods|the Methods section]] below for the full list.) |
|||
=== Examples === |
|||
<source lang="lua"> |
|||
obj:width(220) |
|||
obj:alt('The alt text') |
|||
obj:caption('The caption.') |
|||
</source> |
|||
With the file only: |
|||
You can then produce the link wikitext using the object's <code>render</code> method. |
|||
<source lang="lua"> |
<source lang="lua"> |
||
mFileLink.main{file = 'Example.png'} |
|||
obj:render() |
|||
-- Renders as [[File:Example.png]] |
|||
</source> |
</source> |
||
With format, size, link and caption options: |
|||
This will produce the following wikitext: |
|||
<pre> |
|||
[[File:Example.png|220px|alt=The alt text|The caption.]] |
|||
</pre> |
|||
=== Call-chaining === |
|||
All the object's methods apart from the <code>render</code> method return the object itself, so can be used to call-chain. |
|||
<source lang="lua"> |
<source lang="lua"> |
||
mFileLink.main{ |
|||
obj:width(220):alt('The alt text'):caption('The caption.'):render() |
|||
file = 'Example.png', |
|||
format = 'thumb', |
|||
size = '220px', |
|||
link = 'Wikipedia:Sandbox', |
|||
caption - 'An example.' |
|||
} |
|||
-- Renders as [[File:Example.png|thumb|220px|link=Wikipedia:Sandbox|An example.]] |
|||
</source> |
</source> |
||
With format, size, and border: |
|||
Apart from the <code>name</code> 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 [[#Methods|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 <code>render</code> method, you can call <code>tostring</code> on the object to create the link wikitext. |
|||
<source lang="lua"> |
<source lang="lua"> |
||
mFileLink.main{ |
|||
obj:width(220):alt('The alt text'):caption('The caption.') |
|||
file = 'Example.png', |
|||
tostring(obj) |
|||
format = 'frameless', |
|||
size = '220px', |
|||
border = true |
|||
} |
|||
-- Renders as [[File:Example.png|frameless|border|220px]] |
|||
</source> |
</source> |
||
== Methods == |
|||
=== Name === |
|||
<source lang="lua"> |
|||
obj:name(s) |
|||
</source> |
|||
Sets the filename. <var>s</var> 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 <code>fileLink.new</code>, although in that case it is optional. |
|||
=== Format === |
|||
<source lang="lua"> |
|||
obj:format(s, filename) |
|||
</source> |
|||
Sets the display format. <var>s</var> 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 [[mw:Help:Images#Format|images help page on mediawiki.org]]. Note that the ''border'' format is not set with this method, but with the [[#Border|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 <code><nowiki>[[File:Example.png|frameless|border|caption]]</nowiki></code>.) |
|||
The <var>filename</var> 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 === |
|||
<source lang="lua"> |
|||
obj:border(hasBorder) |
|||
</source> |
|||
Sets a border for images. <var>hasBorder</var> must either be nil, or a boolean value. A border will be set if <var>hasBorder</var> is true. To see the effect of this option, see the [[mw:Help:Images#Format|images help page on mediawiki.org]]. |
|||
=== Width === |
|||
<source lang="lua"> |
|||
obj:width(px) |
|||
</source> |
|||
Sets a width for the file in pixels. <var>px</var> must either be nil or a number value. Width can be used in conjunction with the [[#Height|height method]], but will produce an error if used when the ''upright'' option has been set by the [[#Upright|upright method]]. |
|||
=== Height === |
|||
<source lang="lua"> |
|||
obj:height(px) |
|||
</source> |
|||
Sets a height for the file in pixels. <var>px</var> must either be nil or a number value. Height can be used in conjunction with the [[#Width|width method]], but will produce an error if used when the ''upright'' option has been set by the [[#Upright|upright method]]. |
|||
=== Upright === |
|||
<source lang="lua"> |
|||
obj:upright(isUpright, factor) |
|||
</source> |
|||
Sets the ''upright'' option, used for images that are taller than they are wide. <var>isUpright</var> must either be nil, or a boolean value. The upright option will be set if <var>isUpright</var> is true. <var>factor</var> 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. <var>factor</var> must be either nil or a number value. If a width or height has been set with the [[#Width|width]] or [[#Height|height]] methods, then setting the upright value will result in an error. |
|||
=== ResetSize === |
|||
<source lang="lua"> |
|||
obj:resetSize() |
|||
</source> |
|||
This method clears any size-related data the fileLink object is holding. This includes width, height, the upright option, and any upright factor. |
|||
=== Location === |
|||
<source lang="lua"> |
|||
obj:location(s) |
|||
</source> |
|||
Sets the ''location'' option. This is also known as the ''horizontal alignment'' option. <var>s</var> 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 [[mw:Help:Images#Horizontal alignment|images help page on mediawiki.org]]. |
|||
=== Alignment === |
|||
<source lang="lua"> |
|||
obj:alignment(s) |
|||
</source> |
|||
Sets the ''alignment'' option. This is also known as the ''vertical alignment'' option. <var>s</var> 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 [[mw:Help:Images#Vertical alignment|images help page on mediawiki.org]]. |
|||
=== Link === |
|||
<source lang="lua"> |
|||
obj:link(s) |
|||
</source> |
|||
Sets a link target for the file, instead of the default target of the file description page. <var>s</var> 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: {{code|obj:link('')|lua}}. |
|||
=== Alt === |
|||
<source lang="lua"> |
|||
obj:alt(s) |
|||
</source> |
|||
Sets [[WP:ALT|alt text]] for the file. <var>s</var> must either be nil or a string value. |
|||
=== Page === |
|||
<source lang="lua"> |
|||
obj:page(num) |
|||
</source> |
|||
Sets a page number for multi-paged files such as PDFs. <var>num</var> must either be nil or a number. |
|||
=== Class === |
|||
<source lang="lua"> |
|||
obj:class(s) |
|||
</source> |
|||
Adds a <code>class</code> parameter to image links. The MediaWiki software adds this parameter to the <code>class="..."</code> attribute of the image's <code><nowiki><img /></nowiki></code> element when the page is rendered into HTML. <var>s</var> must either be nil or a string value. |
|||
=== Lang === |
|||
<source lang="lua"> |
|||
obj:lang(s) |
|||
</source> |
|||
Adds a language attribute to specify what language to render the file in. <var>s</var> must either be nil or a string value. This module does not check whether the language tag is valid or not. |
|||
=== StartTime === |
|||
<source lang="lua"> |
|||
obj:startTime(time) |
|||
</source> |
|||
Specifies a start time for audio and video files. <var>time</var> 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 === |
|||
<source lang="lua"> |
|||
obj:endTime(time) |
|||
</source> |
|||
Specifies an end time for audio and video files. <var>time</var> 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 === |
|||
<source lang="lua"> |
|||
obj:thumbTime(time) |
|||
</source> |
|||
Specifies the time to use to generate the thumbnail image for video files. <var>time</var> 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 === |
|||
<source lang="lua"> |
|||
obj:caption(s) |
|||
</source> |
|||
Sets a caption for the file. <var>s</var> must either be nil or a string value. |
|||
=== Render === |
|||
<source lang="lua"> |
|||
obj:render() |
|||
</source> |
|||
Renders the link wikitext. |
|||
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox|| |
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox|| |
Revision as of 13:49, December 18, 2014
This module is used to construct wikitext links to files. It is primarily useful for templates and modules that use complicated logic to make file links. Simple file links should be made with wikitext markup directly, as it uses less resources than calling this module.
Usage from wikitext
From wikitext, this module should be called from a template, usually {{file link}}. Please see the template page for documentation. However, it can also be called using the syntax {{#invoke:File link|main|arguments}}
.
Usage from Lua
First, you need to import the module.
local mFileLink = require('Module:File link')
Then you can make file links using the _main
function.
mFileLink._main(args)
args is a table of arguments that can have the following keys:
file
- the filename. (required)format
- the file format, e.g. 'thumb', 'thumbnail', 'frame', 'framed', or 'frameless'.formatfile
- a filename to specify with the 'thumbnail' format option. The filename specified will be used instead of the automatically generated thumbnail.border
- set this to true or "yes" (or any other value recognized as true by Module:Yesno) to set a border for the image.location
- the horizontal alignment of the file, e.g. 'right', 'left', 'center', or 'none'.alignment
- the vertical alignment of the file, e.g. 'baseline', 'middle', 'sub', 'super', 'text-top', 'text-bottom', 'top', or 'bottom'.size
- the size of the image, e.g. '100px', 'x100px' or '100x100px'.upright
- the 'upright' parameter, used for setting the size of tall and thin images.link
- the page that the file should link to. Use the blank string to suppress the default link to the file description page.alt
- the alt text. Use the blank string to suppress the default alt text.caption
- a caption for the file.page
- sets a page number for multi-paged files such as PDFs.class
- adds aclass
parameter to image links. The MediaWiki software adds this parameter to theclass="..."
attribute of the image's<img />
element when the page is rendered into HTML.lang
- adds a language attribute to specify what language to render the file in.start
- specifies a start time for audio and video files.end
- specifies an end time for audio and video files.thumbtime
- specifies the time to use to generate the thumbnail image for video files.
To see the effect of each of these parameters, see the images help page on mediawiki.org.
Examples
With the file only:
mFileLink.main{file = 'Example.png'}
-- Renders as [[File:Example.png]]
With format, size, link and caption options:
mFileLink.main{
file = 'Example.png',
format = 'thumb',
size = '220px',
link = 'Wikipedia:Sandbox',
caption - 'An example.'
}
-- Renders as [[File:Example.png|thumb|220px|link=Wikipedia:Sandbox|An example.]]
With format, size, and border:
mFileLink.main{
file = 'Example.png',
format = 'frameless',
size = '220px',
border = true
}
-- Renders as [[File:Example.png|frameless|border|220px]]