Modul:Code

ZIM HilfeWiki - das Wiki
Documentation icon Module documentation

This module implements template {{Code}} and {{Code snippet}}.

Usage[Quelltext bearbeiten]

{{#invoke:Code|main}}


--
-- This Module implements {{Code}} and {{Code snippet}}
--

require('Module:No globals')

local p = {}

local getArgs = require('Module:Arguments').getArgs

local function processArgs(args)
	
	local content = args.code or args[2] or 'sample text'
	local processedArgs = {
		-- The lang="name" attribute defines what lexer should be used. 
		lang = args.lang or args[1] or 'text'
	}
	if args.line then
		-- The line attribute enables line numbers.
		processedArgs.line = 1
		if args.start ~= nil then
			-- The start attribute (in combination with line) defines the first line number of the code block.
			processedArgs.start = args.start
		end
	end
	if args.highlight then
		-- The highlight attribute specifies one or more lines that should be marked (by highlighting those lines with a different background color).
		processedArgs.highlight = args.highlight
	end
	if args.inline then
		-- The attribute indicates that the source code should be inline as part of a paragraph (as opposed to being its own block).
		processedArgs.inline = 1
	end
	if args.class then
		-- The css class to use. When inline is used, class="nowrap" (on those wikis that support it; not on MediaWiki itself) specifies that line breaks should not occur at spaces within the code block. 
		processedArgs.class = args.class
	end
	if args.style then
		-- CSS styles to add
		processedArgs.style = args.style
	end

	return content, processedArgs
end

local function render(frame, args)

	local content, tagArgs = processArgs(args)
	return frame:extensionTag{
		name = 'syntaxhighlight', content = content, args = tagArgs
	}
end


function p.main(frame)

	local args
	local cFrame = mw.getCurrentFrame()
	if frame == cFrame then
		-- module called via invoke
		args = getArgs(frame)
	else
		-- used by other modules directly
		args = frame
		frame = cFrame
	end
	
	return render(frame, args)
end

return p
Cookies helfen uns bei der Bereitstellung des ZIM HilfeWikis. Bei der Nutzung vom ZIM HilfeWiki werden die in der Datenschutzerklärung beschriebenen Cookies gespeichert.