Module:Part of speech/sandbox

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

This module is used for producing the purple table used on entry pages. It is used by templates in Category:Part of speech templates. This module should not be used directly on any pages.

-- Parts of speech template module
-- This module is used for generating the nice format for entry pages.
-- Note: This module is used on ALL pages in Wiktionary, please handle with care.
-- Templates not supported by this module, but is in Category:Part of speech templates:
-- verb, verb2, verb3, verb4, verb5

-- This is the configuration settings for the module. Please follow the instructions
-- carefully and seek consensus when changing this.
local config = {
	-- The category that the entry should be added to when the template is used
	category = {
		['abbreviation'] = 'Abbreviations',
		['acronym'] = 'Acronyms',
		['adjective'] = 'Adjectives',
		['adverb'] = 'Adverbs',
		['comparative determiner'] = 'Determiners',
		['conjunction'] = 'Conjunctions',
		['contraction'] = 'Contractions',
		['coordinator'] = 'Coordinators',
		['determiner'] = 'Determiners',
		['expression'] = 'Expressions',
		['initialism'] = 'Initialisms',
		['interjection'] = 'Interjections',
		['letter'] = 'Letters',
		['noun'] = 'Nouns',
		['prefix'] = 'Prefixes',
		['preposition'] = 'Prepositions',
		['pronoun'] = 'Pronouns',
		['subordinator'] = 'Subordinators',
		['suffix'] = 'Suffixes',
		['symbol'] = 'Symbols',
		['verb'] = 'Verbs'
	},

	-- The name of the part of speech, to be displayed above the word name
	-- Note: This section is only applicable for word types that use only one column in the table!
	name = {
		['abbreviation'] = 'Abbreviation',
		['acronym'] = 'Acronym',
		['conjunction'] = 'Conjunction',
		['contraction'] = 'Contraction',
		['coordinator'] = 'Coordinator',
		['determiner'] = 'Determiner',
		['expression'] = 'Expression',
		['initialism'] = 'Initialism',
		['interjection'] = 'Interjection',
		['letter'] = 'Letter',
		['prefix'] = 'Prefix',
		['preposition'] = 'Preposition',
		['pronoun'] = 'Pronoun',
		['subordinator'] = 'Subordinator',
		['suffix'] = 'Suffix',
		['symbol'] = 'Symbol',
	},

	-- The name to be displayed for the first column (only applicable to multi-column tables)
	firstcolumn = {
		['adjective'] = 'Positive',
		['adverb'] = 'Positive',
		['comparative determiner'] = 'Determiner',
		['noun'] = 'Singular',
		['verb'] = 'Plain form'
	},

	-- The name to be displayed for the second column (only applicable to multi-column tables)
	secondcolumn = {
		['adjective'] = 'Comparative',
		['adverb'] = 'Comparative',
		['comparative determiner'] = 'Comparative',
		['noun'] = 'Plural',
		['verb'] = 'Third-person singular'
	},

	-- The name to be displayed for the third column (only applicable to multi-column tables)
	thirdcolumn = {
		['adjective'] = 'Superlative',
		['adverb'] = 'Superlative',
		['comparative determiner'] = 'Superlative',
		['verb'] = 'Past tense'
	},

	-- The name to be displayed for the fourth column (only applicable to multi-column tables)
	fourthcolumn = {
		['verb'] = 'Past participle'
	},

	-- The name to be displayed for the fifth column (only applicable to multi-column tables)
	fifthcolumn = {
		['verb'] = 'Present participle'
	},

	-- The type of table the respective word types uses.
	-- Note: The number represents the number of columns that the table will use.
	tabletype = {
		-- Word types with only one column
		['acronym'] = 'tableone',
		['abbreviation'] = 'tableone',
		['conjunction'] = 'tableone',
		['contraction'] = 'tableone',
		['coordinator'] = 'tableone',
		['determiner'] = 'tableone',
		['expression'] = 'tableone',
		['initialism'] = 'tableone',
		['interjection'] = 'tableone',
		['letter'] = 'tableone',
		['prefix'] = 'tableone',
		['preposition'] = 'tableone',
		['pronoun'] = 'tableone',
		['subordinator'] = 'tableone',
		['suffix'] = 'tableone',
		['symbol'] = 'tableone',

		-- Word types with two columns
		['noun'] = 'tabletwo',

		-- Word types with three columns
		['adjective'] = 'tablethree',
		['adverb'] = 'tablethree',
		['comparative determiner'] = 'tablethree'
	},

	-- The name of the second derivative (used by getRelevantCreatables)
	secondderivative = {
		['adjective'] = 'comparative',
		['adverb'] = 'comparative',
		['comparative determiner'] = 'comparative',
		['noun'] = 'plural'
	},

	-- The name of the third derivative (used by getRelevantCreatables)
	thirdderivative = {
		['adjective'] = 'superlative',
		['adverb'] = 'superlative',
		['comparative determiner'] = 'superlative'
	},

	-- The background color to use for formatting the entry.
	bgcolor = '#e2e2ff'
}

