Module:File link
Appearance
From Wikibooks, open books for an open world
You might want to create a documentation page for this Scribunto module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
-- This module provides a library for formatting file wikilinks. localyesno=require('Module:Yesno') localcheckType=require('libraryUtil').checkType localp={} 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'then error(string.format( "type error in '%s' parameter of '_main' (expected string, got %s)", key,type(val) ),level) end end localret={} -- Adds a positional parameter to the buffer. localfunctionaddPositional(key) localval=args[key] ifnotvalthen returnnil 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. localfunctionaddNamed(key) localval=args[key] ifnotvalthen returnnil end checkArg(key,val,4) ret[#ret+1]=key..'='..val end -- Filename checkArg('file',args.file,3) ret[#ret+1]='File:'..args.file -- Format ifargs.formatthen checkArg('format',args.format) ifargs.formatfilethen checkArg('formatfile',args.formatfile) ret[#ret+1]=args.format..'='..args.formatfile else ret[#ret+1]=args.format end end -- Border ifyesno(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') returnstring.format('[[%s]]',table.concat(ret,'|')) end functionp.main(frame) localorigArgs=require('Module:Arguments').getArgs(frame,{ wrappers='Template:File link' }) ifnotorigArgs.filethen 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. 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'then v='' end args[k]=v end returnp._main(args) end returnp