This module implements the template {{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 = {}

local function get_args(frame)
	local params = {
		[1] = {required = true, default = "code"},
		[""] = {alias_of = 1},
		["line"] = true,
		["highlight"] = true,
		["inline"] = {type = "boolean"},
		["class"] = true,
		["style"] = true,
	}
	
	local lang = process_params(frame.args, {
		["lang"] = true
	}).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

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
	
	-- Unstrip nowiki tags and decode any HTML entities, because
	-- syntaxhighlight won't decode them on display.
	return frame:extensionTag(
		"syntaxhighlight",
		decode_entities(unstripNoWiki(text)), {
		lang = lang,
		line = line,
		start = start,
		highlight = highlight,
		inline = inline,
		class = args.class,
		style = args.style or inline and "white-space:pre-wrap;" or nil
	})
end

return export
"https://si.wiktionary.org/w/index.php?title=Module:code&oldid=183502" වෙතින් සම්ප්‍රවේශනය කෙරිණි