Dokumentacja modułu [zobacz] [edytuj] [historia] [odśwież]
local getArgs = require('Moduł:Arguments').getArgs
local p = {} -- module's table

local am = {}  -- Elements with wiki links
am.H="[[Wodór|H]]";am.He="[[Hel (pierwiastek)|He]]";
am.Li="[[Lit (pierwiastek)|Li]]";am.Be="[[Beryl (pierwiastek)|Be]]";am.B="[[Bor|B]]";am.C="[[Węgiel (pierwiastek)|C]]";am.N="[[Azot|N]]";am.O="[[Tlen|O]]";am.F="[[Fluor|F]]";am.Ne="[[Neon (pierwiastek)|Ne]]";
am.Na="[[Sód|Na]]";am.Mg="[[Magnez|Mg]]";am.Al="[[Glin|Al]]";am.Si="[[Krzem|Si]]";am.P="[[Fosfor|P]]";am.S="[[Siarka|S]]";am.Cl="[[Chlor|Cl]]";am.Ar="[[Argon|Ar]]";
am.K="[[Potas|K]]";am.Ca="[[Wapń|Ca]]";am.Sc="[[Skand|Sc]]";am.Ti="[[Tytan (pierwiastek)|Ti]]";am.V="[[Wanad|V]]";am.Cr="[[Chrom|Cr]]";am.Mn="[[Mangan|Mn]]";am.Fe="[[Żelazo|Fe]]";am.Co="[[Kobalt|Co]]";am.Ni="[[Nikiel|Ni]]";am.Cu="[[Miedź|Cu]]";am.Zn="[[Cynk|Zn]]";am.Ga="[[Gal|Ga]]";am.Ge="[[German|Ge]]";am.As="[[Arsen|As]]";am.Se="[[Selen|Se]]";am.Br="[[Brom|Br]]";am.Kr="[[Krypton|Kr]]";am.Rb="[[Rubid|Rb]]";
am.Sr="[[Stront|Sr]]";am.Y="[[Itr|Y]]";am.Zr="[[Cyrkon (pierwiastek)|Zr]]";am.Nb="[[Niob|Nb]]";am.Mo="[[Molibden|Mo]]";am.Tc="[[Technet|Tc]]";am.Ru="[[Ruten|Ru]]";am.Rh="[[Rod (pierwiastek)|Rh]]";am.Pd="[[Pallad|Pd]]";am.Ag="[[Srebro|Ag]]";am.Cd="[[Kadm|Cd]]";am.In="[[Ind|In]]";am.Sn="[[Cyna]]";am.Sb="[[Antymon|Sb]]";am.Te="[[Tellur|Te]]";am.I="[[Jod|I]]";am.Xe="[[Ksenon|Xe]]";
am.Cs="[[Cez|Cs]]";am.Ba="[[Bar (pierwiastek)|Ba]]";am.La="[[Lantan|La]]";am.Ce="[[Cer|Ce]]";am.Pr="[[Prazeodym|Pr]]";am.Nd="[[Neodym|Nd]]";am.Pm="[[Promet|Pm]]";am.Sm="[[Samar|Sm]]";am.Eu="[[Europ|Eu]]";am.Gd="[[Gadolin|Gd]]";am.Tb="[[Terb|Tb]]";am.Dy="[[Dysproz|Dy]]";am.Ho="[[Holm (pierwiastek)|Ho]]";am.Er="[[Erb|Er]]";am.Tm="[[Tul|Tm]]";am.Yb="[[Iterb|Yb]]";am.Lu="[[Lutet|Lu]]";am.Hf="[[Hafn|Hf]]";am.Ta="[[Tantal (pierwiastek)|Ta]]";am.W="[[Wolfram|W]]";am.Re="[[Ren (pierwiastek)|Re]]";am.Os="[[Osm|Os]]";am.Ir="[[Iryd|Ir]]";am.Pt="[[Platyna|Pt]]";am.Au="[[Złoto|Au]]";am.Hg="[[Rtęć|Hg]]";am.Tl="[[Tal|Tl]]";am.Pb="[[Ołów|Pb]]";am.Bi="[[Bizmut|Bi]]";am.Po="[[Polon|Po]]";am.At="[[Astat|At]]";am.Rn="[[Radon|Rn]]";
am.Fr="[[Frans|Fr]]";am.Ra="[[Rad (pierwiastek)|Ra]]";am.Ac="[[Aktyn|Ac]]";am.Th="[[Tor (pierwiastek)|Th]]";am.Pa="[[Protaktyn|Pa]]";am.U="[[Uran (pierwiastek)|U]]";am.Np="[[Neptun (pierwiastek)|Np]]";am.Pu="[[Pluton (pierwiastek)|Pu]]";am.Am="[[Ameryk|Am]]";am.Cm="[[Kiur (pierwiastek)|Cm]]";am.Bk="[[Berkel|Bk]]";am.Cf="[[Kaliforn|Cf]]";am.Es="[[Einstein (pierwiastek)|Es]]";am.Fm="[[Ferm|Fm]]";am.Md="[[Mendelew|Md]]";am.No="[[Nobel (pierwiastek)|No]]";am.Lr="[[Lorens|Lr]]";am.Rf="[[Rutherford (pierwiastek)|Rf]]";am.Db="[[Dubn|Db]]";am.Sg="[[Seaborg|Sg]]";am.Bh="[[Bohr (pierwiastek)|Bh]]";am.Hs="[[Has|Hs]]";am.Mt="[[Meitner|Mt]]";am.Ds="[[Darmsztadt|Ds]]";am.Rg="[[Roentgen (pierwiastek)|Rg]]";am.Cp="[[Kopernik (pierwiastek)|Cp]]";am.Nh="[[Nihon|Nh]]";am.Fl="[[Flerow|Fl]]";am.Mc="[[Moskow|Mc]]";am.Lv="[[Liwermor|Lv]]";am.Ts="[[Tenes|Ts]]";am.Og="[[Oganeson|Og]]";

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_CHARGERP = 11 -- suffix and (charge plus + radical)
local T_SUF_CHARGE2 = 12 -- suffix and (charge) e.g. 2(2+) from He2(2+)
local T_SUF_CHARGERM = 13 -- suffix and (charge minus + radical)
local T_SPECIAL = 14     -- starting with \ e.g. \d for double bond (=)
local T_ARROWS = 15      -- match: \xx+
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_SUF_RADICAL = 21 -- suffix and (radical)
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.7em; line-height:1.0em; font-size:80%; text-align:left;\">" .. up .. "<br /></span>";
  else
    return "<span style=\"display:inline-block; margin-bottom:-0.3em; vertical-align:-0.3em; line-height:1.0em; font-size:80%; text-align:left;\">" .. up .. "<br />" .. down .. "</span>";
  end
