Module:UtilsMarkup/Format

local p = {}

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

function p.bold(text) return tag("b")(text) end

function p.caption(text) return p.class("caption", text) end

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

function p.code(text) return " " end

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

function p.italic(text) return tag("i")(text) end

function p.inline(text, options) options = options or {} if options.nowiki then text = mw.text.nowiki(text) end if options.caption then text = p.caption(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 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.pre(text, options) options = options or {} local attrs = {} local wrapLines = options and options.wrapLines if wrapLines == false then attrs.style = "white-space: pre;" end return extensionTag("pre", attrs)(text) end

function p.syntaxHighlight(lang, args) local args = utilsTable.merge({}, args or {}, {		lang = lang	}) return extensionTag("syntaxHighlight", args) 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

return p