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

  1. --[[
  2. -- This module produces a link to a main article or articles. It implements the
  3. -- template {{main}}.
  4. --
  5. -- If the module is used in category or category talk space, it produces "The
  6. -- main article for this category is xxx". Otherwise, it produces
  7. -- "Main article: xxx".
  8. --]]
  9.  
  10. local mHatnote = require('Module:Hatnote')
  11. local mTableTools -- lazily initialise
  12. local mArguments -- lazily initialise
  13.  
  14. local p = {}
  15.  
  16. function p.main(frame)
  17. mTableTools = require('Module:TableTools')
  18. mArguments = require('Module:Arguments')
  19. local args = mArguments.getArgs(frame, {parentOnly = true})
  20. local pages = {}
  21. for k, v in pairs(args) do
  22. if type(k) == 'number' then
  23. local display = args['l' .. tostring(k)]
  24. local page = {v, display}
  25. pages[k] = page
  26. end
  27. end
  28. pages = mTableTools.compressSparseArray(pages)
  29. local options = {
  30. selfref = args.selfref
  31. }
  32. return p._main(options, unpack(pages))
  33. end
  34.  
  35. function p._main(options, ...)
  36. -- Get the list of pages. If no first page was specified we use the current
  37. -- page name.
  38. local pages = {...}
  39. local currentTitle = mw.title.getCurrentTitle()
  40. local firstPageTable = pages[1]
  41. local firstPage
  42. if firstPageTable then
  43. firstPage = firstPageTable[1]
  44. else
  45. firstPage = currentTitle.text
  46. firstPageTable = {firstPage}
  47. pages[1] = firstPageTable
  48. end
  49.  
  50. -- Find the pagetype.
  51. local firstPageNs = mHatnote.findNamespaceId(firstPage)
  52. local pagetype = firstPageNs == 0 and 'article' or 'page'
  53.  
  54. -- Make the formatted link text
  55. local links = mHatnote.formatPageTables(unpack(pages))
  56. links = mw.text.listToText(links)
  57.  
  58. -- Build the text.
  59. local isPlural = #pages > 1
  60. local currentNs = currentTitle.namespace
  61. local isCategoryNamespace = currentNs - currentNs % 2 == 14
  62. local stringToFormat
  63. if isCategoryNamespace then
  64. if isPlural then
  65. stringToFormat = 'The main %ss for this'
  66. .. ' [[Wikipedia:Categorization|category]] are %s'
  67. else
  68. stringToFormat = 'The main %s for this'
  69. .. ' [[Wikipedia:Categorization|category]] is %s'
  70. end
  71. else
  72. if isPlural then
  73. stringToFormat = 'Main %ss: %s'
  74. else
  75. stringToFormat = 'Main %s: %s'
  76. end
  77. end
  78. local text = string.format(stringToFormat, pagetype, links)
  79.  
  80. -- Process the options and pass the text to the _rellink function in
  81. -- [[Module:Hatnote]].
  82. options = options or {}
  83. local hnOptions = {
  84. extraclasses = 'relarticle mainarticle',
  85. selfref = options.selfref
  86. }
  87. return mHatnote._hatnote(text, hnOptions)
  88. end
  89.  
  90. return p