Module:Common

Documentation for this module may be created at Module:Common/doc

local getArgs = require('Module:Arguments').getArgs;
local defArgs = require('Module:Arguments').defaultArgs;
local common = {};

function common.FormatName( frame )
  local args = defArgs(getArgs(frame), 1, "GlcNAca/b1-3Xyla-4Galb1-3GalNAca1-4(NeuAc?1-2NeuGc4Mea1-3)GalNAcb1-4(EtnP-6)GlcNAcb1-3Manb1-4Glcb1-1Cer");
  local arg = args[1];

  replacements = {
  ["alpha"] = "α",
  ["beta"] = "β",
  ["chi"]  = "χ",
  ["delta"] = "δ",
  ["Delta"] = "Δ",
  ["epsilon"] = "ε",
  ["zeta"] = "ζ",
  ["phi"] = "φ",
  ["gamma"] = "γ",
  ["Gamma"] = "Γ",
  ["eta"] = "η",
  ["kappa"] = "κ",
  ["lambda"] = "λ", 
  ["mu"] = "μ", 
  ["nu"] = "ν", 
  ["pi"] = "π",
  ["Pi"] = "Π",
  ["theta"] = "θ",
  ["tau"] = "τ",
  ["upsilon"] = "υ",
  ["Upsilon"] = "Υ",
  ["phi"] = "φ",
  ["psi"] = "ψ",
  ["xi"] = "ξ",
  ["omega"] = "ω",
  ["sigma"] = "σ",
  }

  if string.len(arg) > 0 then
    name = arg:match(".+")
---SUPER AND SUBSCRIPTS (SINGLE LETTER)---
    name = string.gsub( name, "%^([-+%p%w])_([-+%p%w]) ", '<span style="line-height: 1em; font-size: x-small; position: absolute; font-family: monospace">%1<br>%2</span><span style="font-family: monospace; font-size: x-small">{{#createstring:&nbsp;|{{#max:{{#length:%1}}|{{#length:%2}}}}}}</span>')
---SUPER AND SUBSCRIPTS (MULTIPLE LETTERS)---
    name = string.gsub( name, "%^{([-+%p%w]+)}_{([-+%p%w]+)}", '<span style="line-height: 1em; font-size: x-small; position: absolute; font-family: monospace">%1<br>%2</span><span style="font-family: monospace; font-size: x-small">{{#createstring:&nbsp;|{{#max:{{#length:%1}}|{{#length:%2}}}}}}</span>')
---SINGLE AND MULTIPLE---
    name = string.gsub( name, "%^([-+%p%w])_{([-+%p%w]+)}", '<span style="line-height: 1em; font-size: x-small; position: absolute; font-family: monospace">%1<br>%2</span><span style="font-family: monospace; font-size: x-small">{{#createstring:&nbsp;|{{#max:{{#length:%1}}|{{#length:%2}}}}}}</span>')
    name = string.gsub( name, "%^{([-+%p%w]+)}_([-+%p%w])", '<span style="line-height: 1em; font-size: x-small; position: absolute; font-family: monospace">%1<br>%2</span><span style="font-family: monospace; font-size: x-small">{{#createstring:&nbsp;|{{#max:{{#length:%1}}|{{#length:%2}}}}}}</span>')
---SUPER/SUBSCRIPTS---
    name = string.gsub( name, "_{([-+%p%w]+)}", "<sub>%1</sub>" )
    name = string.gsub( name, "_([-+%p%w]) ", "<sub>%1</sub>" )
    name = string.gsub( name, "_([-+%p%w])$", "<sub>%1</sub>" )
    name = string.gsub( name, "%^{([-+%p%w]+)}", "<sup>%1</sup>" )
    name = string.gsub( name, "%^([-+%p%w]) ", "<sup>%1</sup>" )
---GREEK LETTERS---
    name = string.gsub (name, '([%a"]+)', 
    function (str)
      return replacements[str] or str
    end
    )
  else
    name = ""
  end

  return frame:preprocess(name)
end

function common.ListMolByReference( frame )
  local args = defArgs(getArgs(frame), 1, "");
  local arg = args[1];
  local text = ""

  idS = {}
  count=0
  for ref, sp, id in arg:gmatch("&?&?([^&]+)&&([%w\., \(\)]+)&&([^\n]+)") do
    local S = idS[id]
    if (S == nil) then idS[id]={} S = idS[id] count = count + 1 end
    if (S[sp] == nil) then S[sp] = {ref}
    else table.insert(S[sp], ref) end
  end
  text = "Showing below ".. count .. " structures."
  for k,v in pairs(idS) do
    text = text .. "\n* [[" ..k.. "&" ..k.. "]] [[Image:" ..k.. ".png&200px]]\n"
    for i,w in pairs(v) do
      text = text .. i
      for j,x in ipairs(w) do
        au, ja, yr = string.match(x, "([%w_]+)[^,]+,([^,]+),([%d]+)" )
        text = text .. '([[Reference:' ..x.. '&' ..au..'_'..yr..'_'..ja.. ']])'
      end
    end
  end

  return text
end

return common