Module:ClimateCats

local ClimateCategories = {} -- 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 climate categories for all climate types given as input. -- Note that "any" has specific meaning that translates into multiple categories. function ClimateCategories.ClimateCats( 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" then table.insert(output, makeCategory('cold')) table.insert(output, makeCategory('temperate')) table.insert(output, makeCategory('warm')) else table.insert(output, makeCategory(v)) end end return table.concat(output) end

return ClimateCategories