Module:File link

From the Croc Wiki, the Croc encyclopedia
Revision as of 00:45, May 30, 2014 by wikipedia>Mr. Stradivarius (beginnings of an image library)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

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



local image = {}

function image.new()
	local obj, data = {}, {}
	
	function data:name(s)
		self.theName = s
	end
	
	function data:format(s, filename)
		local validFormats = {
			thumb = true,
			thumbnail = true,
			frame = true,
			framed = true,
			frameless = true
		}
		if validFormats[s] then
			self.theFormat = s
			self.theFormatFilename = filename
		else
			error('invalid format')
		end
	end
	
	function data:width(px)
		self.theWidth = px
	end
	
	function data:height(px)
		self.theHeight = px
	end
	
	function data:upright(factor)
		self.isUpright = true
		self.uprightFactor = factor
	end
	
	function data:resetSize()
		for i, field in ipairs{'theWidth', 'theHeight', 'isUpright', 'uprightFactor'} do
			self[field] = nil
		end
	end
	
	function data:location(s)
		local validLocations = {
			right = true,
			left = true,
			center = true,
			none = true
		}
		if s and validLocations[s] then
			self.theLocation = s
		else
			error(string.format(
				"bad argument #1 to 'image:location'"
					.. " (must be one of 'right', 'left', 'center' or 'none'; got '%s').",
				tostring(s)
			))
		end
	end
	
	function data:alignment(s)
		local validAlignments = {
			baseline = true,
			middle = true,
			sub = true,
			super = true,
			['text-top'] = true,
			['text-bottom'] = true,
			top = true,
			bottom = true
		}
		if s and validAlignments[s] then
			self.theAlignment = s
		else
			error(string.format(
				"bad argument #1 to 'data:alignment'"
			))
		end
	end
	
	function data:border()
		self.hasBorder = true
	end
	
	function data:link(s)
		self.theLink = s
	end
	
	function data:alt(s)
		self.theAlt = s
	end
	
	function data:caption(s)
		self.theCaption = s
	end
	
	function data:render()
	end
	
	return obj
end

return image