Title: Module:File link - freem URL Source: https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link Markdown Content: _Documentation for this module may be created at [Module:File link/doc](https://freemwiki.com/index.php?title=Module:File\_link/doc&action=edit&redlink=1 "Module:File link/doc (page does not exist)")_ [](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-1)-- This module provides a library for formatting file wikilinks.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-2)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-3)local yesno = require('Module:Yesno')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-4)local checkType = require('libraryUtil').checkType[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-5)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-6)local p = {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-7)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-8)function p._main(args)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-9)checkType('_main', 1, args, 'table')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-10)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-11)-- This is basically libraryUtil.checkTypeForNamedArg, but we are rolling our[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-12)-- own function to get the right error level.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-13)local function checkArg(key, val, level)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-14)if type(val) ~= 'string' then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-15)error(string.format([](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-16)"type error in '%s' parameter of '_main' (expected string, got %s)",[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-17)key, type(val)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-18)), level)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-19)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-20)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-21)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-22)local ret = {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-23)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-24)-- Adds a positional parameter to the buffer.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-25)local function addPositional(key)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-26)local val = args[key][](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-27)if not val then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-28)return nil[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-29)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-30)checkArg(key, val, 4)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-31)ret[#ret + 1] = val[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-32)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-33)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-34)-- Adds a named parameter to the buffer. We assume that the parameter name[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-35)-- is the same as the argument key.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-36)local function addNamed(key)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-37)local val = args[key][](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-38)if not val then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-39)return nil[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-40)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-41)checkArg(key, val, 4)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-42)ret[#ret + 1] = key .. '=' .. val[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-43)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-44)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-45)-- Filename[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-46)checkArg('file', args.file, 3)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-47)ret[#ret + 1] = 'File:' .. args.file[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-48)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-49)-- Format[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-50)if args.format then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-51)checkArg('format', args.format)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-52)if args.formatfile then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-53)checkArg('formatfile', args.formatfile)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-54)ret[#ret + 1] = args.format .. '=' .. args.formatfile[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-55)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-56)ret[#ret + 1] = args.format[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-57)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-58)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-59)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-60)-- Border[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-61)if yesno(args.border) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-62)ret[#ret + 1] = 'border'[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-63)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-64)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-65)addPositional('location')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-66)addPositional('alignment')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-67)addPositional('size')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-68)addNamed('upright')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-69)addNamed('link')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-70)addNamed('alt')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-71)addNamed('page')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-72)addNamed('class')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-73)addNamed('lang')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-74)addNamed('start')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-75)addNamed('end')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-76)addNamed('thumbtime')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-77)addPositional('caption')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-78)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-79)return string.format('[[%s]]', table.concat(ret, '|'))[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-80)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-81)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-82)function p.main(frame)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-83)local origArgs = require('Module:Arguments').getArgs(frame, {[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-84)wrappers = 'Template:File link'[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-85)})[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-86)if not origArgs.file then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-87)error("'file' parameter missing from [[Template:File link]]", 0)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-88)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-89)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-90)-- Copy the arguments that were passed to a new table to avoid looking up[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-91)-- every possible parameter in the frame object.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-92)local args = {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-93)for k, v in pairs(origArgs) do[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-94)-- Make _BLANK a special argument to add a blank parameter. For use in[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-95)-- conditional templates etc. it is useful for blank arguments to be[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-96)-- ignored, but we still need a way to specify them so that we can do[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-97)-- things like [[File:Example.png|link=]].[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-98)if v == '_BLANK' then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-99)v = ''[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-100)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-101)args[k] = v[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-102)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-103)return p._main(args)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-104)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-105)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AFile_link#L-106)return p