local getArgs = require('Module:Arguments').getArgslocal p = -- module's table
-- Elements with wiki linkslocal am =
-- Groups which are redirected from their normal target if wikilinked; never-- autolinked.local groups =
local T_ELEM = 0 -- token typeslocal T_NUM = 1 -- numberlocal T_OPEN = 2 -- open '('local T_CLOSE = 3 -- close ')'local T_PM_CHARGE = 4 -- + or −local T_WATER = 6 -- .xH2O x numberlocal T_CRYSTAL = 9 -- .xlocal 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 e.g. \i for isotope with mass number 12local T_ARROW_R = 17 -- match: ->local T_ARROW_EQ = 18 -- match: <->local T_UNDERSCORE = 19 -- _local T_CARET = 20 -- ^local T_LINKOPEN = 21 -- Opening of link, always like "" even if the source wasn'tlocal T_NOCHANGE = 30 -- Anything else like ☃ function su(up, down) if up "" then return ('%s'):format(up) end return (' function DotIt return '·'end function item(f) -- (iterator) returns one token (type, value) at a time from the formula 'f' local i = 1 return function local t, x = nil, nil if (i 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 and (f:match('^%[%[%[[^[]', i) or f:match('^%]*)', i) -- link target local len = x:len + 3 x = '' -- override link target for common groups if f:sub(len + i, len + i) function p._chem(args) local f = args[1] or f = mw.text.decode(f, true) -- handle entity input (like -): decode right away f = string.gsub(f, "–", "-") -- replace – with - (hyphen not ndash) f = string.gsub(f, "−", "-") -- replace – with - (hyphen not minus sign) local formula = local t, x local link = args['link'] or "" local auto = args['auto'] or "" local seen = local _debug = false if not (link ) or (not am[x]) or seen[x] then formula = formula .. x else formula = ("%s%s"):format(formula, am[x], x); seen[x] = true end elseif t T_NUM then formula = formula .. su("", x); elseif t T_PM_CHARGE then formula = formula .. su(x:gsub("-", "−"), ""); elseif t T_SUF_CHARGE2 then formula = formula .. su(x:match("%(%d*[+-]"):gsub("-", "−"):sub(2, -1), x:match("%d+")) elseif t T_CRYSTAL then formula = formula .. DotIt .. string.gsub(x, "*", , 1); elseif t "s" then formula = formula .. "−" -- single bond elseif parameter "t" then formula = formula .. "≡" -- tripple bond elseif parameter "h" then formula = formula .. "η" -- η, hapticity elseif parameter "-" then formula = formula .. "-" -- - elseif parameter "\'" then formula = formula .. "'" -- html-code for ' end elseif t "h" then --Hapticity if (auto "m" then formula = formula .. "μ" .. x:match('%d+') .. "-" -- mu (bridging ligand) end elseif t T_UNDERSCORE then formula = formula .. su("", x:gsub("-", "−"):sub(3, -2)) -- x contains _ elseif t T_ARROW_R 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 ) then formula = formula .. "]"" then return ('%s'):format(down) end if down
1) 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)
'
' then -- We're going to read the link twice, once as target and once as -- chemical markup, e.g. CH3 => "", "CH3" i = i + 2 else i = i + len end return T_LINKOPEN, x end if not x then x = f:match('^[(|{|%[]', i); t = T_OPEN; 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('^[\\].', i); t = T_SPECIAL2; end -- \y 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 return t, x endend) then formula = formula .. ""; end -- wikilink start for t, x in item(f) do if _debug then formula = ("%s\n* %d %s"):format(formula, t, x) elseif t
formula = mw.getCurrentFrame:preprocess('T_ELEM then if (auto
T_COEFFICIENT then formula = formula .. x elseif t
T_LINKOPEN then formula = formula .. x; -- elseif t
T_OPEN then formula = formula .. x; -- ([{
elseif t == T_CLOSE then formula = formula .. x; --)]} elseif t
T_SUF_CHARGE then formula = formula .. su(x:match("[+-]"):gsub("-", "−"), x:match("%d+"), ""); elseif t
T_CHARGE then formula = formula .. "" if x:match("%d+") then formula = formula .. x:match("%d+"); end formula = formula .. x:match("[%+-]"):gsub("-", "−") .. ""; -- Cannot concatenat a nil value from x:match("%d+"); elseif t
T_SPECIAL then parameter = x:sub(2, 2) -- x fra \x if parameter
"d" then formula = formula .. "=" -- double bond elseif parameter
"q" then formula = formula .. "≣" -- Quadruple bond elseif parameter
"*" then formula = formula .. "*" -- *, normal * elseif parameter
"\\" then formula = formula .. "\\" -- \ elseif parameter
T_SPECIAL2 then -- \y parameter = x:sub(2, 2) -- y fra \y if parameter
) then formula = formula .. "η" .. x:match('%d+') .. "-" else formula = formula .. "η" .. x:match('%d+') .. "-" end elseif parameter
T_WATER then if x:match("^%*[%d.]") then formula = formula .. DotIt .. x:match("%f[%.%d]%d*%.?%d*%f[^%.%d%]
") .. "H2O"; else formula = formula .. DotIt .. "H2O"; end elseif tT_CARET then formula = formula .. su(x:gsub("-", "−"):sub(3, -2), "") -- x contains ^ elseif t
T_ARROW_EQ then formula = formula .. " ⇌ " elseif t
nil or link
function p.chem(frame) local args = getArgs(frame) return p._chem(args)end
-- PRIVATE function to generate documentation.function p._autodoc(frame) local TableTools = require('Module:TableTools') -- we don't want to load this on articles for no reason local result = \n
\nThese must be linked manually; they work as if they were redirects.\n' result[#result + 1] = '
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 p