Module:Category handler

-- -- Sorts pages into a category more appropriately than pagename alone -- e.g. - Ruby amulet would be in Category:Ruby under "R" -- This module will instead modify the sort key to be "Amulet" -- Pages whose whole title match the category exactly will have their sort key as " ", placing the page at the top of the category. --

local p = {}

local onmain = require('Module:Mainonly').on_main local ucf = require('Module:Paramtest').ucfirst

function p.main(frame) -- Just don't bother unless we're in the mainspace if not onmain then return '' end

local args = frame:getParent.args local cats = {}

for _, v in ipairs(args) do		table.insert(cats,v:gsub('[Cc]ategory:')					:gsub('[_%s]+',' ') ) end

return p._main(cats) end

function p._main(cat_list) local pagename = mw.title.getCurrentTitle.fullText local ctg = '' for _, v in ipairs(cat_list) do		if pagename:lower:match('^'..v:lower..'$') then ctg = ctg .. ''		elseif pagename:lower:match('^'..v:lower) then local trunc = ucf( pagename:sub(#v+2) ) ctg = ctg .. ''		else ctg = ctg .. ''		end end

return ctg end

return p