跳转到内容

模組:NoticeBoxUtil

维基百科,自由的百科全书
local p = {}
local lib_doc = require("Module:Documentation")
local yesno = require('Module:Yesno')

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, _sandbox_auto)
	local current_title_name = input_data
	local page_title = input_page_title
	local sandbox_auto = _sandbox_auto
	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']
		sandbox_auto = yesno(input_data.args.auto)
	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)
	if sandbox_auto then sandbox_title = current_title.fullText end
	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, _insert)
	local wikitext = input_data or ""
	local insert_test =_insert
	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 ""
		insert_test = input_data.args.insert 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="%s*[^"]-%-image%s*[^"]-"', '<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, '(class%s*=%s*"%s*[^"]-%-text%s*[^"]-"%s*>.-)<%s*/?%s*br%s*/?%s*>%s*<%s*small%s*>', function(main_match)
		local pos = mw.ustring.find(main_match, "[,、;。]") or 1
		local old_pos = pos
		while type(pos) == type(0) do
			pos = mw.ustring.find(main_match, "[,、;。]", pos+1)
			if type(pos) == type(0) then old_pos = pos end
		end
		local tail_check = mw.ustring.sub(main_match, old_pos+1, -1)
		tail_check = mw.ustring.gsub(tail_check, "<[^<>]->", "")
		local old_tail_check = tail_check .."-"
		while old_tail_check ~= tail_check do
			old_tail_check = tail_check
			tail_check = mw.text.trim(mw.ustring.gsub(tail_check, "<[^<>]->", ""))
		end
		tail_check = mw.text.trim(tail_check)
		pos = -1
		if tail_check == "" then pos = old_pos - 1 end
		return mw.ustring.sub(main_match, 1, pos)..":<small>"
	end)
	mw.log(wikitext)
	wikitext = mw.ustring.gsub(wikitext, "(>'*[本此這这])", "%1"..insert_test)
	return '<div class="compact-ambox">'..wikitext..'</div>'
end

return p