Module:UtilsMarkup/Format

local p = {} local h = {}

local utilsFunction = require("Module:UtilsFunction") local utilsTable = require("Module:UtilsTable")

local function tag(tag, attributes, styles) return function(content) return tostring(mw.html.create(tag)			:attr(attributes or {})			:wikitext(content)) end end local function extensionTag(tag, defaultArgs) return function(content, overrideArgs) local args = utilsTable.merge({}, defaultArgs or {}, overrideArgs or {}) return mw.getCurrentFrame:extensionTag(tag, content, args) .. "\n" -- tags like don't work properly without the newline end end

p.pre = extensionTag("pre", nil) p.nowiki = extensionTag("nowiki") p.code = extensionTag("code") p.bold = tag("b") p.italic = tag("i")

function p.syntaxHighlight(lang, args) local args = utilsTable.merge({}, args or {}, {		lang = lang	}) return extensionTag("syntaxHighlight", args) end

function p.lua(content, options) local wrapLines = options and options.wrapLines local attrs = {} if type(content) ~= "string" then content = utilsTable.print(content) end if wrapLines == false then attrs.class = "nowrapLines" end return p.syntaxHighlight("lua")(content or "nil", attrs) end

function p.explain(caption) return tag("span", {		class = "explain",		title = caption,	}) end

function p.class(class) return tag("span", { class = class }) end

function p.heading(level) local headingTag = string.rep("=", level) return function(content) return "\n" .. headingTag .. content .. headingTag .. "\n" end end

function p.font(options) options = options or {} return utilsFunction.pipe(		options.italic and p.italic or utilsFunction.identity,		options.bold and p.bold or utilsFunction.identity	) end

p.Schemas = { fontOptions = { type = "record", properties = { {				name = "bold", type = "boolean", },			{				name = "italic", type = "boolean", }		}	} }

p.Documentation = { {		name = "bold", params = { {				name = "text", description = "The text to render bold.", },		},		returns = "The bolded text.", cases = { args = { "Fooloo Limpah" }, expected = "Fooloo Limpah", },	},	{		name = "italic", params = { {				name = "text", description = "The text to italicize.", },		},		returns = "The italicized text.", cases = { {				args = { "Fooloo Limpah" }, expected = "Fooloo Limpah", },		},	},	{		name = "font", order = 2, params = { {				{					name = "options", description = "Formatting options.", schema = p.Schemas.fontOptions, },				},			{				{					name = "text", description = "The text to format", },			},		},		returns = "The formatted text.", cases = { {				args = { { 						{ bold = true, italic = true } }, 					{ "Fooloo Limpah" }, },				expected = "Fooloo Limpah", },		},	},	{		name = "heading", order = 2, params = { {				{ name = "level", description = "The heading level, a " }, },			{				{ name = "text", description = "The heading text, a " }, }		},		returns = " of text for the heading", cases = { {				args = { {2}, {"Section"} }, expected = "\n==Section==\n", },			{				args = { {3}, {"Sub-section"} }, expected = "\n===Sub-section===\n" }		}	},	{		name = "lua", resultOnly = true, params = { {				name = "text", description = "A string of text to format as Lua code", }		},		returns = "A block of pre-formatted, syntax-highlighted Lua code", cases = { {				args = { function(foo) 	return foo end }, }		}	} }

return p