Module:Potus work table

From Wikisource
Jump to navigation Jump to search

--[=[
Implements [[Template:Potus work table]]
]=]
require('strict')

local p = {} --p stands for package

local getArgs = require('Module:Arguments').getArgs

local presidents = {
	[1] = {'George Washington', 'Washington, George'},
	[2] = {'John Adams (1735-1826)', 'Adams, John', 'John Adams'},
	[3] = {'Thomas Jefferson', 'Jefferson, Thomas'},
	[4] = {'James Madison', 'Madison, James'},
	[5] = {'James Monroe', 'Monroe, James'},
	[6] = {'John Quincy Adams', 'Adams, John Quincy'},
	[7] = {'Andrew Jackson', 'Jackson, Andrew'},
	[8] = {'Martin Van Buren', 'Buren, Martin Van'},
	[9] = {'William Henry Harrison', 'Harrison, William Henry'},
	[10] = {'John Tyler', 'Tyler, John'},
	[11] = {'James K. Polk', 'Polk, James Knox'},
	[12] = {'Zachary Taylor', 'Taylor, Zachary'},
	[13] = {'Millard Fillmore', 'Fillmore, Millard'},
	[14] = {'Franklin Pierce', 'Pierce, Franklin'},
	[15] = {'James Buchanan', 'Buchanan, James'},
	[16] = {'Abraham Lincoln', 'Lincoln, Abraham'},
	[17] = {'Andrew Johnson', 'Johnson, Andrew'},
	[18] = {'Ulysses S. Grant', 'Grant, Ulysses S.'},
	[19] = {'Rutherford B. Hayes', 'Hayes, Rutherford B.'},
	[20] = {'James Abram Garfield', 'Garfield, James Abram'},
	[21] = {'Chester Alan Arthur', 'Arthur, Chester Alan'},
	[22] = {'Grover Cleveland', 'Cleveland, Grover'},
	[23] = {'Benjamin Harrison (1833-1901)', 'Harrison, Benjamin', 'Benjamin Harrison'},
	[24] = {'Grover Cleveland', 'Cleveland, Grover'},
	[25] = {'William McKinley', 'McKinley, William'},
	[26] = {'Theodore Roosevelt', 'Roosevelt, Theodore'},
	[27] = {'William Howard Taft', 'Taft, William Howard'},
	[28] = {'Woodrow Wilson', 'Wilson, Woodrow'},
	[29] = {'Warren G. Harding', 'Harding, Warren G.'},
	[30] = {'Calvin Coolidge', 'Coolidge, Calvin'},
	[31] = {'Herbert Hoover', 'Hoover, Herbert'},
	[32] = {'Franklin Delano Roosevelt', 'Roosevelt, Franklin Delano'},
	[33] = {'Harry S. Truman', 'Truman, Harry S.'},
	[34] = {'Dwight D. Eisenhower', 'Eisenhower, Dwight D.'},
	[35] = {'John F. Kennedy', 'Kennedy, John F.'},
	[36] = {'Lyndon B. Johnson', 'Johnson, Lyndon B.'},
	[37] = {'Richard Nixon', 'Nixon, Richard'},
	[38] = {'Gerald Ford', 'Ford, Gerald'},
	[39] = {'Jimmy Carter', 'Carter, Jimmy'},
	[40] = {'Ronald Reagan', 'Reagan, Ronald'},
	[41] = {'George Herbert Walker Bush', 'Bush, George Herbert Walker'},
	[42] = {'William Jefferson Clinton', 'Clinton, William Jefferson'},
	[43] = {'George Walker Bush', 'Bush, George Walker'},
	[44] = {'Barack Obama', 'Obama, Barack Hussein'},
	[45] = {'Donald John Trump', 'Trump, Donald John'},
	[46] = {'Joseph Robinette Biden', 'Biden, Joe Robinette'}
}

local function link_td(president, subpages)
	local links = {}
	for k, v in pairs(subpages) do
		local page = mw.title.new(president .. '/' .. v[1], 'Author')
		if page.exists then
			links[#links + 1] = '[[' .. page.fullText .. '|' .. (v[2] or v[1]) .. ']]'
		end
	end
	return '<td>' .. table.concat(links, '<br>') .. '</td>'
end

local function potus_tr(args)
	local n = tonumber(args.n)
	local president = presidents[n][1]
	local president_sort = presidents[n][2]
	local president_display = presidents[n][3] or president
	local other = args.other or ''
	
	local president_td = '<td><span style="display: none;">' .. president_sort .. '</span>[[Author:' .. president .. '|' .. president_display .. ']]</td>'
	
	local subpages = {
		{{'Presidential Proclamations', 'Proclamations'}}, -- Proclamations
		{{'Executive orders'}}, -- Executive orders
		{{'Presidential memoranda', 'Memoranda'}}, -- Memoranda
		{{'Letters'}}, -- Letters
		{{'Podcasts'}, {'Presidential radio addresses', 'Radio addresses'}, {'Weekly addresses'}} -- Media
	}
	local link_list = {}
	for k, v in pairs(subpages) do
		link_list[#link_list + 1] = link_td(president, v)
	end
	local links = table.concat(link_list)
	
	return '<tr><td>' .. n .. '</td>' .. president_td .. links .. '<td>' .. other .. '</td></tr>'
end

function p._potus_table(args)
	local table_open = '<table class="wikitable sortable" style="margin:0 auto 0 auto;">'
	local header_row = '<tr><th>No.</th><th>Name</th><th>Proclamations</th><th>Executive orders</th><th>Memoranda</th><th>Letters</th><th>Media</th><th>Other</th></tr>'
	
	local other_table = {}
	for k, v in pairs(args) do
		if string.sub(k, 1, 5) == 'other' and tonumber(string.sub(k, 6)) then
			other_table[tonumber(string.sub(k, 6))] = v
		end
	end
	
	local row_table = {}
	for k, v in pairs(presidents) do
		local row_args = {
			['n'] = k,
			['other'] = other_table[k]
		}
		row_table[k] = potus_tr(row_args)
	end
	local rows = table.concat(row_table)
	
	return table_open .. header_row .. rows .. '</table>'
end

function p.potus_table(frame)
	return p._potus_table(getArgs(frame))
end

return p