Module:Sister project

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

This module is used to generate links to our sister projects. Other than for testing, this module should not be used directly, please use the relevant templates in Category:Interwiki templates.

-- The sister project linking module
local p = {}
 
-- Function for displaying the project name
local function getProjectTitle( frame ) 
	local projecttitle = {
		['commons'] = 'Wikimedia Commons',
		['wikibooks'] = 'Wikibooks',
		['wikinews'] = 'Wikinews',
		['wikipedia'] = 'Wikipedia',
		['wikiquote'] = 'Wikiquote',
		['wikisource'] = 'Wikisource',
		['wikispecies'] = 'Wikispecies',
		['wikiversity'] = 'Wikiversity',
		['wikivoyage'] = 'Wikivoyage'
	}
	return projecttitle[ frame.args[ 'project' ] ]
end
 
-- Function for getting the image for the project
local function getProjectImage( frame )
	local projectimage = {
		['commons'] = 'Commons-logo.svg|link=c:',
		['wikibooks'] = 'Wikibooks-logo.svg|link=wikibooks:',
		['wikinews'] = 'Wikinews-logo.svg|link=wikinews:',
		['wikipedia'] = 'Wikipedia-logo-simple.png|link=w:',
		['wikiquote'] = 'Wikiquote-logo.svg|link=wikiquote:',
		['wikisource'] = 'Wikisource-logo.svg|link=wikisource:',
		['wikispecies'] = 'Wikispecies-logo.svg|link=species:',
		['wikiversity'] = 'Wikiversity-logo.svg|link=wikiversity:',
		['wikivoyage'] = 'Wikivoyage-logo.svg|link=wikivoyage:'
	}
	return projectimage[ frame.args[ 'project' ] ]
end
 
-- Function for getting the text for the project
local function getProjectText( frame )
	local projecttext = {
		['commons'] = '[[c:|Wikimedia Commons]] has [[media]] related to:',
		['wikibooks'] = 'The [[wikibooks:|English Wikibooks]] has more information on:',
		['wikinews'] = 'The [[wikinews:|English Wikinews]] has news articles on:',
		['wikipedia'] = 'The [[w:|Simple English Wikipedia]] has an article on:',
		['wikiquote'] = 'The [[wikiquote:|English Wikiquote]] has a collection of quotations related to:',
		['wikisource'] = 'The [[wikisource:|English Wikisource]] has original writing on:',
		['wikispecies'] = '[[wikispecies:|Wikispecies]] has more information on:',
		['wikiversity'] = 'The [[wikiversity:|English Wikiversity]] has learning resources on:',
		['wikivoyage'] = 'The [[wikivoyage:|English Wikivoyage]] has travel guides about:',
	}
	return projecttext[ frame.args[ 'project' ] ]
end
 
-- Function for generating the interwiki link
local function getInterwikiLink( frame, link )
	local interwikilink = {
		['commons'] = 'c:Category:',
		['wikibooks'] = 'wikibooks:',
		['wikinews'] = 'wikinews:',
		['wikipedia'] = 'w:',
		['wikiquote'] = 'wikiquote:',
		['wikisource'] = 'wikisource:',
		['wikispecies'] = 'species:',
		['wikiversity'] = 'wikiversity:',
		['wikivoyage'] = 'wikivoyage:',
	}
	return interwikilink[ frame.args[ 'project' ] ] .. link
end
 
-- The function to generate the links in the proper format
local function generateLinks( frame )
	local lang = mw.language.getContentLanguage()
	local title = mw.title.getCurrentTitle()
	local output = ''
	local junk = ''
	local wordlist = {}
	for key, value in pairs( frame.args ) do
		if ( value == '' ) then
			-- Note: This value is not used at all, just a hack to continue
			-- (Lua does not have a "continue" function)
			junk = value
		elseif ( key == 'project' ) then
			-- Note: This value is not used at all, just a hack to continue
			-- (Lua does not have a "continue" function)
			junk = value
		else
			value = lang:ucfirst( value )
			interwikilink = getInterwikiLink( frame, value )
			table.insert( wordlist, '[[' .. interwikilink .. '|' .. value .. ']]' )
		end
	end
 
	local noofwords = table.maxn( wordlist )
	if ( noofwords == 0 ) then
		word = lang:ucfirst( title.text )
		interwikilink = getInterwikiLink( frame, word )
		output = '[[' .. interwikilink .. '|' .. word .. ']]'
	elseif ( noofwords == 1 ) then
		output = wordlist[1]
	else
		output = table.concat( wordlist, ', ', 1, noofwords - 1 ) .. ' and ' .. wordlist[noofwords]
	end
	return output
end
 
-- The main function to run the module
function p.run( frame )
	local output = ''
	local links = generateLinks( frame )
 
	output = frame:expandTemplate {
		title = 'sisterproject',
		args = {
			project = getProjectTitle( frame ),
			image = getProjectImage( frame ),
			text = getProjectText( frame ),
			link = links
		}
	}
 
	return frame:preprocess( output )
end
 
return p