Module:Infobox: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Ganaram inukshuk (talk | contribs)
Moved multiseciton infobox function from infobox mos module
Ganaram inukshuk (talk | contribs)
Added multilink infobox to replace multisection infobox; to be removed once infobox mos switches to multilink
Line 191: Line 191:
s = s .. "|-\n" .. '| colspan="2" style="text-align: center;" | ' .. text .. "\n"
s = s .. "|-\n" .. '| colspan="2" style="text-align: center;" | ' .. text .. "\n"
end
end
end
end
-- End of infobox
s = s .. "|}</div>"
return s
end
-- Multilink infobox
-- The following must be passed in:
-- - Title (self-explanatory)
-- - Sections (see comments for details)
-- - Adjacent links; supports 0, 2, or 8 adjacent links
function p.build_multilink(title, sections, adjacent_links)
-- Boilerplate stuff
local s = '<div style="\n'
.. "border: 1px solid #999;\n"
.. "margin: 0;\n"
.. "margin-left: 1em;\n"
.. "margin-bottom: 0.5em;\n"
.. "padding: 0.5em;\n"
.. "background-color: #f0f0f0;\n"
.. "min-width: 15em;\n"
.. "float: right;\n"
.. "max-width: 100%;\n"
.. "overflow: auto;\n"
.. '">\n'
.. '{| width="100%" style="border-collapse: collapse;"\n'
.. '|+ style="font-weight: bold; text-align: center;" | '
-- Adjacent links
-- If there are two adjacent links (such as with edos), then links
-- will be placed on the left and right of the title
-- [Link 1] Title [Link 2]
-- - Link 1 is previous, link 2 is next
-- If there are eight adjacent links (such as with mosses), then
-- links surround the title, forming a 3x3 grid as such:
-- [Link 1] [Link 2] [Link 3]
-- [Link 4]  Title  [Link 5]
-- [Link 6] [Link 7] [Link 8]
-- - 1: prev_A, prev_B
-- - 2: prev_B
-- - 3: next_A, prev_B
-- - 4: prev_A, 
-- - 5: next_A, 
-- - 6: prev_A, next_B
-- - 7: next_B, 
-- - 8: next_A, next_B
-- Leaving this field nil defaults to only having the title; individual
-- links, as with "border" cases (eg, nothing comes before 0edo), can be
-- left blank.
if adjacent_links == nil then
s = s .. title
s = s .. '\n'
elseif #adjacent_links == 2 then
local prev_link = adjacent_links[1]
local next_link = adjacent_links[2]
s = s
.. '<table style="width: 100%; margin: 0"><tr>'
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
.. (prev_link or "")
.. "</td>"
.. '<td style="width: 70%; padding-left: 1em; padding-right: 1em; text-align: center">'
.. title
.. "</td>"
.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
.. (next_link or "")
.. "</td>"
.. "</tr></table>"
s = s .. '\n'
elseif #adjacent_links == 8 then
s = s .. '<table style="width: 100%; margin: 0"><tr>'
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[1] or "")
.. '</td>'
.. '<td style="\n'
.. 'width: 50%; padding-left: 1em; padding-right: 1em; text-align: center; font-size: smaller">'
.. (adjacent_links[2] or "")
.. '\n</td><td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[3] or "")
.. '</td>'
.. '\n<tr>'
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[4] or "")
.. '</td>'
.. '<td style="width: 50%; padding-left: 1em; padding-right: 1em; text-align: center">'
.. title
.. '</td>'
.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[5] or "")
.. '</td>'
.. '</tr>'
.. '<tr>'
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[6] or "")
.. '</td><td style="\n'
.. 'width: 50%; padding-left: 1em; padding-right: 1em; text-align: center; font-size: smaller">'
.. (adjacent_links[7] or "")
.. '\n</td>'
.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[8] or "")
.. '</td>'
.. '</tr>'
..  '</table>'
s = s .. '\n'
else
s = s .. title
s = s .. '\n'
end
-- Add infobox entries
-- Entries are entered as a jagged array (array of arrays) where each
-- subarray has either one or two entries.
-- These entries form the rows of a two-column table.
-- One-entry arrays are used for entries that must span both columns,
-- such as a section header. Two-entry arrays are used for the main content.
for _, entry in ipairs(entries) do
if #entry > 1 then
local caption = entry[1]
local text = entry[2]
s = s
.. "|-\n"
.. '| style="text-align:right; padding-right: 0.25em" | '
.. caption
.. "\n"
.. '| style="background-color: white; padding-left: 0.25em; font-weight: bold" | '
.. text
.. "\n"
elseif #entry == 1 then
local text = entry[1]
s = s .. "|-\n" .. '| colspan="2" style="text-align: center;" | ' .. text .. "\n"
end
end
end
end

