Module:Cat see also

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Code

require('strict')

local p = {}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	frame = mw.getCurrentFrame()
	-- Special case when the first given link in parameter is 'V', and
	-- the current page name starts by 'V' or 'In'
	if args[1] == 'V' then
		local title = mw.title.getCurrentTitle().text
		local cwttb = mw.text.split( title, ':' );
		if title:sub(1, 1) == 'V' then
			args[1] = 'In' .. mw.getContentLanguage():lcfirst(title)
		elseif title:sub(1, 1) == 'I' then
			args[1] = mw.getContentLanguage():ucfirst(title:sub(3))
		else
			args[1] = nil
		end
		local cwttc = cwttb [1]
		if mw.ustring.sub (cwttc, -5) == ' code' then
			cwttc = mw.ustring.sub (cwttc, 1, #cwttc-5)
		end
		if args[9] ~= nil
			then args['for'] = args[9]
		elseif  mw.ustring.sub (cwttb[1], 1, 22) == 'Valid SVG created with'
			then args['for'] = '[[Template:C' .. mw.ustring.sub (cwttc, 12) .. '|' .. mw.ustring.sub (cwttc, 24) .. ']]'
		elseif mw.ustring.sub (cwttb[1], 1, 24) == 'Invalid SVG created with'
			then args['for'] = '[[Template:C' .. mw.ustring.sub (cwttc, 14) .. '|' .. mw.ustring.sub (cwttc, 26) .. ']]'
		end
	end
	-- filter the list of links in parameters
	local links = {}
	local linkformat = '<span lang="en" dir="ltr">[[:' .. (args.iw or args.SISTER or '') .. 'Category:%s|%s]]</span>'
	for i, v in ipairs(args) do
		-- discard HTML comments and opening or closing "nowiki" tags in values,
		-- then normalize and trim whitespaces
		v = v	:gsub('<!%-%-.-%-%->', '')
			:gsub('<nowiki *>', ''):gsub('</nowiki *>', ''):gsub('<nowiki */>', '')
			:gsub('_', ' '):gsub('%s+', ' '):gsub('^%s*(.-)%s*$', '%1')
		-- do not insert any link for empty values
		if v ~= '' then
			local html = args["h" .. tostring(i)]
			if html ~= nil and html ~= '' then
				table.insert(links, linkformat:format(v, '<' .. html .. '>' .. v .. '</' .. html .. '>'))
			else
				table.insert(links, linkformat:format(v, v))
			end
		end
	end
	-- do not return any box if the filtered list of links is now empty
	if #links == 0 then
		return ''
	end
	-- find the localized template to format the box and message containing the list
	if not args.lang or not mw.language.isSupportedLanguage(args.lang) then
		-- use page language instead of user language on pages translated using the Translate extension
		if frame:callParserFunction('#translation', '') ~= '' then
			args.lang = frame:preprocess('{{PAGELANGUAGE}}')
		else
			args.lang = frame:callParserFunction('Int', 'Lang')
		end
	end
	local fallbacks = mw.language.getFallbacksFor(args.lang)
	table.insert(fallbacks, 1, args.lang)
	for _, lang in ipairs(fallbacks) do
		if mw.title.new('Template:Cat see also/i18n/' .. lang).exists then
			args.lang = lang
			break
		end
	end
	-- prepare the formating of the list according to target language
	args.list = require('Module:Linguistic').conj(links, args.lang, 'and')
	args.length = #links
	-- format the box and its message continaing the list
	return frame:expandTemplate{ title = 'Template:Cat see also/i18n/' .. args.lang, args = args }
end

return p