Module:PHLawAmend

From Wikisource
Jump to navigation Jump to search
Module documentation[create]
local p = {}
-- A table containing the abbreviation, and an array of their long name & category
abbrs = require( "Module:PHLawAmend/abbrs")

--Main function that connects with {{PHLawAmend}}
function p.main( frame )
	action = frame:getParent().args.action;
	title = abbrs.getAbbr(frame.args[1], true)
	inputted = frame:getParent().args
	--Sorts the input, thanks to https://stackoverflow.com/a/27911647/14021404
	table.sort(inputted, function (a, b)
		a = tostring(a.N)
		b = tostring(b.N)
		local patt = '^(.-)%s*(%d+)$'
		local _,_, col1, num1 = a:find(patt)
		local _,_, col2, num2 = b:find(patt)
		if (col1 and col2) and col1 == col2 then
			return tonumber(num1) < tonumber(num2)
		end
		return a < b
	end)
	
	out = {};
	--Loop thru elements of the template called
	for i, v in ipairs( inputted ) do
		out[i] = perLine( action, v, title )
	end
    return table.concat(out, "<br/>") --Concatenate each element with a line break
end

--Executed per line
function perLine( action, abbreviatedLaw, title )
	local full, cat = deAbbreviateLaw( abbreviatedLaw )
	if cat==nil then
		return "[["..abbreviatedLaw.."|".. abbreviatedLaw:gsub("#", " ").."]]"
	else
		return "[["..abbreviatedLaw.."|".. abbreviatedLaw:gsub("#", " ").."]][[Category:"..cat.."/"..action.."/"..full.."|"..title.."]]"
	end
end

function deAbbreviateLaw( abbreviatedLaw )
	--Detect if there's a section (denoted by # then the section), if yes then remove that
	if string.find(abbreviatedLaw, "#") then
		local secPosition = string.find(abbreviatedLaw, "#")
		abbreviatedLaw = string.sub(abbreviatedLaw, 0, secPosition-1)
	end
	
	local numberPosition = string.find(abbreviatedLaw, "%d+")
	local abbr = string.sub(abbreviatedLaw, 0, numberPosition-2)
	toAppend1, toAppend2 = "", ""
	
	--Detect if there's a series year (denoted by s. then the year), if yes then substitute that with the president abbr who made that
	if string.find(abbreviatedLaw, "s%.") then
		local __, seriesPosition =  string.find(abbreviatedLaw, "s%.")
		local year = tonumber(string.sub(abbreviatedLaw, seriesPosition+2, #abbreviatedLaw))
		abbreviatedLaw = string.sub(abbreviatedLaw, 0, seriesPosition-3)
		mw.log(year)

		local pres = {}
		for k, v in pairs( abbrs.presidents ) do
			--mw.log(v[2], v[3])
    		if (year >= tonumber(v[2]) and year <= tonumber(v[3])) then 
    			table.insert(pres, k) --Since there are potential multiple presidents for that year, add them to the array
    		end
		end
		
		--Find the page to make sure of the president
		for i, v in ipairs( pres ) do
			doesExist = mw.getCurrentFrame():callParserFunction("#ifexist", abbrs.main[abbr][1] .. string.sub(abbreviatedLaw, numberPosition, #abbreviatedLaw).." ("..v..") ", "true", "false")
			if (doesExist == "true") then 
				toAppend1 = " ("..v..")" 
				toAppend2 = abbrs.presidents[v][1]
			end
		end
		
		--Throw error if no page found (DISABLED)
		if (toAppend1 == "") then toAppend2="ERROR" end
		--if (toAppend1 == "") then error("The page for "..abbrs.main[abbr][1] .. string.sub(abbreviatedLaw, numberPosition, #abbreviatedLaw).." doesn't exist. Make sure that it has the proper title formatting of \""..abbrs.main[abbr][1]..string.sub(abbreviatedLaw, numberPosition, #abbreviatedLaw).." (AAA)\", wherein AAA is the president abbreviation. For more information, go to Template:PHLawHeading" ) end
	end
	
	if abbrs.main[abbr] == nil then error("Invalid law abbreviation: "..abbr) end
	
	return 
		abbrs.main[abbr][1] .. string.sub(abbreviatedLaw, numberPosition, #abbreviatedLaw) .. toAppend1, --Concatenate the number to the full name of the law
		(toAppend2 ~= "ERROR" and abbrs.main[abbr][2] .. toAppend2 or nil)


end
return p