A documentação para este módulo pode ser criada na página Módulo:code/doc
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 = {
= {required = true, default = "code"},
= {alias_of = 1},
= plain,
= plain,
= {type = "boolean"},
= plain,
= plain,
}
local lang = process_params(frame.args, {
= 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
end
params = {default = "text"}
-- If 2= or "=..." are given, treat 1= as an alias of lang=.
if args or args then
insert(params, 1, {alias_of = "lang"})
params.alias_of = 2
args = process_params(args, params)
return args, args.lang, args
end
-- Otherwise, 1= is just the input text.
args = process_params(args, params)
return args, args.lang, args
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(gsub(text, "\127'\"`UNIQ%-%-nowiki%-+%-QINU`\"'\127", unstripNoWiki)), {
lang = lang,
line = line,
start = start,
highlight = highlight,
inline = inline,
class = args.class,
style = args.style
})
end
end
return export