模組: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 = {}

-- 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 plain = {}
	local params = {
		[1] = {required = true, default = "code"},
		[""] = {alias_of = 1},
		["line"] = plain,
		["highlight"] = plain,
		["inline"] = {type = "boolean"},
		["class"] = plain,
		["style"] = plain,
	}
	
	local lang, args, text = process_params(frame.args, {
		["lang"] = plain
	}).lang
	
	if lang then
		args = process_params(frame:getParent().args, params)
		text = args[1]
	else
		insert(params, 1, {alias_of = "lang"})
		params["lang"] = {default = "text"}
		params[""].alias_of = 2
		args = process_params(frame:getParent().args, params)
		lang = args.lang
		text = args[2]
	end
	
	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

return export