Jump to content

Module:UnitedStatesCode/sandbox

From Wikisource
require('strict')

local p = {} --p stands for package

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

local function plainlinks_span(text)
	if not text then
		return nil
	else
		return mw.html.create('span'):addClass('plainlinksneverexpand plainlinks'):wikitext(text)
	end
end

function p._link(args)
	-- args
	local pipe = args.pipe
	local plainlinks = yesno(args.plainlinks)
	
	local title = args['title'] or args[1] or ''
	local section = args['section']
	local part = args['part']
	local chapter = args['chapter']
	local subtitle = args['subtitle']
	local subchapter = args['subchapter']
	local posttitle = args['posttitle']
	local endtext = args['endtext']
	
	-- link
	local link = "https://www.law.cornell.edu/uscode/text/" .. title
	
	if posttitle then
		link = link .. "/" .. posttitle
	end
	
	if section then
		link = link .. "/" .. section
	else
		if subtitle then
			link = link .. "/subtitle-" .. subtitle
		end
		
		if part then
			link = link .. "/part-" .. part
		end
		
		if chapter then
			link = link .. "/chapter-" .. chapter
		end
		
		if subchapter then
			link = link .. "/subchapter-" .. subchapter
		end
	end
	
	if endtext then
		link = link .. "/" .. endtext
	end
	
	local text
	if pipe then
		text = "[" .. link .. " " .. pipe .. "]"
	else
		text = link
	end
	
	if plainlinks then
		return plainlinks_span(text)
	else
		return text
	end
end

function p.link(frame)
	return p._link(getArgs(frame))
end

function p.UnitedStatesCode(frame)
	local args = getArgs(frame)
	
	local title = args[1] or args['title']
	local section = args[2] or args['sect'] or args['section']
	local pipe = args['pipe']
	
	local text
	if pipe then
		text = p._link({title, section = section, pipe = pipe})
	else
		local titleLink = p._link({title, ['pipe'] = title})
		local sectionLink = p._link({title, ['section'] = section, ['pipe'] = section})
		text = titleLink .. " [[United States Code|U.S.C.]] " .. sectionLink
	end
	return plainlinks_span(text)
end

function p.uscClause(frame)
	local args = getArgs(frame)
	
	local title = args[1] or args['title']
	local section = args[2] or args['sect'] or args['section']
	local clause = args[3] or args['clause']
	local pipe = args.pipe
	
	local text
	if pipe then
		text = p._link({title, section = section, pipe = pipe})
	else
		local titleLink = p._link({title, pipe = title})
		
		local pipeText
		if clause then
			pipeText = section .. "§ " .. clause
		else
			pipeText = section
		end
		
		local sectionLink = p._link({title, section = section, pipe = pipeText})
		
		text = titleLink .. " [[United States Code|U.S.C.]] " .. sectionLink
	end
	return plainlinks_span(text)
end

local function subLinks(args)
	local id_min = 3
	local id_max = 0
	for k in pairs(args) do
		if type(k) == 'number' and k > id_min then
			id_max = math.max(id_max, k)
		end
	end
	
	local ids = {}
	local idsInParens = {}
	for i = id_min, id_max do
		if args[i] then
			table.insert(ids, args[i])
			table.insert(idsInParens, '(' .. args[i] .. ')')
		end
	end
	
	local idJumpText = ''
	if id_count > 0 then
		idJumpText = '#' .. table.concat(ids, '_')
	end
	
	local sectionText = args[2] .. table.concat(idsInParens)
	
	return {idJumpText = idJumpText, sectionText = sectionText}
end

function p._UnitedStatesCodeSub(args)
	local title = args[1]
	local section = args[2]
	local compact = yesno(args.compact)
	
	local subLinks = subLinks(args)
	local idJumpText = subLinks.idJumpText
	local sectionText = subLinks.sectionText
	
	local text
	if compact then
		text = p._link({title, section = section, endtext = idJumpText, pipe = sectionText})
	elseif args.pipe then
		text = p._link({title, section = section, endtext = idJumpText, pipe = args.pipe})
	else
		local liiLink = p._link({title, section = section, endtext = idJumpText, "§ " .. sectionText})
		text = "[[United States Code/Title " .. title .. "|" .. title .. " U.S.C.]] " .. liiLink
	end
	
	return plainlinks_span(text)
end

function p.UnitedStatesCodeSub(frame)
	return p._UnitedStatesCodeSub(getArgs(frame))
end

return p