Module:Resolve category redirect: Difference between revisions

Safer nicotine wiki Tobacco Harm Reduction
Jump to navigation Jump to search
m (Simplify table)
 
imported>Tom.Reding
(Combine similar & most popular regex[1] & [2])
 
(One intermediate revision by one other user not shown)
Line 2: Line 2:


--Returns the target of {{Category redirect}}, if it exists, else returns the original cat.
--Returns the target of {{Category redirect}}, if it exists, else returns the original cat.
--Used by catlinkfollowr(), and so indirectly by all nav_*().
function rtarget( cat )
function rtarget( cat )
local catcontent = mw.title.new( cat or '', 'Category' ):getContent()
local catcontent = mw.title.new( cat or '', 'Category' ):getContent()
if string.match( catcontent or '', '{{ *[Cc]at' ) then
if string.match( catcontent or '', '{{ *[Cc]at' ) then
local regex = {
local regex = {
--the following 11 pages (7 condensed) redirect to [[Template:Category redirect]] (as of 6/2019):
--the following 11 pages (6 condensed) redirect to [[Template:Category redirect]], in descending order, as of 9/2022:
[1] = '{{ *[Cc]ategory *[Rr]edirect', --most likely match 1st
'{{ *[Cc]ate?g?o?r?y?[ _]*[rR]edirect', --505+312+243+1 transclusions
[2] = '{{ *[Cc]at *redirect',         --444+240 transclusions
'{{ *[Cc]atr', --21
[3] = '{{ *[Cc]at *redir',           --8+3
'{{ *[Cc]at[ _]*[rR]edir', --5+3
[4] = '{{ *[Cc]ategory *move',       --6
'{{ *[Cc]at[ _]*[rR]ed', --3+2
[5] = '{{ *[Cc]at *red',             --6
'{{ *[Cc]at[ _]*[mM]ove', --1
[6] = '{{ *[Cc]atr',                  --4
'{{ *[Cc]ategory[ _]*[mM]ove', --0
[7] = '{{ *[Cc]at *move',             --0
}
}
for _, v in pairs (regex) do
for _, v in pairs (regex) do

Latest revision as of 16:23, 18 September 2022

Documentation for this module may be created at Module:Resolve category redirect/doc

local p = {}

--Returns the target of {{Category redirect}}, if it exists, else returns the original cat.
function rtarget( cat )
	local catcontent = mw.title.new( cat or '', 'Category' ):getContent()
	if string.match( catcontent or '', '{{ *[Cc]at' ) then
		local regex = {
			--the following 11 pages (6 condensed) redirect to [[Template:Category redirect]], in descending order, as of 9/2022:
			'{{ *[Cc]ate?g?o?r?y?[ _]*[rR]edirect',	--505+312+243+1 transclusions
			'{{ *[Cc]atr',							--21
			'{{ *[Cc]at[ _]*[rR]edir',				--5+3
			'{{ *[Cc]at[ _]*[rR]ed',				--3+2
			'{{ *[Cc]at[ _]*[mM]ove',				--1
			'{{ *[Cc]ategory[ _]*[mM]ove',			--0
		}
		for _, v in pairs (regex) do
			local rtarget = mw.ustring.match( catcontent, v..'%s*|%s*([^|}]+)' )
			if rtarget then
				rtarget = mw.ustring.gsub(rtarget, '^1%s*=%s*', '')
				rtarget = string.gsub(rtarget, '^[Cc]ategory:', '')
				return rtarget
			end
		end
	end
	return cat
end

function p.main( frame )
	local args = frame:getParent().args
	local cat  = args[1]
	
	if (cat == '') or (cat == nil) then
		return ''
	end
	return rtarget( cat )
end

return p