-- Please do not touch anything below this line, unless you know what you are doing
local p = {}
local yesno = require( 'Module:Yesno' )

-- Get the text to be encoded in URL anchor format
local function getAnchorEncode( text )
	return mw.uri.anchorEncode( text )
end

-- This function implements what used to be Template:creatable
-- This function is intended to help the entry creation script: User:Conrad.Irwin/creation.js
local function getCreatable( one, two, three )
	output = mw.html.create()
	output
		:tag( 'span' )
		:addClass( 'form-of ' .. getAnchorEncode( one ) .. '-form-of-' .. getAnchorEncode( two ) )
		:wikitext( three )
		:done()
	return tostring( output )
end

-- This function implements what used to be Template:creation helper
-- This function is intended to help the entry creation script: User:Conrad.Irwin/creation.js
local function getCreationHelper( ... )
	output = 'inflection-table inflection'
	for i, value in ipairs( arg ) do
		if ( value == '' ) then
			output = output .. '-' .. getAnchorEncode( '~' )
		else
			output = output .. '-' .. getAnchorEncode( value )
		end
	end
	return output
end

-- This function wraps the creatable text in a nice format
local function getCreatableText( type, title, derivative )
	return getCreatable( type, title, "'''[[" .. derivative .. "]]'''" )
end

-- This function creates the wikitext to add the page to the relevant category
local function getCategory( wordtype )
	return '[[Category:' .. config.category[ wordtype ] .. ']]'
end

-- This function formats available arguments given to the module and transforms them
-- into relevant links. For example: If multiple singulars are provided for a noun,
-- it will format it nicely so that both links appear. If not, only one appears.
local function getRelevantLinks( first, second, third )
	if ( first == 'none' and pagetitle ~= 'none' ) then
		output = "''none''"
	else
		wordlist = {}
		if ( first ~= '' ) then
			table.insert( wordlist, "'''[[" .. first .. "]]'''" )
		end
		if ( second ~= '' ) then
			table.insert( wordlist, "'''[[" .. second .. "]]'''" )
		end
		if ( third ~= '' ) then
			table.insert( wordlist, "'''[[" .. third .. "]]'''" )
		end
		number = table.maxn( wordlist )
		if ( number == 0 ) then
			output = "''none''"
		elseif ( number == 1 ) then
			output = wordlist[ 1 ]
		else
			output = table.concat( wordlist, ', ', 1, number - 1 ) .. ' or ' .. wordlist[ number ]
		end
	end
	return output
end

