Module:Works by license subcat

From Wikisource
Jump to navigation Jump to search

local p = {} --p stands for package

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

local namespace = mw.title.getCurrentTitle().nsText
local rootpagename = mw.title.getCurrentTitle().rootText
local catname = string.gsub(rootpagename, "Author%-", "")

function author_subcat(subcat)
	return yesno(subcat or string.find(rootpagename, "Author%-") ~= nil)
end

function categories(subcat, extra, sort_as)
	if namespace == "Category" and subcat then
		return "[[Category:Authors by license|" .. (sort_as or catname) .. "]] [[Category:" .. catname .. "]]" .. (extra or "")
	elseif namespace == "Category" then
		return "[[Category:Works by license|" .. (sort_as or catname) .. "]]" .. (extra or "")
	else
		return ""
	end
end

function p.works_by_license_subcat(frame)
	local args = getArgs(frame)
	
	local template = args.template or catname
	local subcat = author_subcat(args.author_subcat)
	local use_template = yesno(args.use_template or true)
	local sort_as = args.sort_as or catname
	local nocat = yesno(args.nocat or false)
	
	local category
	if not nocat then
		category = categories(subcat, nil, sort_as)
	end
	
	local template_args = args
	template_args.template = nil
	template_args.author_subcat = nil
	template_args.use_template = nil
	template_args.sort_as = nil
	template_args.nocat = 'true'
	
	local text = table.concat(
		{
			"This category is populated by {{[[Template:" .. template .. "|" .. template .. "]]}}.",
			args.text or "",
			category
		},
		" "
	)
	
	local template_demo
	if use_template then
		template_demo = frame:expandTemplate {
			['title'] = template,
			['args'] = template_args
		}
	else
		template_demo = ""
	end
	
	return text .. template_demo
end

function p.pd_old_subcat(frame)
	local args = getArgs(frame)
	
	local ntable = mw.text.split(catname, "-", true)
	local n = args[1] or tonumber(ntable[#ntable]) or "{{{1}}}"
	
	local subcat = author_subcat(args.author_subcat)
	
	local sort_as = args.sort_as or catname
	
	local text = "This category is a meta category, containing categories for "
	if subcat then
		text = text .. "authors whose works"
	else
		text = text .. "works which"
	end
	text = text .. " are in the public domain in countries where copyright expires " .. n .. " years or fewer after the author's death."
	
	local extra
	if subcat then
		extra = "[[Category:Author-PD-old|" .. sort_as .. "]][[Category:Authors with works in the public domain outside the United States|" .. sort_as .. "]]"
	else
		extra = "[[Category:PD-old|" .. sort_as .. "]][[Category:Public domain outside the United States|" .. sort_as .. "]]"
	end
	
	return text .. categories(subcat, extra)
end

return p