模組:Chemicals
外观
![]() | 此模块被引用於約29,000個頁面。 為了避免造成大規模的影響,所有對此模块的編輯應先於沙盒或測試樣例上測試。 測試後無誤的版本可以一次性地加入此模块中,但是修改前請務必於討論頁發起討論。 模板引用數量會自動更新。 |
Module:Chemicals(编辑 | 讨论 | 历史 | 链接 | 监视 | 日志)
本模組中包含一些與化學相關的函式
formula
本函式功能為查詢Module:Element/data資料並產生化學式
Usage:
{{#invoke:Chemicals|formula|<template-argument>}}
Parameters:
- <template-argument>
- 要建構化學式的資料,必須使用模板引用傳遞參數
reaction
本函式功能為查詢Module:Element/data資料並產生反應式
Usage:
{{#invoke:Chemicals|reaction|<template-argument>}}
Parameters:
- <template-argument>
- 要建構化學反應式的資料,必須使用模板引用傳遞參數
check_CAS
- 函數原型(以C語言表示):
boolean_string 模块::Chemicals::check_CAS ( const string cas_no );
本函式功能為查詢檢查CAS號的格式與校驗碼是否正確,僅能由其他模組呼叫。
Datatype:
- boolean_string
- 表示布林值的字串,其值僅會有
"true"
或"false"
Parameters:
- cas_no
- 要判斷的CAS號字串
check_CAS_test
本函式功能為查詢檢查CAS號的格式與校驗碼是否正確,可由模板呼叫。
Usage:
{{#invoke:Chemicals|check_CAS_test|1=<CAS_Number>}}
Parameters:
- <CAS_Number>
- 要判斷CAS號是否正確的CAS號字串
- 例如 :
{{#invoke:Chemicals|check_CAS_test|1=12-35-1}}
→ 第55行Lua错误:attempt to concatenate field '?' (a nil value)- 通常會搭配
{{#ifeq:...|true}}
或{{#ifeq:...|false}}
使用
- 通常會搭配
- 例如 :
{{#ifeq:{{#invoke:Chemicals|check_CAS_test|1=12-35-4}}|true|正確|錯誤}}
→ 錯誤{{#ifeq:{{#invoke:Chemicals|check_CAS_test|1=12-35-1}}|false|{{Nay}}檢查碼校驗失敗|{{Aye}}檢查碼校驗成功}}
→檢查碼校驗成功
- 例如 :
local p = {}
local origArgs
local error = require( 'Module:Error' )
local element= require( 'Module:Element' )
function p.formula(frame)
-- For calling from #invoke.
local pframe = frame:getParent()
local args = pframe.args
return p._formula(args)
end
function p.reaction(frame)
-- For calling from #invoke.
local pframe = frame:getParent()
local args = pframe.args
return p._reaction(args)
end
function p.split_string(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
function p.check_CAS(cas_no)
body = ''
arrs = p.split_string(casid, '-')
split_num = tonumber(table.getn(arrs))
--avoid nil
if (split_num and split_num ~= '') then
if (split_num == 3) then
--cas no have 3 pattern
else
return 'fail'
end
else
--if nil
return 'fail'
end
cas_without_desh = arrs[1] .. arrs[2]
cas_check = tonumber(arrs[3])
if (cas_check and cas_check ~= '') then
else
return 'fail'
end
cas_len = mw.ustring.len(cas_without_desh)
for i = 1,cas_len do
body = body .. cas_without_desh[i] .. ','
end
return body
end
function p.check_CAS_test(frame)
-- For calling from #invoke.
local pframe = frame:getParent()
args = {}
for k, v in pairs( frame.args ) do
args[k] = v;
end
return p._check_CAS_test(args)
end
function p._check_CAS_test(args)
-- For calling from #invoke.
if (args[1] and args[1] ~= '') then
casid = args[1]
else
casid = "000-00-0"
end
return p.check_CAS(casid)
end
function p._formula(args)
local body = ''
if (args['link'] and args['link'] ~= '') then link = args['link'] end
last=''
for v, x in ipairs(args) do
number=tonumber(x)
lastn=tonumber(last)
if(number)then
if((not (lastn)) and last ~= '')then
if(link and link ~= '')then
body = body .. element._element_symbol({last,x})
else
body = body .. element._elementlink({last,x})
end
else if(body == '' and last == '')then
body = x .. body
else if (lastn)then
if(number==1)then
body = body .. '<sup>+</sup>'
else if(number==-1)then
body = body .. '<sup>-</sup>'
else if(number>0)then
body = body .. '<sup>'.. number ..'+</sup>'
else if(number<0)then
body = body .. '<sup>'.. -number ..'-</sup>'
end end end end
end end end
else
if ((not (lastn)) and last ~= '') then
if(link and link ~= '')then
body = body .. element._element_symbol({last})
else
body = body .. element._elementlink({last})
end
end
end
last=x
end
lastn=tonumber(last)
if ((not (lastn)) and last ~= '') then
if(link and link ~= '')then
body = body .. element._element_symbol({last})
else
body = body .. element._elementlink({last})
end
end
if(link and link ~= '')then
body = '[[' .. link .. '|' .. body ..']]'
end
return body
end
function p._reaction(args)
local body = ''
last=''
for v, x in ipairs(args) do
number=tonumber(x)
lastn=tonumber(last)
if(number)then
if((not (lastn)) and last ~= '')then
feq = ''
if(number>1)then feq = feq .. number end
if (last == '+') then
body = body .. ' + ' .. feq
else if (last == 'eq' or last == '→') then
body = body .. ' → ' .. feq
else if (last == '↑') then
body = body .. '↑ ' .. feq
else if (last == 'eqm') then
body = body .. ' [[File:Equilibrium.svg|15px|化学平衡符号]] ' .. feq
else
body = body .. element._elementlink({last,x})
end end end end
else if(body == '' and last == '')then
body = x .. body
else if (lastn)then
if(number==1)then
body = body .. '<sup>+</sup>'
else if(number==-1)then
body = body .. '<sup>-</sup>'
else if(number>0)then
body = body .. '<sup>'.. number ..'+</sup>'
else if(number<0)then
body = body .. '<sup>'.. -number ..'-</sup>'
end end end end
else
body = last .. x .. body
end end end
else
if ((not (lastn)) and last ~= '') then
if (last == '+') then
body = body .. ' + '
else if (last == 'eq' or last == '→') then
body = body .. ' → '
else if (last == '↑') then
body = body .. '↑ '
else if (last == 'eqm') then
body = body .. ' [[File:Equilibrium.svg|15px|化学平衡符号]] '
else
body = body .. element._elementlink({last})
end end end end
end
end
last=x
end
lastn=tonumber(last)
if ((not (lastn)) and last ~= '') then
if (last == '+') then
body = body .. ' + '
else if (last == 'eq' or last == '→') then
body = body .. ' → '
else if (last == '↑') then
body = body .. '↑ '
else if (last == 'eqm') then
body = body .. ' [[File:Equilibrium.svg|15px|化学平衡符号]] '
else
body = body .. element._elementlink({last})
end end end end
end
return body
end
--本模塊的沙盒(測試)函數
function p.sandbox(frame)
-- For calling from #invoke.
local pframe = frame:getParent()
local args = pframe.args
return p._reaction(args)
end
function p._sandbox(args)
return element_data[p.getListID(args[1])].name
end
return p