Jump to content

Module:File link

Permanently protected module
From Wikipedia, the free encyclopedia

-- This module provides a library for formatting file wikilinks.localyesno=require('Module:Yesno')localcheckType=require('libraryUtil').checkTypelocalp={}functionp._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.localfunctioncheckArg(key,val,level)iftype(val)~='string'thenerror(string.format("type error in '%s' parameter of '_main' (expected string, got %s)",key,type(val)),level)endendlocalret={}-- Adds a positional parameter to the buffer.localfunctionaddPositional(key)localval=args[key]ifnotvalthenreturnnilendcheckArg(key,val,4)ret[#ret+1]=valend-- Adds a named parameter to the buffer. We assume that the parameter name-- is the same as the argument key.localfunctionaddNamed(key)localval=args[key]ifnotvalthenreturnnilendcheckArg(key,val,4)ret[#ret+1]=key..'='..valend-- FilenamecheckArg('file',args.file,3)ret[#ret+1]='File:'..args.file-- Formatifargs.formatthencheckArg('format',args.format)ifargs.formatfilethencheckArg('formatfile',args.formatfile)ret[#ret+1]=args.format..'='..args.formatfileelseret[#ret+1]=args.formatendend-- Borderifyesno(args.border)thenret[#ret+1]='border'endaddPositional('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')returnstring.format('[[%s]]',table.concat(ret,'|'))endfunctionp.main(frame)localorigArgs=require('Module:Arguments').getArgs(frame,{wrappers='Template:File link'})ifnotorigArgs.filethenerror("'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.localargs={}fork,vinpairs(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=]].ifv=='_BLANK'thenv=''endargs[k]=vendreturnp._main(args)endreturnp
close