پودمان:Resolve category redirect

از ویکی‌پدیا، دانشنامهٔ آزاد
توضیحات پودمان[ایجاد] [پاکسازی]
local p = {}

--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 )
	local catcontent = mw.title.new( cat or '', 'رده' ):getContent()
	if string.match( catcontent or '', '{{ *رده' ) then
		local regex = {
			--the following 11 pages (7 condensed) redirect to [[Template:Category redirect]] (as of 6/2019):
			{ '1', '{{ *رده *بهتر' }, --most likely match 1st
			{ '2', '{{ *[Cc]ategory *[Rr]edirect' },         --444+240 transclusions
			{ '3', '{{ *تغییرمسیر *رده' },            --8+3
			{ '4', '{{ *تغییر *مسیر *رده' },        --6
			{ '5', '{{ *تغییرمسیررده' },              --6
			{ '6', '{{ *[Cc]atr' },                  --4
			{ '7', '{{ *[Cc]at *move' },             --0
		}
		for k, v in pairs (regex) do
			local rtarget = mw.ustring.match( catcontent, v[2]..'%s*|%s*([^|}]+)' )
			if rtarget then
				rtarget = mw.ustring.gsub(rtarget, '^1%s*=%s*', '')
				rtarget = string.gsub(rtarget, '^رده:', '')
				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