Module:Sandbox/Gonnym/13

local p = {}

local patterns = { -- 1693 in the Province of Massachusetts Bay ["YEAR IN"] = '^(%d%d%d%d) in (%a.+)$', -- 1690s in the Province of Massachusetts Bay ["DECADE IN"] = '^(%d%d%d0)s in (%a.+)$', -- 17th century in the Province of Massachusetts Bay ["CENTURY IN"] = '^(%d%d[snrt][tdh]).century in (%a.+)$', -- 1693 establishments in the Province of Massachusetts Bay ["YEAR ESTABLISHMENTS IN"] = '^(%d%d%d%d) establishments in (%a.+)', -- 1776 disestablishments in the Province of Massachusetts Bay ["YEAR DISESTABLISHMENTS IN"] = '^(%d%d%d%d) disestablishments in (%a.+)', -- 1690s establishments in the Province of Massachusetts Bay ["DECADE ESTABLISHMENTS IN"] = '^(%d%d%d0)s establishments in (%a.+)$', -- 1770s disestablishments in the Province of Massachusetts Bay ["DECADE DISESTABLISHMENTS IN"] = '^(%d%d%d0)s disestablishments in (%a.+)$', -- 17th-century establishments in the Province of Massachusetts Bay ["CENTURY ESTABLISHMENTS IN"] = '^(%d%d[snrt][tdh]).century establishments in (%a.+)$', -- 18th-century disestablishments in the Province of Massachusetts Bay ["CENTURY DISESTABLISHMENTS IN"] = '^(%d%d[snrt][tdh]).century disestablishments in (%a.+)$', -- Years in the Province of Massachusetts Bay -- Decades in the Province of Massachusetts Bay -- Centuries in the Province of Massachusetts Bay ["TIME PERIOD IN"] = '^(%a.+) in (%a.+)$', -- Establishments in the Province of Massachusetts Bay by year -- Establishments in the Province of Massachusetts Bay by decade -- Establishments in the Province of Massachusetts Bay by century ["ESTABLISHMENTS IN BY"] = '^Establishments in (%a.+) by (%a.+)$', -- Disestablishments in the Province of Massachusetts Bay by year -- Disestablishments in the Province of Massachusetts Bay by decade -- Disestablishments in the Province of Massachusetts Bay by century ["DISESTABLISHMENTS IN BY"] = '^Disestablishments in (%a.+) by (%a.+)$', -- History of the Province of Massachusetts Bay by period ["HISTORY OF"] = '^History of (%a.+) by period$', }

local category_types = { ["YEAR IN"] = { ctype = 1, level = "year", explanation = "topics related to the year TIME in the COLONY", categories = { "TIME in the Thirteen Colonies|COLONY", "Years in the COLONY|TIME", },		category_above = "TIME_ABOVE in the COLONY|TIME", category_century = "Years of the CENTURY in the COLONY|TIME", },	["DECADE IN"] = { ctype = 1, level = "decade", explanation = "topics related to the TIME decade in the COLONY", categories = { "TIME in the Thirteen Colonies|COLONY", "Decades in the COLONY|TIME", },		category_above = "TIME_ABOVE in the COLONY|TIME", category_century = "Decades of the CENTURY in the COLONY|TIME", },	["CENTURY IN"] = { ctype = 1, level = "century", explanation = "topics related to the TIME in the COLONY", categories = { "TIME century in the Thirteen Colonies|COLONY", "Centuries in the COLONY|TIME", },	}, -- -->|establish=founded or established|disestablish=abolished or disestablished<!-- ["YEAR ESTABLISHMENTS IN"] = { ctype = 2, level = "year", explanation = "organizations, places or other things ACTIONed in the COLONY in the year TIME", categories = { "TIME ACTIONments in COLONY", "ACTIONments in COLONY by year", "TIME in COLONY|/ACTION", "TIME ACTIONments in the Thirteen Colonies|COLONY", },	},	["DECADE ESTABLISHMENTS IN"] = { ctype = 2, level = "decade", explanation = "organizations, places or other things ACTIONed in the COLONY in the TIME decade", categories = { "TIME-century ACTIONments in COLONY", "ACTIONments in COLONY by decade", "TIME in COLONY|/ACTION", "TIME ACTIONments in the Thirteen Colonies|COLONY" },	},	["CENTURY ESTABLISHMENTS IN"] = { ctype = 2, level = "century", explanation = "organizations, places or other things ACTIONed in the COLONY in the TIME", categories = { "ACTIONments in COLONY by century", "TIME century in COLONY|/ACTION", "TIME-century ACTION in the Thirteen Colonies|COLONY", },	}, }

local function create_category(category) return "" end

