Documentation icon မော်ဂျူးမှတ်တမ်းလက်စွဲ[ဖန်တီး]
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="[[လီသီယမ်|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="[[Yttrium|Y]]";am.Zr="[[ဇာကိုနီယမ်|Zr]]";am.Nb="[[နိုင်အိုဘီယမ်|Nb]]";am.Mo="[[မိုလိပ်ဒီနမ်|Mo]]";am.Tc="[[Technetium|Tc]]";am.Ru="[[Ruthenium|Ru]]";am.Rh="[[Rhodium|Rh]]";am.Pd="[[Palladium|Pd]]";am.Ag="[[ငွေ (သတ္တု)|Ag]]";am.Cd="[[Cadmium|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="[[Lutetium|Lu]]";am.Hf="[[Hafnium|Hf]]";am.Ta="[[Tantalum|Ta]]";am.W="[[တန်စတင်|W]]";am.Re="[[Rhenium|Re]]";am.Os="[[Osmium|Os]]";am.Ir="[[Iridium|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="[[Radon|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="[[Rutherfordium|Rf]]";am.Db="[[Dubnium|Db]]";am.Sg="[[Seaborgium|Sg]]";am.Bh="[[Bohrium|Bh]]";am.Hs="[[Hassium|Hs]]";am.Mt="[[Meitnerium|Mt]]";am.Ds="[[Darmstadtium|Ds]]";am.Rg="[[Roentgenium|Rg]]";am.Cp="[[Copernicium|Cp]]";am.Uut="[[Ununtrium|Uut]]";am.Uuq="[[Ununquadium|Uuq]]";am.Uup="[[Ununpentium|Uup]]";am.Uuh="[[Ununhexium|Uuh]]";am.Uus="[[Ununseptium|Uus]]";am.Uuo="[[Ununoctium|Uuo]]";

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(up, down) -- like template:su
  if (down == "") then 
    return "<span style=\"display:inline-block; margin-bottom:-0.3em; vertical-align:0.8em; line-height:1.2em; font-size:70%; text-align:left;\">" .. up .. "<br /></span>";
  else
    return "<span style=\"display:inline-block; margin-bottom:-0.3em; vertical-align:-0.4em; line-height:1.2em; font-size:70%; text-align:left;\">" .. up .. "<br />" .. down .. "</span>";
  end
end

function DotIt()
  return '&nbsp;<span style="font-weight:bold;">&middot;</span>&#32;'
end


function item(f) -- (iterator) returns one token (type, value) 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('^[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 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
        first = "false"
	return t, x
	end
   end

function p._chem(args)
   
local f = args[1] or ''

   f = string.gsub(f, "–", "-")  -- replace – with -
   f = string.gsub(f, "−", "-")  -- replace – with -

   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   -- wiki link 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("", x);
      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(x, "-", "–"), "");
      elseif t == T_SUF_CHARGE then 
           formula = formula .. su(string.gsub(string.match(x, "[+-]"), "-", "–"), string.match(x, "%d+"), "");
      elseif t == T_SUF_CHARGE2 then 
          formula = formula .. su(string.sub(string.gsub(string.match(x, "%(%d*[+-]"), "-", "–"), 2, -1), string.match(x, "%d+"))

      elseif t == T_CHARGE then formula = formula .. "<sup>"; if string.match(x, "%d+") then formula = formula .. string.match(x, "%d+"); end formula = formula .. string.gsub(string.match(x, "[%+-]"), "-", "–") .. "</sup>";  -- can not concatenat a nil value from string.match(x, "%d+");

      elseif t == T_CRYSTAL then formula = formula .. DotIt() .. string.gsub( x, "*", ' ', 1 );

      elseif t == T_SPECIAL then
          parameter = string.sub(x, 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 .. "-"   -- -
            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(x, 2, 2) -- y fra \y{x} 
          if parameter  == "h" then --[[Hapticity]]
             if (auto == '') then formula = formula .. "η<sup>" .. string.match(x, '%d+') .. "</sup>–" 
               else
             formula = formula .. "[[Hapticity|η<sup>" .. string.match(x, '%d+') .. "</sup>]]–" 
             end
--          elseif parameter == "i" then formula = formula .. su(string.match(x, '%d+'), "") -- [[isotope]]
          elseif parameter == "m" then formula = formula .. "μ<sup>" .. string.match(x, '%d+') .. "</sup>–"   -- mu ([[bridging ligand]])
          end
      elseif t == T_WATER then 
        if string.match(x, "^%*[%d.]") then 
            formula = formula .. DotIt() .. string.match(x, "%f[%.%d]%d*%.?%d*%f[^%.%d%]]") .. "H<sub>2</sub>O";
        else
          formula = formula .. DotIt() .. "H<sub>2</sub>O";
        end  
 
-- not (auto == nil or auto == '') then formula = formula .. DotIt .. 
-- "[[Water of crystallization|H<sub>2</sub>O]]";
--            else
--              formula = formula .. 
-- DotIt .. "H<sub>2</sub>O";
---- xxx brug af sub til tal
--            end

      elseif t == T_UNDERSCORE  then formula = formula .. su("", string.sub(x,3,-2)) -- x contains _{string}
      elseif t == T_CARET then formula = formula .. su(string.sub(x,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;  -- resten, som ikke blev fanget af diverse regular expresions
     
      else error('unreachable - ???') end -- in fact, unreachable

end

   if      sumO > 0 then formula = formula .. "'<span style=\"display:none;font-size:100%\" class=\"error citation-comment\">   Too many (</span>'\";"
    elseif sumO < 0 then formula = formula .. "'<span style=\"display:none;font-size:100%\" class=\"error citation-comment\">   Too many )</span>'\";"
   end

   if not (link == nil or link == '') then formula = formula .. "]]"; end   -- wikilink closing ]]

   return formula
end

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

return p