Modul:citations

A Wikiszótárból, a nyitott szótárból

A modult a Modul:citations/doc lapon tudod dokumentálni

local export = {}

local function make_header(terms, lang, sc, context)
	local link = require("Module:links").full_link
	local canonical_name = lang:getCanonicalName()
	
	return '<h2><span id="' .. canonical_name .. '">' .. canonical_name
		.. "</span> citations of "
		.. require("Module:table").serialCommaJoin(
			require("Module:fun").map(
				function (term)
					return link({ term = term, lang = lang, sc = sc}, "term")
				end,
				terms))
		.. (context and ", in context of ''[[Appendix:" .. context .. "|" .. context .. "]]''" or "")
		.. "</h2>"
end

-- If the term is a valid pagename and there isn't an entry for it,
-- return a category.
local function add_undefined_term_category(term, canonical_name, sort)
	local success, title = pcall(mw.title.new, term)
	if success and title then
		local existence
		success, existence = pcall(function () return title.exists end)
		if success and not existence then
			return canonical_name .. " citations of undefined terms"
		end
	end
	
	return nil
end

function export.make_header_and_categories(frame)
	local title		= mw.title.getCurrentTitle()
	local namespace = title.nsText
	local baseText	= title.baseText

	local parent_args = frame:getParent().args
	local compat = parent_args["lang"]

	local params = {
		[compat and "lang" or 1] = { required = true },
		[compat and 1 or 2] = { list = true, default = baseText },
		lang2 = {},
		sc = {},
		ct = {}, -- context
		sort = {},
	}
	
	local args = require("Module:parameters").process(parent_args, params)
	
	local lang = require("Module:languages").getByCode(args[compat and "lang" or 1])
		or require("Module:languages").err(args[compat and "lang" or 1], "lang")
	
	local sc = args.sc
	sc = sc and require("Module:scripts").getByCode(sc)
	
	local terms = args[compat and 1 or 2]
	
	local text = {}
	
	table.insert(text, make_header(terms, lang, sc))
	
	if namespace == "Citations" then
		local categories		= {} -- categories that need to be formatted by [[Module:utilities]]
		local canonical_name	= lang:getCanonicalName()
		
		if args.sort then
			table.insert(text, "\n[[Category:Citations pages with manual sortkey]]")
		end
		
		table.insert(categories, canonical_name .. " citations")
		
		for _, term in ipairs(terms) do
			table.insert(categories, add_undefined_term_category(term, canonical_name, args.sort))
		end
		
		if terms[2] then
			table.insert(text, "[[Category:Citations of multiple entries]]")
		end
		
		-- "true" to force output in Citations namespace, where [[Module:utilities]]
		-- may not continue to add categories.
		table.insert(text, require("Module:utilities").format_categories(
			categories, lang, args.sort, nil, true))
	end
	
	return table.concat(text)
end

return export