genshin impact
Documentation icon Module documentation
[view] [edit] [history] [purge]

This module implements {{Code}}.

local p = {}

local lib = require('Module:Feature')

function p.main(frame)
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame, {wrapper='Template:Code'})
	return p._main(args, frame)
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'
	block = block or detachEval
	eval = eval or detachEval
	
	local source = lib.unstripNoWiki(args[1])
	
	local codeArgs = {lang="text"}
	if highlight then 
		codeArgs.lang = 'html+handlebars' -- there's no mediawiki lang support yet
	end
	if not block then
		codeArgs.inline = 1
	end
	if detachEval then
		-- force pre element to expand to full size of its div.mw-highlight wrapper
		codeArgs.style = "display: grid"
	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'):wikitext(evaluated)
			evaluatedContainer:css{
				border = '1px solid #4C5067', -- var(--theme-border-color)
				padding = '6px',
			}
			if not detachEval then
				-- collapse shared border and add vertical margin
				evaluatedContainer:css('border-top', 'none')
				return mw.html.create('div'):node(out)
					:css{
						margin='.8em 0',
						-- set display to prevent floats from decoupling the
						-- widths of the wikitext and evaluatedContainer
						display='flow-root'
					}
    		end
		end
	end
	return out
end

return p