Module Documentation
Note: portions of the module sample may not be visible without values provided.
Description
This module provides syntax highlighting functionality for wiki code blocks. This module is invoked by the {{Code}} template.
Credits
- Originally from: Genshin Impact Wiki – Module:Code
- Authors: Iluvredwall and Awesome Aasim
local p = {}
local lib = require('Dev:Feature')
local mTemplateStyles = require("Module:TemplateStyles")
function p.main(frame)
local getArgs = require("Module:Arguments").getArgs
local args = getArgs(frame, {wrapper='Template:Code'})
local out = p._main(args, frame)
return mTemplateStyles.wrap(frame, "Template:Code/styles.css", out)
end
function p._main(args, frame)
frame = frame or mw.getCurrentFrame()
local eval = tostring(args['eval']) == '1'
local block = tostring(args['block']) == '1'
local highlight = tostring(args['highlight']) == '1'
local detachEval = tostring(args.detachEval) == '1'
local all = tostring(args.all) == '1'
block = block or detachEval or all
eval = eval or detachEval or all
highlight = highlight or all
local source = lib.unstripNoWiki(args[1])
local codeArgs = {lang="text"}
if highlight then
codeArgs.lang = 'wikitext'
end
if not block then
codeArgs.inline = 1
codeArgs.class = 'code-inline'
else
codeArgs.class = 'code-block'
end
if detachEval then
-- force pre element to expand to full size of its div.mw-highlight wrapper
codeArgs.class = 'code-block-detached'
end
local out = mw.html.create()
out:wikitext(frame:extensionTag('syntaxhighlight', source, codeArgs))
-- always use syntaxhighlight (even when highlight isn't specified) since it
-- automatically escapes HTML syntax and escape codes
-- (e.g., users can input "­" instead of "­")
if eval then
local evaluated = frame:preprocess(source)
if not block then
out:wikitext(' ⇒ ', evaluated)
else
local evaluatedContainer = out:tag('div'):addClass('code-evaluated'):wikitext(evaluated)
if not detachEval then
-- collapse shared border and add vertical margin
evaluatedContainer:addClass('code-evaluated-attached')
return mw.html.create('div'):addClass('code-wrapper'):tag('div'):addClass('code-block-container'):node(out)
-- set display to prevent floats from decoupling the
-- widths of the wikitext and evaluatedContainer
end
end
end
return out
end
return p