ATLAS Wiki
m
m
Line 4: Line 4:
   
 
local listtype, extrastyle = 'ul', ''
 
local listtype, extrastyle = 'ul', ''
 
local commas = false
 
local iconsize = args.iconsize or '20px'
 
local iconsize = args.iconsize or '20px'
 
local showOne = args.showQuantityOne or false
 
local showOne = args.showQuantityOne or false
Line 23: Line 24:
 
if args.listtype == 'ol' then
 
if args.listtype == 'ol' then
 
listtype = 'ol'
 
listtype = 'ol'
 
elseif args.listtype == 'comma' then
  +
commas = true
 
else -- none
 
else -- none
 
extrastyle = extrastyle..'list-style:none;margin-left:0;'
 
extrastyle = extrastyle..'list-style:none;margin-left:0;'
Line 66: Line 69:
 
-- output itemList
 
-- output itemList
 
local output = {}
 
local output = {}
local widestItem = 0
 
 
local quantitySign = asPerCent and '% ' or ' × '
 
local quantitySign = asPerCent and '% ' or ' × '
 
for _, item in ipairs(itemList) do
 
for _, item in ipairs(itemList) do
 
local fileName = string.gsub(item[1],':',' ')
 
local fileName = string.gsub(item[1],':',' ')
if #item[1] > widestItem then
+
if commas then
 
table.insert(output, ', '..(item[2] ~= nil and item[2]..quantitySign or '')..'[[File:'..fileName..'.png|'..iconsize..']] '..'[['..(item[1])..']]')
widestItem = #item[1]
 
 
else
  +
table.insert(output, '<li>'..(item[2] ~= nil and item[2]..quantitySign or '')..'[[File:'..fileName..'.png|'..iconsize..']] '..'[['..(item[1])..']]'..'</li>')
 
end
 
end
 
table.insert(output, '<li>'..(item[2] ~= nil and item[2]..quantitySign or '')..'[[File:'..fileName..'.png|'..iconsize..']] '..'[['..(item[1])..']]'..'</li>')
 
 
end
 
end
   
Line 81: Line 83:
 
end
 
end
   
--[[ If maxRows is specified and columns is not specified; break into new columns at given intervals.
+
-- if maxRows is specified and columns is not specified, break into new columns at given intervals
Then if columnWidth is not specified sets column-width to fit the longest item.
 
]]
 
 
if args.maxRows ~= nil and args.columns == nil then
 
if args.maxRows ~= nil and args.columns == nil then
extrastyle = extrastyle..'column-count:'..math.ceil(#output/args.maxRows)..';'
+
extrastyle = extrastyle..'column-count:'..math.ceil(#output/args.maxRows)..';'
if args.columnWidth ~= nil then
 
extrastyle = extrastyle..'column-width: calc('..widestItem..'em+1em+'..iconsize..');'
 
end
 
 
end
 
end
   
 
-- if the list is long and has no explicit style, break it into columns automatically
 
-- if the list is long and has no explicit style, break it into columns automatically
if #output > 5 and extrastyle == '' then
+
if #output > 5 and extrastyle == '' and not commas then
 
extrastyle = 'column-width:15em;'
 
extrastyle = 'column-width:15em;'
 
end
 
end
  +
if commas then
 
  +
output[1] = output[1]:sub(3, #output[1])
return '<'..listtype..(extrastyle ~= '' and ' style="'..extrastyle..'"' or '')..'>'..table.concat(output, '')..'</'..listtype..'>'
 
  +
return '<div style="white-space:nowrap;'..(extrastyle ~= '' and ' style="'..extrastyle..'"' or '')..'>'..table.concat(output, '')..'</div>'
  +
else
 
return '<'..listtype..(extrastyle ~= '' and ' style="'..extrastyle..'"' or '')..'>'..table.concat(output, '')..'</'..listtype..'>'
 
end
 
end
 
end
   

Revision as of 01:22, 12 August 2020

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

local p = {}
function p.itemlist( f )
  local args = f:getParent().args

  local listtype, extrastyle = 'ul', ''
  local commas = false
  local iconsize = args.iconsize or '20px'
  local showOne = args.showQuantityOne or false
  local asPerCent = args.showQuantityAsPerCent or false

  if args.columns ~= nil then
    extrastyle = extrastyle..'column-count:'..args.columns..';'
  end

  if args.columnWidth ~= nil then
    extrastyle = extrastyle..'column-width:'..args.columnWidth..';'
  end

  if args.marginLeft ~= nil then
    extrastyle = extrastyle..'margin-left:'..args.marginLeft..';'
  end

  if args.listtype ~= nil then
    if args.listtype == 'ol' then
      listtype = 'ol'
    elseif args.listtype == 'comma' then
      commas = true
    else -- none
      extrastyle = extrastyle..'list-style:none;margin-left:0;'
    end
  end

  -- split first value at comma, e.g. for data from module:dv
  local items = {}
  for v in mw.text.gsplit(args[1], ',', true) do
    if #v > 0 then -- skip empty
      table.insert(items, v)
    end
  end

  -- append rest parameters
  for _,v in ipairs(args) do 
    if _ > 1 and #v > 0 then
      table.insert(items, v)
    end
  end

  -- read arguments list into itemList (consists of tables, at index 1 is the text, at index 2 the quantity (optional))
  local itemList, i, lastItem = {}, 0, nil
  for _, item in ipairs(items) do
    item = item:match "^%s*(.-)%s*$" -- trim

    if string.len(item) > 0 then
      if lastItem and (item:match "^[%d.]*$" or item:match "^[%d.]*%-[%d.]*$") then
        if asPerCent then
          itemList[i][2] = item * 100 -- item is per cent value
        elseif item ~= '1' or showOne then
          itemList[i][2] = item -- item is quantity
        end
        lastItem = nil
      else
        table.insert(itemList, {item, nil} ) -- item is text
        i = i + 1
        lastItem = item
      end
    end
  end

  -- output itemList
  local output = {}
  local quantitySign = asPerCent and '% ' or ' × '
  for _, item in ipairs(itemList) do
    local fileName = string.gsub(item[1],':',' ')
    if commas then
      table.insert(output, ', '..(item[2] ~= nil and item[2]..quantitySign or '')..'[[File:'..fileName..'.png|'..iconsize..']] '..'[['..(item[1])..']]')
    else
      table.insert(output, '<li>'..(item[2] ~= nil and item[2]..quantitySign or '')..'[[File:'..fileName..'.png|'..iconsize..']] '..'[['..(item[1])..']]'..'</li>')
    end
  end

  if #output == 0 then
    return
  end

  -- if maxRows is specified and columns is not specified, break into new columns at given intervals
  if args.maxRows ~= nil and args.columns == nil then
	extrastyle = extrastyle..'column-count:'..math.ceil(#output/args.maxRows)..';'
  end

  -- if the list is long and has no explicit style, break it into columns automatically
  if #output > 5 and extrastyle == '' and not commas then
    extrastyle = 'column-width:15em;'
  end
  if commas then
    output[1] = output[1]:sub(3, #output[1])
    return '<div style="white-space:nowrap;'..(extrastyle ~= '' and ' style="'..extrastyle..'"' or '')..'>'..table.concat(output, '')..'</div>'
  else
    return '<'..listtype..(extrastyle ~= '' and ' style="'..extrastyle..'"' or '')..'>'..table.concat(output, '')..'</'..listtype..'>'
  end
end

return p