Module:File link/doc: Difference between revisions
add three methods |
no longer needed |
||
(25 intermediate revisions by 10 users not shown) | |||
Line 1: | Line 1: | ||
{{Module rating|protected}} |
|||
This module is used to construct wikitext links to files, using a fluent Lua interface. This is done by creating a <var>fileLink</var> 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. |
|||
{{Lua|Module:Yesno|Module:Arguments}} |
|||
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. For help with wikitext file markup please refer to the [[mw:Help:Images|documentation at mediawiki.org]]. |
|||
== Usage == |
== Usage from wikitext == |
||
From wikitext, this module should be called from a template. 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. |
||
< |
<syntaxhighlight lang="lua"> |
||
local |
local mFileLink = require('Module:File link') |
||
</syntaxhighlight> |
|||
</source> |
|||
Then, create the object using the <code>fileLink.new</code> function. The first parameter is the filename, and is optional. |
|||
<source lang="lua"> |
|||
local obj = fileLink.new('Example.png') |
|||
</source> |
|||
=== Basic usage === |
|||
You can add parameters to the file link using the fileLink object's methods. (See [[#Methods|the Methods section]] below for the full list.) |
|||
<source lang="lua"> |
|||
obj:width(220) |
|||
obj:alt('The alt text') |
|||
obj:caption('The caption.') |
|||
</source> |
|||
You can then produce the link wikitext using the object's <code>render</code> method. |
|||
<source lang="lua"> |
|||
obj:render() |
|||
</source> |
|||
=== 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"> |
|||
obj:width(220):alt('The alt text'):caption('The caption.'):render() |
|||
</source> |
|||
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"> |
|||
obj:width(220):alt('The alt text'):caption('The caption.') |
|||
tostring(obj) |
|||
</source> |
|||
== Methods == |
|||
=== Name === |
|||
Then you can make file links using the <code>_main</code> function. |
|||
<source lang="lua"> |
|||
obj:name(s) |
|||
</source> |
|||
<syntaxhighlight lang="lua"> |
|||
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. |
|||
mFileLink._main(args) |
|||
</syntaxhighlight> |
|||
<var>args</var> is a table of arguments that can have the following keys: |
|||
=== Format === |
|||
* <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 <nowiki>''</nowiki> to suppress the default link to the file description page. |
|||
* <code>alt</code> - the alt text. Use the blank string <nowiki>''</nowiki> 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]]. |
|||
<source lang="lua"> |
|||
obj:format(s, filename) |
|||
</source> |
|||
=== Examples === |
|||
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 options, 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>.) |
|||
With the file only: |
|||
=== Border === |
|||
<syntaxhighlight lang="lua"> |
|||
mFileLink.main{file = 'Example.png'} |
|||
-- Renders as [[File:Example.png]] |
|||
</syntaxhighlight> |
|||
With format, size, link and caption options: |
|||
<source lang="lua"> |
|||
<syntaxhighlight lang="lua"> |
|||
obj:border(hasBorder) |
|||
mFileLink.main{ |
|||
</source> |
|||
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.]] |
|||
</syntaxhighlight> |
|||
With format, size, and border: |
|||
Sets a border for the image. <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]]. |
|||
<syntaxhighlight lang="lua"> |
|||
mFileLink.main{ |
|||
file = 'Example.png', |
|||
format = 'frameless', |
|||
size = '220px', |
|||
border = true |
|||
} |
|||
-- Renders as [[File:Example.png|frameless|border|220px]] |
|||
</syntaxhighlight> |
|||
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox|| |
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox|| |
Latest revision as of 01:17, July 19, 2022
Template:Module rating Template:Lua 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. For help with wikitext file markup please refer to the documentation at mediawiki.org.
Usage from wikitext
From wikitext, this module should be called from a template. 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]]