Difference between revisions of "Module:Chem2"

From blackwiki
Jump to navigation Jump to search
blackwiki>Christian75
blackwiki>Christian75
(update)
Line 1: Line 1:
--Husk nyt navn, ikke mm
 
 
 
local c = {} -- module's table
 
local c = {} -- module's table
 
  
 
local am = {}  -- Elements with wiki links
 
local am = {}  -- Elements with wiki links
Line 14: Line 11:
 
local T_OPEN = 2      -- open '('
 
local T_OPEN = 2      -- open '('
 
local T_CLOSE = 3      -- close ')'
 
local T_CLOSE = 3      -- close ')'
local T_PM = 4    -- + or –
+
local T_PM_CHARGE = 4    -- + or –
 
local T_WATER = 6  -- .xH2O x number
 
local T_WATER = 6  -- .xH2O x number
 
local T_CRYSTAL = 9 -- .x
 
local T_CRYSTAL = 9 -- .x
Line 23: Line 20:
 
local T_SPECIAL = 14  -- starting with \ e.g. \d for double bond (=)
 
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_SPECIAL2 = 16  -- starting with \y{x} e.g. \i{12} for isotope with mass number 12
local T_UNDERSCORE = 17 -- _{ ... }
+
local T_ARROW_R = 17    -- match: ->
 +
local T_ARROW_EQ = 18    -- match: <->
 +
local T_UNDERSCORE = 19 -- _{ ... }
  
 
function su(up, down) -- like template su
 
function su(up, down) -- like template su
Line 40: Line 39:
 
function item(f) -- (iterator) returns one token (type, value) at a time from the formula 'f'
 
function item(f) -- (iterator) returns one token (type, value) at a time from the formula 'f'
 
   local i = 1
 
   local i = 1
 +
  local first = "true";
 +
 
   return function ()
 
   return function ()
 
local t, x = nil, nil
 
local t, x = nil, nil
if i <= f:len() then
+
 
                               x = f:match('^%u%l*', i); t = T_ELEM;         -- matching letters like Aaaaa
+
        if (first == "true" and f:match('^[%d.]+', i)) then
 +
                x = f:match('^[%d.]+', i); t = T_REST; i = i + x:len();  -- matching coefficient (need a space first)
 +
 
 +
        elseif i <= f:len() then
 +
                               x = f:match('^%s+[%d.]+', i); t = T_REST;  -- matching coefficient (need a space first)
 +
if not x then x = f:match('^%s+[+]', i); t = T_REST; end      -- matching + (H2O + H2O)
 +
if not x then x = f:match('^%&%#[%w%d]+%;', i); t = T_REST; end      -- &#...;
 +
if not x then x = f:match('^%s+%<%-%>', i); t = T_ARROW_EQ; end      -- matching <->
 +
if not x then x = f:match('^%s+%-%>', i); t = T_ARROW_R; end      -- matching ->
 +
if not x then x = f:match('^%u%l*', i); t = T_ELEM; end        -- matching letters 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+[+-]', 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+%(%d*[+-]%)', i); t = T_SUF_CHARGE2; end        -- matching x(y+/-), x(+/-)
Line 50: Line 60:
 
if not x then x = f:match('^[(|{|%[]', i); t = T_OPEN; end    -- matching ({[
 
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_CLOSE; end          -- matching )}]
if not x then x = f:match('^[+-]', i); t = T_PM; end        -- matching '+' or '-'
+
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.]*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_CRYSTAL; end -- Crystal
Line 56: Line 66:
 
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 -- \x
 
if not x then x = f:match('^_{[^}]*}', i); t = T_UNDERSCORE; end -- \x
if not x then x = f:match('^.', i); t = T_REST; end  --Resten en ad gangen
+
if not x then x = f:match('^.', i); t = T_REST; 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
end
+
end
 +
        first = "false"
 
return t, x
 
return t, x
 
end
 
end
Line 69: Line 80:
  
 
   local config = frame.args
 
   local config = frame.args
 
-- Tror ikke det bliver brugt, der er link nedenunder her og auto virker vist ikke
 
--  local link = frame.args['link']
 
--  local auto = frame.args['auto']
 
  
 
   local sumO = 0
 
   local sumO = 0
Line 81: Line 88:
 
--  local auto = config.auto or ""
 
