Jump to content

Module:Onyms

From Wiktionary
Documentation icon Module documentation[view] [change] [history] [refresh]

This module is used for generating a list of either {{synonyms}} or {{antonyms}} in a nice, horizontal, list format. This module should only be used by those two templates and should not be invoked directly.

-- The 'onyms module
local p = {}

-- Generate the single-line list given the parameters
local function generateLinks( frame )
	local junk = ''
	local wordlist = {}
	for key, value in pairs( frame.args ) do
		if ( key == 'type' ) then
			-- Note: This value is not used at all, just a hack to continue
			-- (Lua does not have a "continue" function)
			junk = value
		elseif ( value == '' ) then
			junk = value
		else
			table.insert( wordlist, '[[' .. value .. ']]' )
		end
	end

	return wordlist
end

-- Run the module
function p.run( frame )
	local wordlist = generateLinks( frame )
	local onymtype = frame.args['type']
	local root = mw.html.create()
	local noofwords = table.maxn( wordlist )
	local cssclass = ''
	local onymname = ''
	local prefix = ''
	local words = ''

	if ( onymtype == 'synonym' ) then
		cssclass = 'onyms-collapse onyms-collapse-synonyms'
		onymname = 'Synonym'
	elseif ( onymtype == 'antonym' ) then
		cssclass = 'onyms-collapse onyms-collapse-antonyms'
		onymname = 'Antonym'
	else
		-- This should NOT happen unless when testing
		onymname = onymtype
	end

	if ( noofwords == 0 ) then
		prefix = "'''" .. onymname .. ":''' "
		words = "''none''"
	elseif ( noofwords == 1 ) then
		prefix = "'''" .. onymname .. ":''' "
		words = wordlist[1]
	else
		prefix = "'''" .. onymname .. "s:''' "
		words = table.concat( wordlist, ', ', 1, noofwords - 1 ) .. " ''and'' " .. wordlist[noofwords]
	end

	root
		:tag( 'ul' )
			:addClass( cssclass )
			:tag( 'li' )
				:wikitext( prefix )
				:wikitext( words )
				:done()
			:done()

	local output = tostring( root )
	return output
end

return p