模組: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>
- 要建構化學反應式的資料,必須使用模板引用傳遞參數
- 例如模板中寫 :
{{#invoke:Chemicals|reaction}}
- 叫用模板 :
{{模板名稱|H|2|O|+|C|O|2|eqm|H|2|C|O|3}}
→ 脚本错误:函数“reaction”不存在。
- 例如模板中寫 :
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}}
→ 脚本错误:函数“check_CAS_test”不存在。- 通常會搭配
{{#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._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
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._formula1(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
if (last == '+') then
body = body .. ' + '
else if (last == 'eq' or last == '→') then
body = body .. ' → '
else if (last == 'eqm') then
body = body .. ' [[File:Equilibrium.svg|15px|化学平衡符号]] '
else
body = body .. element._elementlink({last,x})
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
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 == 'eqm') then
body = body .. ' [[File:Equilibrium.svg|15px|化学平衡符号]] '
else
body = body .. element._elementlink({last})
end end end
end
end
last=x
end
return body
end
--本模塊的沙盒(測試)函數
function p.sandbox(frame)
-- For calling from #invoke.
local pframe = frame:getParent()
local args = pframe.args
return p._formula1(args)
end
function p._sandbox(args)
return element_data[p.getListID(args[1])].name
end
return p