Пређи на садржај

Модул:code

Документацију овог модула можете да направите на страници Модул:code/док

local decode_entities = require("Module:string utilities").decode_entities
local gsub = string.gsub
local insert = table.insert
local match = string.match
local process_params = require("Module:parameters").process
local tonumber = tonumber
local unstripNoWiki = mw.text.unstripNoWiki
local yesno = require("Module:yesno")

local export = {}

do
	local function get_args(frame)
		local plain = {}
		local params = {
			[1] = {required = true, default = "code"},
			[""] = {alias_of = 1},
			["line"] = plain,
			["highlight"] = plain,
			["inline"] = {type = "boolean"},
			["class"] = plain,
			["style"] = plain,
		}
		
		local lang = process_params(frame.args, {
			["lang"] = plain
		}).lang
		local args = frame:getParent().args
		
		-- Specialised language templates (e.g. {{lua}}).
		if lang then
			args = process_params(args, params)
			return args, lang, args[1]
		end
		
		params["lang"] = {default = "text"}
		
		-- If 2= or "=..." are given, treat 1= as an alias of lang=.
		if args[2] or args[""] then
			insert(params, 1, {alias_of = "lang"})
			params[""].alias_of = 2
			args = process_params(args, params)
			return args, args.lang, args[2]
		end
		
		-- Otherwise, 1= is just the input text.
		args = process_params(args, params)
		return args, args.lang, args[1]
	end
	
	-- Unstrip nowiki tags and decode any HTML entities, because
	-- syntaxhighlight won't decode them on display.
	local function unstrip(str)
		return decode_entities(unstripNoWiki(str))
	end
	
	function export.show(frame)
		local args, lang, text = get_args(frame)
		
		lang = lang == "js" and "javascript" or lang == "py" and "python" or lang
		
		local inline, line, start, highlight = args.inline
		if not inline then
			line = args.line
			if line then
				start = match(line, "^%d+$")
				if not start then
					line = yesno(line) or nil
				end
			end
			highlight = args.highlight
			if start then
				local offset = tonumber(start) - 1
				highlight = gsub(highlight, "%d+", function(n)
					return tonumber(n) - offset
				end)
			end
			inline = inline == nil and not (line or highlight) or nil
		end
		
		return frame:extensionTag(
			"syntaxhighlight",
			gsub(text, "\127'\"`UNIQ%-%-nowiki%-[%dA-F]+%-QINU`\"'\127", unstrip), {
			lang = lang,
			line = line,
			start = start,
			highlight = highlight,
			inline = inline,
			class = args.class,
			style = args.style
		})
	end
end

return export