Module:User:Wyang/zh-cat

This is a private module sandbox of Wyang, for their own experimentation. Items in this module may be added and removed at Wyang's discretion; do not rely on this module's stability.


local export = {}
local sub = mw.ustring.sub
local gsub = mw.ustring.gsub
local match = mw.ustring.match

function export.cat(frame)
	local text = frame.args[1]
	local pagetitle = mw.title.getCurrentTitle().text
	local content = mw.title.new(pagetitle):getContent()
	
	if content then
		while match(content, "%{%{zh%-cat%|[^%}]+%}%}\n%{%{zh%-cat%|[^%}]+%}%}") do
			original = match(content, "%{%{zh%-cat%|[^%}]+%}%}\n%{%{zh%-cat%|[^%}]+%}%}")
			cats = sub(gsub(original, "%}%}\n%{%{zh%-cat", ""), 10, -3)
			local empty, target = {}, {}
			for cat in mw.text.gsplit(cats, "|") do
				if not empty[cat] then
					empty[cat] = true
					table.insert(target, cat)
				end
			end
			content = gsub(content, "%{%{zh%-cat%|[^%}]+%}%}\n%{%{zh%-cat%|[^%}]+%}%}", "{{zh-cat|" .. table.concat(target, "|") .. "}}")
		end
	end
	
	return content
end

return export