Module:UtilsMarkup/Format

local p = {}

local utilsArg = require("Module:UtilsArg") local utilsTable = require("Module:UtilsTable")

local function tag(tag, attributes, styles) return function(content) return tostring(mw.html.create(tag)			:attr(attributes or {})			:css(styles 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.bold = tag("b") p.italic = tag("i")

function p.code(text) return " " end

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.tooltip(baseText, tooltipText, type) local attrs = { class = "explain", title = tooltipText, }	local styles = {} if type == "highlight" then styles.color = "yellow" end return tag("span", attrs, styles)(baseText) 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.inline(options) local err = utilsArg.schemaValidate(p.Schemas.inline.options, "inline.options", options, "options") err = err and ("" .. err .. "") or "" options = options or {} return function(text) if options.nowiki then text = mw.text.nowiki(text) end if options.code then text = p.code(text) end if options.italic then text = p.italic(text) end if options.bold then text = p.bold(text) end if options.tooltip then text = p.tooltip(text, options.tooltip) end if options.class then text = p.class(options.class)(text) end return text .. err end end

p.Schemas = { bold = { text = { type = "string", required = true, desc = "The text to bold." },	},	code = { text = { type = "string", required = true, desc = "The text to render monospaced." },			},	italic = { text = { type = "string", required = true, desc = "The text to italicize." },		},	inline = { text = { type = "string", required = true, }, options = { type = "record", desc = "Formatting options are applied in the reverse order in which they are listed below. In other words,  is always the outermost tag and   is always the innermost.", properties = { {					name = "nowiki", type = "boolean", },				{					name = "code", type = "boolean", },				{					name = "class", type = "string", },				{					name = "tooltip", type = "string", },				{					name = "bold", type = "boolean", },				{					name = "italic", type = "boolean", },			},		},	},	heading = { level = { type = "number", required = true, desc = "The heading level." ,		},		text = { type = "string", required = true, desc = "The heading text.", },	},	tooltip = { baseText = { type = "string", required = true, desc = "The text to receive a tooltip.", },		tooltipText = { type = "string", required = true, desc = "The text to display on hover.", },		type = { type = "string", enum = {"highlight"}, desc = 'If, colors the base text to make the presence of tooltip text more evident.', },	},	lua = { text = { type = "string", required = true, desc = "A string of text to format as Lua code" },	} }

p.Documentation = { {		name = "bold", params = {"text"}, returns = "The bolded text.", cases = { {				args = { "Fooloo Limpah" }, expect = "Fooloo Limpah", },		},	},	{		name = "italic", params = {"text"}, returns = "The italicized text.", cases = { {				args = { "Fooloo Limpah" }, expect = "Fooloo Limpah", },		},	},	{		name = "class", order = 2, params = {{"class"}, {"text"}}, returns = "Text wrapped in a span tag with the given class string.", cases = { {				args = {{"term error"}, {"Fooloo Limpah"}}, expect = Fooloo Limpah },		},	},	{		name = "heading", order = 2, params = {{"level"}, {"text"}}, returns = " of text for the heading", cases = { {				args = { {2}, {"Section"} }, expect = "\n==Section==\n", },			{				args = { {3}, {"Sub-section"} }, expect = "\n===Sub-section===\n" }		}	},	{		name = "tooltip", params = {"baseText", "tooltipText", "type"}, cases = { {				args = {"hover over me", "hello world!"}, expect = 'hover over me ' },			{				args = {"foo", "bar", "highlight"}, expect = ' foo ' },		},	},	{		name = "code", params = {"text"}, returns = "The formatted text.", cases = { {				args = {"code stuff"}, expect = " ", },		},	},	{		name = "inline", order = 2, params = {{"options"}, {"text"}}, returns = "The formatted text.", cases = { {				args = { {						{ 							class = "error", tooltip = "Don't steal them!", bold = true, italic = true, },					},					{"Fooloo Limpah"}, },				expect = Fooloo Limpah  , },			{				args = { {						{							code = true, nowiki = true, bold = true, },					},					{"Fooloo Limpah"}, },			},			{				args = { {						{ italic= true, invalidOption = "foo"}, },					{"Fooloo Limpah"}, },				expect = "Fooloo Limpah", },		},	},	{		name = "lua", resultOnly = true, params = {"text"}, returns = "A block of pre-formatted, syntax-highlighted Lua code", cases = { {				args = { function(foo) 	return foo end }, }		}	}, }

return p