Module:Nested Lists: Difference between revisions

ReisuDesign (talk | contribs)
No edit summary
ReisuDesign (talk | contribs)
No edit summary
Line 25: Line 25:
local iconFormat = args.icon
local iconFormat = args.icon
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 46: Line 54:
local level = levels[marker] or 0
local level = levels[marker] or 0


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


Line 54: Line 62:
end
end


-- The raw item text is kept separately.
-- Separate page target and display label.
local itemText = text
--
-- Example:
-- Princess Peach|Peach
--
-- target = Princess Peach
-- label  = Peach
local target = text
local label = text
 
local separator = mw.ustring.find(text, '|', 1, true)
 
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 page exists.
-- 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 68: Line 90:


if iconFormat and iconFormat ~= '' and not noIcon then
if iconFormat and iconFormat ~= '' and not noIcon then
local iconName = string.gsub(
-- {item} always uses the raw page target,
-- never the generated wiki link or display label.
local iconName = mw.ustring.gsub(
iconFormat,
iconFormat,
'{item}',
'{item}',
itemText
function()
return target
end
)
)


Line 79: Line 105:
iconName,
iconName,
iconSize,
iconSize,
itemText
target
)
)
else
else
Line 90: Line 116:
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