Jump to content

Module:Sister project

From Wiktionary
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=b:',
		['wikinews'] = 'Wikinews-logo.svg|link=n:',
		['wikipedia'] = 'Wikipedia-logo-simple.png|link=w:',
		['wikiquote'] = 'Wikiquote-logo.svg|link=q:',
		['wikisource'] = 'Wikisource-logo.svg|link=s:',
		['wikispecies'] = 'Wikispecies-logo.svg|link=species:',
		['wikiversity'] = 'Wikiversity-logo.svg|link=v:',
		['wikivoyage'] = 'Wikivoyage-logo.svg|link=voy:'
	}
	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 [[b:|English Wikibooks]] has more information on:',
		['wikinews'] = 'The [[n:|English Wikinews]] has news articles on:',
		['wikipedia'] = 'The [[w:|Simple English Wikipedia]] has an article on:',
		['wikiquote'] = 'The [[q:|English Wikiquote]] has a collection of quotations related to:',
		['wikisource'] = 'The [[s:|English Wikisource]] has original writing on:',
		['wikispecies'] = '[[species:|Wikispecies]] has more information on:',
		['wikiversity'] = 'The [[v:|English Wikiversity]] has learning resources on:',
		['wikivoyage'] = 'The [[voy:|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'] = 'b:',
		['wikinews'] = 'n:',
		['wikipedia'] = 'w:',
		['wikiquote'] = 'q:',
		['wikisource'] = 's:',
		['wikispecies'] = 'species:',
		['wikiversity'] = 'v:',
		['wikivoyage'] = 'voy:',
	}
	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