Module:Nested Lists: Difference between revisions

ReisuDesign (talk | contribs)
No edit summary
ReisuDesign (talk | contribs)
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 16: Line 16:
wrapper = {'Template:Nested Lists'}
wrapper = {'Template:Nested Lists'}
})
})
return p._main(args)
return p._main(args)
end
end
Line 24: Line 23:
local link = args.link == '1'
local link = args.link == '1'
local iconFormat = args.icon
local iconFormat = args.icon
local iconMain = args.icon_main == '1'
local iconSize = args.icon_size or '25'
local iconSize = args.icon_size or '25'
-- Allow icon_size to be written as either 25 or 25px.
iconSize = mw.text.trim(tostring(iconSize))
iconSize = mw.ustring.gsub(iconSize, '[pP][xX]$', '')
if iconSize == '' then
iconSize = '25'
end


local output = {}
local output = {}
Line 38: Line 45:
-- Determine indentation marker.
-- Determine indentation marker.
local prefix = mw.ustring.sub(item, 1, 2)
local prefix = mw.ustring.sub(item, 1, 2)
if levels[prefix] ~= nil then
if levels[prefix] ~= nil then
marker = prefix
marker = prefix
Line 46: Line 52:
local level = levels[marker] or 0
local level = levels[marker] or 0


-- Check for [no] icon override.
-- [no] disables the icon for this item.
local noIcon = false
local noIcon = false
local customIcon = nil


if mw.ustring.sub(text, 1, 4) == '[no]' then
if mw.ustring.sub(text, 1, 4) == '[no]' then
noIcon = true
noIcon = true
text = mw.text.trim(mw.ustring.sub(text, 5))
text = mw.text.trim(mw.ustring.sub(text, 5))
else
-- [Icon Name.png] specifies a custom icon.
local customIconName, remainingText =
mw.ustring.match(text, '^%[([^%]]+)%]%s*(.*)$')
if customIconName then
customIcon = mw.text.trim(customIconName)
text = mw.text.trim(remainingText)
end
end
end


-- The raw item text is kept separately.
-- Separate page target and display label.
local itemText = text
local target = text
local label = text
local separator = mw.ustring.find(text, '|', 1, true)


-- Check whether the page exists.
if separator then
target = mw.text.trim(mw.ustring.sub(text, 1, separator - 1))
label = mw.text.trim(mw.ustring.sub(text, separator + 1))
end
 
-- Check whether the target page exists.
local pageExists = false
local pageExists = false


if link then
if link and target ~= '' then
pageExists = exists.checkExists(itemText)
pageExists = exists.checkExists(target)
end
end


Line 67: Line 90:
local icon = ''
local icon = ''


if iconFormat and iconFormat ~= '' and not noIcon then
if not noIcon then
local iconName = string.gsub(
local iconName = customIcon
iconFormat,
'{item}',
itemText
)


if pageExists then
-- Use the global icon format unless icon_main is enabled
icon = string.format(
-- and this is not a level 0 item.
'[[File:%s|%spx|link=%s]]',
if not iconName and iconFormat and iconFormat ~= '' and (not iconMain or level == 0) then
iconName,
iconName = mw.ustring.gsub(
iconSize,
iconFormat,
itemText
'{item}',
)
function()
else
return target
icon = string.format(
end
'[[File:%s|%spx]]',
iconName,
iconSize
)
)
end
if iconName and iconName ~= '' then
-- Check whether the requested icon exists.
if not exists.checkExists('File:' .. iconName) then
iconName = 'Unknown.png'
end
-- Link the icon to the item page if it exists.
if pageExists then
icon = string.format(
'[[File:%s|%spx|link=%s]]',
iconName,
iconSize,
target
)
else
icon = string.format(
'[[File:%s|%spx]]',
iconName,
iconSize
)
end
end
end
end
end


-- Generate the text link.
-- Generate the text.
local displayText = itemText
local displayText = label


if pageExists then
if pageExists then
displayText = '[[' .. itemText .. ']]'
displayText = '[[' .. target .. '|' .. label .. ']]'
end
end


-- Add icon and text.
-- Combine icon and text.
local content = displayText
local content = displayText