Documentation for this module may be created at Module:File link/doc

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