Module:Wiktionaries

From Wiktionary
Jump to: navigation, search
Documentation icon Module documentation[view] [change] [history] [refresh]

This module is used for generating the list of Wiktionaries on the Main Page. It uses the list of Wiktionaries given in the configuration page and formats the links before displaying it to the reader.

Note: This module (along with its configuration) is used directly on the Main Page. Please change the page with care.

Updating the list[change]

Go to the configuration page and move the language code accordingly between the lists. Please make sure that the language codes are in alphabetical order, so as to be neutral about which wiki will appear first in the list.

-- List of Wiktionaries module
local cfgTable = mw.loadData( 'Module:Wiktionaries/configuration' )
local p = {}
local codelist = {}

-- Get the HTML string for the horizontal rule
local function getHorizontalRule()
	return '<hr style="margin-top:0;color:#EEE4CE;">'
end

-- Get the footer in a nice HTML string
local function getFooter()
	local root = mw.html.create()
	root
		:tag( 'center' )
			:wikitext( "''[[meta:Wiktionary#List of Wiktionaries|Meta list]]'' + " )
			:tag( 'span' )
				:addClass( 'plainlinks' )
				:wikitext( '[https://www.wiktionary.org/ All Wiktionaries]' )
				:done()
			:done()

	local output = tostring( root )
	return output
end

-- Get the native language name of the language code
local function getNativeLanguageName( code )
	return mw.language.fetchLanguageName( code )
end

-- Get the local language name of the language code
local function getLocalLanguageName( code )
	local lang = mw.language.getContentLanguage()
	return mw.language.fetchLanguageName( code, lang:getCode() )
end

-- Get the generated list given a table
local function getGeneratedList( langlist )
	return table.concat( langlist, ' &bull; \n' )
end

-- Get the link name to display
local function getLinkName( code, nativelang, locallang )
	return mw.ustring.format( '[[:%s:|%s (%s)]]', code, nativelang, locallang )
end

-- Get the sidebar interwiki links
local function getSidebarLink( code )
	return mw.ustring.format( '[[%s:]]', code )
end

-- Parse the tables
local function parseTable( langtable, prefix )
	local langs = cfgTable[ langtable ]
	local output = ''
	local langlist = {}

	for key, code in pairs( langs ) do
		table.insert( langlist, getLinkName( code, getNativeLanguageName( code ), getLocalLanguageName( code ) ) )
		if ( langtable ~= 4 and langtable ~= 5 ) then
			table.insert( codelist, getSidebarLink( code ) )
		end
	end

	prefix = prefix .. ': '
	if ( langtable == 4 ) then
		output = prefix .. getGeneratedList( langlist )
	elseif ( langtable == 5 ) then
		output = '<small>' .. prefix .. getGeneratedList( langlist ) .. '</small>'
	else
		prefix = mw.ustring.format( '<big>%s</big>', prefix )
		output = prefix .. getGeneratedList( langlist )
	end
	return output
end

-- Run the module
function p.run( frame )
	local tables = cfgTable[ 'tables' ]
	local output = ''
	-- Hack to make sure the order we want is given (largest to smallest)
	output = output .. parseTable( 1, tables[1] ) .. '\n'
	output = output .. getHorizontalRule() .. '\n'
	output = output .. parseTable( 2, tables[2] ) .. '\n'
	output = output .. getHorizontalRule() .. '\n'
	output = output .. parseTable( 3, tables[3] ) .. '\n'
	output = output .. getHorizontalRule() .. '\n'
	output = output .. parseTable( 4, tables[4] ) .. '\n'
	output = output .. getHorizontalRule() .. '\n'
	output = output .. parseTable( 5, tables[5] ) .. '\n'
	output = output .. getHorizontalRule() .. '\n'
	output = output .. getFooter() .. '\n'
	output = output .. table.concat( codelist, '\n' )
	return frame:preprocess( output )
end

return p