跳转到内容

模組:Special wikitext

本页使用了标题或全文手工转换
被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由A2569875留言 | 贡献2021年5月1日 (六) 16:15 (不限於字串標記,但只讀單行)编辑。这可能和当前版本存在着巨大的差异。

local p={
	wikiTextKey = '_addText'
}
function p.check()
	local file_name = mw.title.getCurrentTitle().fullText
	local file_name_spilt = mw.text.split(file_name, "%.")
	local file_ext = file_name_spilt[#file_name_spilt]
	if #file_name_spilt < 2 then return '' end
	if mw.ustring.lower(file_ext) == 'json' then
		return p.getJSONwikitext()
	elseif mw.ustring.lower(file_ext) == 'js' then
		return p.getJSwikitext()
	elseif mw.ustring.lower(file_ext) == 'css' then
		return p.getCSSwikitext()
	end
end

local TemplateParameters = {}
function _addText(input_str,new_str,escape)
	if not TemplateParameters._get_escape then TemplateParameters = require('Module:TemplateParameters') end
	if new_str~='' then
		if input_str~='' then input_str = input_str .. '\n' end
		local text = new_str
		if escape then text = TemplateParameters._get_escape(new_str) end
		input_str = input_str .. text
	end
	return input_str
end
function _getString(str)
	local test_str = mw.ustring.match(str, "[^\n]*%*/")
	if test_str then test_str = mw.ustring.sub(test_str,1,-3) 
	else test_str = str end
	local trim_check = mw.text.trim(test_str)
	local first_char = mw.ustring.sub(trim_check,1,1)
	if first_char == mw.ustring.sub(trim_check,-1,-1) and (first_char == "'" or first_char == '"') then
		return mw.ustring.sub(trim_check,2,-2)
	else
		return test_str
	end
end

function _getContentText(str)
	local wikitext = ''
	xpcall( function()
		local it=mw.ustring.gmatch(str, p.wikiTextKey .. "%s*%{[^c%}]*content%s*:%s*[^\n]*")
		local text=it()
		while text do
			local temp_text = mw.ustring.gsub(mw.text.trim(
				mw.ustring.match(text,"content%s*:%s*[^\n]*"), "\t\r\n\f ;}"
			),"%s*content%s*:%s*","")
			if wikitext ~= '' then wikitext = wikitext .. '\n' end
			wikitext = wikitext .. _getString(temp_text)
			text=it()
		end
	end, function()end )
	return wikitext
end
function _getObjText(str)
	local wikitext = ''
	xpcall( function()
		local it=mw.ustring.gmatch(str, p.wikiTextKey .. "%s*[%=:]%s*[^\n]*")
		local text=it()
		while text do
			local temp_text = mw.ustring.gsub(
				mw.text.trim(text, "\t\r\n\f ;}"),
			p.wikiTextKey .. "%s*[%=:]%s*","")
			if wikitext ~= '' then wikitext = wikitext .. '\n' end
			wikitext = wikitext .. _getString(temp_text)
			text=it()
		end
	end, function()end )
	return wikitext
end
function p.getCSSwikitext()
	local this_frame = mw.getCurrentFrame()
	local wikitext = ''
	local css_text = mw.title.getCurrentTitle():getContent()
	--匹配 _addText { content:"XXX" } 模式
	wikitext = _addText(wikitext, _getContentText(css_text), true)
	--同時亦匹配 /* _addText:XXX */ 模式
	wikitext = _addText(wikitext, _getObjText(css_text), true)
	if wikitext ~= '' then wikitext = this_frame:preprocess(wikitext) end
	return wikitext
end
function p.getJSwikitext()
	local this_frame = mw.getCurrentFrame()
	local wikitext = ''
	local js_text = mw.title.getCurrentTitle():getContent()
	wikitext = _addText(wikitext, _getObjText(js_text))
	if wikitext ~= '' then wikitext = this_frame:preprocess(wikitext) end
	return wikitext
end
function p.getJSONwikitext()
	local this_frame = mw.getCurrentFrame()
	local wikitext = ''
	xpcall( function()
		local json_data = mw.text.jsonDecode(mw.title.getCurrentTitle():getContent())
		for k,v in pairs(json_data) do 
			if mw.ustring.match(k,p.wikiTextKey) and type(v) == type('')then
				wikitext = _addText(wikitext, v)
			end
			--//如果是陣列物件會多包一層
			if type(v) == type({}) then
				for testArr_k,testArr_v in pairs(v) do 
					if mw.ustring.match(testArr_k,p.wikiTextKey) and type(testArr_v) == type('')then
						wikitext = _addText(wikitext, testArr_v)
					end
				end
			end
		end
	end, function()end )
	if wikitext ~= '' then wikitext = this_frame:preprocess(wikitext) end
	return wikitext
end
return p