Module:TerrainCats

local TerrainCategories = {} -- Helper function that replaces a huge switch statement local ctTrans = require('Module:CTtrans').climateTerrainString

-- Helper function for constructing a category local function Category( category, sortkey ) if sortkey == nil then sortkey = '' else sortkey = '|' .. sortkey end -- The '' is done this way to prevent this page from being categorized. return string.format('%s'..'Category:%s%s', '[[', category, sortkey) end

-- Return a terrain category if ctTrans finds a matching terrain type local function makeCategory( inputText ) if ctTrans(inputText) ~= "" then return Category(           ('Creatures found in %s'):format(ctTrans(inputText))        ) else return nil end end

-- Generate terrain categories for all terrain types given as input. -- Note that "any land" and "any space" have specific meanings that translate -- into multiple categories. function TerrainCategories.TerrainCats( frame ) local output = {} local splitText = mw.text.split(mw.ustring.lower(frame.args[1]), '%s*%p%s*') for _,v in ipairs(splitText) do       if v == "any land" then table.insert(output, makeCategory('desert')) table.insert(output, makeCategory('forest')) table.insert(output, makeCategory('hills')) table.insert(output, makeCategory('marshes')) table.insert(output, makeCategory('mountains')) table.insert(output, makeCategory('plains')) elseif v == "any space" then table.insert(output, makeCategory('wildspace')) table.insert(output, makeCategory('phlogiston')) else table.insert(output, makeCategory(v)) end end return table.concat(output) end

return TerrainCategories