Anonymous user
Module:Chem2: Difference between revisions
Per edit request
Richardpruen (talk | contribs) m (1 revision imported: Templates and CSS files) |
imported>Pppery (Per edit request) |
||
Line 237: | Line 237: | ||
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 | ||
Line 246: | Line 246: | ||
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) | ||
Line 269: | Line 271: | ||
end | end | ||
elseif t == T_COEFFICIENT then formula = formula .. x | elseif t == T_COEFFICIENT then formula = formula .. x | ||
elseif t == T_NUM | elseif t == T_NUM then formula = formula .. su("", x); | ||
elseif t == T_LINKOPEN | elseif t == T_LINKOPEN then formula = formula .. x; -- [[Link| | ||
elseif t == T_OPEN | 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+"), ""); | ||
Line 310: | Line 312: | ||
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 | 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 |