Jump to content

Module:R from sort name

From Wikipedia, the free encyclopedia
local c = {
	disambig_magic_word = '__DISAMBIG__',
	template_unambiguous = 'R from sort name/unambiguous',
	template_ambiguous = 'R from sort name/ambiguous',
	cat_unambiguous_from = 'Category:Redirects from sort names',
	cat_unambiguous_to = 'Category:Redirects from sort names by article title',
	cat_ambiguous_from = 'Category:Redirects from ambiguous sort names',
	cat_ambiguous_to = 'Category:Redirects from ambiguous sort names by article title',
	err_not_redirect = '[[Template:R from sort name]] error: page is not a redirect',
}

local remove_accents = require('Module:Latin')._removeaccents

local category = function(category, chr)
	return '[[' .. category .. ', ' .. remove_accents(chr) .. ']]'
end

local p = {}

p.core = function(title, frame)
	if title.exists then
		if title.isRedirect then
			char_from = mw.ustring.sub(title.fullText, 1, 1)
			char_to = mw.ustring.sub(title.redirectTarget.fullText, 1, 1)
			if mw.ustring.find(
				frame:preprocess(
					title.redirectTarget:getContent()
				),
				c.disambig_magic_word,
				1,
				true
			) then
				return (
					frame:expandTemplate{title = c.template_ambiguous} ..
					category(c.cat_ambiguous_from, char_from) ..
					category(c.cat_ambiguous_to, char_to)
				)
			else
				return (
					frame:expandTemplate{title = c.template_umambiguous} ..
					category(c.cat_unambiguous_from, char_from) ..
					category(c.cat_unambiguous_to, char_to)
				)
			end
		else
			return error(c.err_not_redirect)
		end
	else
		return frame:expandTemplate{
			title = c.template_unambiguous
		}
	end
end

p.main = function(frame)
	return p.core(mw.title.getCurrentTitle(), frame)
end

return p