Module:Categories

local p = {} local h = {} local cargo = mw.ext.cargo local utilsCode = require("Module:UtilsCode") local utilsGame = require("Module:UtilsGame") local utilsNavbox = require("Module:UtilsNavbox") local utilsTable = require("Module:UtilsTable")

-- See the module for documentation about subcategories. local SUBCATEGORIES = require("Module:Categories/Subcategories")

function p._CategorizeEntries(frame) local args = frame:getParent.args return p.CategorizeEntries(frame.args["plain"], args) end

function p.CategorizeEntries(plain, lists) local result = "" result = result .. p.PlainToNavboxes(plain) result = result .. p.PlainToCategories(plain) result = result .. p.GamesToCategories(lists) return result end

function p.PlainToNavboxes(plain) result = "" local plaintable = mw.text.split(plain, '%s*,%s*') local navboxTitle = "" local rows = {} local rowDisplayName = "" local notCategories = "" local notParentCategories = "" local dplQuery = "" local dplParameters = '|namespace=|includesubpages=false|skipthispage=no|mode=userformat|format=,%PAGE%,;,|ordermethod=titlewithoutnamespace|noresultsheader=emptyCategory}}' -- For every category entered in the template for key, category in ipairs(plaintable) do		rows = {} if not utilsCode.IsEmpty(category) then if tonumber(mw.getCurrentFrame:callParserFunction{name = 'PAGESINCATEGORY', args = {category, "R", "pages"}}) <= 100 then -- Sets the title, which is "X in " navboxTitle = "" .. category .. " in " .. mw.getCurrentFrame:expandTemplate{ title = "TLoZ", args = { "Series" } } local isPrimaryNav = h.isPrimaryNav(category, plaintable) -- If subcategories exist, handles that if not utilsCode.IsEmpty(SUBCATEGORIES[category]) then notCategories = "" for key2, subCategory in ipairs(SUBCATEGORIES[category]) do						notParentCategories = "" notCategories = notCategories .. "|notcategory=" .. subCategory["category"] if not utilsCode.IsEmpty(subCategory["parents"]) then for key3, parentCategory in ipairs(subCategory["parents"]) do notParentCategories = notParentCategories .. "|notcategory=" .. parentCategory end end dplQuery = h.dplToExternalLinks(mw.getCurrentFrame:preprocess("{{#dpl:|category=" .. category .. "|category=" .. subCategory["category"] .. notParentCategories .. dplParameters)) if not (dplQuery == "emptyCategory") then if utilsCode.IsEmpty(subCategory["display"]) then rowDisplayName = subCategory["category"] else rowDisplayName = subCategory["display"] end table.insert(rows, {title = rowDisplayName, content = dplQuery}) end end -- Other(s) row dplQuery = h.dplToExternalLinks(mw.getCurrentFrame:preprocess("{{#dpl:|category=" .. category .. notCategories .. dplParameters)) if not (dplQuery == "emptyCategory") then table.insert(rows, {title = "Other(s)", content = dplQuery}) end result = result .. utilsNavbox.CreateRowNavbox(rows, navboxTitle, not isPrimaryNav) -- else just outputs everything else dplQuery = h.dplToExternalLinks(mw.getCurrentFrame:preprocess("{{#dpl:|category=" .. category .. dplParameters)) if not (dplQuery == "emptyCategory") then table.insert(rows, {title = "All", content = dplQuery}) result = result .. utilsNavbox.CreateRowNavbox(rows, navboxTitle, not isPrimaryNav) end end end end end return tostring(result) end

function h.isPrimaryNav(categoryName, categoriesInList) local categoryConfig = SUBCATEGORIES[categoryName] if not categoryConfig or not categoryConfig.primary then return false end local categorySet = utilsCode.Set(categoriesInList) -- A nav is not primary if a subcategory is present that is primary for _, subcategory in ipairs(categoryConfig) do		local subcategoryName = subcategory.category local pageHasSubcategory = categorySet[subcategoryName] local subcategoryConfig = SUBCATEGORIES[subcategoryName] if pageHasSubcategory and subcategoryConfig and subcategoryConfig.primary then return false end end return true end

function h.dplToExternalLinks(dplResult) local result = "" local firstLink = true dplResult = mw.text.split(dplResult, '%s*;%s*') -- Removing the last entry in the table since it's "empty" due to how DPL -- adds a ";" at the end of each entry instead of just in-between entries table.remove(dplResult) for key, link in ipairs(dplResult) do		if firstLink == true then firstLink = false else result = result .. ' · '		end result = result .. ' [https://zelda.gamepedia.com/' .. mw.getCurrentFrame:callParserFunction{name = 'urlencode', args = {link, "WIKI"}} .. ' ' .. link .. '] '	end return result end

function p.PlainToCategories(plain) result = "" local plaintable = mw.text.split(plain, '%s*,%s*') for key, category in ipairs(plaintable) do		if not utilsCode.IsEmpty(category) then result = result .. ""		end end return result end

function p.GamesToCategories(lists) local result = "" local categories = {} local acceptedLists = { {parameter = "bosses", display = "Bosses"}, {parameter = "characters", display = "Characters"}, {parameter = "dungeons", display = "Dungeons"}, {parameter = "enemies", display = "Enemies"}, {parameter = "items", display = "Items"}, {parameter = "levels", display = "Levels"}, {parameter = "objects", display = "Objects"}, {parameter = "places", display = "Places"}, {parameter = "playable", display = "Playable Characters"}, {parameter = "songs", display = "Songs"}, {parameter = "stages", display = "Stages"}, {parameter = "sub-bosses", display = "Sub-Bosses"}, }

for key, list in ipairs(acceptedLists) do		if not (utilsCode.IsEmpty(lists[list["parameter"]])) then for key2, entry in ipairs(mw.text.split(lists[list["parameter"]], '%s*,%s*')) do				table.insert(categories, {list["display"], entry}) end end end for _, game in ipairs(utilsGame.GetSortOrder("canon")) do		for _2, category in ipairs(categories) do			if game == category[2] then result = result .. ""			end end end return result end

return p