local function getRelevantCreatables( type, pagetitle, first, second, third )
	if ( first == 'none' and pagetitle ~= 'none' ) then
		output = "''none''"
	else
		wordlist = {}
		if ( first ~= '' ) then
			table.insert( wordlist, getCreatable( type, pagetitle, first ) )
		end
		if ( second ~= '' ) then
			table.insert( wordlist, getCreatable( type, pagetitle, second ) )
		end
		if ( third ~= '' ) then
			table.insert( wordlist, getCreatable( type, pagetitle, third ) )
		end
		number = table.maxn( wordlist )
		if ( number == 0 ) then
			output = "''none''"
		elseif ( number == 1 ) then
			output = wordlist[ 1 ]
		else
			output = table.concat( wordlist, ', ', 1, number - 1 ) .. ' or ' .. wordlist[ number ]
		end
	end
	return output
end

local function getTableThreeMixedLinks( type, pagetitle, first, second, third )
	if ( first == 'none' and pagetitle ~= 'none' ) then
		output = "''none''"
	else
		wordlist = {}
		if ( first ~= '' ) then
			table.insert( wordlist, "'''" .. first .. "'''" )
		end
		if ( second ~= '' ) then
			table.insert( wordlist, getCreatable( type, pagetitle, second ) )
		end
		if ( third ~= '' ) then
			table.insert( wordlist, getCreatable( type, pagetitle, third ) )
		end
		number = table.maxn( wordlist )
		if ( number == 0 ) then
			output = "''none''"
		elseif ( number == 1 ) then
			output = wordlist[ 1 ]
		else
			output = table.concat( wordlist, ', ', 1, number - 1 ) .. ' or ' .. wordlist[ number ]
		end
	end
	return output
end

-- Produce a table with only one column (width will be set to 100%)
local function getTableOne( wordtype, title )
	output = mw.html.create()
	output
		:tag( 'table' )
		:attr( 'border', '0' )
		:attr( 'width', '100%' )
			:tag( 'tr' )
				:tag( 'td' )
				:attr( 'bgcolor', config.bgcolor )
				:attr( 'valign', 'top' )
				:attr( 'width', '100%' )
				:css( 'padding-left', '5px' )
				:tag( 'p' )
					:wikitext( config.name[ wordtype ] )
						:tag( 'br' )
						:done()
					:wikitext( "'''" .. title .. "'''" )
					:done()
				:done()
			:done()
		:done()
	return tostring( output )
end

-- Produce a table with two columns (width will be set to 49% each)
-- This function is used by: Nouns
local function getTableTwo( wordtype, pagetitle, fir1, fir2, fir3, sec1, sec2, sec3 )
	output = mw.html.create()
	output
		:tag( 'table' )
		:attr( 'border', '0' )
		:attr( 'width', '100%' )
		:addClass( getCreationHelper( wordtype, fir1, fir2, fir3, sec1, sec2, sec3 ) )
			:tag( 'tr' )
				:tag( 'td' )
				:attr( 'bgcolor', config.bgcolor )
				:attr( 'valign', 'top' )
				:attr( 'width', '49%' )
				:css( 'padding-left', '5px' )
				:tag( 'p' )
					:wikitext( config.firstcolumn[ wordtype ] )
						:tag( 'br' )
						:done()
					:wikitext( getRelevantLinks( fir1, fir2, fir3 ) )
					:done()
				:done()
				:tag( 'td' )
				:attr( 'width', '0.5%' )
				:done()
				:tag( 'td' )
				:attr( 'bgcolor', config.bgcolor )
				:attr( 'valign', 'top' )
				:attr( 'width', '49%' )
				:css( 'padding-left', '5px' )
				:tag( 'p' )
					:wikitext( config.secondcolumn[ wordtype ] )
						:tag( 'br' )
						:done()
					:wikitext( getRelevantCreatables( config.secondderivative[ wordtype ], pagetitle, sec1, sec2, sec3 ) )
					:done()
				:done()
				:tag( 'td' )
				:attr( 'width', '0.5%' )
				:done()
			:done()
		:done()
	return tostring( output )
end

