Jump to content

Module:Nested Lists

From The Petit Planet Wiki
Revision as of 10:58, 3 September 2026 by 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....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 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

			-- Check for 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

			-- Link the item if requested and the page exists.
			if link and exists.checkExists(text) then
				text = '[[' .. text .. ']]'
			end

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

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

return p