Module:Template:also

Definition from Wiktionary, the free dictionary
Jump to: navigation, search
The following documentation is located at Module:Template:also/documentation. [edit]

This module implements the template {{also}}.


local export = {}
 
local yesno = require('Module:yesno')
 
local function serial_comma_join(seq)
	if #seq == 0 then
		return ""
	end
	if #seq == 1 then
		return seq[1] -- nothing to join
	end
	if #seq == 2 then
		return seq[1] .. " ''and'' " .. seq[2]
	end
	return table.concat(seq, ", ", 1, #seq - 1) .. "<span class='serial-comma'>,</span>" ..
		"''<span class='serial-and'> and</span>'' " ..
		seq[#seq]
end
 
function export.main(frame)
	local pargs = frame:getParent().args
	local dsc = pargs.sc
	local duni = yesno((pargs.uni == 'auto') or pargs.uni) and 'auto' or false
	local title = mw.title.getCurrentTitle()
 
	local items = {}
	for i, arg in ipairs(pargs) do
		local uni = (pargs['uni' .. i] ~= nil) and pargs['uni' .. i] or duni
		if not yesno(uni, uni) then
			uni = false
		end
		local sc = pargs['sc' .. i] or dsc
 
		local s
		local has_piped_link, _, link_text = arg:find("%[%[[^%[%]|]+|(.+)]]")
		if has_piped_link then
			s = ("'''%s'''"):format(arg)
			arg = mw.text.decode(link_text)
		else
			local has_link, _, link_text = arg:find("%[%[([^%[%]|]+)]]")
			if has_link then
				s = ("'''%s'''"):format(arg)
				arg = mw.text.decode(link_text)
			else
				s = ("'''[[%s]]'''"):format(arg)
			end
		end
		local codepoint
		if uni == 'auto' then
			codepoint = (mw.ustring.len(arg) == 1) and mw.ustring.codepoint(arg, 1, 1)
		elseif uni then
			codepoint = tonumber(uni)
		end
		if codepoint then
			local m_unidata = require('Module:Unicode data')
			s = s .. (" <small>[U+%04X %s]</small>"):format(
				codepoint,
				m_unidata.lookup_name(codepoint):gsub("<", "&lt;")
			)
		end
		if arg ~= title.fullText then
			table.insert(items, s)
		end
	end
 
	if #items == 0 then
		table.insert(items, "{{{1}}}")
	end
 
	return ("<div class=\"disambig-see-also%s\">''See also:'' %s</div>"):format(
		(#items == 2) and "-2" or "",
		serial_comma_join(items)
	)
end
 
return export