Module:File link: Difference between revisions

From NvWiki
Jump to navigation Jump to search
Undid revision 1318199768 by Sundarradan (talk) - Whatever that is, it doesn't seem relevant to Wikipedia.
m 1 revision imported: Import modules used with Template:Infobox software
Line 1: Line 1:
== Do we really need this? ==
-- This module provides a library for formatting file wikilinks.


{{ping|Mr. Stradivarius}} We're going to have a library like this tied into PHP soon-ish. Are you sure it's worth building this one? [[User:Jackmcbarn|Jackmcbarn]] ([[User talk:Jackmcbarn|talk]]) 02:32, 30 May 2014 (UTC)
local yesno = require('Module:Yesno')
:{{ping|Jackmcbarn}} Maybe not, if the PHP library also formats image wikilinks. Do you have a link to it? — '''''[[User:Mr. Stradivarius|<span style="color: #194D00; font-family: Palatino, Times, serif">Mr. Stradivarius</span>]]''''' <sup>[[User talk:Mr. Stradivarius|♪ talk ♪]]</sup> 02:35, 30 May 2014 (UTC)
local checkType = require('libraryUtil').checkType
::The WIP is at [[gerrit:67588]], though there's not much to see there (most of the work is being done elsewhere in MediaWiki core). [[User:Jackmcbarn|Jackmcbarn]] ([[User talk:Jackmcbarn|talk]]) 02:38, 30 May 2014 (UTC)
:::I don't see anything there that indicates an overlap - this module is about formatting the image wikitext, not about anything to do with file metadata. So the question is, would there be an overlap between the functionality in core and what I've written here? Or in other words, is the new Scribunto library aiming to format wikitext, or just to provide image data? — '''''[[User:Mr. Stradivarius|<span style="color: #194D00; font-family: Palatino, Times, serif">Mr. Stradivarius</span>]]''''' <sup>[[User talk:Mr. Stradivarius|♪ talk ♪]]</sup> 02:48, 30 May 2014 (UTC)
::::I don't think there will be an overlap then. Carry on. [[User:Jackmcbarn|Jackmcbarn]] ([[User talk:Jackmcbarn|talk]]) 02:53, 30 May 2014 (UTC)


== Colon trick ==
local p = {}


I sometimes use the colon trick to have the link spelled out like <code><nowiki>[[:File:Example.png]]</nowiki></code>. (That is in talkpage and debugging mostly). Does the object provide that option? -[[User:DePiep|DePiep]] ([[User talk:DePiep|talk]]) 08:29, 3 June 2014 (UTC)
function p._main(args)
:Not at the moment, but I have been thinking of adding it in. The question is, how should we set the display value? Using the colon trick on the code <code><nowiki>[[:File:Example.png|thumb|220px|alt=Foo|Bar]]</nowiki></code> produces an output of "[[:File:Example.png|thumb|220px|alt=Foo|Bar]]", so we will have to set the display value explicitly. How about an <code>escape</code> method with parameters {{code|obj:escape(isEscaped, display)|lua}}? The link would only be escaped if <var>isEscaped</var> was true, and <var>display</var> would set a custom display value, with the default being the full filename. Let me know what you think. — '''''[[User:Mr. Stradivarius|<span style="color: #194D00; font-family: Palatino, Times, serif">Mr. Stradivarius</span>]]''''' <sup>[[User talk:Mr. Stradivarius|♪ talk ♪]]</sup> 08:50, 3 June 2014 (UTC)
checkType('_main', 1, args, 'table')
::I've gone ahead and added the escape method to [[Module:File link/sandbox]]. — '''''[[User:Mr. Stradivarius|<span style="color: #194D00; font-family: Palatino, Times, serif">Mr. Stradivarius</span>]]''''' <sup>[[User talk:Mr. Stradivarius|♪ talk ♪]]</sup> 09:18, 3 June 2014 (UTC)
:::Yes, a fine solution (btw, not my specialty, but there are no iw or sister project prefixes involved?). -[[User:DePiep|DePiep]] ([[User talk:DePiep|talk]]) 09:31, 3 June 2014 (UTC)
::::It's possible to use an interwiki link to link to a file, yes. But it's not possible to display that file on your local wiki, as far as I know - unless it's on Commons and you're using InstantCommons. If we want to set interwiki prefixes for files, it brings up the question of how those interwiki prefixes get set, and of what happens when the file is not escaped but an interwiki prefix is set. Do you ignore the prefix, and output what may be a completely different file? Or do you output an escaped link, where the author was expecting a file to be displayed? I think it would be best to skirt around this issue by not providing any interwiki prefix options at all - at least that way the module would be consistent. — '''''[[User:Mr. Stradivarius|<span style="color: #194D00; font-family: Palatino, Times, serif">Mr. Stradivarius</span>]]''''' <sup>[[User talk:Mr. Stradivarius|♪ talk ♪]]</sup> 10:21, 3 June 2014 (UTC)
:::::OK, I can get it. -[[User:DePiep|DePiep]] ([[User talk:DePiep|talk]]) 18:02, 3 June 2014 (UTC)