-- Produce a table with three columns (width will be set to 32% - 33%)
-- This function is used by: Adjectives, Adverbs, Comparative determiners, Modal verbs
-- Not supported: Modal verbs
local function getTableThree( wordtype, more, pagetitle, fir1, fir2, fir3, sec1, sec2, sec3, thi1, thi2, thi3 )
	if ( yesno( more ) ) then
		second = getTableThreeMixedLinks( config.secondderivative[ wordtype ], pagetitle, "more " .. pagetitle, sec2, sec3 )
		third = getTableThreeMixedLinks( config.thirdderivative[ wordtype ], pagetitle, "most " .. pagetitle, thi2, thi3 )
	else
		second = getRelevantCreatables( config.secondderivative[ wordtype ], pagetitle, sec1, sec2, sec3 )
		third = getRelevantCreatables( config.thirdderivative[ wordtype ], pagetitle, thi1, thi2, thi3 )
	end
		
	output = mw.html.create()
	output
		:tag( 'table' )
		:attr( 'border', '0' )
		:attr( 'width', '100%' )
		:addClass( getCreationHelper( wordtype, fir1, fir2, fir3, sec1, sec2, sec3, thi1, thi2, thi3 ) )
			:tag( 'tr' )
				:tag( 'td' )
				:attr( 'bgcolor', config.bgcolor )
				:attr( 'valign', 'top' )
				:attr( 'width', '33%' )
				:css( 'padding-left', '5px' )
				:tag( 'p' )
					:wikitext( config.firstcolumn[ wordtype ] )
						:tag( 'br' )
						:done()
					:wikitext( getRelevantLinks( fir1, fir2, fir3 ) )
					:done()
				:done()
				:tag( 'td' )
				:attr( 'width', '0.5%' )
				:done()
				:tag( 'td' )
				:attr( 'bgcolor', config.bgcolor )
				:attr( 'valign', 'top' )
				:attr( 'width', '32.5%' )
				:css( 'padding-left', '5px' )
				:tag( 'p' )
					:wikitext( config.secondcolumn[ wordtype ] )
						:tag( 'br' )
						:done()
					:wikitext( second )
					:done()
				:done()
				:tag( 'td' )
				:attr( 'width', '0.5%' )
				:done()
				:tag( 'td' )
				:attr( 'bgcolor', config.bgcolor )
				:attr( 'valign', 'top' )
				:attr( 'width', '33%' )
				:css( 'padding-left', '5px' )
				:tag( 'p' )
					:wikitext( config.thirdcolumn[ wordtype ] )
						:tag( 'br' )
						:done()
					:wikitext( third )
					:done()
				:done()
				:tag( 'td' )
				:attr( 'width', '0.5%' )
				:done()
			:done()
		:done()
	return tostring( output )
end

-- Main function to be called by templates
function p.run( frame )
	local wordtype = frame.args[ 'type' ]
	local tabletype = config.tabletype[ wordtype ]
	output = ''
	if ( tabletype == 'tableone' ) then
		if ( wordtype == 'letter' ) then
			title = frame.args[ '1' ] .. "''' or '''" .. frame.args[ 'pagename' ]
		else
			title = frame.args[ '1' ]
		end
		output = output .. getTableOne( wordtype, title )
	elseif ( tabletype == 'tabletwo' ) then
		output = output .. getTableTwo( wordtype, frame.args[ 'pagename' ], frame.args[ 'first1' ], frame.args[ 'first2' ], frame.args[ 'first3' ], frame.args[ 'second1' ], frame.args[ 'second2' ], frame.args[ 'second3' ]  )
	elseif ( tabletype == 'tablethree' ) then
		output = output .. getTableThree( wordtype, frame.args[ 'more' ], frame.args[ 'pagename' ], frame.args[ 'first1' ], frame.args[ 'first2' ], frame.args[ 'first3' ], frame.args[ 'second1' ], frame.args[ 'second2' ], frame.args[ 'second3' ], frame.args[ 'third1' ], frame.args[ 'third2' ], frame.args[ 'third3' ]  )
	end
	output = output .. getCategory( wordtype )
	return frame:preprocess( output )
end

return p