새 문서: local getArgs = require('Module:Arguments').getArgs local p = {} -- module's table local am = {} -- Elements with wiki links am.H="H";am.He="He"; am.Li="H";am.He="He";
am.Li="Li";am.Be="Be";am.B="B";am.C="C";am.N="N";am.O="O";am.F="F";am.Ne="Ne";
am.Na="Na";am.Mg="Mg";am.Al="Al";am.Si="Si";am.P="P";am.S="S";am.Cl="Cl";am.Ar="Ar";
am.K="K";am.Ca="Ca";am.Sc="Sc";am.Ti="Ti";am.V="V";am.Cr="Cr";am.Mn="Mn";am.Fe="Fe";am.Co="Co";am.Ni="Ni";am.Cu="Cu";am.Zn="Zn";am.Ga="Ga";am.Ge="Ge";am.As="As";am.Se="Se";am.Br="Br";am.Kr="Kr";am.Rb="Rb";
am.Sr="Sr";am.Y="Y";am.Zr="Zr";am.Nb="Nb";am.Mo="Mo";am.Tc="Tc";am.Ru="Ru";am.Rh="Rh";am.Pd="Pd";am.Ag="Ag";am.Cd="Cd";am.In="In";am.Sn="Sn";am.Sb="Sb";am.Te="Te";am.I="I";am.Xe="Xe";
am.Cs="Cs";am.Ba="Ba";am.La="La";am.Ce="Ce";am.Pr="Pr";am.Nd="Nd";am.Pm="Pm";am.Sm="Sm";am.Eu="Eu";am.Gd="Gd";am.Tb="Tb";am.Dy="Dy";am.Ho="Ho";am.Er="Er";am.Tm="Tm";am.Yb="Yb";am.Lu="Lu";am.Hf="Hf";am.Ta="Ta";am.W="W";am.Re="Re";am.Os="Os";am.Ir="Ir";am.Pt="Pt";am.Au="Au";am.Hg="Hg";am.Tl="Tl";am.Pb="Pb";am.Bi="Bi";am.Po="Po";am.At="At";am.Rn="Rn";
am.Fr="Fr";am.Ra="Ra";am.Ac="Ac";am.Th="Th";am.Pa="Pa";am.U="U";am.Np="Np";am.Pu="Pu";am.Am="Am";am.Cm="Cm";am.Bk="Bk";am.Cf="Cf";am.Es="Es";am.Fm="Fm";am.Md="Md";am.No="No";am.Lr="Lr";am.Rf="Rf";am.Db="Db";am.Sg="Sg";am.Bh="Bh";am.Hs="Hs";am.Mt="Mt";am.Ds="Ds";am.Rg="Rg";am.Cp="Cp";am.Nh="Nh";am.Fl="Fl";am.Mc="Mc";am.Lv="Lv";am.Ts="Ts";am.Og="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 -- like template:su
 if (down == "") then
 return "";
 else
 return "";
 end
end

function DotIt
 return '&middot;'
end

function item(f) -- (iterator) returns one token  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) then
 x = f:match; t = T_NOCHANGE; i = i + x:len; -- matching coefficient (need a space first)

 elseif i <= f:len then
 x = f:match; t = T_NOCHANGE; -- matching coefficient (need a space first)
		if not x then x = f:match; t = T_NOCHANGE; end -- matching + (H2O + H2O)
		if not x then x = f:match; t = T_NOCHANGE; end -- &#...;
		if not x then x = f:match('^%
		if not x then x = f:match; t = T_ARROW_R; end -- matching ->
		if not x then x = f:match; t = T_ELEM; end -- matching symbols like Aaaaa
		if not x then x = f:match; 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; t = T_NUM; end -- matching number
		if not x then x = f:match('^[; 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; t = T_PM_CHARGE; end -- matching + or -
		if not x then x = f:match; t = T_WATER; end -- Crystal water
		if not x then x = f:match; t = T_CRYSTAL; end -- Crystal
		if not x then x = f:match; t = T_SPECIAL2; end -- \y{x}
		if not x then x = f:match; t = T_SPECIAL; end -- \x
		if not x then x = f:match; t = T_UNDERSCORE; end -- _{...}
		if not x then x = f:match; t = T_CARET; end -- ^{...}
		if not x then x = f:match; 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 -- replace – with - (hyphen not ndash)
 f = string.gsub -- 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;
 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, "");
 elseif t == T_SUF_CHARGE then
 formula = formula .. su(string.gsub(string.match, "-", "−"), string.match, "");
 elseif t == T_SUF_CHARGE2 then
 formula = formula .. su(string.sub(string.gsub(string.match(x, "%(%d*[+-]"), "-", "−"), 2, -1), string.match)
 elseif t == T_CHARGE then formula = formula .. ""; -- can not concatenat a nil value from string.match;
 elseif t == T_CRYSTAL then formula = formula .. DotIt .. string.gsub;
 elseif t == T_SPECIAL then
 parameter = string.sub -- 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 .. "&#39;" -- html-code for '
 end
 elseif t == T_SPECIAL2 then -- \y{x}
 parameter = string.sub -- y fra \y{x}
 if parameter == "h" then --합토수
 if (auto == '') then formula = formula .. "η-"
 else
 formula = formula .. "η-"
 end
 elseif parameter == "m" then formula = formula .. "μ-" -- mu (:en:bridging ligand)
 end
 elseif t == T_WATER then
 if string.match then
 formula = formula .. DotIt .. string.match .. "HO";
 else
 formula = formula .. DotIt .. "HO";
 end
 elseif t == T_UNDERSCORE then formula = formula .. su("", string.sub) -- x contains _{string}
 elseif t == T_CARET then formula = formula .. su(string.sub, "") -- 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 ''
end

function p.chem(frame)
	local args = getArgs(frame)
	return p._chem(args)
end