Module:Part of speech

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:
-- adjective, adverb, comparative determiner, irrnoun, noun, noun2, noun3
-- proper noun, 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 = {
		['acronym'] = 'Acronyms',
		['abbreviation'] = 'Abbreviations',
		['conjunction'] = 'Conjunctions',
		['contraction'] = 'Contractions',
		['coordinator'] = 'Coordinators',
		['determiner'] = 'Determiners',
		['expression'] = 'Expressions',
		['initialism'] = 'Initialisms',
		['interjection'] = 'Interjections',
		['letter'] = 'Letters',
		['prefix'] = 'Prefixes',
		['preposition'] = 'Prepositions',
		['pronoun'] = 'Pronouns',
		['subordinator'] = 'Subordinators',
		['suffix'] = 'Suffixes',
		['symbol'] = 'Symbols'
	},

	-- 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 = {
		['acronym'] = '[[acronym|Acronym]]',
		['abbreviation'] = '[[abbreviation|Abbreviation]]',
		['conjunction'] = 'Conjunction',
		['contraction'] = 'Contraction',
		['coordinator'] = 'Coordinator',
		['determiner'] = 'Determiner',
		['expression'] = '[[expression|Expression]]',
		['initialism'] = '[[initialism|Initialism]]',
		['interjection'] = '[[interjection|Interjection]]',
		['letter'] = '[[letter|Letter]]',
		['prefix'] = '[[prefix|Prefix]]',
		['preposition'] = '[[preposition|Preposition]]',
		['pronoun'] = '[[pronoun|Pronoun]]',
		['subordinator'] = 'Subordinator',
		['suffix'] = '[[suffix|Suffix]]',
		['symbol'] = '[[symbol|Symbol]]',
	},

	-- The type of table the respective word types uses.
	-- Note: The number represents the number of columns that the table will use.
	tabletype = {
		['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'
	},

	-- 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 = {}

-- 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( frame )
	output = mw.html.create()
	output
		:tag( 'span' )
		:addClass( 'form-of ' .. getAnchorEncode( frame.args[ '1' ] ) .. '-form-of-' .. getAnchorEncode( frame.args[ '2' ] ) )
		:wikitext( frame.args[ '3' ] )
		: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( frame )
	args = { '1', '2', '3', '4', '5', '6', '7', '8' }
	output = 'inflection-table inflection'
	for arg  in args do
		if ( frame.args[ arg ] == '' ) then
			output = output .. '-' .. getAnchorEncode( '~' )
		else
			output = output .. getAnchorEncode( frame.args[ arg ] )
		end
	end
	return output
end

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

-- Produce a table with only one word (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', '1em' )
				:tag( 'p' )
					:wikitext( config.name[ wordtype ] )
						:tag( 'br' )
						:done()
					:wikitext( "'''" .. title .. "'''" )
					:done()
				: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 )
	end
	output = output .. getCategory( wordtype )
	return frame:preprocess( output )
end

return p