模組:Element/ElectronConfiguration

被永久保护的模块
维基百科,自由的百科全书
文档图示 模块文档[创建]
local p={}
local lib_arg = {}
local eleconfdata = {'s','p','d','f','g','h','i','k','l','m','n','o'}
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)
	return p._ElectronConfiguration(element_lib._loadArgFromFrame(frame))
end

local yesno = nil

function p._ElectronConfiguration(args)
	--去除模板引用時可能產生的空格
	local eleString='' if (args[1] and args[1] ~= '') then eleString = mw.text.trim(args[1]) else return '' end
	local 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 = ''
		local period = _period
		local shell_length = math.ceil((_period+1)/2)
		if not(showall == true) then --不顯示全部 照原始順序與數量
			shell_length = #eles
		end
		for shell_it=1,shell_length do
			if showall == true then --顯示全部: 能級交錯 (s,t不變,其餘-2,如7s 7p 6d 5f)
				period = _period - math.max(shell_it - 2,0)
			end --不顯示全部: 原樣
			if eleconf[period][shell_it] then
				local shell = shell_it
				local elecount = eleconf[period][shell_it]
				
				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
		end
		elecdis = elecdis .. elecstr
	end
	if cen ~= '' and not(showall == true) then
		elecdis = '[' .. cen .. ']' .. elecdis
	end
	return elecdis
end

function p.ElectronShell(frame)
	return p._electronShell(element_lib._loadArgFromFrame(frame))
end

function p._electronShell(args)
	--去除模板引用時可能產生的空格
	local eleString='' if (args[1] and args[1] ~= '') then eleString = mw.text.trim(args[1]) else return '' end
	local 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 comma = ','
	if args["comma"] and args["comma"]~='' then comma = args["comma"] or comma end
	if (ele1.Z and ele1.Z ~= '') then
		return p._eleshelltostring(ele1.EC, display_all)
	end
	return ''
end

function p._eleshelltostring(eleconf, _comma)
	local comma=_comma or','
	local elecdis = ''
	for period, eles in ipairs(eleconf) do
		local ele_shell_count = 0
		for shell, elecount in ipairs(eles) do
			if type(elecount) == type({}) then
				for shell_id, shell_ent in ipairs(elecount) do
					local shell_name = shell_ent[2]
					local shell_elecount = shell_ent[1]
					ele_shell_count = ele_shell_count + shell_elecount
				end
			else
				ele_shell_count = ele_shell_count + elecount
			end
		end
		if elecdis~='' then elecdis=elecdis..comma end
		elecdis=elecdis..tostring(ele_shell_count)
	end
	return elecdis
end
return p