ARK: Survival Evolved Wiki
(added globalindent)
(added parameter for delimiter)
(36 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
 
function p.itemlist( f )
 
function p.itemlist( f )
  +
local dlclink = require('Module:DLCLink').link
 
local args = f:getParent().args
 
local args = f:getParent().args
local itemList, iconsize, columns, indent, globalindent = {}, '30px', 1, 0, 0
 
   
  +
local listtype, extrastyle = 'ul', ''
if args.iconsize ~= nil then
 
iconsize = args.iconsize
+
local delimiter = args.delimiter or ','
  +
local iconsize = args.iconsize or '30px'
  +
local showOne = args.showQuantityOne or false
  +
local asPerCent = args.showQuantityAsPerCent or false
  +
local noDlcIcon = args.noDlcIcon or false
  +
if noDlcIcon and ( noDlcIcon == '' or noDlcIcon == 0) then -- lua evaluates anything to true except nil and false
  +
noDlcIcon = false
 
end
 
end
  +
 
if args.columns ~= nil then
 
if args.columns ~= nil then
columns = args.columns
+
extrastyle = extrastyle..'column-count:'..args.columns..';'
 
end
 
end
  +
if args.globalindent ~= nil then
+
if args.columnWidth ~= nil then
globalindent = args.globalindent
 
  +
extrastyle = extrastyle..'column-width:'..args.columnWidth..';'
 
end
 
end
   
  +
if args.marginLeft ~= nil then
for _,item in ipairs(args) do
 
  +
extrastyle = extrastyle..'margin-left:'..args.marginLeft..';'
-- get indentation
 
  +
end
indent = globalindent
 
  +
while string.sub(item,1,1) == '*' do
 
 
if args.listtype ~= nil then
item = string.sub(item,2)
 
indent = indent + 1
+
if args.listtype == 'ol' then
  +
listtype = 'ol'
  +
else -- none
  +
extrastyle = extrastyle..'list-style:none;margin-left:0;'
 
end
 
end
table.insert(itemList, string.rep('*',indent)..'* [[File:'..item..'.png|'..iconsize..']] [['..item..']]')
 
 
end
 
end
  +
return '<div style="column-count:'..columns..';-moz-column-count:'..columns..';-webkit-column-count:'..columns..'">\n'..table.concat(itemList, '\n')..'\n</div>'
 
  +
-- split first value at comma, e.g. for data from module:dv
  +
local items = {}
  +
for v in mw.text.gsplit(args[1], delimiter, 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(string.gsub(item[1], '[:/]', ' '), '+', '-')
  +
  +
table.insert(output, '<li>'..(item[2] ~= nil and item[2]..quantitySign or '')..'[[File:'..fileName..'.png|'..iconsize..'|link='..item[1]..']] '..dlclink(item[1],noDlcIcon)..'</li>')
  +
end
  +
  +
if #output == 0 then
  +
return
  +
end
  +
  +
-- if the list is long and has no explicit style, break it into columns automatically
  +
if #output > 5 and extrastyle == '' then
  +
extrastyle = 'column-width:15em;'
  +
end
  +
  +
return '<'..listtype..(extrastyle ~= '' and ' style="'..extrastyle..'"' or '')..'>'..table.concat(output, '')..'</'..listtype..'>'
 
end
 
end
  +
 
return p
 
return p

Revision as of 22:38, 3 October 2020

Template-info Documentation

Lists items (as well as creatures) with their icon.

Append each item or creature name as a single parameter. A quantity of x, x..y, or x-y can be used as the next parameter.

Parameters

Parameter Default Description
iconsize 30px size of the items
columns 1
columnWidth set a desired width for columns. default is 15em
listtype set to 'ol' for a numbered list, 'none' for list without bullets
showQuantityOne false set to true to also show '1 ×' if a quantity of 1 is given
showQuantityAsPerCent false set to true to also show e.g. '50%' if a quantity of 0.5 is given
noDlcIcon set to 1 to hide the DLC-/mod-icons
mod if set, assumes that each item in the list is a part of the specified mod, automatically adding the namespace prefix at runtime

Usage / Examples

{{ItemList|Ankylosaurus|Thatch}}
{{ItemList|iconsize=50px|Argentavis|Quetzal|Pteranodon}}
{{ItemList|iconsize=20px|columns=2|Thatch|Wood|Raw Meat|Raw Prime Meat|Dodo|Mesopithecus|Wooden Door}}
{{ItemList|iconsize=20px|columnWidth=20em|Thatch|Wood|Raw Meat|Raw Prime Meat|Dodo|Mesopithecus|Wooden Door}}
{{ItemList|listtype=ol|Berries|Rockarrot|Longrass}}
  1. Berries Berries
  2. Rockarrot Rockarrot
  3. Longrass Longrass
{{ItemList|listtype=none|Berries|Sulfur (Scorched Earth)|Clay (Primitive Plus)}}
{{ItemList|listtype=none|noDlcIcon=1|Berries|Sulfur (Scorched Earth)|Clay (Primitive Plus)}}
{{ItemList|Ankylosaurus|1|Thatch|10..20}}
{{ItemList|showQuantityOne=true|Ankylosaurus|1|Thatch|10-20}}
{{ItemList|showQuantityAsPerCent=true|Cooking Pot|1|Refining Forge|0.5}}

Displaying a comma-delimited array from a data-module:

{{ItemList|columnWidth=20em|{{Dv|Pteranodon|canDragCreatures}}}}

local p = {}
function p.itemlist( f )
  local dlclink = require('Module:DLCLink').link
  local args = f:getParent().args

  local listtype, extrastyle = 'ul', ''
  local delimiter = args.delimiter or ','
  local iconsize = args.iconsize or '30px'
  local showOne = args.showQuantityOne or false
  local asPerCent = args.showQuantityAsPerCent or false
  local noDlcIcon = args.noDlcIcon or false
  if noDlcIcon and ( noDlcIcon == '' or noDlcIcon == 0) then -- lua evaluates anything to true except nil and false
    noDlcIcon = false
  end

  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'
    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], delimiter, 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(string.gsub(item[1], '[:/]', ' '), '+', '-')

    table.insert(output, '<li>'..(item[2] ~= nil and item[2]..quantitySign or '')..'[[File:'..fileName..'.png|'..iconsize..'|link='..item[1]..']] '..dlclink(item[1],noDlcIcon)..'</li>')
  end

  if #output == 0 then
    return
  end

  -- if the list is long and has no explicit style, break it into columns automatically
  if #output > 5 and extrastyle == '' then
    extrastyle = 'column-width:15em;'
  end

  return '<'..listtype..(extrastyle ~= '' and ' style="'..extrastyle..'"' or '')..'>'..table.concat(output, '')..'</'..listtype..'>'
end

return p