本模板用于template:元素信息框。
local p = {}
local fstr = mw.ustring.format -- shortcut for formattig a string
local utils = require([[Module:Utils]])
local i18nde = require([[Module:I18n]]).loadMessages([[Module:I18n/Elements]])
local i18ndt = require([[Module:I18n]]).loadMessages([[Module:I18n/Misc]])
local elData = require([[Module:Data/Elements]])
local _e = function(...) return i18nde:msg(...) end
local _t = function(...) return i18ndt:msg(...) end
local getArgs = require('Dev:Arguments').getArgs
local function addIdx(dt)
local locIdx = {}
for k, v in pairs(dt) do locIdx[v.localizationID] = k end
dt.getByLoc = function(self, loc) return self[locIdx[loc]] end
return dt
end
local dataEl = addIdx(elData)
local function getEle(frame)
local args = getArgs(frame)
local eleId = args[1]
local ele = eleId and dataEl[eleId]
if ele == nil then
local msgId = i18nde:msgRev(args.pagename or "")
ele = dataEl:getByLoc(msgId)
end
return ele
end
function p.tagCode(t) return "STRINGS.MISC.TAGS." .. string.upper(t) end
function p.id2locName(id)
local lID = dataEl[id] and dataEl[id].localizationID or id
local t = _e(lID)
if utils.isDefaultT(lID, t) then return id end
return t
end
function p.id2pageName(id)
local t = {}
if t[id] ~= nil then return t[id] end
return p.id2locName(id)
end
local function toPercent(n)
local n = tonumber(n)
n = n * 100
if n % 1 < 1e-5 then
n = math.floor(n)
return fstr("%d%%", n)
end
return fstr("%.2f%%", n)
end
function p.getInfo(frame, ele)
local args = getArgs(frame)
local out = {}
if ele == nil then
out["名称"] =
"找不到元素,请检查[[module:元素信息框]]。<br/>" ..
"args[1]=" .. tostring(args[1]) .. "<br/>" .. "args.pagename=" ..
tostring(args.pagename)
return out
end
out["图片"] =
(args.img and #(args.img) ~= 0) and mw.text.nowiki(args.img) or
fstr("%s.png", _e(ele.localizationID))
out["名称"] = _e(ele.localizationID)
local descCode = mw.ustring.gsub(ele.localizationID, "NAME", "DESC")
out["图片说明"] = _e(descCode)
if utils.isDefaultT(descCode, out["图片说明"]) then
out["图片说明"] = nil
end
out["比热"] = ele.specificHeatCapacity
out["热导率"] = ele.thermalConductivity
out["低温相变温度"] = ele.lowTemp and
fstr("%.2f", tonumber(ele.lowTemp) - 273.15)
if ele.lowTempTransitionOreId then
local page1 = p.id2pageName(ele.lowTempTransitionTarget)
local page2 = p.id2pageName(ele.lowTempTransitionOreId)
local ratio2 = tonumber(ele.lowTempTransitionOreMassConversion)
local ratio1 = 1 - ratio2
out["低温相变产物"] = fstr("[[%s]]:[[%s]] = %.2f : %.2f", page1,
page2, ratio1, ratio2)
elseif ele.lowTempTransitionTarget ~= nil then
local page1 = p.id2pageName(ele.lowTempTransitionTarget)
out["低温相变产物"] = fstr("[[%s]]", page1)
end
if ele.state == "Liquid" and ele.lowTempTransitionTarget ~= nil then
local target = dataEl[ele.lowTempTransitionTarget]
if target.state ~= "Liquid" then -- 盐水相变主要产物为浓盐水,因此不结块
local ratio = 1 - tonumber(ele.lowTempTransitionOreMassConversion or 0)
out["结块临界质量"] = 0.8 * target.defaultMass / ratio
end
end
out["高温相变温度"] = ele.highTemp and
fstr("%.2f", tonumber(ele.highTemp) - 273.15)
if ele.highTempTransitionOreId then
local page1 = p.id2pageName(ele.highTempTransitionTarget)
local page2 = p.id2pageName(ele.highTempTransitionOreId)
local ratio2 = tonumber(ele.highTempTransitionOreMassConversion)
local ratio1 = 1 - ratio2
out["高温相变产物"] = fstr("[[%s]]:[[%s]] = %.2f : %.2f", page1,
page2, ratio1, ratio2)
elseif ele.highTempTransitionTarget ~= nil then
local page1 = p.id2pageName(ele.highTempTransitionTarget)
out["高温相变产物"] = fstr("[[%s]]", page1)
end
out["硬度"] = ele.hardness
out["毒性"] = ele.toxicity and ele.toxicity ~= 0 and ele.toxicity or nil
out["摩尔质量"] = ele.molarMass and fstr("%.2f", ele.molarMass)
out["光吸收率"] = ele.lightAbsorptionFactor and
toPercent(ele.lightAbsorptionFactor)
out["辐射吸收率"] = ele.radiationAbsorptionFactor and
toPercent(ele.radiationAbsorptionFactor)
out["辐射"] =
ele.radiationPer1000Mass and ele.radiationPer1000Mass ~= 0 and
math.ceil(ele.radiationPer1000Mass * 1.1) or nil
out["黏度"] = ele.speed
out["标签"] = {}
for _, t in ipairs(ele.tags or {}) do
local tName = _t(p.tagCode(t))
if utils.isDefaultT(p.tagCode(t), tName) then tName = t end
table.insert(out["标签"], fstr("[[:category:%s|%s]]", tName, tName))
end
out["标签"] = table.concat(out["标签"], ",")
out["标签"] = out["标签"] ~= "" and out["标签"] or nil
out["分类"] = ele.materialCategory and
fstr("[[:category:%s|%s]]",
_t(p.tagCode(ele.materialCategory)),
_t(p.tagCode(ele.materialCategory)))
out["单格最大质量"] = ele.maxMass
out["最小垂直流动"] = ele.minVerticalFlow
out["最小水平流动"] = ele.minHorizontalFlow
-- add categories
-- empty args.namespace stands for main pages
if (not args.namespace and not args.nocat) then
out["页面分类"] = {}
if ele.materialCategory then
table.insert(out["页面分类"], fstr("[[category:%s]]", _t(
p.tagCode(
ele.materialCategory))))
end
for _, t in ipairs(ele.tags or {}) do
local tName = _t(p.tagCode(t))
if utils.isDefaultT(p.tagCode(t), tName) then tName = t end
table.insert(out["页面分类"], fstr("[[category:%s]]", tName))
end
if ele.state then
local sName = _e("STRINGS.ELEMENTS.STATE." .. ele.state:upper())
table.insert(out["页面分类"], fstr("[[category:%s]]", sName))
end
out["页面分类"] = #(out["页面分类"]) > 0 and
table.concat(out["页面分类"], "\n") or nil
end
return out
end
-- test by: =p.main(require("Module:debug").frame({},{pagename="气态铝"}))
-- test by: =p.main(require("Module:debug").frame({},{"AluminumGas"}))
-- test by: =p.main(require("Module:debug").frame({},{"Brine"}))
-- test by: =p.main(require("Module:debug").frame({},{"Aerogel"}))
-- test by: =p.main(require("Module:debug").frame({},{"Vacuum"}))
function p.main(frame)
local ele = getEle(frame)
return frame:expandTemplate{
title = "元素信息框/信息框",
args = p.getInfo(frame, ele)
}
end
return p