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 getCleanArgs(frame) 	return getArgs(frame, { 		trim = true, 		removeBlanks = true 	}) 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 captions local function replaceNewlines(s) 	return mw.ustring.gsub(s, '%c', '') 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, 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 = { 					file = value, 					title = title, 					credit = credit, 					caption = caption 				} 				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, subpages end  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 Template:Portal POTD local function formatPortalPotd(frame, potdDate) 	return frame:expandTemplate{ 		title = "Portal POTD", 		args = { 			potdDate, 			more = 'no' 		} 	} end  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 Template:Portal POTD local function formatPotdText(frame, titleText, creditText, captionText, moreHtml) 	local res = frame:expandTemplate{ 		title = "POTD portal layout/text", 		args = { 			title = titleText, 			credit = creditText, 			caption = captionText, 			['margin-top']='0.5em' 		} 	} 	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 res end  local function formatFile(filename) 	local html = mw.html.create('div') 		:attr("class", "center") 		:wikitext("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, moreHtml) 	local potdText = getPotdPart(frame, "POTD/" .. potdDate, "portal layout/text") 	potdText = replaceNewlines(potdText) 	if moreHtml 	then 		potdText = potdText .. moreHtml 	end 	return potdText end  -- prepare arguments for Module:Random slideshow -- 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', {t=dates, limit=limit}) 		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 = '200px|center' 			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', {t=images, limit=limit}) 		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', {t=subpages, limit=math.floor(limit / 2)}) 		for _, subpage in pairs(randomSubpages) do 			table.insert(slideShowArgs, 'Blank.png') 			local subpageContent = replaceNewlines(frame:expandTemplate{ title = subpage }) 			table.insert(slideShowArgs, subpageContent) 		end 	end 	return slideShowArgs end  -- == Main functions == -- Create a list gallery of all passed images local function gallery(frame) 	local args = getCleanArgs(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 .. formatPortalPotd(frame, value)) 	end 	for _, image in pairs(images) do 		local topText = "[[:File:" .. image['file'] .. "]]\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 = ":" .. 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 = 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{ name='templatestyles', args = { src='Module:Random slideshow/styles.css'} } .. 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