Module:Infobox

From Portals of Phereon Wiki

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

local p = {};
local cargo = mw.ext.cargo;
local utils = require( "Module:Utils" );
--infobox
function p.infobox(frame)
	local args = frame.args["args"] or "";
	args = utils.toTable(args,'=',';','');
	local theme = frame.args["theme"] or "default";
	local theme_source = frame.args["theme-source"];
	local itype = frame.args["type"];
	local accent_color_source = frame.args["accent-color-source"];
	local accent_color_default = frame.args["accent-color-default"];
	local accent_color_text_source = frame.args["accent-color-text-source"];
	local accent_color_text_default = frame.args["accent-color-text-default"];
	local style = "";
	local layout = frame.args["layout"] or "default";
	local name = frame.args["name"];
	local infobox = mw.html.create("aside"):addClass("portable-infobox noexcerpt pi-background"):addClass("pi-layout-"..layout);
	if (theme_source == nil) then infobox:addClass("pi-theme-"..theme); else infobox:addClass("pi-theme-"..args[theme_source]); end
	if (itype ~= nil) then infobox:addClass("pi-type-"..itype); end
	if (accent_color_source ~= nil) then style = "background-color:"..args[accent_color_source]..";"; 
	elseif (accent_color_default ~= nil) then style = "background-color:"..accent_color_default..";"; end
	if (accent_color_text_source ~= nil) then style = style .. "color:"..args[accent_color_text_source]..";";
	elseif (accent_color_text_default ~= nil) then style = style .. "color:"..accent_color_text_default..";"; end
	if (style ~= "") then infobox:attr("style",style); end
	if (name ~= nil) then infobox:attr("data-item-name",name); end
	for i = 1, 100, 1 do 
		if (frame.args[i] == nil) then break end
		local elem = utils.toTable(frame.args[i],'=',';','');
		if (elem["type"] == "title") then 
			infobox:node(titleTag(elem,args)):done();
		elseif (elem["type"] == "image") then 
			infobox:node(imageTag(elem,args)):done();
		elseif (elem["type"] == "header") then 
			infobox:node(headerTag(elem,args)):done();
		elseif (elem["type"] == "navigation") then 
			infobox:node(navigationTag(elem,args)):done();
		elseif (elem["type"] == "data") then 
			infobox:node(dataTag(elem,args)):done();
		elseif (elem["type"] == "group") then 
			infobox:node(groupTag(elem,args)):done();
		elseif (elem["type"] == "panel") then 
			infobox:node(panelTag(elem,args)):done();
		end
	end
    return tostring(infobox);
end
--title
function titleTag(elem,args)
	local source = elem["source"];
	local default = elem["default"];
	local tformat = elem["format"];
	local title = mw.html.create("h2"):addClass("pi-item pi-item-spacing pi-title pi-secondary-background");
	if (source ~= nil) then	title:attr("data-source",source); end
	if (source ~= nil and args[source] ~= nil) then
		if (tformat ~= nil) then
			title:wikitext(tformat);
		else
			title:wikitext(args[source]);
		end
	elseif (default ~= nil) then
		title:wikitext(default);
	end
	return title;
end
--data
--default
--format
function dataTag(elem,args)
	local default = elem["default"];
	local label = elem["label"];
	local dformat = elem["format"];
	local source = elem["source"];
	--local span = elem["span"];
	--local layout = elem["layout"];
	local name = elem["name"];
	local data = mw.html.create("div"):addClass("pi-item pi-data pi-item-spacing pi-border-color");
	if (source ~= nil) then data:attr("data-source",source); end
	if (name ~= nil) then data:attr("data-item-name",name); end
	if (label ~= nil) then data:node(labelTag(label)); end
	data = data:tag('div'):addClass("pi-data-value pi-font");
	if (source ~= nil and args[source] ~= nil) then
		if (dformat ~= nil) then
			data:wikitext(dformat);
		else
			data:wikitext(args[source]);
		end
	elseif (default ~= nil) then
		data:wikitext(default);
	end
	data:done();
	return data;
end
--label
function labelTag(text)
	local label = mw.html.create('h3'):addClass("pi-data-label pi-secondary-font"):wikitext(text):done();
	return label;
