Module:Gallery

From the Aadenboy Wiki
Jump to navigation Jump to search

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

local Yesno = require("Module:Yesno")
local File = require("Module:File")
local p = {}

function p.getarg(content, argument)
	local arg = content:match("%|%s*"..argument:gsub("[%^%$%[%]%(%)%+%-%*%?%%%.]", "%%%0").."%s*=%s*([^|}]+)")
    return arg and arg:gsub("<!%-%-.-%-%->", ""):gsub("^%s*(.-)%s*$", "%1")
end

function p.parse(list, callback)
	local build = ""
	local amount = 0
	for item in list:gmatch("[^;]+") do
		local title = mw.title.new(item)
		local content = title and title:getContent()
		if content then
			local result = callback(item, title, content)
			amount = amount + (result and 1 or 0)
			build = build..(result and result.."\n" or "")
		end
	end
	return build:sub(1, -2), amount
end

function p.build(frame) -- so jank I need to redo this later
	local mode = frame.args.mode or "gallery"
	local entries = frame.args.entries or ""
	local expanded = Yesno(frame.args.expanded)
	local maximum = tonumber(frame.args.maximum)
	local strings = {}
	local _, amount = p.parse(entries, function(entry, title, content)
		local title = p.getarg(content, "title")
		local involved = File.fetchInvolvement{args = {p.getarg(content, "involved"), frame.args.involvement}}
		mw.log(p.getarg(content, "involved"), involved, frame.args.involvement)
		local explanation = p.getarg(content, "explanation")
		local transcription = p.getarg(content, "transcription")
		local date = p.getarg(content, "date")
		mw.log(date)
		local str = ""
		if mode == "gallery" then
			str = entry
			    ..((explanation or involved ~= "") and "|" or "")
			    .."''["..(date or "Unknown date").."]''<br />"
			    ..(explanation and "([["..explanation.."]]) " or "")
			    ..involved
			    ..(transcription and "|alt="..transcription or "")
			    .."\n"
		elseif mode == "list" then
			str = string.format("<li>[[%s]]</li>", explanation or ":"..entry)
		end
		table.insert(strings, {mw.getLanguage("en"):formatDate("U", date or "1 January 1970"), str})
		return ""
	end)
	table.sort(strings, function(a, b) return a[1] > b[1] end)
	local data = ""
	for i,v in ipairs(strings) do data = data..v[2].."\n" end
	if mode == "gallery" then
		return (frame:extensionTag(
			"gallery", data,
			amount > (maximum or 5) and {
				class = "mw-collapsible"..(expanded and "" or " mw-collapsed"),
				["data-expandtext"] = "Show all "..amount.." artworks",
				["data-collapsetext"] = "Hide artwork",
				caption = frame.args.caption,
				widths = frame.args.widths,
				heights = frame.args.heights
			} or nil
		):gsub("\n+$", ""))
	elseif mode == "list" then
		return (("<ul"..(amount > (maximum or 15) and string.format(' class="mw-collapsible%s" data-expandtext="%s" data-collapsetext="%s"',
			expanded and "" or " mw-collapsed",
			"Show all "..amount.." artowrks",
			"Hide artwork"
		) or "")..">"..data.."</ul>"):gsub("\n+$", ""))
	end
end

return p