模組:Element/ElectronConfiguration
外观
local p={}
local lib_arg = {}
local eleconfdata = {'s','p','d','f','g','h','i','k','l'}
local element_main_data = mw.loadData("Module:Element/data")
local element_lib = require("Module:Element")
local utils = require( 'Module:PeriodicTable/utils' )
function p.ElectronConfiguration(frame)
-- For calling from #invoke.
local args
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame) --frame.args
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
if type(frame) ~= type({}) then args = {frame}
else args = frame end
end
return p._ElectronConfiguration(args)
end
local yesno = nil
function p._ElectronConfiguration(args)
--去除模板引用時可能產生的空格
eleString='' if (args[1] and args[1] ~= '') then eleString = string.gsub(args[1] , "%s$", "") else return '' end
eleid = element_lib.getListID(eleString)
local ele1 = p._copytable(element_main_data[eleid])
if (eleid==-1) then
return ''
end
if (ele1.correct and ele1.correct ~= '') then
return ''
end
local display_all = false
if args["displayAll"] then
if type(yesno) ~= type(tonumber) then yesno = require('Module:Yesno') end
display_all = yesno(args["displayAll"] or 'no')
end
if (ele1.Z and ele1.Z ~= '') then
return p._eleconftostring(ele1.EC, display_all)
end
return ''
end
function p._copytable(input_table)
local output_table
if type(input_table) == type({}) then
output_table = {}
for k,v in pairs(input_table) do
local temp
if type(input_table) == type({}) then
temp = mw.clone(p._copytable(v))
elseif type(input_table) == type(tonumber) then
else
temp = mw.copy(v)
end
output_table[k]=temp
end
elseif type(input_table) == type(tonumber) then
--do nothing
else
output_table = mw.clone(input_table)
end
return output_table
end
function p._eleconftostring(eleconf, showall)
local cen = eleconf.cen or ''
local ceneledata
if cen ~= '' then
local eleid = element_lib.getListID(cen)
ceneledata = p._copytable(element_main_data[eleid])
end
if not ceneledata then ceneledata = p._copytable(element_main_data[2]) end
local elecdis = ''
for period, eles in ipairs(eleconf) do
local elecstr = ''
for shell, elecount in ipairs(eles) do
if type(elecount) == type({}) then--亞軌域
for shell_id, shell_ent in ipairs(elecount) do
local shellstr = ''
local shell_name = shell_ent[2]
local shell_elecount = shell_ent[1]
local valence = shell_elecount
local censhell_ent = ((ceneledata.EC[period] or {})[shell] or {{0,''}})[shell_id]
--尋找同名亞軌域
local shell_ent_check = (ceneledata.EC[period] or {})[shell]
if type(shell_ent_check) == type({}) then
for subshell_id, subshell_ent in ipairs(shell_ent_check) do
if subshell_ent[2] == shell_name then censhell_ent = {subshell_ent[1],subshell_ent[2]} break end
end
else
censhell_ent = {shell_ent_check,''}
end
if not(showall == true) then
valence = valence - ((censhell_ent or {})[1] or 0)
end
if valence > 0 then
local frame = mw.getCurrentFrame()
--{{su|p=2|b=1/2}}
shellstr = tostring(period) .. eleconfdata[shell] .. frame:expandTemplate{ title = 'su', args = { p=tostring(valence), b=tostring(shell_name) } }
elecstr = elecstr .. shellstr
end
end
else
local shellstr = ''
local valence = elecount
if not(showall == true) then
local subshell_name = ''
local shell_ent = (ceneledata.EC[period] or {})[shell]
if type(shell_ent) == type({}) then--亞軌域
for subshell_id, subshell_ent in ipairs(shell_ent) do
if subshell_ent[2] == subshell_name then shell_ent = subshell_ent[1] break end
end
end
valence = valence - (shell_ent or 0)
end
if valence > 0 then
shellstr = tostring(period) .. eleconfdata[shell] .. '<sup>' .. tostring(valence) .. '</sup>'
elecstr = elecstr .. shellstr
end
end
end
elecdis = elecdis .. elecstr
end
if cen ~= '' and not(showall == true) then
elecdis = '[' .. cen .. ']' .. elecdis
end
return elecdis
end
return p