Module:Elements

local p = {} local fstr = mw.ustring.format -- shortcut for formattig a string

local i18ndm = require("module:i18n/misc") local i18nde = require("module:i18n/elements") local _m = function(...) return i18ndm["en"][(({...})[1])] end local _e = function(...) return i18nde["en"][(({...})[1])] end local nav = require("module:Navbox") local mergeArgs = require("Module:ProcessArgs").merge

-- those are pages that can not be mapped correctly local pageMatch = { ChlorineGas = "Chlorine", Chlorine = "Liquid Chlorine", SaltGas = "Salt Gas", MoltenCarbon = "Liquid Carbon", MoltenCobalt = "Liquid Cobalt", MoltenCopper = "Liquid Copper", MoltenGold = "Liquid Gold", MoltenIron = "Liquid Iron", MoltenNiobium = "Liquid Niobium", MoltenSteel = "Liquid Steel", MoltenSucrose = "Liquid Sucrose", MoltenTungsten = "Liquid Tungsten", MoltenUranium = "Liquid Uranium", LiquidPropane = "Liquid Propane", LiquidMethane = "Methane", SolidMethane = "Solid Methane", Resin = "Liquid Resin", SolidResin = "Methane" }

-- add index and query functions to elements data function addIdx(dt) local idIdx = {} local locIdx = {} for i, v in ipairs(dt) do       idIdx[v.elementId] = i        locIdx[v.localizationID] = i    end dt.getById = function(self, id) return self[idIdx[id]] end dt.getByLoc = function(self, loc) return self[locIdx[loc]] end return dt end

local dataGa = addIdx(require("Module:Data/gas")) local dataLi = addIdx(require("Module:Data/liquid")) local dataSo = addIdx(require("Module:Data/solid")) local dataSp = addIdx(require("Module:Data/special")) local stateData = { solid = dataSo, liquid = dataLi, gas = dataGa, special = dataSp }

function isIn(x, xs) for _, e in ipairs(xs) do if x == e then return true end end return false end

function cateLink(s) return fstr("%s", s, s) end function entityLink(s) return mw.getCurrentFrame:expandTemplate{ title = "pic", args = {24, s, text = 1} } end

-- convert element in yaml files to pagename on this wiki function ele2pagename(e) if pageMatch[e.elementId] then return pageMatch[e.elementId] end local eleName = _e(e.localizationID) or e.localizationID if isDefaultMsg(e.localizationID, eleName) then eleName = e.elementId end -- gas if string.sub(e.elementId, -3) == "Gas" and string.sub(eleName, -3) ~= "Gas" then -- liquid return "Gas " .. eleName elseif string.sub(e.elementId, 0, 6) == "Molten" and string.sub(eleName, 0, 6) ~= "Liquid" and string.sub(eleName, 0, 6) ~= "Molten" then return "Molten " .. eleName elseif string.sub(e.elementId, 0, 6) == "Liquid" and string.sub(eleName, 0, 6) ~= "Liquid" and string.sub(eleName, 0, 6) ~= "Molten" then -- solid return "Liquid " .. eleName elseif string.sub(e.elementId, 0, 5) == "Solid" and string.sub(eleName, 0, 5) ~= "Solid" then return "Solid " .. eleName else return eleName end end

function tagCode(t) return "STRINGS.MISC.TAGS." .. string.upper(t) end

function isDefaultMsg(s, t) return ("&#60;" .. s .. "&#62;") == t or t:sub(0, 7) == "STRINGS" end

function p._main(args) local out = {}

out.title = "Elements" out.template = mw.getCurrentFrame:getParent:getTitle:sub(#"Template:" + 1)

local cateCount = 1 for _, state in ipairs({"solid", "liquid", "gas", "special"}) do       out[fstr("header%d", cateCount)] = cateLink(_m(tagCode(state))) local cates = {} for _, e in ipairs(stateData[state]) do           repeat -- simulate continue inside for loop local currCate = e.materialCategory if e.isDisabled then local showDisabled = args.showDisabled == true if type(args.showDisabled) == "string" then showDisabled = #args.showDisabled ~= 0 and args.showDisabled:lower ~= 'no' and args.showDisabled:lower ~= 'false' end if not showDisabled then do break end -- continue for loop end currCate = "" elseif currCate == nil then currCate = "~" end local eleName = ele2pagename(e) if cates[currCate] then table.insert(cates[currCate], eleName) else cates[currCate] = {eleName} end until true end

cateIds = {} for k, _ in pairs(cates) do table.insert(cateIds, k) end table.sort(cateIds) for _, cate in ipairs(cateIds) do           if cate == "~" then if state == "liquid" then out[fstr("group%d", cateCount)] = "Liquid" else out[fstr("group%d", cateCount)] = "Uncategorised" end elseif cate == "" then out[fstr("group%d", cateCount)] = "Disabled" else out[fstr("group%d", cateCount)] = cateLink(_m(tagCode(cate))) end local eles = cates[cate] table.sort(eles) local listContent = {} for _, e in ipairs(eles) do               table.insert(listContent, entityLink(e)) end out[fstr("list%d", cateCount)] = table.concat(listContent, " &bull; ") cateCount = cateCount + 1 end end -- out.headerstyle = "" -- out.groupstyle = "" out.liststyle = "text-align:left" out.rowstyle = "width:100%" out.sectionstyle = "width:100%"

return nav.main(out) end -- test by: = p._main({pagename = "testPageName"}) -- test by: = p._main({pagename = "testPageName", showDisabled=true}) function p.main(frame) local args = mergeArgs(frame.args, frame:getParent.args) return p._main(args) -- body end return p