Module:Chem2: திருத்தங்களுக்கு இடையிலான வேறுபாடு
Jump to navigation
Jump to search
உள்ளடக்கம் நீக்கப்பட்டது உள்ளடக்கம் சேர்க்கப்பட்டது
imported>Kanags "local getArgs = require('Module:Arguments').getArgs local p = {} -- module's table -- Elements with wiki links local am = { H = "Hydrogen", He = "Helium", Li = "Lithium", Be = "Beryllium", B = "Boron", C = "Carbon", N = "Nitrogen", O = "Oxygen", F = "Fluorine", Ne = "Neon", Na = "Sodium", Mg = "Magnesium", Al = "Aluminium", Si = "Silicon", P = "Pho..."-இப்பெயரில் புதிய பக்கம் உருவாக்கப்பட்டுள்ளது |
imported>Kanags சிNo edit summary |
||
| வரிசை 4: | வரிசை 4: | ||
-- Elements with wiki links |
-- Elements with wiki links |
||
local am = { |
local am = { |
||
H = " |
H = "ஐதரசன்", |
||
He = " |
He = "ஈலியம்", |
||
Li = " |
Li = "இலித்தியம்", |
||
Be = " |
Be = "பெரிலியம்", |
||
B = " |
B = "போரான்", |
||
C = " |
C = "கரிமம்", |
||
N = " |
N = "நைட்ரசன்", |
||
O = " |
O = "ஒட்சிசன்", |
||
F = " |
F = "புளோரின்", |
||
Ne = " |
Ne = "நியான்", |
||
Na = " |
Na = "சோடியம்", |
||
Mg = " |
Mg = "மக்னீசியம்", |
||
Al = " |
Al = "அலுமினியம்", |
||
Si = " |
Si = "சிலிக்கான்", |
||
P = " |
P = "பாசுபரசு", |
||
S = " |
S = "கந்தகம்", |
||
Cl = " |
Cl = "குளோரின்", |
||
Ar = " |
Ar = "ஆர்கான்", |
||
K = "பொட்டாசியம்", |
|||
K = "Potassium", |
|||
Ca = " |
Ca = "கல்சியம்", |
||
Sc = "இசுக்காண்டியம்", |
|||
Sc = "Scandium", |
|||
Ti = " |
Ti = "தைட்டானியம்", |
||
V = " |
V = "வனேடியம்", |
||
Cr = " |
Cr = "குரோமியம்", |
||
Mn = " |
Mn = "மாங்கனீசு", |
||
Fe = " |
Fe = "இரும்பு", |
||
Co = " |
Co = "கோபால்ட்டு", |
||
Ni = " |
Ni = "நிக்கல்", |
||
Cu = " |
Cu = "செப்பு", |
||
Zn = " |
Zn = "துத்தநாகம்", |
||
Ga = " |
Ga = "காலியம்", |
||
Ge = " |
Ge = "செருமேனியம்", |
||
As = " |
As = "ஆசனிக்கு", |
||
Se = " |
Se = "செலீனியம்", |
||
Br = " |
Br = "புரோமின்", |
||
Kr = " |
Kr = "கிரிப்டான்", |
||
Rb = " |
Rb = "உருபீடியம்", |
||
Sr = "இசுட்ரோன்சியம்", |
|||
Sr = "Strontium", |
|||
Y = " |
Y = "இற்றியம்", |
||
Zr = "சிர்க்கோனியம்", |
|||
Zr = "Zirconium", |
|||
Nb = " |
Nb = "நையோபியம்", |
||
Mo = " |
Mo = "மாலிப்டினம்", |
||
Tc = "தெக்கினீசியம்", |
|||
Tc = "Technetium", |
|||
Ru = "உருத்தேனியம்", |
|||
Ru = "Ruthenium", |
|||
Rh = " |
Rh = "உரோடியம்", |
||
Pd = " |
Pd = "பலேடியம்", |
||
Ag = "வெள்ளி (தனிமம்)", |
|||
Ag = "Silver", |
|||
Cd = " |
Cd = "காட்மியம்", |
||
In = " |
In = "இண்டியம்", |
||
Sn = " |
Sn = "வெள்ளீயம்", |
||
Sb = " |
Sb = "அந்திமனி", |
||
Te = " |
Te = "வெண்கந்தகம்", |
||
I = " |
I = "அயோடின்", |
||
Xe = " |
Xe = "செனான்", |
||
Cs = " |
Cs = "சீசியம்", |
||
Ba = " |
Ba = "பேரியம்", |
||
La = " |
La = "இலந்தனம்", |
||
Ce = " |
Ce = "சீரியம்", |
||
Pr = "பிரசியோடைமியம்", |
|||
Pr = "Praseodymium", |
|||
Nd = " |
Nd = "நியோடிமியம்", |
||
Pm = "புரோமித்தியம்", |
|||
Pm = "Promethium", |
|||
Sm = " |
Sm = "சமாரியம்", |
||
Eu = " |
Eu = "யூரோப்பியம்", |
||
Gd = " |
Gd = "கடோலினியம்", |
||
Tb = " |
Tb = "தெர்பியம்", |
||
Dy = "டிசிப்ரோசியம்", |
|||
Dy = "Dysprosium", |
|||
Ho = " |
Ho = "ஓல்மியம்", |
||
Er = " |
Er = "எர்பியம்", |
||
Tm = " |
Tm = "தூலியம்", |
||
Yb = "இட்டெர்பியம்", |
|||
Yb = "Ytterbium", |
|||
Lu = "இலூட்டீசியம்", |
|||
Lu = "Lutetium", |
|||
Hf = " |
Hf = "ஆஃபினியம்", |
||
Ta = "தாண்டலம்", |
|||
W = " |
W = "தங்குதன்", |
||
Re = " |
Re = "இரேனியம்", |
||
Os = " |
Os = "ஓசுமியம்", |
||
Ir = " |
Ir = "இரிடியம்", |
||
Pt = " |
Pt = "பிளாட்டினம்", |
||
Au = " |
Au = "தங்கம்", |
||
Hg = " |
Hg = "பாதரசம்", |
||
Tl = " |
Tl = "தாலியம்", |
||
Pb = " |
Pb = "ஈயம்", |
||
Bi = " |
Bi = "பிசுமத்", |
||
Po = " |
Po = "பொலோனியம்", |
||
At = "அசுட்டட்டைன்", |
|||
At = "Astatine", |
|||
Rn = " |
Rn = "இரேடான்", |
||
Fr = "பிரான்சியம் (தனிமம்)", |
|||
Fr = "Francium", |
|||
Ra = " |
Ra = "ரேடியம்", |
||
Ac = " |
Ac = "ஆக்டினியம்", |
||
Th = " |
Th = "தோரியம்", |
||
Pa = "புரோடாக்டினியம்", |
|||
Pa = "Protactinium", |
|||
U = " |
U = "யுரேனியம்", |
||
Np = "நெப்டியூனியம்", |
|||
Np = "Neptunium", |
|||
Pu = "புளுட்டோனியம்", |
|||
Pu = "Plutonium", |
|||
Am = " |
Am = "அமெரிசியம்", |
||
Cm = " |
Cm = "கியூரியம்", |
||
Bk = "பெர்க்கிலியம்", |
|||
Bk = "Berkelium", |
|||
Cf = "கலிபோர்னியம்", |
|||
Cf = "Californium", |
|||
Es = "ஐன்சுடைனியம்", |
|||
Es = "Einsteinium", |
|||
Fm = " |
Fm = "பெர்மியம்", |
||
Md = "மெண்டலீவியம்", |
|||
Md = "Mendelevium", |
|||
No = " |
No = "நொபிலியம்", |
||
Lr = "இலாரென்சியம்", |
|||
Lr = "Lawrencium", |
|||
Rf = "இரதர்ஃபோர்டியம்", |
|||
Rf = "Rutherfordium", |
|||
Db = " |
Db = "தூப்னியம்", |
||
Sg = " |
Sg = "சீபோர்கியம்", |
||
Bh = " |
Bh = "போரியம்", |
||
Hs = " |
Hs = "ஆசியம்", |
||
Mt = "மெய்ட்னீரியம்", |
|||
Mt = "Meitnerium", |
|||
Ds = "டார்ம்சிட்டாட்டியம்", |
|||
Ds = "Darmstadtium", |
|||
Rg = "இரோயன்ட்கெனியம்", |
|||
Rg = "Roentgenium", |
|||
Cn = "கோப்பர்நீசியம்", |
|||
Cp = "Copernicium", |
|||
Nh = " |
Nh = "நிகோனியம்", |
||
Fl = " |
Fl = "பிளெரோவியம்", |
||
Mc = "மாசுக்கோவியம்", |
|||
Mc = "Moscovium", |
|||
Lv = "லிவர்மோரியம்", |
|||
Lv = "Livermorium", |
|||
Ts = " |
Ts = "தென்னிசீன்", |
||
Og = " |
Og = "ஒகனிசோன்", |
||
-- Groups etc with element-like names |
-- Groups etc with element-like names |
||
Bn = 'Benzyl group', |
Bn = 'Benzyl group', |
||
Bu = 'Butyl group', |
|||
Bz = 'Benzoyl group', |
Bz = 'Benzoyl group', |
||
Cp = 'Cyclopentadienyl', |
|||
D = 'Deuterium', |
D = 'Deuterium', |
||
Et = 'Ethyl group', |
Et = 'Ethyl group', |
||
Ln = 'Lanthanide', |
Ln = 'Lanthanide', |
||
Me = 'Methyl group', |
|||
Nu = 'Nucleophile', |
Nu = 'Nucleophile', |
||
Ph = 'Phenyl group', |
Ph = 'Phenyl group', |
||
Pn = 'Pentyl group', |
|||
R = 'Substituent', |
R = 'Substituent', |
||
T = 'Tritium', |
T = 'Tritium', |
||
| வரிசை 237: | வரிசை 241: | ||
if not x then x = f:match('^[\\].', i); t = T_SPECIAL; end -- \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_UNDERSCORE; end -- _{...} |
||
if not x then x = f:match('^ |
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 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 |
if x then i = i + x:len(); else i = i + 999; error("Invalid character in formula! : "..f) end |
||
| வரிசை 246: | வரிசை 250: | ||
function p._chem(args) |
function p._chem(args) |
||
local f = args[1] or '' |
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 ndash) |
||
f = string.gsub(f, "−", "-") -- replace – with - (hyphen not minus sign) |
f = string.gsub(f, "−", "-") -- replace – with - (hyphen not minus sign) |
||
| வரிசை 269: | வரிசை 275: | ||
end |
end |
||
elseif t == T_COEFFICIENT then formula = formula .. x |
elseif t == T_COEFFICIENT then formula = formula .. x |
||
elseif t == T_NUM then formula = formula .. su("", x); |
elseif t == T_NUM then formula = formula .. su("", x); |
||
elseif t == T_LINKOPEN then formula = formula .. x; -- [[Link| |
elseif t == T_LINKOPEN then formula = formula .. x; -- [[Link| |
||
elseif t == T_OPEN then formula = formula .. x; -- ([{ |
elseif t == T_OPEN then formula = formula .. x; -- ([{ |
||
elseif t == T_CLOSE then formula = formula .. x; -- )]} |
elseif t == T_CLOSE then formula = formula .. x; -- )]} |
||
elseif t == T_PM_CHARGE |
elseif t == T_PM_CHARGE then formula = formula .. su(x:gsub("-", "−"), ""); |
||
elseif t == T_SUF_CHARGE then |
elseif t == T_SUF_CHARGE then |
||
formula = formula .. su(x:match("[+-]"):gsub("-", "−"), x:match("%d+"), ""); |
formula = formula .. su(x:match("[+-]"):gsub("-", "−"), x:match("%d+"), ""); |
||
| வரிசை 310: | வரிசை 316: | ||
else |
else |
||
formula = formula .. DotIt() .. "H<sub>2</sub>O"; |
formula = formula .. DotIt() .. "H<sub>2</sub>O"; |
||
end |
end |
||
elseif t == T_UNDERSCORE then formula = formula .. su("", x:sub(3,-2)) -- x contains _{string} |
elseif t == T_UNDERSCORE then formula = formula .. su("", x:gsub("-", "−"):sub(3, -2)) -- x contains _{string} |
||
elseif t == T_CARET then formula = formula .. su(x:sub(3,-2), "") -- x contains ^{string} |
elseif t == T_CARET then formula = formula .. su(x:gsub("-", "−"):sub(3, -2), "") -- x contains ^{string} |
||
elseif t == T_ARROW_R then formula = formula .. " → " |
elseif t == T_ARROW_R then formula = formula .. " → " |
||
elseif t == T_ARROW_EQ 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. |
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 |
else error('unreachable - ???') end -- in fact, unreachable |
||
end |
end |
||
11:04, 23 சனவரி 2026 இல் கடைசித் திருத்தம்
Documentation for this module may be created at Module:Chem2/doc
local getArgs = require('Module:Arguments').getArgs
local p = {} -- module's table
-- Elements with wiki links
local am = {
H = "ஐதரசன்",
He = "ஈலியம்",
Li = "இலித்தியம்",
Be = "பெரிலியம்",
B = "போரான்",
C = "கரிமம்",
N = "நைட்ரசன்",
O = "ஒட்சிசன்",
F = "புளோரின்",
Ne = "நியான்",
Na = "சோடியம்",
Mg = "மக்னீசியம்",
Al = "அலுமினியம்",
Si = "சிலிக்கான்",
P = "பாசுபரசு",
S = "கந்தகம்",
Cl = "குளோரின்",
Ar = "ஆர்கான்",
K = "பொட்டாசியம்",
Ca = "கல்சியம்",
Sc = "இசுக்காண்டியம்",
Ti = "தைட்டானியம்",
V = "வனேடியம்",
Cr = "குரோமியம்",
Mn = "மாங்கனீசு",
Fe = "இரும்பு",
Co = "கோபால்ட்டு",
Ni = "நிக்கல்",
Cu = "செப்பு",
Zn = "துத்தநாகம்",
Ga = "காலியம்",
Ge = "செருமேனியம்",
As = "ஆசனிக்கு",
Se = "செலீனியம்",
Br = "புரோமின்",
Kr = "கிரிப்டான்",
Rb = "உருபீடியம்",
Sr = "இசுட்ரோன்சியம்",
Y = "இற்றியம்",
Zr = "சிர்க்கோனியம்",
Nb = "நையோபியம்",
Mo = "மாலிப்டினம்",
Tc = "தெக்கினீசியம்",
Ru = "உருத்தேனியம்",
Rh = "உரோடியம்",
Pd = "பலேடியம்",
Ag = "வெள்ளி (தனிமம்)",
Cd = "காட்மியம்",
In = "இண்டியம்",
Sn = "வெள்ளீயம்",
Sb = "அந்திமனி",
Te = "வெண்கந்தகம்",
I = "அயோடின்",
Xe = "செனான்",
Cs = "சீசியம்",
Ba = "பேரியம்",
La = "இலந்தனம்",
Ce = "சீரியம்",
Pr = "பிரசியோடைமியம்",
Nd = "நியோடிமியம்",
Pm = "புரோமித்தியம்",
Sm = "சமாரியம்",
Eu = "யூரோப்பியம்",
Gd = "கடோலினியம்",
Tb = "தெர்பியம்",
Dy = "டிசிப்ரோசியம்",
Ho = "ஓல்மியம்",
Er = "எர்பியம்",
Tm = "தூலியம்",
Yb = "இட்டெர்பியம்",
Lu = "இலூட்டீசியம்",
Hf = "ஆஃபினியம்",
Ta = "தாண்டலம்",
W = "தங்குதன்",
Re = "இரேனியம்",
Os = "ஓசுமியம்",
Ir = "இரிடியம்",
Pt = "பிளாட்டினம்",
Au = "தங்கம்",
Hg = "பாதரசம்",
Tl = "தாலியம்",
Pb = "ஈயம்",
Bi = "பிசுமத்",
Po = "பொலோனியம்",
At = "அசுட்டட்டைன்",
Rn = "இரேடான்",
Fr = "பிரான்சியம் (தனிமம்)",
Ra = "ரேடியம்",
Ac = "ஆக்டினியம்",
Th = "தோரியம்",
Pa = "புரோடாக்டினியம்",
U = "யுரேனியம்",
Np = "நெப்டியூனியம்",
Pu = "புளுட்டோனியம்",
Am = "அமெரிசியம்",
Cm = "கியூரியம்",
Bk = "பெர்க்கிலியம்",
Cf = "கலிபோர்னியம்",
Es = "ஐன்சுடைனியம்",
Fm = "பெர்மியம்",
Md = "மெண்டலீவியம்",
No = "நொபிலியம்",
Lr = "இலாரென்சியம்",
Rf = "இரதர்ஃபோர்டியம்",
Db = "தூப்னியம்",
Sg = "சீபோர்கியம்",
Bh = "போரியம்",
Hs = "ஆசியம்",
Mt = "மெய்ட்னீரியம்",
Ds = "டார்ம்சிட்டாட்டியம்",
Rg = "இரோயன்ட்கெனியம்",
Cn = "கோப்பர்நீசியம்",
Nh = "நிகோனியம்",
Fl = "பிளெரோவியம்",
Mc = "மாசுக்கோவியம்",
Lv = "லிவர்மோரியம்",
Ts = "தென்னிசீன்",
Og = "ஒகனிசோன்",
-- Groups etc with element-like names
Bn = 'Benzyl group',
Bu = 'Butyl group',
Bz = 'Benzoyl group',
Cp = 'Cyclopentadienyl',
D = 'Deuterium',
Et = 'Ethyl group',
Ln = 'Lanthanide',
Me = 'Methyl group',
Nu = 'Nucleophile',
Ph = 'Phenyl group',
Pn = 'Pentyl group',
R = 'Substituent',
T = 'Tritium',
Tf = 'Trifluoromethylsulfonyl group',
X = 'Halogen',
}
-- Groups which are redirected from their normal target if wikilinked; never
-- autolinked.
local groups = {
CH3 = 'Methyl group',
CO3 = 'Carbonate',
COOH = 'Carboxyl group',
ClO = 'Hypochlorite',
ClO2 = 'Chlorite',
ClO3 = 'Chlorate',
ClO4 = 'Perchlorate',
H2O = 'Water of crystallization',
H3O = 'Hydronium',
NH2 = 'Amine group',
NH4 = 'Ammonium',
NO3 = 'Nitrate',
PO3 = 'Phosphite',
PO4 = 'Phosphate',
SH = 'Thiol group',
SO3 = 'Sulfite',
SO4 = 'Sulfate',
SeH = 'Selenol group'
}
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_LINKOPEN = 21 -- Opening of link, always like "[[target|" even if the source wasn't
local T_NOCHANGE = 30 -- Anything else like ☃
function su(up, down)
if up == "" then
return ('<sub class="template-chem2-sub">%s</sub>'):format(down)
end
if down == "" then
return ('<sup class="template-chem2-sup">%s</sup>'):format(up)
end
return ('<span class="template-chem2-su"><span>%s</span><span>%s</span></span>'):format(up, down)
end
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 == 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)
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)) then
i = i + 1; return T_OPEN, '[' end -- escape [[[X or [X (relevant to auto-linking)
if not x and f:sub(i, i + 1) == '[[' then
x = f:match('^%[%[([^]|]*)', i) -- link target
local len = x:len() + 3
x = '[[' .. (groups[x] or am[x] or x) .. '|' -- override link target for common groups
if f:sub(len + i, len + i) == ']' then
-- We're going to read the link twice, once as target and once as
-- chemical markup, e.g. [[CH3]] => "[[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_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
return t, x
end
end
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 == '') then formula = formula .. "[[" .. link .. "|"; end -- wikilink start [[link|
for t, x in item(f) do
if _debug then
formula = ("%s\n* %d %s"):format(formula, t, x)
elseif t == T_ELEM then
if (auto == '') or (not am[x]) or seen[x] then formula = formula .. x
else formula = ("%s[[%s|%s]]"):format(formula, am[x], x); seen[x] = true
end
elseif t == T_COEFFICIENT then formula = formula .. x
elseif t == T_NUM then formula = formula .. su("", x);
elseif t == T_LINKOPEN then formula = formula .. x; -- [[Link|
elseif t == T_OPEN then formula = formula .. x; -- ([{
elseif t == T_CLOSE then formula = formula .. x; -- )]}
elseif t == T_PM_CHARGE then formula = formula .. su(x:gsub("-", "−"), "");
elseif t == T_SUF_CHARGE then
formula = formula .. su(x:match("[+-]"):gsub("-", "−"), x:match("%d+"), "");
elseif t == T_SUF_CHARGE2 then
formula = formula .. su(x:match("%(%d*[+-]"):gsub("-", "−"):sub(2, -1), x:match("%d+"))
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 .. "'" -- 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 .. "[[Hapticity|η<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:gsub("-", "−"):sub(3, -2)) -- x contains _{string}
elseif t == T_CARET then formula = formula .. su(x:gsub("-", "−"):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 class="chemf nowrap">' .. formula .. '</span>'
if args[2] or args[3] or args[4] then
formula = formula .. require('Module:If preview')._warning{
'{{chem2}} was called with multiple positional arguments. It should have just one, e.g. {{chem2|H2O}}.'
}
end
return formula
end
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 = {
'===Elements and element-style symbols===\nThese may be automatically linked or used as if they were redirects.\n',
'{| class="wikitable"\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] = '{| class="wikitable"\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