Module:UtilsMarkup/List

local p = {}

local utilsTable = require("Module:UtilsTable")

function p.list(items) return table.concat(items, "\n\n") .. "\n" end

local function tag(tag, content, attributes) if not tag then return content end if attributes or styles then return tostring(mw.html.create(tag)			:attr(attributes)			:wikitext(mw.getCurrentFrame:preprocess(content))		) end return ("%s"):format(tag, content, tag) end

local function tagList(listTag, itemTag, level) level = level or 1 return function(items) local listAttributes = { start = items.start } if #items == 0 then return "" end local list = "" for _, item in ipairs(items) do			if utilsTable.isArray(item) then list = list .. tagList(listTag, itemTag, level + 1)(item) else list = list .. tag(itemTag, item) end end return tag(listTag, list, listAttributes) .. (level == 1 and "\n" or "") end end

p.bulletList = tagList("ul", "li") p.numberList = tagList("ol", "li")

function p.definitionList(items, level) level = level or 1 local listItems = {} for _, pair in ipairs(items) do		local key = pair[1] local value = pair[2] if type(value) == "table" then value = p.definitionList(value, level + 1) end table.insert(listItems, tag("dt", key or "")) table.insert(listItems, tag("dd", value or "")) end local list = tagList("dl", nil, level)(listItems) return list end

p.Documentation = { {		name = "list", params = { {				name = "items", description = "list items", }		},		returns = "A multiline string with each item on its own line.", cases = { {				args = { {} }, expected = "\n", },			{				args = { { "single item" } },				expected = 					single item				, },			{				args = { { "multiple", "items", "" }, },				expected = 					multiple					items },		},	},	{		name = "bulletList", params = { {				name = "items", description = "list items", },		},		returns = "A string representation of an unordered list using HTML syntax", cases = { {				args = { {} }, expected = "", },			{				args = { { "single item" } },				expected = "single item\n", },			{				args = { { "multiple", "items", "" }, },				expected = "multipleitems\n", },			{				args = { {						"list", {							"with", {"nested", "items"}, "inside" },					},				},				expected = "listwith</li>nested</li>items</li></ul>inside</li></ul></ul>\n" },		}	},	{		name = "numberList", params = { {				name = "items", description = "list items", },		},		returns = "A string representation of an ordered list using HTML syntax", cases = { {				args = { {} }, expected = "", },			{				args = { { "single item" } },				expected = "<ol>single item</li></ol>\n", },			{				args = { { "multiple", "items", "" }, },				expected = "<ol>multiple</li>items</li></li></ol>\n" },			{				args = { {						"list", {							"with", {"nested", "items"}, "inside" },					},				},				expected = "<ol>list</li><ol>with</li><ol>nested</li>items</li></ol>inside</li></ol></ol>\n" },			{				args = { {						start = 8, "Eight", "Nine", "Ten" },				},				expected = ' <li>Eight</li><li>Nine</li><li>Ten</li></ol>\n', },		}	},	{		name = "definitionList", params = { {				name = "pairs", description = "Array of  of pairs where the first pair item is a term and the value is a definition", },		},		returns = "A string representation of a definition list using HTML syntax", cases = { {				args = { {} }, expected = "", },			{				args = { {						{ "key1", "value1" }, }				},				expected = "<dl><dt>key1</dt><dd>value1</dd></dl>\n", },			{				args = { {						{ "key1", "value1" }, { "key2", "value2" }, }				},				expected = "<dl><dt>key1</dt><dd>value1</dd><dt>key2</dt><dd>value2</dd></dl>\n", },			{				args = { {						{ "", "value1" }, { nil, "value2" }, { "key3", "" }, { "key4", nil }, },				},				expected = "<dl><dt></dt><dd>value1</dd><dt></dt><dd>value2</dd><dt>key3</dt><dd></dd><dt>key4</dt><dd></dd></dl>\n" },			{				args = { {						{ "key 1", { {"key 1.1", { { "key 1.1.1", "value 1.1.1" }, }},							{"key 1.2", "value 1.2" }, }},						{ "key2", "value2" }, }				},				expected = "<dl><dt>key 1</dt><dd><dl><dt>key 1.1</dt><dd><dl><dt>key 1.1.1</dt><dd>value 1.1.1</dd></dl></dd><dt>key 1.2</dt><dd>value 1.2</dd></dl></dd><dt>key2</dt><dd>value2</dd></dl>\n" }		}	} } return p