Module:Gallery

Template-info.svg 模块文档  [创建] [刷新]
  1. -- This module implements {{gallery}}
  2. local p = {}
  3. local templatestyles = 'Template:Gallery/styles.css'
  4. local function trim(s)
  5. return (mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1"))
  6. end
  7. function p.gallery(frame)
  8. local origArgs
  9. -- If called via #invoke, use the args passed into the invoking template.
  10. -- Otherwise, for testing purposes, assume args are being passed directly in.
  11. if type(frame.getParent) == 'function' then
  12. origArgs = frame:getParent().args
  13. else
  14. origArgs = frame
  15. end
  16. -- ParserFunctions considers the empty string to be false, so to preserve the previous
  17. -- behavior of {{gallery}}, change any empty arguments to nil, so Lua will consider
  18. -- them false too.
  19. local args = {}
  20. for k, v in pairs(origArgs) do
  21. if v ~= '' then
  22. args[k] = v
  23. end
  24. end
  25. local tbl = mw.html.create('table')
  26. if args.state then
  27. tbl
  28. :addClass('gallery-mod-collapsible')
  29. :addClass('collapsible')
  30. :addClass(args.state)
  31. :addClass(args.class)
  32. end
  33. if args.style then
  34. tbl:cssText(args.style)
  35. else
  36. tbl
  37. :addClass('gallery-mod')
  38. end
  39. if args.align then
  40. if args.align == 'center' then
  41. tbl
  42. :addClass('gallery-mod-center')
  43. else
  44. tbl:css('float', args.align)
  45. end
  46. end
  47. -- <gallery>把packed的边框也取消了,所以这里给packed也设置不加边框
  48. if (args.mode or '') == 'nolines' or (args.mode or '') == 'packed' then
  49. tbl:addClass('gallery-mod-nolines')
  50. end
  51. if args.title then
  52. tbl
  53. :tag('tr')
  54. :tag('th')
  55. :addClass('gallery-mod-title')
  56. :wikitext(args.title)
  57. end
  58. local mainCell = tbl:tag('tr'):tag('td')
  59. local imageCount = math.ceil(#args / 2)
  60. local cellWidth = tonumber(args.cellwidth) or tonumber(args.width) or 180
  61. local imgHeight = tonumber(args.height) or 180
  62. local lines = tonumber(args.lines) or 2
  63. local captionstyle = args.captionstyle
  64. for i = 1, imageCount do
  65. local img = trim(args[i*2 - 1] or '')
  66. local caption = trim(args[i*2] or '')
  67. local imgWidth = args['width' .. i] or args.width or '180'
  68. local alt = args['alt' .. i] or ''
  69. if (args.mode or '') == 'packed' then imgWidth = '' end
  70. local textWidth
  71. if cellWidth < 30 then
  72. textWidth = imgHeight + 27
  73. else
  74. textWidth = cellWidth + 7
  75. end
  76. if img ~= '' then
  77. local imgTbl = mainCell:tag('table')
  78. local img_wikitext = function()
  79. if frame:callParserFunction{ name = 'filepath', args = { img:gsub('^%s-[Ff][Ii][Ll][Ee]:', '') } } ~= '' then
  80. -- 内部图片
  81. return string.format('[[%s|center|%sx%dpx|alt=%s|%s]]', img, imgWidth, imgHeight, alt, mw.text.unstrip(caption))
  82. else
  83. -- 外部图片
  84. return string.format('<img src="%s" alt="%s" style="max-width:%spx; max-height:%dpx" >', img, alt, imgWidth, imgHeight)
  85. end
  86. end
  87. imgTbl
  88. :css('width', (cellWidth + 20) .. 'px')
  89. :addClass('gallery-mod-box')
  90. :tag('tr')
  91. :tag('td')
  92. :addClass('thumb')
  93. :css('height', (imgHeight + 20) .. 'px')
  94. :wikitext(img_wikitext())
  95. :done()
  96. :done()
  97. :tag('tr')
  98. :addClass('gallery-mod-text')
  99. :tag('td')
  100. :addClass('core')
  101. :tag('div')
  102. :addClass('caption')
  103. :css('min-height', (0.1 + 1.5*lines) .. 'em')
  104. :css('width', textWidth .. 'px')
  105. :cssText(captionstyle)
  106. :wikitext(caption .. '&nbsp;')
  107. end
  108. end
  109. if args.footer then
  110. tbl
  111. :tag('tr')
  112. :tag('td')
  113. :addClass('gallery-mod-footer')
  114. :wikitext(args.footer)
  115. end
  116. if args.perrow then
  117. tbl:css('width', 8 + (cellWidth + 20 + 6)*tonumber(args.perrow) .. 'px')
  118. end
  119. return frame:extensionTag{ name = 'templatestyles', args = { src = templatestyles} } .. tostring(tbl)
  120. end
  121. return p
返回顶部