Module: List

From Mr Love Queen's Choice
Jump to: navigation, search

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

local p = {}

--explode-like split function taken from LUA documentation
function split(d,p)
   local t, ll
   t={}
   ll=0
   if(#p == 1) then
      return {p}
   end
   while true do
      l = string.find(p, d, ll, true) -- find the next d in the string
      if l ~= nil then -- if "not not" found then..
         table.insert(t, string.sub(p,ll,l-1)) -- Save it in our array.
         ll = l + 1 -- save just after where we found it for searching next time.
      else
         table.insert(t, string.sub(p,ll)) -- Save what's left in our array.
         break -- Break at end, as it should be, according to the lua manual.
      end
   end
   return t
end

function all_trim(s)
   return s:match( "^%s*(.-)%s*$" )
end

p._parseKarmaAsList = function (args, frame)
   local output = mw.html.create('div')
   output:css('white-space', 'nowrap')
   
   -- Split the cell values
   for i,arg in ipairs(args) do
      local karma_list = split(",", arg)
   
      -- Loop through all personality values
      for idx, karma in ipairs(karma_list) do
        local div = output:tag('div')
          :css('font-size', '.8em;')
          :wikitext('[[' .. karma .. ']]')
      end
   end
   return tostring(output)
end

p._parseKarmaAsImages = function (args, frame)
   local output = mw.html.create('div')
   output:css('white-space', 'nowrap')
   
   -- Split the cell values
   for i,arg in ipairs(args) do
      local karma_list = split(",", arg)
   
      -- Loop through all personality values
      for idx, karma in ipairs(karma_list) do
        local div = output:tag('div')
          :css('display', 'inline-block')
          :wikitext(frame:expandTemplate{ title = "Karma icon", args={all_trim(karma), all_trim(karma), size="52px"}})
      end
   end
   return tostring(output)
end

p._parseMaterials = function (args, frame)
   local output = mw.html.create('div')
   output:css('white-space', 'nowrap')
   
   -- Split the cell values
   for i,arg in ipairs(args) do
      local material_list = split(",", arg)
   
      -- Loop through all personality values
      for idx, material in ipairs(material_list) do
        local div = output:tag('div')
          :wikitext(frame:expandTemplate{ title = "Material icon", args={all_trim(material), size="52px"}})
      end
   end
   return tostring(output)
end

p.parseKarmaAsList = function (frame)
  local getArgs = require 'Module:Arguments'.getArgs
  return p._parseKarmaAsList(getArgs(frame, {wrappers = 'Template:Karma text list'}), frame)
end

p.parseMaterials = function (frame)
  local getArgs = require 'Module:Arguments'.getArgs
  return p._parseMaterials(getArgs(frame, {wrappers = 'Template:Material parse list'}), frame)
end

p.parseKarmaAsImages = function (frame)
  local getArgs = require 'Module:Arguments'.getArgs
  return p._parseKarmaAsImages(getArgs(frame, {wrappers = 'Template:Karma image list'}), frame)
end

return p