跳转到内容

模組:Infobox element isotopes/sandbox

维基百科,自由的百科全书

这是本页的一个历史版本,由A2569875-sandbox留言 | 贡献2018年1月25日 (四) 12:24编辑。这可能和当前版本存在着巨大的差异。

local p = {}

--引入的程式庫
local other_wiki_link_module = require('Module:Ilh')
local yesno = require('Module:Yesno')
local elements = require('Module:Element')
local err_msg = require('Module:Error')

--靜態變數與函數
local static_funcs = { 
	default_link = "化學元素",
	default_symbol = "X",
	table_split = "\n|-",
	table_newline = "\n|",
	no_decay_data_row = "\n| colspan=\"3\" style=\"text-align:left;\" |",
	stable_data_row = "\n| colspan=\"4\" style=\"text-align:left;\" |",
	no_decay_data_row_with_energy = "\n| colspan=\"4\" style=\"text-align:left;\" |",
	stable_data_row_with_energy = "\n| colspan=\"5\" style=\"text-align:left;\" |",
	table_head = "{| class=\"wikitable\" cellpadding=0 style=\" text-align:center;width:100%; border-collapse:collapse; margin:0; padding:0;\"\n|-",
	table_head_isotope = "[[同位素]]",
	table_head_decay = "[[放射性#衰变|衰變]]",
	table_head_natural_abundance = "[[丰度]]",
	table_head_decay_halflife = "[[半衰期]] <small><span style=\"font-weight:normal;\">(\'\'t\'\'<sub>1/2</sub>)</span></small>",
	table_head_decay_mode = "[[衰變方式|方式]]",
	table_head_decay_energy = "[[衰變能量|能量]]<small>([[電子伏特|MeV]])</small>",
	table_head_decay_product = "[[衰变产物|產物]]",
	table_head_elementbox_isotope_header = "\n! rowspan=2 |",
	table_head_isobox_isotope_header = "\n! |",
	table_head_isobox_isotope_outline_header = "\n! colspan=3 |",
	no_value = ' -'--
}
function static_funcs.Isotope_name(mass_data, symbol_data) return "<sup>" .. mass_data .. "</sup>" .. symbol_data end
function static_funcs.get_count(input_list) 
	count = 0 for __id, __value in pairs(input_list.list) do 
		if __value ~= nil then 
			if __value.mode ~= nil then
				if __value.decays ~= nil then
					count = count + __value.decays.count
				else
					count = count + 1
				end
			end
		end
	end 
	return count 
end
function static_funcs.observationally_stable(first_line) if first_line then return "[[觀測上穩定的同位素|觀測上穩定]]" else return "觀測上穩定" end end
function static_funcs.stable(first_line) if first_line then return "[[穩定同位素|穩定]]" else return "穩定" end end
function static_funcs.neutron(neutron_count) return ",帶" .. neutron_count .. "個中子" end
function static_funcs.ref(ref_str) if ref_str then return ref_str else return '' end end
function static_funcs.default_decay_arg(flag) if flag then return "default" else return "__default__" end end
function static_funcs.rowspan(count, head) 
	if head then return "\n! rowspan=\"" .. count .. "\" style=\"text-align:right; vertical-align:middle;\" | " 
	else return "\n| rowspan=\"" .. count .. "\" style=\"text-align:right; vertical-align:middle;\" | " end
end
function static_funcs.decay_data_and_newline(mode, ref) return "\n| |" .. mode .. ref .. "\n| style=\"text-align:right;\" | " end
function static_funcs.check_natural_abundance(str)
	result = mw.ustring.find(str, "%%$", 1, false)
	if result == nil then result = 0 end 
	return result
end
function static_funcs.get_link(loc_link, tsl_link, tsl_lang, display_sym) 
	if loc_link == nil then link_str = display_sym else link_str = "[[" .. loc_link .. "|" .. display_sym .."]]" end
	if tsl_link ~= nil and tsl_lang ~= nil and loc_link ~= nil then
		--construct ilh args
		link_str = other_wiki_link_module.main({
			['lang'] = tsl_lang, ['lang-code'] = tsl_lang, [1] = loc_link, [2] = tsl_link, ['d'] = display_sym
		})
	end
	return link_str
end

--------------------------------------
--debug
--------------------------------------

