Module:Skill
From D2R-XL 2012 Wiki
More actions
Documentation for this module may be created at Module:Skill/doc
-- The skill infobox and per-level table of Template:Skill.
local p = {}
-- the per-level table's separator between cells (tools/wiki/gen_skills.py writes it)
local SEP = '¦'
local function lines(text)
local out = {}
for line in mw.text.gsplit(text or '', '\n', true) do
line = mw.text.trim(line)
if line ~= '' then
table.insert(out, line)
end
end
return out
end
local function cells(line)
local out = {}
for cell in mw.text.gsplit(line, SEP, true) do
table.insert(out, mw.text.trim(cell))
end
return out
end
local function given(v)
return v and mw.text.trim(v) ~= ''
end
-- "Fire Arrow, Ice Arrow": a link per skill
local function links(text)
local out = {}
for name in mw.text.gsplit(text or '', ',', true) do
name = mw.text.trim(name)
if name ~= '' then
table.insert(out, '[[' .. name .. ']]')
end
end
return table.concat(out, ', ')
end
local function row(box, label, value)
local r = box:tag('div'):addClass('d2-skill-row')
r:tag('span'):addClass('d2-skill-label'):wikitext(label)
r:tag('span'):wikitext(value)
end
local function infobox(a)
local box = mw.html.create('div'):addClass('d2-skill')
if given(a.image) then
box:tag('div'):addClass('d2-skill-image'):wikitext('[[File:' .. a.image .. '|link=|alt=]]')
end
box:tag('div'):addClass('d2-skill-name'):wikitext(a.name or mw.title.getCurrentTitle().text)
if given(a.class) then
local where = '[[' .. a.class .. ']]'
if given(a.tree) then
where = where .. ' · ' .. a.tree
end
box:tag('div'):addClass('d2-skill-class'):wikitext(where)
elseif given(a.classes) then
box:tag('div'):addClass('d2-skill-class'):wikitext('Item skill (' .. a.classes .. ')')
end
local desc = box:tag('div'):addClass('d2-skill-desc')
for _, line in ipairs(lines(a.description)) do
desc:tag('div'):wikitext(line)
end
local facts = box:tag('div'):addClass('d2-skill-facts')
if given(a.reqlevel) then
row(facts, 'Required level', a.reqlevel)
end
if given(a.prereqs) then
row(facts, 'Requires', links(a.prereqs))
end
if given(a.synergies) then
row(facts, 'Synergies', links(a.synergies))
end
if given(a.maxlvl) then
row(facts, 'Maximum level', a.maxlvl)
end
if given(a.hardcap) then
row(facts, 'Hard points', 'up to ' .. a.hardcap)
end
local fixed = lines(a.static)
local formulas = lines(a.formulas)
if #fixed + #formulas > 0 then
local block = box:tag('div'):addClass('d2-skill-lines')
for _, line in ipairs(fixed) do
block:tag('div'):wikitext(line)
end
for _, line in ipairs(formulas) do
block:tag('div'):addClass('d2-skill-formula'):wikitext(line)
end
end
return box
end
local function level_table(a)
local columns = cells(a.columns or '')
local rows = lines(a.levels)
if #rows == 0 or #columns == 0 then
return nil
end
local t = mw.html.create('table')
:addClass('wikitable mw-collapsible mw-collapsed d2-skill-levels')
t:tag('caption'):wikitext('Values per skill level')
local head = t:tag('tr')
head:tag('th'):wikitext('Level')
for _, c in ipairs(columns) do
head:tag('th'):wikitext(c)
end
for _, line in ipairs(rows) do
local tr = t:tag('tr')
local v = cells(line)
tr:tag('th'):wikitext(v[1])
for i = 1, #columns do
tr:tag('td'):wikitext(v[i + 1] or '')
end
end
return t
end
function p.skill(frame)
local a = frame:getParent().args
local out = frame:extensionTag('templatestyles', '', { src = 'Template:Skill/styles.css' })
.. tostring(infobox(a))
local t = level_table(a)
if t then
out = out .. tostring(t)
end
return out
end
return p