Jump to content

Module:Nested Lists: Difference between revisions

From The Petit Planet Wiki
ReisuDesign (talk | contribs)
Created page with "local p = {} local exists = require('Module:Exists') local levels = { [''] = 0, ['//'] = 1, ['$$'] = 2, ['%%'] = 3, ['&&'] = 4, } function p.main(frame) local args = require('Module:Arguments').getArgs(frame, { parentFirst = true, wrapper = {'Template:Nested Lists'} }) return p._main(args) end function p._main(args) local input = args[1] or '' local link = args.link == '1' local output = {} -- Each semicolon separates an item. for item in mw.text...."
 
ReisuDesign (talk | contribs)
No edit summary
Line 23: Line 23:
local input = args[1] or ''
local input = args[1] or ''
local link = args.link == '1'
local link = args.link == '1'
local iconFormat = args.icon
local iconSize = args.icon_size or '25'


local output = {}
local output = {}
Line 34: Line 36:
local text = item
local text = item


-- Check for indentation marker.
-- Determine indentation marker.
local prefix = mw.ustring.sub(item, 1, 2)
local prefix = mw.ustring.sub(item, 1, 2)


Line 44: Line 46:
local level = levels[marker] or 0
local level = levels[marker] or 0


-- Link the item if requested and the page exists.
-- Check for [no] icon override.
if link and exists.checkExists(text) then
local noIcon = false
text = '[[' .. text .. ']]'
 
if mw.ustring.sub(text, 1, 4) == '[no]' then
noIcon = true
text = mw.text.trim(mw.ustring.sub(text, 5))
end
 
-- The raw item text is kept separately.
local itemText = text
 
-- Check whether the page exists.
local pageExists = false
 
if link then
pageExists = exists.checkExists(itemText)
end
 
-- Generate the icon.
local icon = ''
 
if iconFormat and iconFormat ~= '' and not noIcon then
local iconName = mw.text.gsub(
iconFormat,
'{item}',
itemText
)
 
if pageExists then
icon = string.format(
'[[File:%s|%spx|link=%s]]',
iconName,
iconSize,
itemText
)
else
icon = string.format(
'[[File:%s|%spx]]',
iconName,
iconSize
)
end
end
 
-- Generate the text link.
local displayText = itemText
 
if pageExists then
displayText = '[[' .. itemText .. ']]'
end
 
-- Add icon and text.
local content = displayText
 
if icon ~= '' then
content = icon .. ' ' .. displayText
end
end


table.insert(
table.insert(
output,
output,
string.rep('*', level + 1) .. ' ' .. text
string.rep('*', level + 1) .. ' ' .. content
)
)
end
end

Revision as of 11:20, 3 September 2026

Module documentation

This module implements {{Nested Lists}}.

local p = {}

local exists = require('Module:Exists')

local levels = {
	[''] = 0,
	['//'] = 1,
	['$$'] = 2,
	['%%'] = 3,
	['&&'] = 4,
}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		wrapper = {'Template:Nested Lists'}
	})

	return p._main(args)
end

function p._main(args)
	local input = args[1] or ''
	local link = args.link == '1'
	local iconFormat = args.icon
	local iconSize = args.icon_size or '25'

	local output = {}

	-- Each semicolon separates an item.
	for item in mw.text.gsplit(input, ';', true) do
		item = mw.text.trim(item)

		if item ~= '' then
			local marker = ''
			local text = item

			-- Determine indentation marker.
			local prefix = mw.ustring.sub(item, 1, 2)

			if levels[prefix] then
				marker = prefix
				text = mw.text.trim(mw.ustring.sub(item, 3))
			end

			local level = levels[marker] or 0

			-- Check for [no] icon override.
			local noIcon = false

			if mw.ustring.sub(text, 1, 4) == '[no]' then
				noIcon = true
				text = mw.text.trim(mw.ustring.sub(text, 5))
			end

			-- The raw item text is kept separately.
			local itemText = text

			-- Check whether the page exists.
			local pageExists = false

			if link then
				pageExists = exists.checkExists(itemText)
			end

			-- Generate the icon.
			local icon = ''

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

				if pageExists then
					icon = string.format(
						'[[File:%s|%spx|link=%s]]',
						iconName,
						iconSize,
						itemText
					)
				else
					icon = string.format(
						'[[File:%s|%spx]]',
						iconName,
						iconSize
					)
				end
			end

			-- Generate the text link.
			local displayText = itemText

			if pageExists then
				displayText = '[[' .. itemText .. ']]'
			end

			-- Add icon and text.
			local content = displayText

			if icon ~= '' then
				content = icon .. ' ' .. displayText
			end

			table.insert(
				output,
				string.rep('*', level + 1) .. ' ' .. content
			)
		end
	end

	return table.concat(output, '\n')
end

return p