Module:Code blocks

--

-- A demonstration of various methods to create code blocks in Scribunto.

local p = {} local getArgs = require("Dev:Arguments").getArgs local userError = require("Dev:User error")

-- A tag created with pure wikitext, which automatically preprocesses -- anything inside. Created by inserting a space character at the beginning of -- every line. Cannot recieve classes or inline CSS.

function p.definitely_unescaped_pre(frame) local args = getArgs(frame) local code = args[1]

if not code then return userError("no code provided") end

return " " .. table.concat(mw.text.split(code, "\n"), "\n ") end

-- Scribunto-exclusive. A "raw" tag, which DOESN'T disable preprocessing. -- Can recieve classes and inline CSS.

function p.possibly_unescaped_pre(frame) local args = getArgs(frame) local code = args[1]

if not code then return userError("no code provided") end

local code_block = mw.html.create("pre") :wikitext(code)

return tostring(code_block) end

-- The "magical" tag, which automatically disables preprocessing. -- Functionally equivalent to starting a tag immediately after a space -- at the beginning of a line. Can recieve classes and inline CSS.

function p.escaped_pre(frame) local args = getArgs(frame) local code = args[1]

if not code then return userError("no code provided") end

local code_block = mw.html.create("pre") :wikitext(code)

return frame:preprocess(tostring(code_block)) end

-- A tag, for comparison. Automatically disables -- preprocessing. Can recieve classes and inline CSS, but also has special -- functionality which might effect results.

function p.syntaxhighlight(frame) local args = getArgs(frame) local code = args[1]

if not code then return userError("no code provided") end

local code_block = mw.html.create("syntaxhighlight") :attr("lang", "lua") :wikitext(code)

return frame:preprocess(tostring(code_block)) end

return p