end
--image
function imageTag(elem,args)
	local default = elem["default"];
	local alt = elem["alt"];
	local altText = nil;
	local caption = elem["caption"];
	local source = elem["source"];
	local name = elem["name"];
	local image = mw.html.create("figure"):addClass("pi-item pi-image");
	if (source ~= nil) then image:attr("data-source",source); end
	if (name ~= nil) then image:attr("data-item-name",name); end
	--alt
	if (alt ~= nil) then 
		local altElem = utils.toTable(alt,'=',';','');
		if (altElem["source"] ~= nil and args[altElem["source"]] ~= nil) then
			altText = args[altElem["source"]];
		elseif (altElem["default"] ~= nil) then
			altText = args[altElem["default"]];
		end
	end
	if (source ~= nil and args[source] ~= nil) then
		if (args[source]:sub(1, 2) == "[[") then
			if (altText ~= nil) then
				image:wikitext("[[File:"..args[source]:match("%[%[(.-)[|%]]").."|alt="..altText.."]]");
			else
				image:wikitext("[[File:"..args[source]:match("%[%[(.-)[|%]]").."]]");
			end
		else
			if (altText ~= nil) then
				image:wikitext("[[File:"..args[source].."|alt="..altText.."]]");
			else
				image:wikitext("[[File:"..args[source].."]]");
			end
		end
	end
	--caption
	if (caption ~= nil) then 
		local captionElem = utils.toTable(caption,'=',';','');
		local captionTag = mw.html.create("figcaption"):addClass("pi-item-spacing pi-caption");
		if (captionElem["source"] ~= nil and args[captionElem["source"]] ~= nil) then
			if (captionElem["format"] ~= nil) then
				captionTag:wikitext(format);
			else
				captionTag:wikitext(args[captionElem["source"]]);
			end
		elseif (captionElem["default"] ~= nil) then
			captionTag:wikitext(default);
		end
		image:node(captionTag);
	end
	return image;
end
--image horizontal head
function imagehlTag(elem,args)
	local caption = elem["caption"];
	local source = elem["source"];
	local name = elem["name"];
	local image = mw.html.create("th"):addClass("pi-horizontal-group-item pi-font pi-border-color");
	if (caption ~= nil) then 
		local captionElem = utils.toTable(caption,'=',';','');
		local captionTag = mw.html.create("figcaption"):addClass("pi-item-spacing pi-caption");
		if (captionElem["source"] ~= nil and args[captionElem["source"]] ~= nil) then
			if (captionElem["format"] ~= nil) then
				captionTag:wikitext(format);
			else
				captionTag:wikitext(args[captionElem["source"]]);
			end
		elseif (captionElem["default"] ~= nil) then
			captionTag:wikitext(default);
		end
		if (source ~= nil) then captionTag:attr("data-source",source); end
		if (name ~= nil) then captionTag:attr("data-item-name",name); end
		image:node(captionTag);
	end
	return image;
end
--image horizontal body
function imagehdTag(elem,args)
	local default = elem["default"];
	local alt = elem["alt"];
	local altText = nil;
	local source = elem["source"];
	local name = elem["name"];
	local image = mw.html.create("td"):addClass("pi-horizontal-group-item pi-font pi-border-color"):tag("figure"):addClass("pi-item pi-image");
	if (source ~= nil) then image:attr("data-source",source); end
	if (name ~= nil) then image:attr("data-item-name",name); end
	if (alt ~= nil) then 
		local altElem = utils.toTable(alt,'=',';','');
		if (altElem["source"] ~= nil and args[altElem["source"]] ~= nil) then
			altText = args[altElem["source"]];
		elseif (altElem["default"] ~= nil) then
			altText = args[altElem["default"]];
		end
	end
	if (source ~= nil and args[source] ~= nil) then
		if (args[source]:sub(1, 2) == "[[") then
			if (altText ~= nil) then
				image:wikitext("[[File:"..args[source]:match("%[%[(.-)[|%]]").."|alt="..altText.."]]");
			else
				image:wikitext("[[File:"..args[source]:match("%[%[(.-)[|%]]").."]]");
			end
		else
			if (altText ~= nil) then
				image:wikitext("[[File:"..args[source].."|alt="..altText.."]]");
			else
				image:wikitext("[[File:"..args[source].."]]");
			end
		end
	end
	image:done();
	return image;