end

function DotIt()
  return '<span style="font-weight:bold;">&middot;</span>'
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('^%,%d', i); t = T_NOCHANGE; end       -- fraction
		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('^[\\].[%l]+', i); t = T_ARROWS; end  -- matching \xx+
		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+%([\\][r]%)', i); t = T_SUF_RADICAL; end -- matching x(\r)
		if not x then x = f:match('^%d+%([\\][r]%-%)', i); t = T_SUF_CHARGERM; end -- matching x(\r-)
		if not x then x = f:match('^%d+%([\\][r]%+%)', i); t = T_SUF_CHARGERP; end -- matching x(\r+)
		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_SUF_RADICAL then 
          formula = formula .. su("•", string.match(x, "%d+"))      	
      elseif t == T_SUF_CHARGERM then 
          formula = formula .. su("•−", string.match(x, "%d+"))
      elseif t == T_SUF_CHARGERP then 
          formula = formula .. su("•+", string.match(x, "%d+"))
      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 .. "<span style=\"display:inline-block; margin-bottom:-0.3em; vertical-align:0.7em; line-height:1.0em; font-size:80%; text-align:left;\">"; if string.match(x, "%d+") then formula = formula .. string.match(x, "%d+"); end formula = formula .. string.gsub(string.match(x, "[%+-]"), "-", "−") .. "</br></span>";  -- 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_ARROWS then
  		  parameter = string.sub(x, 2)
  		  if	   parameter == "arrow"   then formula = formula .. "<abbr class=\"abbr\" title=\"Przebieg reakcji w prawo\" style=\"text-decoration:none !important\">→</abbr>"   -- rightwards arrow
  		  	elseif parameter == "reaguje"  then formula = formula .. "<abbr class=\"abbr\" title=\"Przebieg reakcji w prawo\" style=\"text-decoration:none !important\">→</abbr>"
  		  	elseif parameter == "larrow"  then formula = formula .. "<abbr class=\"abbr\" title=\"Przebieg reakcji w lewo\" style=\"text-decoration:none !important\">←</abbr>"   -- leftwards arrow
			elseif parameter == "darrow"  then formula = formula .. "<abbr class=\"abbr\" title=\"Reakcja odwracalna\" style=\"text-decoration:none !important\">⇄</abbr>"   -- double arrow (rightwards arrow over leftwards arrow)
			elseif parameter == "dlarrow" then formula = formula .. "<abbr class=\"abbr\" title=\"Reakcja odwracalna\" style=\"text-decoration:none !important\">⇆</abbr>"   -- double arrow (leftwards arrow over rightwards arrow)
			elseif parameter == "equil"   then formula = formula .. "<abbr class=\"abbr\" title=\"Reakcja równowagowa\" style=\"text-decoration:none !important\">⇌</abbr>"   -- double arrow (chemical equilibrium)
			elseif parameter == "redox"   then formula = formula .. "<abbr class=\"abbr\" title=\"Reakcja połówkowa redoks\" style=\"text-decoration:none !important\">=</abbr>"   -- equals sign (redox reaction)				
			elseif parameter == "meso"    then formula = formula .. "<abbr class=\"abbr\" title=\"Rezonans chemiczny\" style=\"text-decoration:none !important\">↔</abbr>"   -- left right arrow (mesomerizm)
			elseif parameter == "up"      then formula = formula .. "<abbr class=\"abbr\" title=\"Reagent opuszcza środowisko reakcji\" style=\"text-decoration:none !important\">↑</abbr>"   -- upwards arrow
			elseif parameter == "opuszcza"      then formula = formula .. "<abbr class=\"abbr\" title=\"Reagent opuszcza środowisko reakcji\" style=\"text-decoration:none !important\">↑</abbr>"   -- upwards arrow
			elseif parameter == "down"    then formula = formula .. "<abbr class=\"abbr\" title=\"Reagent tworzy nierozpuszczalny osad\" style=\"text-decoration:none !important\">↓</abbr>"   -- downwards arrow
			elseif parameter == "osad"    then formula = formula .. "<abbr class=\"abbr\" title=\"Reagent tworzy nierozpuszczalny osad\" style=\"text-decoration:none !important\">↓</abbr>"   -- downwards arrow
			elseif parameter == "prim"    then formula = formula .. "<abbr class=\"abbr\" title=\"Prim\" style=\"text-decoration:none !important\">′</abbr>"   -- prim
			elseif parameter == "bis"     then formula = formula .. "<abbr class=\"abbr\" title=\"Bis\" style=\"text-decoration:none !important\">″</abbr>"   -- bis
			elseif parameter == "tris"    then formula = formula .. "<abbr class=\"abbr\" title=\"Ter\" style=\"text-decoration:none !important\">‴</abbr>"   -- tris
			elseif parameter == "quat"    then formula = formula .. "<abbr class=\"abbr\" title=\"Kwater\" style=\"text-decoration:none !important\">⁗</abbr>"   -- quater
			elseif parameter == "ds"      then formula = formula .. "<abbr class=\"abbr\" title=\"Dwa wiązania pojedyncze\" style=\"text-decoration:none !important\">></abbr>"   -- two single bonds
			elseif parameter == "dr"      then formula = formula .. "<abbr class=\"abbr\" title=\"Wolna para elektronowa\" style=\"text-decoration:none !important; font-weight:bold\">&#58;</abbr>"   -- diradical
			elseif parameter == "hbond"	  then formula = formula .. "<abbr class=\"abbr\" title=\"Wiązanie wodorowe\" style=\"text-decoration:none !important\">⋯</abbr>"   --  hydrogen bond
            elseif parameter == "wforce"  then formula = formula .. "<abbr class=\"abbr\" title=\"Słabe oddziaływanie międzycząsteczkowe\" style=\"text-decoration:none !important\">⋯</abbr>"   --  weak intermolecular force
            elseif parameter == "minus"   then formula = formula .. "−"   --  minus

		  end
      elseif t == T_SPECIAL then
          parameter = string.sub(x, 2, 2) -- x fra \x  
          if       parameter == "s" then formula = formula .. "<abbr class=\"abbr\" title=\"Wiązanie pojedyncze\" style=\"text-decoration:none !important\">−</abbr>"   -- single bond
            elseif parameter == "d" then formula = formula .. "<abbr class=\"abbr\" title=\"Wiązanie podwójne\" style=\"text-decoration:none !important\">=</abbr>"   -- double bond
            elseif parameter == "t" then formula = formula .. "<abbr class=\"abbr\" title=\"Wiązanie potrójne\" style=\"text-decoration:none !important\">≡</abbr>"   -- tripple bond
            elseif parameter == "q" then formula = formula .. "<abbr class=\"abbr\" title=\"Wiązanie poczwórne\" style=\"text-decoration:none !important\">≣</abbr>"   -- quadruple bond
            elseif parameter == "p" then formula = formula .. "<abbr class=\"abbr\" title=\"Wiązanie połówkowe, niepełne\" style=\"text-decoration:none !important\">⋯</abbr>"   --  partial bond
            elseif parameter == "r" then formula = formula .. su("<abbr class=\"abbr\" title=\"Niesparowany elektron\" style=\"text-decoration:none !important\">•</abbr>","") -- radical
            elseif parameter == "h" then formula = formula .. "η"   -- η, hapticity
            elseif parameter == "m" then formula = formula .. "μ"   -- μ, bridging ligand
            elseif parameter == "-" then formula = formula .. "-"   -- -
            elseif parameter == "\\" then formula = formula .. "\\"   -- \
            elseif parameter == "\'" then formula = formula .. "′"   -- prime
          end
      elseif t == T_SPECIAL2 then  -- \y{x}
         parameter = string.sub(x, 2, 2) -- y fra \y{x} 
          if parameter  == "h" then --[[Ligandy]]
             if (auto == '') then formula = formula .. "η<sup>" .. string.match(x, '%d+') .. "</sup>-" 
               else
             formula = formula .. "[[Ligandy|η<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)
          elseif parameter == "n" then formula = formula .. string.sub(x, 4, -2) -- rzadkie przypadki, gdy coś ma pozostać bez zmian, a moduł coś knoci

          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<span style=\"display:inline-block; margin-bottom:-0.3em; vertical-align:-0.3em; line-height:1.0em; font-size:80%; text-align:left;\"><br />2</span>O";
        else
          formula = formula .. DotIt() .. "H<span style=\"display:inline-block; margin-bottom:-0.3em; vertical-align:-0.3em; line-height:1.0em; font-size:80%; text-align:left;\"><br />2</span>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(string.gsub(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

-- usunięte: wyświetla problemy przy linkowaniu
--   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