Module:Chem2
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Chem2/doc
local getArgs = require('Module:Arguments').getArgs
local p = {} -- module's table
local am = {} -- Elements with wiki links
am.H="[[Chemistry:Hydrogen|H]]";am.He="[[Chemistry:Helium|He]]";
am.Li="[[Chemistry:Lithium|Li]]";am.Be="[[Chemistry:Beryllium|Be]]";am.B="[[Chemistry:Boron|B]]";am.C="[[Chemistry:Carbon|C]]";am.N="[[Chemistry:Nitrogen|N]]";am.O="[[Chemistry:Oxygen|O]]";am.F="[[Chemistry:Fluorine|F]]";am.Ne="[[Chemistry:Neon|Ne]]";
am.Na="[[Chemistry:Sodium|Na]]";am.Mg="[[Chemistry:Magnesium|Mg]]";am.Al="[[Chemistry:Aluminium |Al]]";am.Si="[[Chemistry:Silicon|Si]]";am.P="[[Chemistry:Phosphorus|P]]";am.S="[[Chemistry:Sulfur|S]]";am.Cl="[[Chemistry:Chlorine|Cl]]";am.Ar="[[Chemistry:Argon|Ar]]";
am.K="[[Chemistry:Potassium|K]]";am.Ca="[[Chemistry:Calcium|Ca]]";am.Sc="[[Chemistry:Scandium|Sc]]";am.Ti="[[Chemistry:Titanium|Ti]]";am.V="[[Chemistry:Vanadium|V]]";am.Cr="[[Chemistry:Chromium|Cr]]";am.Mn="[[Chemistry:Manganese|Mn]]";am.Fe="[[Chemistry:Iron|Fe]]";am.Co="[[Chemistry:Cobalt|Co]]";am.Ni="[[Chemistry:Nickel|Ni]]";am.Cu="[[Chemistry:Copper|Cu]]";am.Zn="[[Chemistry:Zinc|Zn]]";am.Ga="[[Chemistry:Gallium|Ga]]";am.Ge="[[Chemistry:ermanium|Ge]]";am.As="[[Chemistry:Arsenic|As]]";am.Se="[[Chemistry:Selenium|Se]]";am.Br="[[Chemistry:Bromine|Br]]";am.Kr="[[Chemistry:Krypton|Kr]]";am.Rb="[[Chemistry:Rubidium|Rb]]";
am.Sr="[[Chemistry:Strontium|Sr]]";am.Y="[[Chemistry:Yttrium|Y]]";am.Zr="[[Chemistry:Zirconium|Zr]]";am.Nb="[[Chemistry:Niobium|Nb]]";am.Mo="[[Chemistry:Molybdenum|Mo]]";am.Tc="[[Chemistry:Technetium|Tc]]";am.Ru="[[Chemistry:Ruthenium|Ru]]";am.Rh="[[Chemistry:Rhodium|Rh]]";am.Pd="[[Chemistry:Palladium|Pd]]";am.Ag="[[Chemistry:Silver|Ag]]";am.Cd="[[Chemistry:Cadmium|Cd]]";am.In="[[Chemistry:Indium|In]]";am.Sn="[[Chemistry:Tin|Sn]]";am.Sb="[[Chemistry:Antimony|Sb]]";am.Te="[[Chemistry:Tellurium|Te]]";am.I="[[Chemistry:Iodine|I]]";am.Xe="[[Chemistry:Xenon|Xe]]";
am.Cs="[[Chemistry:Caesium|Cs]]";am.Ba="[[Chemistry:Barium|Ba]]";am.La="[[Chemistry:Lanthanum|La]]";am.Ce="[[Chemistry:Cerium|Ce]]";am.Pr="[[Chemistry:Praseodymium|Pr]]";am.Nd="[[Chemistry:Neodymium|Nd]]";am.Pm="[[Chemistry:Promethium|Pm]]";am.Sm="[[Chemistry:Samarium|Sm]]";am.Eu="[[Chemistry:Europium|Eu]]";am.Gd="[[Chemistry:Gadolinium|Gd]]";am.Tb="[[Chemistry:Terbium|Tb]]";am.Dy="[[Chemistry:Dysprosium|Dy]]";am.Ho="[[Chemistry:Holmium|Ho]]";am.Er="[[Chemistry:Erbium|Er]]";am.Tm="[[Chemistry:Thulium|Tm]]";am.Yb="[[Chemistry:Ytterbium|Yb]]";am.Lu="[[Chemistry:Lutetium|Lu]]";am.Hf="[[Chemistry:Hafnium|Hf]]";am.Ta="[[Chemistry:Tantalum|Ta]]";am.W="[[Chemistry:Tungsten|W]]";am.Re="[[Chemistry:Rhenium|Re]]";am.Os="[[Chemistry:Osmium|Os]]";am.Ir="[[Chemistry:Iridium|Ir]]";am.Pt="[[Chemistry:Platinum|Pt]]";am.Au="[[Chemistry:Gold|Au]]";am.Hg="[[Chemistry:Mercury (element)|Hg]]";am.Tl="[[Chemistry:Thallium|Tl]]";am.Pb="[[Chemistry:Lead|Pb]]";am.Bi="[[Chemistry:Bismuth|Bi]]";am.Po="[[Chemistry:Polonium|Po]]";am.At="[[Chemistry:Astatine|At]]";am.Rn="[[Chemistry:Radon|Rn]]";
am.Fr="[[Chemistry:Francium|Fr]]";am.Ra="[[Chemistry:Radium|Ra]]";am.Ac="[[Chemistry:Actinium|Ac]]";am.Th="[[Chemistry:Thorium|Th]]";am.Pa="[[Chemistry:Protactinium|Pa]]";am.U="[[Chemistry:Uranium|U]]";am.Np="[[Chemistry:Neptunium|Np]]";am.Pu="[[Chemistry:Plutonium|Pu]]";am.Am="[[Chemistry:Americium|Am]]";am.Cm="[[Chemistry:Curium|Cm]]";am.Bk="[[Chemistry:Berkelium|Bk]]";am.Cf="[[Chemistry:Californium|Cf]]";am.Es="[[Chemistry:Einsteinium|Es]]";am.Fm="[[Chemistry:Fermium|Fm]]";am.Md="[[Chemistry:Mendelevium|Md]]";am.No="[[Chemistry:Nobelium|No]]";am.Lr="[[Chemistry:Lawrencium|Lr]]";am.Rf="[[Chemistry:Rutherfordium|Rf]]";am.Db="[[Chemistry:Dubnium|Db]]";am.Sg="[[Chemistry:Seaborgium|Sg]]";am.Bh="[[Chemistry:Bohrium|Bh]]";am.Hs="[[Chemistry:Hassium|Hs]]";am.Mt="[[Chemistry:Meitnerium|Mt]]";am.Ds="[[Chemistry:Darmstadtium|Ds]]";am.Rg="[[Roentgenium|Rg]]";am.Cp="[[Chemistry:Copernicium|Cp]]";am.Nh="[[Chemistry:Nihonium|Nh]]";am.Fl="[[Chemistry:Flerovium|Fl]]";am.Mc="[[Chemistry:Moscovium|Mc]]";am.Lv="[[Chemistry:Livermorium|Lv]]";am.Ts="[[Chemistry:Tennessine|Ts]]";am.Og="[[Chemistry:Oganesson|Og]]";
local T_ELEM = 0 -- token types
local T_NUM = 1 -- number
local T_OPEN = 2 -- open '('
local T_CLOSE = 3 -- close ')'
local T_PM_CHARGE = 4 -- + or –
local T_WATER = 6 -- .xH2O x number
local T_CRYSTAL = 9 -- .x
local T_CHARGE = 8 -- charge (x+), (x-)
local T_SUF_CHARGE = 10 -- suffix and charge e.g. 2+ from H2+
local T_SUF_CHARGE2 = 12 -- suffix and (charge) e.g. 2(2+) from He2(2+)
local T_SPECIAL = 14 -- starting with \ e.g. \d for double bond (=)
local T_SPECIAL2 = 16 -- starting with \y{x} e.g. \i{12} for isotope with mass number 12
local T_ARROW_R = 17 -- match: ->
local T_ARROW_EQ = 18 -- match: <->
local T_UNDERSCORE = 19 -- _{ ... }
local T_CARET = 20 -- ^{ ... }
local T_NOCHANGE = 30 -- Anything else like ☃
function su(up, down) -- like template:su
if (down == "") then
return "<span style=\"display:inline-block; margin-bottom:-0.3em; vertical-align:0.8em; line-height:1.2em; font-size:70%; text-align:left;\">" .. up .. "<br /></span>";
else
return "<span style=\"display:inline-block; margin-bottom:-0.3em; vertical-align:-0.4em; line-height:1.2em; font-size:70%; text-align:left;\">" .. up .. "<br />" .. down .. "</span>";
end
end
function DotIt()
return ' <span style="font-weight:bold;">·</span> '
end
function item(f) -- (iterator) returns one token (type, value) at a time from the formula 'f'
local i = 1
local first = "true";
return function ()
local t, x = nil, nil
if (first == "true" and f:match('^[0-9]', i)) then
x = f:match('^[%d.]+', i); t = T_NOCHANGE; i = i + x:len(); -- matching coefficient (need a space first)
elseif i <= f:len() then
x = f:match('^%s+[%d.]+', i); t = T_NOCHANGE; -- matching coefficient (need a space first)
if not x then x = f:match('^%s[+]', i); t = T_NOCHANGE; end -- matching + (H2O + H2O)
if not x then x = f:match('^%&%#[%w%d]+%;', i); t = T_NOCHANGE; end -- &#...;
if not x then x = f:match('^%<%-%>', i); t = T_ARROW_EQ; end -- matching <->
if not x then x = f:match('^%-%>', i); t = T_ARROW_R; end -- matching ->
if not x then x = f:match('^%u%l*', i); t = T_ELEM; end -- matching symbols like Aaaaa
if not x then x = f:match('^%d+[+-]', i); t = T_SUF_CHARGE; end -- matching x+, x-
if not x then x = f:match('^%d+%(%d*[+-]%)', i); t = T_SUF_CHARGE2; end -- matching x(y+/-), x(+/-)
if not x then x = f:match('^%(%d*[+-]%)', i); t = T_CHARGE; end -- matching (x+) (xx+), (x-) (xx-)
if not x then x = f:match('^[%d.]+', i); t = T_NUM; end -- matching number
if not x then x = f:match('^[(|{|%[]', i); t = T_OPEN; end -- matching ({[
if not x then x = f:match('^[)|}|%]]', i); t = T_CLOSE; end -- matching )}]
if not x then x = f:match('^[+-]', i); t = T_PM_CHARGE; end -- matching + or -
if not x then x = f:match('^%*[%d.]*H2O', i); t = T_WATER; end -- Crystal water
if not x then x = f:match('^%*[%d.]*', i); t = T_CRYSTAL; end -- Crystal
if not x then x = f:match('^[\\].{%d+}', i); t = T_SPECIAL2; end -- \y{x}
if not x then x = f:match('^[\\].', i); t = T_SPECIAL; end -- \x
if not x then x = f:match('^_{[^}]*}', i); t = T_UNDERSCORE; end -- _{...}
if not x then x = f:match('^\^{[^}]*}', i); t = T_CARET; end -- ^{...}
if not x then x = f:match('^.', i); t = T_NOCHANGE; end --the rest - one by one
if x then i = i + x:len(); else i = i + 999; error("Invalid character in formula!!!!!!! : "..f) end
end
first = "false"
return t, x
end
end
function p._chem(args)
local f = args[1] or ''
f = string.gsub(f, "–", "-") -- replace – with - (hyphen not ndash)
f = string.gsub(f, "−", "-") -- replace – with - (hyphen not minus sign)
local sumO = 0
local formula = ''
local t, x
local link = args['link'] or ""
local auto = args['auto'] or ""
if not (link == '') then formula = formula .. "[[" .. link .. "|"; end -- wikilink start [[link|
for t, x in item(f) do
if t == T_ELEM then if (auto == '') then formula = formula .. x elseif am[x] then formula = formula .. am[x]; am[x] = x else formula = formula .. x end
elseif t == T_COEFFICIENT then formula = formula .. x
elseif t == T_NUM then formula = formula .. su("", x);
elseif t == T_OPEN then formula = formula .. x; sumO = sumO + 1; -- ( {
elseif t == T_CLOSE then formula = formula .. x; sumO = sumO -1; -- ) }
elseif t == T_PM_CHARGE then formula = formula .. su(string.gsub(x, "-", "−"), "");
elseif t == T_SUF_CHARGE then
formula = formula .. su(string.gsub(string.match(x, "[+-]"), "-", "−"), string.match(x, "%d+"), "");
elseif t == T_SUF_CHARGE2 then
formula = formula .. su(string.sub(string.gsub(string.match(x, "%(%d*[+-]"), "-", "−"), 2, -1), string.match(x, "%d+"))
elseif t == T_CHARGE then formula = formula .. "<sup>"; if string.match(x, "%d+") then formula = formula .. string.match(x, "%d+"); end formula = formula .. string.gsub(string.match(x, "[%+-]"), "-", "−") .. "</sup>"; -- can not concatenat a nil value from string.match(x, "%d+");
elseif t == T_CRYSTAL then formula = formula .. DotIt() .. string.gsub( x, "*", ' ', 1 );
elseif t == T_SPECIAL then
parameter = string.sub(x, 2, 2) -- x fra \x
if parameter == "s" then formula = formula .. "–" -- single bond
elseif parameter == "d" then formula = formula .. "=" -- double bond
elseif parameter == "t" then formula = formula .. "≡" -- tripple bond
elseif parameter == "q" then formula = formula .. "≣" -- Quadruple bond
elseif parameter == "h" then formula = formula .. "η" -- η, hapticity
elseif parameter == "*" then formula = formula .. "*" -- *, normal *
elseif parameter == "-" then formula = formula .. "-" -- -
elseif parameter == "\\" then formula = formula .. "\\" -- \
elseif parameter == "\'" then formula = formula .. "'" -- html-code for '
end
elseif t == T_SPECIAL2 then -- \y{x}
parameter = string.sub(x, 2, 2) -- y fra \y{x}
if parameter == "h" then --[[Hapticity]]
if (auto == '') then formula = formula .. "η<sup>" .. string.match(x, '%d+') .. "</sup>−"
else
formula = formula .. "[[Hapticity|η<sup>" .. string.match(x, '%d+') .. "</sup>]]−"
end
elseif parameter == "m" then formula = formula .. "μ<sup>" .. string.match(x, '%d+') .. "</sup>−" -- mu ([[bridging ligand]])
end
elseif t == T_WATER then
if string.match(x, "^%*[%d.]") then
formula = formula .. DotIt() .. string.match(x, "%f[%.%d]%d*%.?%d*%f[^%.%d%]]") .. "H<sub>2</sub>O";
else
formula = formula .. DotIt() .. "H<sub>2</sub>O";
end
elseif t == T_UNDERSCORE then formula = formula .. su("", string.sub(x,3,-2)) -- x contains _{string}
elseif t == T_CARET then formula = formula .. su(string.sub(x,3,-2), "") -- x contains ^{string}
elseif t == T_ARROW_R then formula = formula .. " → "
elseif t == T_ARROW_EQ then formula = formula .. " ⇌ "
elseif t == T_NOCHANGE then formula = formula .. x; -- The rest - everything which isn't captured by the regular expresions. E.g. wikilinks and pipes
else error('unreachable - ???') end -- in fact, unreachable
end
if not (link == nil or link == '') then formula = formula .. "]]"; end -- wikilink closing ]]
return '<span class="chemf nowrap">' .. formula .. '</span>'
end
function p.chem(frame)
local args = getArgs(frame)
return p._chem(args)
end
return p