Revision as of 17:50, 13 April 2024

Module documentation[view] [edit] [history] [purge]
This module implements a metatemplate, and may be invoked by templates using its corresponding template Template:Infobox, or used directly from other modules.

Module:Infobox is a module that implements the {{Infobox}} template. Infobox templates can be made by using the template or by calling the _sidebar function from another module.

On templates, you can create an infobox by using {{Infobox}}, which calls this module's wrapper function.

On modules, you can include local infobox= require("Module:Infobox")._infobox to create an infobox.
Introspection summary for Module:Infobox 
Functions provided (3)
Line Function Params
3 build (title, entries, prev_link, next_link)
62 build_multisection (title, sections, adjacent_links)
206 build_multilink (title, sections, adjacent_links)
Lua modules required (0)
Variable Module Functions used

No function descriptions were provided. The Lua code may have further information.


local p = {}

function p.build(title, entries, prev_link, next_link)
	local s = '<div style="\n'
		.. "border: 1px solid #999;\n"
		.. "margin: 0;\n"
		.. "margin-left: 1em;\n"
		.. "margin-bottom: 0.5em;\n"
		.. "padding: 0.5em;\n"
		.. "background-color: #f0f0f0;\n"
		.. "min-width: 15em;\n"
		.. "float: right;\n"
		.. "max-width: 100%;\n"
		.. "overflow: auto;\n"
		.. '">\n'
		.. '{| width="100%" style="border-collapse: collapse;"\n'
		.. '|+ style="font-weight: bold; text-align: center;" | '
	local has_adjacent = (prev_link and #prev_link > 0) or (next_link and #next_link > 0)
	if has_adjacent then
		s = s
			.. '<table style="width: 100%; margin: 0"><tr>'
			.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
			.. (prev_link or "")
			.. "</td>"
			.. '<td style="width: 70%; padding-left: 1em; padding-right: 1em; text-align: center">'
			.. title
			.. "</td>"
			.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
			.. (next_link or "")
			.. "</td>"
			.. "</tr></table>"
	else
		s = s .. title
	end
	s = s .. "\n"
	for _, entry in ipairs(entries) do
		if #entry > 1 then
			local caption = entry[1]
			local text = entry[2]
			s = s
				.. "|-\n"
				.. '| style="text-align:right; padding-right: 0.25em" | '
				.. caption
				.. "\n"
				.. '| style="background-color: white; padding-left: 0.25em; font-weight: bold" | '
				.. text
				.. "\n"
		elseif #entry == 1 then
			local text = entry[1]
			s = s .. "|-\n" .. '| colspan="2" style="text-align: center;" | ' .. text .. "\n"
		end
	end
	s = s .. "|}</div>"
	return s
end

-- Multisection multilink infobox
-- The following must be passed in:
-- - Title (self-explanatory)
-- - Sections (see comments for details)
-- - Adjacent links; supports 0, 2, or 8 adjacent links
function p.build_multisection(title, sections, adjacent_links)
	-- Boilerplate stuff
	local s = '<div style="\n'
		.. "border: 1px solid #999;\n"
		.. "margin: 0;\n"
		.. "margin-left: 1em;\n"
		.. "margin-bottom: 0.5em;\n"
		.. "padding: 0.5em;\n"
		.. "background-color: #f0f0f0;\n"
		.. "min-width: 15em;\n"
		.. "float: right;\n"
		.. "max-width: 100%;\n"
		.. "overflow: auto;\n"
		.. '">\n'
		.. '{| width="100%" style="border-collapse: collapse;"\n'
		.. '|+ style="font-weight: bold; text-align: center;" | '
		
	-- Adjacent links
	-- If there are two adjacent links (such as with edos), then links
	-- will be placed on the left and right of the title
	--		[Link 1] Title [Link 2]
	-- - Link 1 is previous, link 2 is next
	-- If there are eight adjacent links (such as with mosses), then
	-- links surround the title, forming a 3x3 grid as such:
	--		[Link 1] [Link 2] [Link 3]
	--		[Link 4]   Title  [Link 5]
	--		[Link 6] [Link 7] [Link 8]
	-- - 1: prev_A, prev_B
	-- - 2: prev_B
	-- - 3: next_A, prev_B
	-- - 4: prev_A,  
	-- - 5: next_A,  
	-- - 6: prev_A, next_B
	-- - 7: next_B,  
	-- - 8: next_A, next_B
	-- Leaving this field nil defaults to only having the title; individual
	-- links, as with "border" cases (eg, nothing comes before 0edo), can be
	-- left blank.
	if adjacent_links == nil then
		s = s .. title
		s = s .. '\n'
	elseif #adjacent_links == 2 then
		local prev_link = adjacent_links[1]
		local next_link = adjacent_links[2]
		s = s
			.. '<table style="width: 100%; margin: 0"><tr>'
			.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
			.. (prev_link or "")
			.. "</td>"
			.. '<td style="width: 70%; padding-left: 1em; padding-right: 1em; text-align: center">'
			.. title
			.. "</td>"
			.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
			.. (next_link or "")
			.. "</td>"
			.. "</tr></table>"
		s = s .. '\n'
	elseif #adjacent_links == 8 then
		s = s .. '<table style="width: 100%; margin: 0"><tr>'
			.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
			.. (adjacent_links[1] or "")
			.. '</td>'
			.. '<td style="\n' 
			.. 'width: 50%; padding-left: 1em; padding-right: 1em; text-align: center; font-size: smaller">'
			.. (adjacent_links[2] or "")
			.. '\n</td><td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
			.. (adjacent_links[3] or "")
			.. '</td>' 
			.. '\n<tr>'
			.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
			.. (adjacent_links[4] or "")
			.. '</td>'
			.. '<td style="width: 50%; padding-left: 1em; padding-right: 1em; text-align: center">'
			.. title
			.. '</td>'
			.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
			.. (adjacent_links[5] or "")
			.. '</td>'
			.. '</tr>'
			.. '<tr>'
			.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
			.. (adjacent_links[6] or "")
			.. '</td><td style="\n' 
			.. 'width: 50%; padding-left: 1em; padding-right: 1em; text-align: center; font-size: smaller">'
			.. (adjacent_links[7] or "")
			.. '\n</td>'
			.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
			.. (adjacent_links[8] or "")
			.. '</td>'
			.. '</tr>'
			..  '</table>'
		s = s .. '\n'
	else
		s = s .. title
		s = s .. '\n'
	end
	
	-- Build up sections
	-- In code, sections are built up as an array of pairs, coded as such:
	-- {{"Header", entries}, {"Header", entries}}
	-- Each set of entries is also built up as an array of pairs, as such:
	-- {{"Caption 1", "Text 1"}, {"Caption 2", "Text 2"}}
	-- Sections are written in order in which they appear, as are their
	-- respective entries.
	-- Section headers may be blank, written as ""
	for _, section in ipairs(sections) do
		-- Get the header and entries
		local header = section[1]
		local entries = section[2]
		
		-- Section header
		s = s .. '|-\n'
	    .. '|colspan="2" style="text-align:center;"| <b>' .. (header or "") .. '</b>\n'
	    
	    -- Section entries
    	for _, entry in ipairs(entries) do
			if #entry > 1 then
				local caption = entry[1]
				local text = entry[2]
				s = s
					.. "|-\n"
					.. '| style="text-align:right; padding-right: 0.25em" | '
					.. caption
					.. "\n"
					.. '| style="background-color: white; padding-left: 0.25em; font-weight: bold" | '
					.. text
					.. "\n"
			elseif #entry == 1 then
				local text = entry[1]
				s = s .. "|-\n" .. '| colspan="2" style="text-align: center;" | ' .. text .. "\n"
			end
		end
	end
	
	-- End of infobox
	s = s .. "|}</div>"
	return s
end

-- Multilink infobox
-- The following must be passed in:
-- - Title (self-explanatory)
-- - Sections (see comments for details)
-- - Adjacent links; supports 0, 2, or 8 adjacent links
function p.build_multilink(title, sections, adjacent_links)
	-- Boilerplate stuff
	local s = '<div style="\n'
		.. "border: 1px solid #999;\n"
		.. "margin: 0;\n"
		.. "margin-left: 1em;\n"
		.. "margin-bottom: 0.5em;\n"
		.. "padding: 0.5em;\n"
		.. "background-color: #f0f0f0;\n"
		.. "min-width: 15em;\n"
		.. "float: right;\n"
		.. "max-width: 100%;\n"
		.. "overflow: auto;\n"
		.. '">\n'
		.. '{| width="100%" style="border-collapse: collapse;"\n'
		.. '|+ style="font-weight: bold; text-align: center;" | '
		
	-- Adjacent links
	-- If there are two adjacent links (such as with edos), then links
	-- will be placed on the left and right of the title
	--		[Link 1] Title [Link 2]
	-- - Link 1 is previous, link 2 is next
	-- If there are eight adjacent links (such as with mosses), then
	-- links surround the title, forming a 3x3 grid as such:
	--		[Link 1] [Link 2] [Link 3]
	--		[Link 4]   Title  [Link 5]
	--		[Link 6] [Link 7] [Link 8]
	-- - 1: prev_A, prev_B
	-- - 2: prev_B
	-- - 3: next_A, prev_B
	-- - 4: prev_A,  
	-- - 5: next_A,  
	-- - 6: prev_A, next_B
	-- - 7: next_B,  
	-- - 8: next_A, next_B
	-- Leaving this field nil defaults to only having the title; individual
	-- links, as with "border" cases (eg, nothing comes before 0edo), can be
	-- left blank.
	if adjacent_links == nil then
		s = s .. title
		s = s .. '\n'
	elseif #adjacent_links == 2 then
		local prev_link = adjacent_links[1]
		local next_link = adjacent_links[2]
		s = s
			.. '<table style="width: 100%; margin: 0"><tr>'
			.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
			.. (prev_link or "")
			.. "</td>"
			.. '<td style="width: 70%; padding-left: 1em; padding-right: 1em; text-align: center">'
			.. title
			.. "</td>"
			.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
			.. (next_link or "")
			.. "</td>"
			.. "</tr></table>"
		s = s .. '\n'
	elseif #adjacent_links == 8 then
		s = s .. '<table style="width: 100%; margin: 0"><tr>'
			.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
			.. (adjacent_links[1] or "")
			.. '</td>'
			.. '<td style="\n' 
			.. 'width: 50%; padding-left: 1em; padding-right: 1em; text-align: center; font-size: smaller">'
			.. (adjacent_links[2] or "")
			.. '\n</td><td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
			.. (adjacent_links[3] or "")
			.. '</td>' 
			.. '\n<tr>'
			.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
			.. (adjacent_links[4] or "")
			.. '</td>'
			.. '<td style="width: 50%; padding-left: 1em; padding-right: 1em; text-align: center">'
			.. title
			.. '</td>'
			.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
			.. (adjacent_links[5] or "")
			.. '</td>'
			.. '</tr>'
			.. '<tr>'
			.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
			.. (adjacent_links[6] or "")
			.. '</td><td style="\n' 
			.. 'width: 50%; padding-left: 1em; padding-right: 1em; text-align: center; font-size: smaller">'
			.. (adjacent_links[7] or "")
			.. '\n</td>'
			.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
			.. (adjacent_links[8] or "")
			.. '</td>'
			.. '</tr>'
			..  '</table>'
		s = s .. '\n'
	else
		s = s .. title
		s = s .. '\n'
	end
	
	-- Add infobox entries
	-- Entries are entered as a jagged array (array of arrays) where each
	-- subarray has either one or two entries.
	-- These entries form the rows of a two-column table.
	-- One-entry arrays are used for entries that must span both columns,
	-- such as a section header. Two-entry arrays are used for the main content.
	for _, entry in ipairs(entries) do
		if #entry > 1 then
			local caption = entry[1]
			local text = entry[2]
			s = s
				.. "|-\n"
				.. '| style="text-align:right; padding-right: 0.25em" | '
				.. caption
				.. "\n"
				.. '| style="background-color: white; padding-left: 0.25em; font-weight: bold" | '
				.. text
				.. "\n"
		elseif #entry == 1 then
			local text = entry[1]
			s = s .. "|-\n" .. '| colspan="2" style="text-align: center;" | ' .. text .. "\n"
		end
	end
	
	-- End of infobox
	s = s .. "|}</div>"
	return s
end

return p