elseif t == T_CHARGE then
formula = formula .. "<sup>"
if x:match("%d+") then formula = formula .. x:match("%d+"); end
formula = formula .. x:match("[%+-]"):gsub("-", "−") .. "</sup>";
-- Cannot concatenat a nil value from x:match("%d+");
elseif t == T_CRYSTAL then formula = formula .. DotIt .. string.gsub( x, "*", '', 1 );
elseif t == T_SPECIAL then
parameter = x:sub(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 .. "&#39;" -- html-code for '
end
elseif t == T_SPECIAL2 then -- \y{x}
parameter = x:sub(2, 2) -- y fra \y{x}
if parameter == "h" then --Hapticity
if (auto == '') then formula = formula .. "η<sup>" .. x:match('%d+') .. "</sup>-"
else
formula = formula .. "η<sup>" .. x:match('%d+') .. "</sup>-"
end
elseif parameter == "m" then formula = formula .. "μ<sub>" .. x:match('%d+') .. "</sub>-" -- mu (bridging ligand)
end
elseif t == T_WATER then
if x:match("^%*[%d.]") then
formula = formula .. DotIt .. x:match("%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("", x:sub(3,-2)) -- x contains _{string}
elseif t == T_CARET then formula = formula .. su(x:sub(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.  
else error('unreachable - ???') end -- in fact, unreachable
end  
if not (link == nil or link == '') then formula = formula .. "]]"; end -- wikilink closing ]]
formula = mw.getCurrentFrame:preprocess('<templatestyles src="Module:Chem2/styles.css"/>') ..
'<span>' .. formula .. '</span>'
if args[2] or args[3] or args[4] then
formula = formula .. require('Module:If preview')._warning{
'&#123;&#123;chem2&#125;&#125; was called with multiple positional arguments. It should have just one, e.g. &#123;&#123;chem2&#124;H2O&#125;&#125;.'
}
end
return formula
end  
function p.chem
local args = getArgs
return p._chem(args)
end  
-- PRIVATE function to generate documentation.
function p._autodoc
local TableTools = require('Module:TableTools') -- we don't want to load this on articles for no reason
local result = {
'===Elements and element-style symbols===\nThese may be automatically linked or used as if they were redirects.\n',
'{|\n! Symbol !! Link target\n'
}
for symbol, target in TableTools.sortedPairs(am) do
result[#result + 1] = ('|-\n| %s || %s\n'):format(symbol, target)
end
result[#result + 1] = '|}\n===Groups===\nThese must be linked manually; they work as if they were redirects.\n'
result[#result + 1] = '{|\n! Symbol !! Link target\n'
for symbol, target in TableTools.sortedPairs(groups) do
result[#result + 1] = ('|-\n| %s || %s\n'):format(symbol, target)
end
result[#result + 1] = '|}'
return table.concat(result)
end  
return p