Pergi ke kandungan

Modul:demo

Daripada Wikikamus

local export = {}

local string_remove_comments_module = "Module:string/removeComments"
local string_utilities_module = "Module:string utilities"

local require = require
local tonumber = tonumber
local unstrip_nowiki = mw.text.unstripNoWiki

local function remove_comments(...)
	remove_comments = require(string_remove_comments_module)
	return remove_comments(...)
end

-- Ported from [[w:Module:Demo]] by Benwing2 on Sep 1st 2023 around 6am UTC.

local nowiki_strip_marker_pattern = "\127'\"`UNIQ%-%-nowiki%-" .. ("[%dA-F]"):rep(8) .. "%-QINU`\"'\127"

local nowiki_replacements = {
	["gt"] = ">",
	["lt"] = "<",
	["#123"] = "{",
	["#125"] = "}",
}

function export.main(frame)
	local boolean = {type = "boolean"}
	local args = require("Module:parameters").process(frame:getParent().args, {
		[1] = true,
		["fmt"] = {
			set = {
				["compact"] = true, ["c"] = "compact",
				["inline"] = true, ["i"] = "inline", ["1"] = "inline",
				["multiline"] = true, ["m"] = "multiline",
				["raw"] = true,
				["twoline"] = true, ["2"] = "twoline",
				["twocol"] = true, ["2c"] = "twocol", ["2col"] = "twocol",
			},
			default = frame.args.fmt or "compact"
		},
		["br"] = true,
		["fullsep"] = true,
		["sep"] = true,
		["reverse"] = boolean,
		["nocat"] = boolean,
		["style"] = true,
		["omit"] = {sublist = true, default = "pagename"},
		["indent"] = {default = ""},
	})

	local source = args[1]
	if source:match(nowiki_strip_marker_pattern) then
		-- Unescape the four patterns that automatically get escaped by nowiki
		-- (taken from CoreTagHooks.php).
		source = unstrip_nowiki(source)
		source = source:gsub("()&([#gl][1t]2?[35]?);", function(pos, code)
			local ch = nowiki_replacements[code]
			if ch == "{" then -- -{
				return source:byte(pos - 1) == 0x2D and ch or nil
			elseif ch == "}" then -- }-
				return source:byte(pos + 6) == 0x2D and ch or nil
			end
			return ch
		end)
	end

	local output = remove_comments(frame:preprocess(source), "POST")
	-- FIXME: this will miss some categories.
	if args.nocat then
		output = output:gsub("%[%[Kategori.-%]%]", "")
	end
	for _, param in ipairs(args.omit) do
		if param ~= "-" then
			-- FIXME, this doesn't correctly handle nested template calls or HTML comments in the param value
			source = source:gsub(("| *%s *=[^{}|]*"):format(require(string_utilities_module).pattern_escape(param)), "")
		end
	end

	local fullsep, sep, fmt, rev, indent = args.fullsep, args.sep, args.fmt, args.reverse, args.indent
	if indent == "-" then
		indent = ""
	end
	local newline = indent == "" and "<br />" or "\n"
	if not fullsep then
		local br = args.br
		if br then
			fullsep = tonumber(br) and ("<br />"):rep(br) or br
		elseif fmt == "inline" or fmt == "twoline" then
			fullsep = (sep or (rev and "⇐" or "⇒")) .. (fmt == "twoline" and newline .. "\n" or "")
		elseif fmt == "raw" then
			fullsep = sep or ""
		elseif fmt == "twocol" then
			fullsep = sep or "||"
		else
			fullsep = (sep or (rev and "generated from" or "which produces")) .. "<br />\n"
			if fmt == "compact" then
				fullsep = newline .. fullsep
			end
		end
	end

	if #fullsep > 0 and not (fullsep:find("\n", nil, true) or fullsep:match("<br%f[%W].->")) then
		fullsep = " " .. fullsep .. " "
	end

	source = frame:extensionTag("syntaxhighlight", source, {
		lang = "wikitext",
		inline = (fmt == "inline" or fmt == "twoline" or fmt == "compact" or fmt == "twocol") and true or nil,
		style = args.style
	})

	return indent .. (rev and output .. fullsep .. source or source .. fullsep .. output)
end

return export