跳转到内容

模組:NoticeBoxUtil

维基百科,自由的百科全书

这是本页的一个历史版本,由A2569875留言 | 贡献2024年1月7日 (日) 23:19编辑。这可能和当前版本存在着巨大的差异。

local p = {}
local lib_doc = require("Module:Documentation")

function p.processInput(input_data, default_value)
	local retult = input_data
	if type((input_data or {}).args) ~= type(nil) then --input_data is a frame
		retult = input_data.args['1'] or input_data.args[1] or default_value
	end
	return retult
end

function p.makeSandboxTitle(input_data, page_tail)
	local str_page_tail = page_tail or "sandbox"
	local re_page_tail = "/"..str_page_tail.."$"
	local text = p.processInput(input_data, "")
	local i, j = mw.ustring.find(text, re_page_tail)
	i = i or -1
	i = mw.ustring.sub(text, i, i) == '/' and (i-1) or i
	return mw.ustring.sub(text, 1, i).."/"..str_page_tail
end

local function path_escape(text)
	local result = text:gsub("%%","%%%%")
	result = result:gsub("%+","%%+")
	result = result:gsub("%-","%%-")
	result = result:gsub("%*","%%*")
	result = result:gsub("%.","%%.")
	return result
end

function p.makeSandboxNotice(input_data, input_page_title)
	local current_title_name = input_data
	local page_title = input_page_title
	if type((input_data or {}).args) ~= type(nil) then --input_data is a frame
		current_title_name = input_data.args['1'] or input_data.args[1]
		page_title = input_data.args['2'] or input_data.args[2] or input_data.page_title or input_data['page title']
	end
	local current_title = (type(current_title_name) ~= type(nil)) and ({pcall(mw.title.new, current_title_name)})[2] or mw.title.getCurrentTitle()
	local page_title = page_title and ({pcall(mw.title.new, page_title)})[2]
	if type(page_title) == type("string") then page_title = nil end
	if current_title:inNamespace(mw.title.new("title").namespace) then
		current_title = current_title.talkPageTitle
	end
	local sandbox_title = p.makeSandboxTitle(current_title.fullText)
	local lib_doc = require("Module:Documentation")
	local args = {page=sandbox_title}
	local env = lib_doc.getEnvironment(args)
	local result = lib_doc.sandboxNotice(args, env)
	if page_title then
		local re_orig_path = path_escape(tostring(mw.uri.fullUrl('Special:ComparePages',
			{page1 = "A", page2 = sandbox_title})):match("page2%=(.+)$"))
		local rp_result_path = tostring(mw.uri.fullUrl('Special:ComparePages',
			{page1 = "A", page2 = page_title.fullText})):match("page2%=(.+)$"):gsub("%%","%%%%")
		result = mw.ustring.gsub(result, re_orig_path, rp_result_path)
	end
	return result
end

function p.makeProtectionNotice(input_data)
	local current_title_name = input_data
	if type((input_data or {}).args) ~= type(nil) then --input_data is a frame
		current_title_name = input_data.args['1'] or input_data.args[1]
	end
	local current_title = (type(current_title_name) ~= type(nil)) and ({pcall(mw.title.new, current_title_name)})[2] or mw.title.getCurrentTitle()
	
	local lib_doc = require("Module:Documentation")
	local args = {page=current_title.fullText}
	local env = lib_doc.getEnvironment(args)
	local result = lib_doc.protectionTemplate(env)
	return result or ""
end

function p.compactNotice(input_data)
	local wikitext = input_data or ""
	if type((input_data or {}).args) ~= type(nil) then --input_data is a frame
		wikitext = input_data.args['1'] or input_data.args[1] or ""
	end
	wikitext=mw.ustring.gsub(wikitext, "<%s*table", "<ul")
	wikitext=mw.ustring.gsub(wikitext, "table%s*>", "ul>")
	wikitext=mw.ustring.gsub(wikitext, "<%s*td%s*class=\".*%-image\"", "<li style=\"display:none;\"")
	wikitext=mw.ustring.gsub(wikitext, "<%s*td", "<li")
	wikitext=mw.ustring.gsub(wikitext, "td%s*>", "li>")
	wikitext=mw.ustring.gsub(wikitext, "<%s*/?%s*tr%s*/?%s*>", "")
	wikitext=mw.ustring.gsub(wikitext, "[。:;,、]?<%s*/?%s*br%s*/?%s*>%s*<%s*small%s*>", ":<small>")
	return '<div class="compact-ambox">'..wikitext..'</div>'
end

return p