Module:ListAllChains

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

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

function lac.func1( frame )
  local args = defArgs(getArgs(frame), 1, "");
  local arg = args[1];

  local text = ""
  local sep = "=="
  for word in arg:gmatch("([%S \t]+)") do
    local title = word:match("&&([^|]+)|")
    local structure = word:match("Structure=(.*)$")

    local items = {}
    for part in structure:gmatch("([^-]+)") do
      table.insert(items, part)
    end

    local stackIndex = 1
    local stack = {}
    local preB = ""
    local postB = ""
    for index = #items, 1, -1 do
      if items[index]:find("%(") ~= nil then
        preB = "("
        postB = ")"
      elseif items[index]:find("%[") ~= nil then
        preB = "["
        postB = "]"
      else
        preB = ""
        postB = ""
      end
  
      if preB:len() > 0 then
        local closed = false
        local str = ""
        local pos = items[index]:find("%" .. preB)
        local prefix = items[index]:sub(1, pos-1)
        str = items[index]:sub(pos+1)
        local tmp = ""
        for j = stackIndex-1, 1, -1 do
          if closed == false and stack[j]:find("%" .. postB) ~= nil then
            local pos2 = stack[j]:find("%" .. postB)
            tmp = prefix .. stack[j]:sub(pos2+1)
            str = str .. "-" .. stack[j]
            stackIndex = stackIndex - 1
            closed = true
          else
            str = str .. "-" .. stack[j]
            if closed == false then
              stack[j] = ""
              stackIndex = stackIndex - 1
            end
          end
        end
        stack[stackIndex] = tmp
        stackIndex = stackIndex + 1
        local res = str:gsub("%)",""):gsub("%]","")
        text = text .. "* " .. title .. sep .. res .. sep .. "[" .. "[File:" .. title .. ".png|link=" .. title .. "]" .. "]\n"
      else
        stack[stackIndex] = items[index]
        stackIndex = stackIndex + 1
      end
    end
    local str = stack[stackIndex-1]
    for j = stackIndex-2, 1, -1 do
      str = str .. "-" .. stack[j]
    end
    text = text .. "* " .. title .. sep .. str .. sep .. "[" .. "[File:" .. title .. ".png|link=" .. title .. "]" .. "]\n"
  end

  return text;
end

return lac