end
--group
function groupTag(elem,args)
	local layout = elem["layout"] or "default";
	local show = elem["show"] or "default";
	local collapse = elem["collapse"] or "open";
	local row_items = elem["row-items"];
	local name = elem["name"];
	local group = mw.html.create("section"):addClass("pi-item pi-group pi-border-color");
	if (collapse ~= nil and elem[1] ~= nil and elem[1]["type"] == "header") then 
		if (collapse == "open") then
			group:addClass("pi-collapse pi-collapse-open");
		elseif (collapse == "closed") then
			group:addClass("pi-collapse pi-collapse-closed");
		end
	end
	if (name ~= nil) then group:attr("data-item-name",name); end
	if (layout == "horizontal") then
		group = group:tag("table"):addClass("pi-horizontal-group");
		if (frame.args[1] == nil) then
			if (gelem["type"] == "header") then 
				group:node(headerhTag(gelem,args)):done();
				y = 2;
			else
				y = 1;
			end
		end
		group:tag("thead"):tag("tr");
		for i = y, 100, 1 do 
			if (frame.args[i] == nil) then break end
			local gelem = utils.toTable(frame.args[i],'=',';','');
			if (gelem["type"] == "data") then 
				group:node(datahlTag(gelem,args)):done();
			elseif (gelem["type"] == "image") then 
				group:node(imagehlTag(gelem,args)):done();
		--	elseif (gelem["type"] == "group") then 
		--		group:node(groupTag(gelem,args)):done();
		--	elseif (gelem["type"] == "panel") then 
		--		group:node(panelTag(gelem,args)):done();
			end
		end
		group:done():done():tag("tbody"):tag("tr");
		for i = y, 100, 1 do 
			if (frame.args[i] == nil) then break end
			local gelem = utils.toTable(frame.args[i],'=',';','');
			if (gelem["type"] == "data") then 
				group:node(datahdTag(gelem,args)):done();
			elseif (gelem["type"] == "image") then 
				group:node(imagehdTag(gelem,args)):done();
		--	elseif (gelem["type"] == "group") then 
		--		group:node(groupTag(gelem,args)):done();
		--	elseif (gelem["type"] == "panel") then 
		--		group:node(panelTag(gelem,args)):done();
			end
		end
	elseif (row_items ~= nil) then
		for i = 1, 100, 1 do 
			if (frame.args[i] == nil) then break end
			local gelem = utils.toTable(frame.args[i],'=',';','');
			if (gelem["type"] == "data") then 
				group:node(dataTag(gelem,args)):done();
			elseif (gelem["type"] == "header") then 
				group:node(headerTag(gelem,args)):done();
			elseif (gelem["type"] == "image") then 
				group:node(imageTag(gelem,args)):done();
			elseif (gelem["type"] == "title") then 
				group:node(titleTag(gelem,args)):done();
			elseif (gelem["type"] == "group") then 
				group:node(groupTag(gelem,args)):done();
			elseif (gelem["type"] == "navigation") then 
				group:node(navigationTag(gelem,args)):done();
			elseif (gelem["type"] == "panel") then 
				group:node(panelTag(gelem,args)):done();
			end
		end
	else
		for i = 1, 100, 1 do 
			if (frame.args[i] == nil) then break end
			local gelem = utils.toTable(frame.args[i],'=',';','');
			if (gelem["type"] == "data") then 
				group:node(dataTag(gelem,args)):done();
			elseif (gelem["type"] == "header") then 
				group:node(headerTag(gelem,args)):done();
			elseif (gelem["type"] == "image") then 
				group:node(imageTag(gelem,args)):done();
			elseif (gelem["type"] == "title") then 
				group:node(titleTag(gelem,args)):done();
			elseif (gelem["type"] == "group") then 
				group:node(groupTag(gelem,args)):done();
			elseif (gelem["type"] == "navigation") then 
				group:node(navigationTag(gelem,args)):done();
			elseif (gelem["type"] == "panel") then 
				group:node(panelTag(gelem,args)):done();
			end
		end
	end
	group:allDone();
	return group;
end
--header
function headerTag(elem,text)
	local name = elem["name"];
	local header = mw.html.create('h2'):addClass("pi-item pi-header pi-secondary-font pi-item-spacing pi-secondary-background"):wikitext(text):done();
	if (name ~= nil) then header:attr("data-item-name",name); end
	return header;
end
--header horizontal
function headerhTag(elem,text)
	local name = elem["name"];
	local header = mw.html.create('caption'):addClass("pi-header pi-secondary-font pi-secondary-background pi-item-spacing"):wikitext(text):done();
	if (name ~= nil) then header:attr("data-item-name",name); end
	return header;
end
--navigation
function navigationTag(elem,text)
	local name = elem["name"];
	local navigation = mw.html.create('nav'):addClass("pi-navigation pi-item-spacing pi-secondary-font"):wikitext(text):done();
	if (name ~= nil) then navigation:attr("data-item-name",name); end
	return navigation;
end
--data head horizontal
function datahlTag(elem,args)
	local default = elem["default"];
	local label = elem["label"];
	local dformat = elem["format"];
	local source = elem["source"];
	local name = elem["name"];
	local data = mw.html.create("th"):addClass("pi-horizontal-group-item pi-data-label pi-secondary-font pi-border-color pi-item-spacing");
	if (source ~= nil) then data:attr("data-source",source); end
	if (name ~= nil) then data:attr("data-item-name",name); end
	if (label ~= nil) then data:wikitext(label); end
	return data;
end
--data body horizontal
function datahdTag(elem,args)
	local default = elem["default"];
	local label = elem["label"];
	local dformat = elem["format"];
	local source = elem["source"];
	local name = elem["name"];
	local data = mw.html.create("td"):addClass("pi-horizontal-group-item pi-data-value pi-font pi-border-color pi-item-spacing");
	if (source ~= nil) then data:attr("data-source",source); end
	if (name ~= nil) then data:attr("data-item-name",name); end
	if (source ~= nil and args[source] ~= nil) then
		if (dformat ~= nil) then
			data:wikitext(dformat);
		else
			data:wikitext(args[source]);
		end
	elseif (default ~= nil) then
		data:wikitext(default);
	end
	return data;
end
--needed
--header smart
--image smart
--navigation head horizontal
--navigation body horizontal
--navigation smart
--data smart
--title head horizontal
--title body horizontal
--title smart
--panel head horizontal
--panel body horizontal
--panel smart
--sections

--=p.infobox(mw.getCurrentFrame():newChild{title="whatever",args={["args"]="Name=test",["type"]="wtf","type=title;default={{{PAGENAME}}};source=Name"}}) 

return p