--  local auto = config.auto or ""
  
   if not (link == nil or link == '') then formula = formula .. "[[" .. link .. "|"; end  -- wikilink start [[link|
+
   if not (link == nil or link == '') then formula = formula .. "[[" .. link .. "|"; end  -- wiki link start [[link|
 
   
 
   
 
   for t, x in item(f) do  
 
   for t, x in item(f) do  
Line 87: Line 94:
 
                     else formula = formula .. x  
 
                     else formula = formula .. x  
 
                     end
 
                     end
 +
      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_OPEN  then formula = formula .. x; sumO = sumO + 1;        -- ( {
 
       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_CLOSE then formula = formula .. x; sumO = sumO -1;        -- ) }
       elseif t == T_PM   then formula = formula .. su(string.gsub(x, "-", "–"), "");
+
       elseif t == T_PM_CHARGE   then formula = formula .. su(string.gsub(x, "-", "–"), "");
 
       elseif t == T_SUF_CHARGE then  
 
       elseif t == T_SUF_CHARGE then  
 
           formula = formula .. su(string.gsub(string.match(x, "[+-]"), "-", "–"), string.match(x, "%d+"), "");
 
           formula = formula .. su(string.gsub(string.match(x, "[+-]"), "-", "–"), string.match(x, "%d+"), "");
Line 102: Line 110:
 
       elseif t == T_SPECIAL then
 
       elseif t == T_SPECIAL then
 
           parameter = string.sub(x, 2, 2) -- x fra \x   
 
           parameter = string.sub(x, 2, 2) -- x fra \x   
           if      parameter == "s" then formula = formula .. "–"   -- single bond
+
           if      parameter == "s" then formula = formula .. "–"   -- single bond
 
             elseif parameter == "d" then formula = formula .. "="  -- double bond
 
             elseif parameter == "d" then formula = formula .. "="  -- double bond
 
             elseif parameter == "t" then formula = formula .. "≡"  -- tripple bond
 
             elseif parameter == "t" then formula = formula .. "≡"  -- tripple bond
Line 109: Line 117:
 
             elseif parameter == "-" then formula = formula .. "-"  -- -
 
             elseif parameter == "-" then formula = formula .. "-"  -- -
 
           end
 
           end
       elseif t == T_SPECIAL2 then  
+
       elseif t == T_SPECIAL2 then -- \y{x}
 
         parameter = string.sub(x, 2, 2) -- y fra \y{x}  
 
         parameter = string.sub(x, 2, 2) -- y fra \y{x}  
-- xxxxx mangler links med auto
+
-- xxxxx missing: links with auto
 
           if parameter  == "h" then formula = formula .. "η<sup>" .. string.match(x, '%d+') .. "</sup>" --[haptacity]]
 
           if parameter  == "h" then formula = formula .. "η<sup>" .. string.match(x, '%d+') .. "</sup>" --[haptacity]]
 
           elseif parameter == "i" then formula = formula .. su(string.match(x, '%d+'), "") -- [[isotope]]
 
           elseif parameter == "i" then formula = formula .. su(string.match(x, '%d+'), "") -- [[isotope]]
Line 132: Line 140:
  
 
       elseif t == T_UNDERSCORE  then formula = formula .. su("", string.match(x, "[^{^_][%w%W]*[^}]"))
 
       elseif t == T_UNDERSCORE  then formula = formula .. su("", string.match(x, "[^{^_][%w%W]*[^}]"))
 +
 +
      elseif t == T_ARROW_R then formula = formula .. " → "
 +
      elseif t == T_ARROW_EQ then formula = formula .. " ⇌ "
  
 
       elseif t == T_REST  then formula = formula .. x;  -- resten, som ikke blev fanget af diverse regular expresions
 
       elseif t == T_REST  then formula = formula .. x;  -- resten, som ikke blev fanget af diverse regular expresions

Revision as of 21:34, 11 June 2015

This module implements {{chem2}}. Please see its documentation for details.


local c = {} -- module's table

local am = {}  -- Elements with wiki links
am.H="[[Hydrogen|H]]"; am.He="[[Helium|He]]";
am.Li="[[Lithium|Li]]"; am.Be="[[Beryllium|Be]]"; am.B="[[Boron|B]]"; am.C="[[Carbon|C]]"; am.N="[[Nitrogen|N]]"; am.O="[[Oxygen|O]]"; am.F="[[Fluorine|F]]"; am.Ne="[[Neon|Ne]]"; am.Phen="[[phenanthroline|Phen]]";

am.Ph="[[phenyl|Ph]]";

local T_ELEM = 0   -- token types
local T_NUM = 1
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_REST = 7   -- Anything else like ☃
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 -- _{ ... }

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('^[%d.]+', i)) then 
                 x = f:match('^[%d.]+', i); t = T_REST; i = i + x:len();   -- matching coefficient (need a space first)

        elseif i <= f:len() then
                              x = f:match('^%s+[%d.]+', i); t = T_REST;  -- matching coefficient (need a space first)
		if not x then x = f:match('^%s+[+]', i); t = T_REST; end       -- matching + (H2O + H2O)
		if not x then x = f:match('^%&%#[%w%d]+%;', i); t = T_REST; end       -- &#...;
		if not x then x = f:match('^%s+%<%-%>', i); t = T_ARROW_EQ; end       -- matching <->
		if not x then x = f:match('^%s+%-%>', i); t = T_ARROW_R; end       -- matching ->
		if not x then x = f:match('^%u%l*', i); t = T_ELEM; end        -- matching letters 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 -- \x
		if not x then x = f:match('^.', i); t = T_REST; 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 c.mm(frame)
   local f = frame.args[1]

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

   local config = frame.args

   local sumO = 0
   local formula = ''
   local t, x

   local link = config.link or ""
--   local auto = config.auto or ""

   if not (link == nil or link == '') then formula = formula .. "[[" .. link .. "|"; end   -- wiki link start [[link|
 
   for t, x in item(f) do 
      if     t == T_ELEM then if not 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 .. "η"   -- η, haptacity
            elseif parameter == "-" then formula = formula .. "-"   -- -
          end
      elseif t == T_SPECIAL2 then  -- \y{x}
         parameter = string.sub(x, 2, 2) -- y fra \y{x} 
-- xxxxx missing: links with auto
          if parameter  == "h" then formula = formula .. "η<sup>" .. string.match(x, '%d+') .. "</sup>" --[haptacity]]
          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.match(x, "[^{^_][%w%W]*[^}]"))

      elseif t == T_ARROW_R then formula = formula .. " → "
      elseif t == T_ARROW_EQ then formula = formula .. " ⇌ "

      elseif t == T_REST  then formula = formula .. x;  -- resten, som ikke blev fanget af diverse regular expresions
     
      else error('unreachable - ???') end -- in fact, unreachable   (Skulle gerne være havnet i T_REST

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



return c -- exports c.mm()