Module:Copyright-until

From Wikisource
Jump to navigation Jump to search

--[=[
Implements [[Template:Copyright-until]]
]=]
require('strict')

local p = {}

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

function p._copyright_until(args)
	local title = args[1]
	local display = args.display or title
	
	local expiry = args.expiry or args['until'] or args[2]
	-- hacky, needs to handle non-math input into expr better
	if expiry and tonumber(string.sub(expiry, 1, 1)) then
		expiry = tonumber(mw.ext.ParserFunctions.expr(tostring(expiry)))
	else
		expiry = nil
	end
	
	local wikipedia = args.wikipedia
	local author = args.author
	local author_display = args.author_display or args['author-display'] or args['author display'] or author
	local pubyear = args.pubyear
	local extra = args.extra
	
	local renewal = args.renewal
	if renewal then
		local frame = mw.getCurrentFrame()
		if mw.ustring.len(renewal) == 12 then
			if mw.ustring.sub(renewal, 1, 2) == "RE" then
				renewal = frame:expandTemplate {
					['title'] = 'CO Copyright renewal',
					['args'] = {renewal, 'yes'}
				}
			else
				renewal = nil
			end
		else
			renewal = frame:expandTemplate {
				['title'] = 'Copyright renewal',
				['args'] = {renewal, 'yes'}
			}
		end
	end
	
	local currentyear = tonumber(os.date("%Y"))
	-- assume copyright if no expiry date given
	local copyright_expired = expiry and expiry <= currentyear
	
	local namespace = mw.title.getCurrentTitle().nsText
	
	local text = ""
	if title then
		if copyright_expired then
			text = "''[[" .. title .. "|" .. display .. "]]''"
		elseif wikipedia then
			text = "''[[w:" .. wikipedia .. "|" .. display .. "]]''"
		elseif display then
			text = "''" .. display .. "''"
		else
			text = "''" .. title .. "''"
		end
		if author then
			text = text .. " by [[Author:" .. author .. "|" .. author_display .. "]]"
		end
		if pubyear or extra then
			text = text .. " ("
			if extra then
				text = text .. extra
				if pubyear then
					text = text .. ", "
				end
			end
			if pubyear then
				text = text .. pubyear
			end
			text = text .. ")"
		end
	end
	
	if not copyright_expired then
		if title then
			text = text .. "—"
		end
		text = text .. "Copyrighted in the United States"
		if expiry then
			text = text .. " until " .. expiry
		end
		if renewal then
			text = text .. " due to " .. renewal
		end
	end
	
	if namespace ~= "Template" and namespace ~= "Template talk" then
		if copyright_expired then
			text = text .. "[[Category:Pages listing works with possibly expired copyrights]]"
		end
		if not expiry then
			text = text .. "[[Category:Pages claiming copyright without date]]"
		end
	end
	
	return text
end

function p.copyright_until(frame)
	return p._copyright_until(getArgs(frame))
end

return p