ARK: Survival Evolved Wiki
TigerMehMat (обсуждение | вклад)
Нет описания правки
мНет описания правки
 
(не показаны 2 промежуточные версии этого же участника)
Строка 5: Строка 5:
   
 
local listtype, extrastyle = 'ul', ''
 
local listtype, extrastyle = 'ul', ''
  +
local delimiter = args.delimiter or ','
 
local iconsize = args.iconsize or '30px'
 
local iconsize = args.iconsize or '30px'
 
local showOne = args.showQuantityOne or false
 
local showOne = args.showQuantityOne or false
Строка 19: Строка 20:
 
if args.columnWidth ~= nil then
 
if args.columnWidth ~= nil then
 
extrastyle = extrastyle..'column-width:'..args.columnWidth..';'
 
extrastyle = extrastyle..'column-width:'..args.columnWidth..';'
  +
end
  +
  +
if args.noMargin ~= nil then
  +
extrastyle = extrastyle..'margin:0;'
 
end
 
end
   
Строка 35: Строка 40:
 
-- split first value at comma, e.g. for data from module:dv
 
-- split first value at comma, e.g. for data from module:dv
 
local items = {}
 
local items = {}
for v in mw.text.gsplit(args[1], ',', true) do
+
for v in mw.text.gsplit(args[1], delimiter, true) do
 
if #v > 0 then -- skip empty
 
if #v > 0 then -- skip empty
 
table.insert(items, v)
 
table.insert(items, v)
Строка 45: Строка 50:
 
if _ > 1 and #v > 0 then
 
if _ > 1 and #v > 0 then
 
table.insert(items, v)
 
table.insert(items, v)
  +
end
  +
end
  +
  +
-- prepend mod wiki namespace to items if "mod" argument is specified
  +
if args.mod ~= nil then
  +
for i, v in ipairs(items) do
  +
items[i] = "Mod:"..args.mod.."/"..v
 
end
 
end
 
end
 
end
Строка 73: Строка 85:
 
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(string.gsub(item[1], ':', ' '), '+', '-')
+
local fileName = string.gsub(string.gsub(item[1], '[:/]', ' '), '+', '-')
   
table.insert(output, '<li>'..(item[2] ~= nil and item[2]..quantitySign or '')..'[[File:'..fileName..'.png|'..iconsize..']] '..dlclink(item[1],noDlcIcon)..'</li>')
+
table.insert(output, '<li>'..(item[2] ~= nil and item[2]..quantitySign or '')..'[[File:'..fileName..'.png|'..iconsize..'|link='..item[1]..'|alt=]] '..dlclink(item[1],noDlcIcon)..'</li>')
 
end
 
end
   

Текущая версия от 14:59, 5 июля 2021

Для документации этого модуля может быть создана страница Модуль:ItemList/doc

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.noMargin ~= nil then
  	extrastyle = extrastyle..'margin:0;'
  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

  -- prepend mod wiki namespace to items if "mod" argument is specified
  if args.mod ~= nil then
    for i, v in ipairs(items) do
      items[i] = "Mod:"..args.mod.."/"..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]..'|alt=]] '..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