模組:Resolve category redirect

本页使用了标题或全文手工转换
维基百科,自由的百科全书
文档图示 模块文档[查看] [编辑] [历史] [清除缓存]

分類重定向問題解決模板。

本模板只有一個參數,也就是分類名稱。本模板會自動回傳該分類名稱,除非其檢測發現為「{{分類重定向}}」。

用法

{{Resolve category redirect|分類名稱}}

範例

分類存在,而且不是重定向:Category:1970年代
  • {{Resolve category redirect|1970年代}} → 1970年代
  • {{Resolve category redirect|Category:1970年代}} → Category:1970年代
分類存在,且是重定向:Category:基督新教
  • {{Resolve category redirect|基督新教}} → 基督新教
  • {{Resolve category redirect|Category:基督新教}} → Category:基督新教
分類存在,且是重定向:Category:北朝鮮城市
  • {{Resolve category redirect|北朝鮮城市}} → 北朝鮮城市
  • {{Resolve category redirect|Category:北朝鮮城市}} → Category:北朝鮮城市
分類不存在:Category:無色的綠色事物
  • {{Resolve category redirect|無色的綠色事物}} → 無色的綠色事物
  • {{Resolve category redirect|Category:無色的綠色事物}} → 無色的綠色事物

參見

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 '', 'Category' ):getContent()
	if string.match( catcontent or '', '{{ *[Cc]at' ) then
		local regex = {
			--the following 11 pages (7 condensed) redirect to [[Template:Category redirect]] (as of 6/2019):
			[1] = '{{ *[Cc]ategory *[Rr]edirect', --most likely match 1st
			[2] = '{{ *[Cc]at *redirect',         --444+240 transclusions
			[3] = '{{ *[Cc]at *redir',            --8+3
			[4] = '{{ *[Cc]ategory *move',        --6
			[5] = '{{ *[Cc]at *red',              --6
			[6] = '{{ *[Cc]atr',                  --4
			[7] = '{{ *[Cc]at *move',             --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