Module:CategoryTable

From Akurasu Wiki
Jump to navigationJump to search

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

--{{#invoke:CategoryTable|main|ListSource=SRW30PartsList|Categories=All,Mechs,Attack,Defense,Move,Spirit,Consumable,Bonus,Special|class="oddeven"}}
--All is a special category, everything will show up in this category if it's listed.

StringBuilder = require("Module:StringBuilder")
require("Module:Split")

local p = {}
 
function p.main(frame)
  local cat_list_page = frame.args['ListSource']
  local category_names = frame.args['Categories']
  local class = frame.args['class']
  return p.render(frame, cat_list_page, category_names, class)
end

function p.render(frame, cat_list_page, category_names, class)
  local string_data = frame:expandTemplate{ title = cat_list_page }
  string_data = string.gsub(string_data, "\n", "")

  local all = true
  if Split(category_names,",")["All"] ~= nil then
    all = false
  end

  out = StringBuilder()
  cat_data = {}

  if all == true then
    -- If All is in Category List, make it the first Category
    cat_data["All"] = {}
  end
  cat_data_header = {}
  cat_data_align = {}

  for i, line in ipairs(Split(string_data, ";")) do
    if i == 1 then
      -- Process header line here
      for h, jline in ipairs(Split(line, "|")) do
        cat_data_align[h] = 0
        cleanline = jline:gsub("^%s*(.-)%s*$", "%1") -- not sure you need to trim whitespace
        if string.sub(cleanline, 1, 1) == "*" then
          cat_data_align[h] = cat_data_align[h] + 1
          cleanline = cleanline:sub(2)
        end
        if string.sub(cleanline, -1) == "*" then
          cat_data_align[h] = cat_data_align[h] + 2
          cleanline = cleanline:sub(1, -2)
        end -- if string

        table.insert(cat_data_header, cleanline)
      end -- for
      
    else
      if #line ~= 0 then
        row = Split(line, "|")
        cat = Split(row[1], ",")
        --part = table.concat(row, "<td>", 2)
        outputline = StringBuilder()
        for k, dat in ipairs(row) do
          if k > 1 then
            -- Align Right
            if cat_data_align[k] == 2 then
              outputline:append("<td style='text-align:right;'>")
            
            -- Align Center
            elseif cat_data_align[k] == 3 then
              outputline:append("<td style='text-align:center;'>")
            
            -- Align Left
            else
              outputline:append("<td>")
            end

            outputline:append(dat .. "</td>")
          end
        end
        part = outputline:tostring()

        if all then
          table.insert(cat_data["All"], part)
        end
        for j, cat in ipairs(Split(row[1], ",")) do
          if cat_data[cat] == nil then cat_data[cat] = {} end
          table.insert(cat_data[cat], part)
        end -- end for
      end -- if len(line)==0
    end -- if i == 1
  end -- for

  out:append("<tabs>")
  for _, cat in ipairs(Split(category_names,",")) do
    out:append_format('<tab name="%s">', cat)
    if class == nil then
      out:append_line("<table>")
    else
      out:append_line("<table class=" .. class .. ">")
    end
    if cat_data[cat] ~= nil then
      -- Output Table Header here
      out:append_line("<tr>")
      for i, hline in ipairs(cat_data_header) do
        if i > 1 then
          out:append("<th>" .. hline .. "</th>")
        end
      end
      out:append("</tr>")
      -- Output data lines
      for h, oline in ipairs(cat_data[cat]) do
        out:append_line("<tr>" .. oline .. "</tr>")
      end
    end
    out:append("</table>")
    out:append('</tab>')
  end
  out:append_line("</tabs>")

  return frame:preprocess(out:tostring())
end
return p