function p.sandbox_match_test(frame)
	-- For calling from #invoke.
	local pframe = frame:getParent()
	args = {}
	for k, v in pairs( frame.args ) do
		args[k] = v;	   
	end
	body = ''
	decay_list = p.get_decay_list(args)
	
	for decay_id, decay_value in pairs(decay_list.list) do
		body = body .. "\n:" .. "decay data " .. decay_value.id
		
		for decay_it, decay_data in pairs(decay_value) do
			if type(decay_data) ~= "table" then
				body = body .. "\n::" .. decay_it .. " = " .. decay_data
			end
		end
		
		body = body .. "\n::" .. "decay datas [" .. decay_value.decays.count .. "] :"
		for decay_it_entity, decay_entity in pairs(decay_value.decays.list) do
			body = body .. "\n:::" .. "decay data:"
			for decay_it1, decay_data1 in pairs(decay_entity) do
				body = body .. "\n::::" .. decay_it1 .. " = " .. decay_data1
			end
		end
		
	end
	
	return body
end

----------------------------------------------------------------------------
--本體
----------------------------------------------------------------------------

--主函數
function p.main(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	get_args = getArgs(frame, {parentFirst=true})
	
	return p._gen_table(get_args)
end

--產生表格頭部
function p.create_table_head(frame)
	body  = ''
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	get_args = getArgs(frame, {parentFirst=true})
	if get_args['element_box'] ~= nil then is_ele = yesno(get_args['element_box']) end
	if get_args['with_energy'] ~= nil then with_energy = yesno(get_args['with_energy']) end
	body = body .. static_funcs.table_head
	
	if is_ele then
		body = body .. static_funcs.table_head_elementbox_isotope_header .. static_funcs.table_head_isotope
		body = body .. static_funcs.table_head_elementbox_isotope_header .. static_funcs.table_head_natural_abundance
		body = body .. static_funcs.table_head_elementbox_isotope_header .. static_funcs.table_head_decay_halflife
		body = body .. static_funcs.table_head_isobox_isotope_outline_header .. static_funcs.table_head_decay
		body = body .. static_funcs.table_split
	else
		body = body .. static_funcs.table_head_isobox_isotope_outline_header .. static_funcs.table_head_isotope
		body = body .. static_funcs.table_head_isobox_isotope_outline_header .. static_funcs.table_head_decay
		body = body .. static_funcs.table_split .. static_funcs.table_head_isobox_isotope_header
		body = body .. static_funcs.table_head_isobox_isotope_header .. static_funcs.table_head_natural_abundance
		body = body .. static_funcs.table_head_isobox_isotope_header .. static_funcs.table_head_decay_halflife
	end
	body = body .. static_funcs.table_head_isobox_isotope_header .. static_funcs.table_head_decay_mode
	if with_energy then
		body = body .. static_funcs.table_head_isobox_isotope_header .. static_funcs.table_head_decay_energy
	end
	body = body .. static_funcs.table_head_isobox_isotope_header .. static_funcs.table_head_decay_product
	
	return body
end

--穩定同位素資料列
function p.stable_table(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	get_args = getArgs(frame, {parentFirst=true})
	
	return p._gen_table(args, true)
end

--產生適合有有衰敗能量資訊表格的穩定同位素資料列
function p.stable_table_with_energy(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	get_args = getArgs(frame, {parentFirst=true})
	
	return p._gen_table(get_args, true, true)
end

--產生有衰敗能量資訊的資料列
function p.table_with_energy(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	get_args = getArgs(frame, {parentFirst=true})
	
	return p._gen_table(get_args, false, true)
end

--不適合模板包含的呼叫方式
function p.gen_table(frame)
	-- For calling from #invoke.
	local pframe = frame:getParent()
	args = {}
	for k, v in pairs( frame.args ) do
		args[k] = v;	   
	end
	return p._gen_table(args)
end

--產生同位素資料列主函數
function p._gen_table(args, is_stable, with_energy)
	local body = ''
	iso_symbol = static_funcs.default_symbol
	iso_mass = ''
	iso_na = ''
	iso_halflife = static_funcs.no_value
	first_link = true
	check_flag = true

    --非穩定同位素,取得衰變方式個數
	if not is_stable then
		decay_list = p.get_decay_list(args)
		count = static_funcs.get_count(decay_list)
	end
	
	--檢查參數
	if args['mn'] ~= nil then iso_mass = args['mn'] end
	if args['link'] ~= nil then iso_link = args['link'] end
	if args['na'] ~= nil then iso_na = args['na'] end
	if args['halflife'] ~= nil then iso_halflife = args['halflife'] end
	if args['hl'] ~= nil then iso_halflife = args['hl'] end
	if args['firstlinks'] ~= nil then first_link = yesno(args['firstlinks']) end
	
	if args['sym'] ~= nil then 
		if args['sym'] == '' then check_flag = false else iso_symbol = args['sym']  end
	else check_flag = false end
	
	--產生連結
	iso_link_data = static_funcs.get_link(iso_link, args['tsl'], args['lang'], static_funcs.Isotope_name(iso_mass, iso_symbol))
	check_flag = check_flag and elements.getListID(iso_symbol) >= 0

	--確定rowspan
	if not is_stable then
		rowspan = 1 if count > 1 then rowspan = count end
	else
		rowspan = 1
	end
	--建立表格
	body = body .. static_funcs.table_split .. static_funcs.rowspan(rowspan, true and not with_energy) .. iso_link_data
	
	--豐度置中設定
	checkna = static_funcs.check_natural_abundance(iso_na)
	body = body .. "\n| rowspan=\"" .. rowspan .. "\" style=\""
	if checkna > 0 then 
		body = body .. "text-align:right"
	else
		body = body .. "text-align:center"
	end
	body = body .. "; vertical-align:middle;\" | " .. iso_na
	
	if not is_stable then
		--半衰期
		body = body
		--.. static_funcs.table_newline
		.. '\n' .. static_funcs.rowspan(rowspan) .. iso_halflife .. static_funcs.ref(args['halflife_ref'])
	
		--衰變資訊
		if count <= 0 then
			--無衰變資訊
			if with_energy then
				body = body .. static_funcs.no_decay_data_row_with_energy
			else
				body = body .. static_funcs.no_decay_data_row
			end
			
			if args['stable_data'] ~= nil then
				body = body .. args['stable_data'] .. static_funcs.ref(args['halflife_ref'])
			else
				body = body .. static_funcs.observationally_stable(first_link) .. static_funcs.ref(args['halflife_ref'])
			end

		else
			first_flag = true
			--列出所有衰變資訊
			for decay_i = 1,decay_list.count do
				decay_value = nil
				for decay_data_id, decay_data_value in pairs(decay_list.list) do if decay_data_value.iter == decay_i then
					decay_value = decay_data_value
				end end
				if decay_value ~= nil then
				
					if first_flag==true then first_flag = false else body = body .. static_funcs.table_split end
					
					if decay_value.mode == nil then decay_value.mode = ' -' end
					decay_first_flag = true
					body = body .. static_funcs.rowspan(decay_value.decays.count) .. decay_value.mode
					
					first_decay_flag = true
					for i = 1,decay_value.decays.count do
						iterator = nil
						for decay_data_id, decay_data_value in pairs(decay_value.decays.list) do if decay_data_value.iter == i then
							iterator = decay_data_value
						end end
						if iterator ~= nil then 
						if first_decay_flag==true then first_decay_flag = false else body = body .. static_funcs.table_split end
						
						if iterator.energy == nil then iterator.energy = '' end
								
						if with_energy then
							if iterator.mode_ref ~= nil then body = body .. static_funcs.ref(iterator.mode_ref) end
							body = body .. static_funcs.decay_data_and_newline(iterator.energy, static_funcs.ref(iterator.mode_ref))
						else
							body = body .. static_funcs.decay_data_and_newline(iterator.mode, static_funcs.ref(iterator.mode_ref))
						end
						product_mass = ''
						if iterator.mass ~= nil then
							product_mass = iterator.mass
						end
						product_symbol = 'X'
						if iterator.symbol ~= nil then
							product_symbol = iterator.symbol
							check_flag = check_flag and elements.getListID(product_symbol) >= 0
						end
						body = body .. static_funcs.get_link(
							iterator.link, iterator.otherwiki, iterator.lang_code, 
							static_funcs.Isotope_name(product_mass, product_symbol)
						) .. static_funcs.ref(iterator.product_ref)
						end
					end
				
				end
			end

		end
	else
		--穩定同位素
		observationally_stable = "no"
		if args['observationally_stable'] ~= nil then
			observationally_stable = args['observationally_stable']
		end
		
		if with_energy then
			body = body .. static_funcs.stable_data_row_with_energy
		else
			body = body .. static_funcs.stable_data_row
		end

		if yesno(observationally_stable) then
			body = body .. static_funcs.observationally_stable(first_link) .. static_funcs.ref(args['halflife_ref'])
		else
			if args['stable_data'] ~= nil then
				body = body .. args['stable_data'] .. static_funcs.ref(args['halflife_ref'])
			else
				body = body .. static_funcs.stable(first_link) .. static_funcs.ref(args['halflife_ref'])
			end
		end
	end
    --檢查是否有錯誤的元素符號
	if (not check_flag) and mw.title.getCurrentTitle().namespace == 0 then body = body .. "[[Category:含有錯誤元素符號的條目]]" end
	
	if decay_list ~= nil then for error_category_string, ___ in pairs(decay_list.error_category) do
		body = body .. "[[Category:" .. error_category_string .. "]]"
	end body = body .. decay_list.err_msg end
	
	return body
end

--找出衰變資訊
function p.get_decay_list(args)
	--宣告空的衰變列表
	decay_list = { count = 0, list = {}, error_category = {}, max_number = 0, err_msg = '' }
	unknow_arg = nil
	has_noid = false
	has_linkid = false
	if args['dm'] ~= nil or args['de'] ~= nil or args['halflife1'] ~= nil or args['hl1'] ~= nil or
		args['ps'] ~= nil or args['pn'] ~= nil or args['product_ref'] ~= nil or
		args['mode_ref'] ~= nil or args['halflife_ref'] ~= nil or args['energy_ref'] ~= nil then
			has_noid = true
			decay_list.count = 1
			decay_list.max_number = 1
			decay_list.list[static_funcs.default_decay_arg()] = { iter = 1, id = '1', number = 1}
			if args['de'] ~= nil or args['pn'] ~= nil or args['ps'] ~= nil or args['product_ref'] ~= nil or args['energy_ref'] ~= nil
			or args['link1'] ~= nil or args['tsl1'] ~= nil or args['lang1'] ~= nil then
				decay_list.list[static_funcs.default_decay_arg()].decays = { count = 1, list = {} }
				decay_list.list[static_funcs.default_decay_arg()].decays.list[1] = {iter = 1, id = '1'}
			end
			if args['link1'] ~= nil or args['tsl1'] ~= nil or args['lang1'] ~= nil then has_linkid = true end
			decay_list.list[static_funcs.default_decay_arg()].mode = args['dm']
			decay_list.list[static_funcs.default_decay_arg()].decays.list[1].energy = args['de'] 
			if args['halflife0'] ~= nil then decay_list.list[static_funcs.default_decay_arg()].halflife = args['halflife0'] end
			if args['hl0'] ~= nil then decay_list.list[static_funcs.default_decay_arg()].halflife = args['hl0'] end
			decay_list.list[static_funcs.default_decay_arg()].decays.list[1].link = args['link1'] 
			decay_list.list[static_funcs.default_decay_arg()].decays.list[1].otherwiki = args['tsl1'] 
			decay_list.list[static_funcs.default_decay_arg()].decays.list[1].lang_code = args['lang1'] 
			decay_list.list[static_funcs.default_decay_arg()].decays.list[1].symbol = args['ps'] 
			decay_list.list[static_funcs.default_decay_arg()].decays.list[1].mass = args['pn'] 
			decay_list.list[static_funcs.default_decay_arg()].decays.list[1].product_ref = args['product_ref'] 
			decay_list.list[static_funcs.default_decay_arg()].decays.list[1].energy_ref = args['energy_ref'] 
			decay_list.list[static_funcs.default_decay_arg()].mode_ref = args['mode_ref'] 
			decay_list.list[static_funcs.default_decay_arg()].halflife_ref = args['halflife_ref'] 
	end
    --搜尋所有參數
	for arg_name, arg_value in pairs(args) do
		if arg_value ~= '' then if mw.text.trim(arg_value) ~= nil then if mw.text.trim(arg_value) ~= '' then
			--{
			--mode, decays { energy, symbol, mass, link, tsl, lang }
			--}
			match_full_name = mw.ustring.match(arg_name, "[^0-9]+[0-9]+%a*")
			if match_full_name == nil then match_full_name = '' end
			match_full_data_name = mw.ustring.match(match_full_name, "[^0-9]+[0-9]+")
			if match_full_data_name == nil then match_full_data_name = '' end
			match_number = mw.ustring.match(arg_name, "[0-9]+")
			match_number_id = mw.ustring.match(arg_name, "[0-9]+%a*")
			if match_number == nil then match_number = '0' decay_id = "default" else decay_id = match_number end
			if match_number_id == nil then match_number_id = '0' end
			decay_data_id = mw.ustring.match(match_number_id, "%a+")
			match_data_name = mw.ustring.match(match_full_data_name, "[^0-9]+")
			deacy_number = tonumber(match_number)
			
			if has_noid and has_linkid and match_number == '1' then
				decay_list.err_msg = decay_list.err_msg .. err_msg.error{ "參數編號“" .. deacy_number .. '”重複使用。' } .. " <br/> "
				decay_list.error_category['模板參數錯誤的頁面'] = true
			elseif deacy_number == nil then
				--if unknow_arg == nil then unknow_arg =  "“" .. arg_name .. "”" 
				--else unknow_arg = unknow_arg .. "、“" .. arg_name .. "”" end
				--decay_list.error_category['模板參數未知的頁面'] = true
			else
				if match_full_name ~= nil and match_data_name ~= nil then
					if deacy_number > decay_list.max_number then decay_list.max_number = deacy_number end
					match_data_name = mw.text.trim(match_data_name)
					if decay_list.list[decay_id] == nil then 
						decay_list.count = decay_list.count + 1
						decay_list.list[decay_id] = { iter = decay_list.count, number = deacy_number, id = decay_id, decays = { count = 0, list = {} } } 
					end
					if decay_data_id ~= nil then 
						if decay_list.list[decay_id].decays.list[decay_data_id] == nil then 
							decay_list.list[decay_id].decays.count = decay_list.list[decay_id].decays.count + 1
							decay_list.list[decay_id].decays.list[decay_data_id] = {iter = decay_list.list[decay_id].decays.count, id = decay_data_id }
						end
						if match_data_name == "de" then decay_list.list[decay_id].decays.list[decay_data_id].energy = arg_value
						elseif match_data_name == "pn" then decay_list.list[decay_id].decays.list[decay_data_id].mass = arg_value
						elseif match_data_name == "ps" then decay_list.list[decay_id].decays.list[decay_data_id].symbol = arg_value
						elseif match_data_name == "lang" then decay_list.list[decay_id].decays.list[decay_data_id].lang_code = arg_value
						elseif match_data_name == "tsl" then decay_list.list[decay_id].decays.list[decay_data_id].otherwiki = arg_value
						elseif match_data_name == "link" then decay_list.list[decay_id].decays.list[decay_data_id].link = arg_value
						elseif match_data_name == "product_ref" then decay_list.list[decay_id].decays.list[decay_data_id].product_ref = arg_value
						elseif match_data_name == "energy_ref" then decay_list.list[decay_id].decays.list[decay_data_id].energy_ref = arg_value end
					else
						if (match_data_name == "de" or match_data_name == "pn" or match_data_name == "ps" or 
							match_data_name == "product_ref" or match_data_name == "energy_ref") and decay_list.list[decay_id].decays.list[1] == nil then
								decay_list.list[decay_id].decays.list[1] = {iter = 1, id = '1'}
								decay_list.list[decay_id].decays.count = decay_list.list[decay_id].decays.count + 1
						end
						if match_data_name == "de" then decay_list.list[decay_id].decays.list[1].energy = arg_value
						elseif match_data_name == "pn" then decay_list.list[decay_id].decays.list[1].mass = arg_value
						elseif match_data_name == "ps" then decay_list.list[decay_id].decays.list[1].symbol = arg_value
						elseif match_data_name == "product_ref" then decay_list.list[decay_id].decays.list[1].product_ref = arg_value
						elseif match_data_name == "energy_ref" then decay_list.list[decay_id].decays.list[1].energy_ref = arg_value 
						elseif match_data_name == "dm" then decay_list.list[decay_id].mode = arg_value 
						elseif match_data_name == "halflife" or match_data_name == "hl" then decay_list.list[decay_id].halflife = arg_value
						elseif match_data_name == "mode_ref" then decay_list.list[decay_id].mode_ref = arg_value 
						elseif match_data_name == "halflife_ref" then decay_list.list[decay_id].halflife_ref = arg_value end
					end
					--
				end
				--
			end
		end end end
	end
	if unknow_arg ~= nil then decay_list.err_msg = decay_list.err_msg .. err_msg.error{ "未知的參數" .. unknow_arg .. '。' } end
	return decay_list
end

return p