local function remove_the(colony) return string.gsub(colony, "the", "") end

-- 1775 -- Province of Massachusetts Bay

-- 1770s -- Province of Massachusetts Bay

-- 18th century -- Province of Massachusetts Bay local function get_categories(category_type, time_period, colony) colony = remove_the(colony) local categories = {} for _, category in ipairs(category_type.categories) do		local current_category = string.gsub(category, "TIME", time_period) current_category = string.gsub(current_category, "COLONY", colony) table.insert(categories, create_category(current_category)) end local frame = mw.getCurrentFrame if category_type.category_above then if category_type.level == "year" then local decade = frame:expandTemplate{title = "DECADE", args = {time_period}} local current_category = string.gsub(category_type.category_above, "TIME_ABOVE", decade) current_category = string.gsub(current_category, "COLONY", colony) current_category = string.gsub(current_category, "TIME", time_period) table.insert(categories, create_category(current_category)) else local century = frame:expandTemplate{title = "Century name from decade or year", args = {time_period}} local current_category = string.gsub(category_type.category_above, "TIME_ABOVE", century) current_category = string.gsub(current_category, "COLONY", colony) current_category = string.gsub(current_category, "TIME", time_period) table.insert(categories, create_category(current_category)) end end if category_type.category_century then local century = frame:expandTemplate{title = "Century name from decade or year", args = {time_period}} local current_category = string.gsub(category_type.category_century, "CENTURY", century) current_category = string.gsub(current_category, "COLONY", colony) current_category = string.gsub(current_category, "TIME", time_period) table.insert(categories, create_category(current_category)) end table.sort(categories) return table.concat(categories) end

local function get_category_see_also --	return "" end

local function get_category_main_article(time_period, colony) local article_title = string.gsub("TIME in COLONY", "TIME", time_period) article_title = string.gsub(article_title, "COLONY", colony) local article = mw.title.makeTitle('', article_title) if article.exists then return mw.getCurrentFrame:expandTemplate{title = "Category main article", args = {article_title}} end return "" end

local function get_category_explanation(category_type, time_period, colony) local frame = mw.getCurrentFrame if category_type.level == "year" then text = string.gsub(category_type.explanation, "TIME", time_period) elseif category_type.level == "decade" then text = string.gsub(category_type.explanation, "TIME", frame:expandTemplate{title = "DECADE", args = {time_period}}) else text = string.gsub(category_type.explanation, "TIME", time_period .. " century") end

text = string.gsub(text, "COLONY", remove_the(colony)) local category_explanation = mw.getCurrentFrame:expandTemplate{title = "Category explanation", args = {text}} return category_explanation end

local function get_portals(colony) local portals = {"History", "North America", "British Empire", "United States"} local colony_portal = mw.title.makeTitle("Portal", colony) if colony_portal.exists then table.insert(portals, 1, "colony") end

if colony == "New York" then table.insert(portals, 1, 'New York (state)') end

local portal = mw.getCurrentFrame:expandTemplate{title = 'Portal', args = portals} return portal end

-- returns time period, colony name local function get_time_period_and_colony_name(title, category_type) return mw.ustring.match(title, patterns[category_type]) end

local function get_category_type(title) for category_type, pattern in pairs(patterns) do		if mw.ustring.match(title, pattern) then return category_type end end return "" end

-- Order of operations: -- 1. Get category title parameters -- 2. Get the category type from title parameters -- 3. Get categories for category type -- 4. Replace placehodlers with actual values -- 5. Add: -- 6. Add: -- 7. Add: -- 8. Add: -- 9. Add: -- 10. Add: -- 11. Add: Categories function p.main(frame) local getArgs = require("Module:Arguments").getArgs local args = getArgs(frame) local title = args.title local category_type = get_category_type(title) if category_type == "" then return error("Not a valid category") end

local time_period local colony if category_types[category_type].ctype < 3 then --TODO: refactor at the end time_period, colony = get_time_period_and_colony_name(title, category_type) end local action if ctype == 2 then action = "establish" end

local portals = get_portals(colony) local explanation = get_category_explanation(category_types[category_type], time_period, colony) local main_article = get_category_main_article(time_period, colony) local see_also = get_category_see_also local series_navigation = frame:expandTemplate{title = 'Category series navigation', args = {["skip-gaps"] = "yes"}} local CatAutoTOC = frame:expandTemplate{title = 'CatAutoTOC', args = {}} local categories = get_categories(category_types[category_type], time_period, colony) local layout = portals .. '\n' .. explanation .. '\n' .. main_article .. '\n' .. see_also ..'\n' .. series_navigation .. '\n' .. CatAutoTOC .. '\n' .. categories return layout end

return p