Module:Portal pictures/sandbox explained

local getArgs = require('Module:Arguments').getArgslocal randomModule = require('Module:Random')local yesno = require('Module:Yesno')local slideshowModule = require('Module:Random slideshow')

p =

--

Utility functions

local function getCleanArgs(frame) return getArgs(frame,)end

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 captionslocal function replaceNewlines(s) return mw.ustring.gsub(s, '%c', '
')end

--

Argument preparation functions

-- Extracts dates, custom images, and subpages from argslocal 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, 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] or "" local image = table.insert(images, 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, subpagesend

local function getDefaultMoreLink(args) local root = args['demoroot'] or (mw.title.getCurrentTitle.rootPageTitle.prefixedText) local picturesPage = args['subpage'] or 'Selected picture' return "More selected pictures"end

--

Formatting functions

-- transclude local function formatPortalPotd(frame, potdDate) return frame:expandTemplateend

local function formatMoreHtml(more) if not yesno(more, true) then return "" end local moreHtml = mw.html.create('div') :attr("class", "noprint") :attr("style", "margin-top:0.5em; font-weight:bold; width:100%; text-align:right;") :wikitext(more) return tostring(moreHtml)end

-- manual recreation of the bottom half of local function formatPotdText(frame, titleText, creditText, captionText, moreHtml) local res = frame:expandTemplate if moreHtml 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 res = res .. moreHtml end return resend

local function formatFile(filename) local html = mw.html.create('div') :attr("class", "center") :wikitext("") return tostring(html)end

-- Extract value named "paramName" from a subpage of local function getPotdPart(frame, potdSubpage, paramName) return frame:expandTemplateend

-- bottom half of for a POTD subpagelocal function getPotdText(frame, potdDate, moreHtml) local potdText = getPotdPart(frame, "POTD/" .. potdDate, "portal layout/text") potdText = replaceNewlines(potdText) if moreHtml then potdText = potdText .. moreHtml end return potdTextend

-- prepare arguments for -- alternating: image1, text1, image2, text2, ...local function makeSlideShowArgs(frame, dates, images, subpages, more, limit) local slideShowArgs = local moreHtml = formatMoreHtml(more) if #dates > 0 then local randomDates = randomModule.main('array',) for _, potdDate in ipairs(randomDates) do local image = getPotdPart(frame, "POTD/" .. potdDate, "image") local image2 = if image

'' then image = getPotdPart(frame, "POTD/" .. potdDate, "image1") image2 = '' end local text = image2 .. getPotdText(frame, potdDate, moreHtml) table.insert(slideShowArgs, image) table.insert(slideShowArgs, text) end end if #images > 0 then local randomImages = randomModule.main('array',) for _, image in pairs(randomImages) do table.insert(slideShowArgs, image['file']) local text = formatPotdText(frame, image['title'], image['credit'], image['caption'], moreHtml) table.insert(slideShowArgs, text) end end if #subpages > 0 then local randomSubpages = randomModule.main('array',) for _, subpage in pairs(randomSubpages) do table.insert(slideShowArgs, 'Blank.png') local subpageContent = replaceNewlines(frame:expandTemplate) table.insert(slideShowArgs, subpageContent) end end return slideShowArgsend

--

Main functions

-- Create a list gallery of all passed imageslocal function gallery(frame) local args = getCleanArgs(frame) local dates, images, subpages = extractPictures(args) local texts =

for _, value in pairs(dates) do local topText = '

' .. "\n\n" table.insert(texts, topText .. formatPortalPotd(frame, value)) end for _, image in pairs(images) do local topText = "\n\n" local bottomText = formatPotdText(frame, 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 = "\n\n" local subpageContent = frame:expandTemplate table.insert(texts, topText .. subpageContent) end return frame:preprocess(table.concat(texts, "\n----\n"))end

-- Create a slideshow of passed images using local function slideShow(frame) local args = getCleanArgs(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:extensionTag .. frame:preprocess(slideShow)end

-- entry point for the modulefunction main(frame) local title = mw.title.getCurrentTitle if title.isSubpage then return gallery(frame) else return slideShow(frame) endend

p.main = mainp.slideShow = slideShowp.gallery = gallery

return p