== Size in ''number'' or in ''number''px ==
-- 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


I understand that the size is to be entered as a number for number-of-px (say "250"). My (feature) suggestion is to allow & accept "250px" too. I remember always having to go to the documentation for a given template, to check whether the unit should be present or absent. To compare, [[module:InfoboxImage]] requires "250px" not "250" (here I sigh). Seen from the editors point, it would be an obvious feature. (What also could be in play, is when this object replaces existing wikicode templates -- they can have the 'wrong' format. Must say, I do not oversee this point). -[[User:DePiep|DePiep]] ([[User talk:DePiep|talk]]) 18:13, 3 June 2014 (UTC)
local ret = {}
:That's a good idea, and I've added the code to the sandbox. I also added a check to make sure that the number given is a positive integer. I've also been toying the idea of adding a <code>size</code> method, where you can specify any of width, height, upright and upright ratio, and the method works out which ones you entered. Do you think that would be useful, or is it redundant to the width, height and upright methods already included? — '''''[[User:Mr. Stradivarius|<span style="color: #194D00; font-family: Palatino, Times, serif">Mr. Stradivarius</span>]]''''' <sup>[[User talk:Mr. Stradivarius|♪ talk ♪]]</sup> 06:19, 4 June 2014 (UTC)
::Thx. I have too little experience with the issue you mention. When I encounter this need, I always make a little research & documentation check (starting at [[WP:EIS]]). That existing parameter interacting logic is a puzzle (also with border & format & caption interactions).
::If you mean to reproduce file linking as it already does (reading that "x250px" is a height setting &tc), that sounds like a covering of existing parameter option, so that could be good from programmers POV. -[[User:DePiep|DePiep]] ([[User talk:DePiep|talk]]) 13:42, 4 June 2014 (UTC)


{{od|2}}
-- Adds a positional parameter to the buffer.
===More general===
local function addPositional(key)
{{U|Mr. Stradivarius}} Please allow me. This is not about this specific module.Read and use as you think good.<br>In <nowiki>[[:File]]</nowiki> usage I encounter logic that says: "thumb does not show a border, but it will not show the caption (unless alt= is set); then do not set width."<br>
local val = args[key]
This is pre-wiki logic (logic it is), and I will not spend time on it. For an article editor, this is deadly. For a template builder (like me), it is deadly too. -[[User:DePiep|DePiep]] ([[User talk:DePiep|talk]]) 21:21, 6 June 2014 (UTC)
if not val then
:What do you mean? for <code><nowiki>[[:File]]</nowiki></code>, the whole link is escaped, so thumb, caption and width have no meaning. — '''''[[User:Mr. Stradivarius|<span style="color: #194D00; font-family: Palatino, Times, serif">Mr. Stradivarius</span>]]''''' <sup>[[User talk:Mr. Stradivarius|♪ talk ♪]]</sup> 00:19, 7 June 2014 (UTC)
return nil
::(into a subsection topic). I meant to give an example if interacting parameter settings, with complicated logic. My text example was made up. For real (from the documentation): "'''frameless''', like thumbnail, respect user preferences for image width, but without border and no right float" (+ a caption effect; DP). That is not parameters set independently. A more modern user interface could provide a editor-oriented set of independent parameters. I see no obvious logic to disappear the caption (I intentionally entered as a caption) because of a border setting. -[[User:DePiep|DePiep]] ([[User talk:DePiep|talk]]) 13:38, 9 June 2014 (UTC)
end
checkArg(key, val, 4)
ret[#ret + 1] = val
end


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


What does the ''class'' add, exactly? Do I understand that is/are supposed to be class names? Any WP: link? -[[User:DePiep|DePiep]] ([[User talk:DePiep|talk]])
-- Filename
:See [[mw:Help:Images#Syntax]]. I've updated the module documentation. '''''[[User:Mr. Stradivarius|<span style="color: #194D00; font-family: Palatino, Times, serif">Mr. Stradivarius</span>]]''''' <sup>[[User talk:Mr. Stradivarius|♪ talk ♪]]</sup> 00:27, 7 June 2014 (UTC)
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

Revision as of 16:56, 26 December 2025

Template:Module rating

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, 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 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.
  • 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]]

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