Module:Portal pictures
Appearance
![]() | This module depends on the following other modules: |
Implements template {{Portal pictures}}.
Usage
{{#invoke:Portal pictures|function name}}
{{#invoke:Portal pictures|main}}
- Automatic mode. Uses function
slideShow
on a basepage, andgallery
on a subpage. {{#invoke:Portal pictures|slideShow}}
- Shows a slide show with a random picture selected. Used for testing.
{{#invoke:Portal pictures|gallery}}
- Shows all pictures. Used for testing.
Parameters
See Template:Portal pictures/doc#Parameters
See also
local getArgs = require('Module:Arguments').getArgs
local randomModule = require('Module:Random')
local yesno = require('Module:Yesno')
local slideshowModule = require('Module:Random slideshow')
p = {}
-- == Utility functions ==
local function wikiError(message)
return mw.html.create('div'):addClass('error'):wikitext(message)
end
-- replace all newlines in the string with break tags
-- slideshow only supports single line captions
local function replaceNewlines(s)
return mw.ustring.gsub(s, '%c', '<br />')
end
-- == Argument preparation functions ==
-- Extracts dates, custom images, and subpages from args
local function extractPictures(args)
local dates = {}
local images = {}
local subpages = {}
for key, value in pairs(args) do
if value then
if type(key) == "number" then
-- positional parameters correspond to POTD dates
table.insert(dates, key, value)
elseif string.sub(key, 1, 1) == "i" then
-- named parameters with prefixes "i", "title", "credit", and "caption"
-- correspond to custom images
local num = string.sub(key, 2)
local title = args["title" .. num]
local credit = args["credit" .. num]
local caption = args["caption" .. num]
local image = {
file = value,
title = title,
credit = credit,
caption = caption
}
table.insert(images, tonumber(num), image)
end
end
end
local subpageMax = tonumber(args['subpagemax']) or 0
if subpageMax > 0
then
local root = args['demoroot'] or (mw.title.getCurrentTitle().rootPageTitle.prefixedText)
local picturesRoot = root .. '/' .. (args['subpage'] or 'Selected picture')
for i = 1, subpageMax do
table.insert(subpages, picturesRoot .. '/' .. tostring(i))
end
end
return dates, images, subpages
end
local function getDefaultMoreLink(args)
local root = args['demoroot'] or (mw.title.getCurrentTitle().rootPageTitle.prefixedText)
local picturesPage = args['subpage'] or 'Selected picture'
return "[[" .. root .. "/" .. picturesPage .. "|More selected pictures]]"
end
-- == Formatting functions ==
-- transclude [[Template:Portal POTD]]
local function getPortalPotdText(frame, potdDate, more)
return frame:expandTemplate{
title = "Portal POTD",
args = {
potdDate,
["more"] = more
}
}
end
-- manual recreation of the bottom half of [[Template:Portal POTD]]
local function formatPotdText(titleText, creditText, captionText, more)
local root = mw.html.create("div")
:attr("class", "center")
local titleCredit = root:tag("div")
:attr("style", "margin: 0.6em 0.25em 0.85em;")
titleCredit:tag("div")
:attr("style", "font-size: large;")
:wikitext(titleText)
titleCredit:tag("small"):wikitext(creditText)
root:tag("div")
:attr("style", "text-align: left;")
:wikitext(captionText)
if yesno(more, true)
then
-- ideally, the "more" text should be trancluded _once_ for the whole box
-- but we do it for _every_ image for compatibility with portals, which use numbered subpages
root:tag('div')
:attr("class", "noprint")
:attr("style", "margin-top:0.5em; font-weight:bold; width:100%; text-align:right;")
:wikitext(more)
end
return tostring(root)
end
local function formatFile(filename)
local html = mw.html.create('div')
:attr("class", "center")
:wikitext("[[File:" .. filename .. "|380px]]")
return tostring(html)
end
-- Extract value named "paramName" from a subpage of [[Template:POTD]]
local function getPotdPart(frame, potdSubpage, paramName)
return frame:expandTemplate{
title = potdSubpage,
args = { paramName }
}
end
-- bottom half of [[Template:Portal POTD]] for a POTD subpage
local function getPotdText(frame, potdDate, more)
local titleText = getPotdPart(frame, "POTD/" .. potdDate, "title")
local creditText = getPotdPart(frame, "POTD/" .. potdDate, "credit")
local captionText = getPotdPart(frame, "POTD/" .. potdDate, "caption")
captionText = replaceNewlines(captionText)
return formatPotdText(titleText, creditText, captionText, more)
end
-- prepare arguments for [[Module:Random slideshow]]
-- alternating: image1, text1, image2, text2, ...
local function makeSlideShowArgs(frame, dates, images, subpages, more, limit)
local slideShowArgs = {}
local randomDates = randomModule.main('array', {t=dates, limit=limit})
for _, potdDate in ipairs(randomDates) do
local image = getPotdPart(frame, "POTD/" .. potdDate, "image")
local text = getPotdText(frame, potdDate, more)
table.insert(slideShowArgs, image)
table.insert(slideShowArgs, text)
end
local randomImages = randomModule.main('array', {t=images, limit=limit})
for _, image in pairs(randomImages) do
table.insert(slideShowArgs, image['file'])
local text = formatPotdText(image['title'], image['credit'], image['caption'], more)
table.insert(slideShowArgs, text)
end
for _, subpage in pairs(subpages) do
table.insert(slideShowArgs, 'Blank.png')
local subpageContent = replaceNewlines(frame:expandTemplate{ title = subpage })
table.insert(slideShowArgs, subpageContent)
end
return slideShowArgs
end
-- == Main functions ==
-- Create a list gallery of all passed images
local function gallery(frame)
local args = getArgs(frame)
local dates, images, subpages = extractPictures(args)
local texts = {}
for _, value in pairs(dates) do
local topText = "[[Template:POTD/" .. value .. "]]\n\n"
table.insert(texts, topText .. getPortalPotdText(frame, value, "no"))
end
for _, image in pairs(images) do
local topText = "[[:File:" .. image['file'] .. "]]\n\n"
local bottomText = formatPotdText(image['title'], image['credit'], image['caption'])
table.insert(texts, topText .. formatFile(image['file']) .. "\n\n" .. bottomText)
end
for _, subpage in pairs(subpages) do
local topText = "[[:" .. subpage .. "]]\n\n"
local subpageContent = frame:expandTemplate{ title = subpage }
table.insert(texts, topText .. subpageContent)
end
return frame:preprocess(table.concat(texts, "\n----\n"))
end
-- Create a slideshow of passed images using [[Module:Random slideshow]]
local function slideShow(frame)
local args = getArgs(frame)
local more = args["more"] or getDefaultMoreLink(args)
local dates, images, subpages = extractPictures(args)
local limit = 15
local slideShowArgs = makeSlideShowArgs(frame, dates, images, subpages, more, limit)
local slideShow = slideshowModule._main(slideShowArgs, false, 'portalSlideshow-container')
return frame:preprocess(slideShow)
end
-- entry point for the module
function main(frame)
local title = mw.title.getCurrentTitle()
if title.isSubpage
then
return gallery(frame)
else
return slideShow(frame)
end
end
p.main = main
p.slideShow = slideShow
p.gallery = gallery
return p