模組:沙盒/a2569875/UserPageFunc

--[[
<includeonly><onlyinclude>{{#invoke:沙盒/a2569875/UserPageFunc|getTemplateVar|:{{FULLPAGENAME}}|Polyhedronbox|Face}}</onlyinclude></includeonly>
]]
local p={}
local lib_arg={};
local lib_para = {}
function p.getlest()
	--{{#invoke:沙盒/a2569875/UserPageFunc|getlest}}
	local frame = mw.getCurrentFrame()
	local body = ''
	local result = mw.text.split(frame.args[1] or frame.args['1'], frame.args[2] or frame.args['2'])
	if result then
		return result[#result]
	end
	return ''
end
function p.testParOrder()
	--{{#invoke:沙盒/a2569875/UserPageFunc|testParOrder}}
	local frame = mw.getCurrentFrame()
	local body = ''
	for v,k in frame:argumentPairs() do
		body = body .. '*參數' ..  v .. '值為' .. k .. '\n'
	end
	return body
end
function p.delUINQ(frame)
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {
        	parentFirst=true,
        	trim = false,
			removeBlanks = false
        })
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_text = mw.text.trim(args["1"] or args[1] or "")
    return mw.ustring.gsub(input_text,"\127",'')
end

function p.testStrip(frame)
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {
        	parentFirst=true,
        	trim = false,
			removeBlanks = false
        })
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local mark_name = mw.text.trim(args["1"] or args[1] or "")
    if mark_name == '' then return '' end
    local mark_get_mart = frame:extensionTag( mark_name, mark_name )
    if mw.ustring.find(mark_get_mart,'\127') then
    	local max_id = ''
    	mw.ustring.gsub(mark_get_mart, mark_name .. '%-([0-9A-Za-z]+)%-', function(id)max_id = id end)
    	return max_id
    end
    return ''
end

function p.makeStrip(frame)
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {
        	parentFirst=true,
        	trim = false,
			removeBlanks = false
        })
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local mark_name = mw.text.trim(args["1"] or args[1] or "")
    local mark_id = mw.text.trim(args["2"] or args[2] or "")
    if mark_name ~= '' and mark_id ~= '' then
    	return "\127'\"`UNIQ--" .. mark_name .. "-" .. mark_id .. "-QINU`\"'\127"
    end
    return ''
end

function p.countSaveTime(frame)
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {
        	parentFirst=true,
        	trim = false,
			removeBlanks = false
        })
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
	local mTemplateInvocation = require('Module:Template invocation')
	local name = mTemplateInvocation.name(frame:getParent():getTitle())
	local get_text = args['1'] or args[1]
	local head, tail = mw.ustring.find(get_text, '%d+')
	local counts = tonumber(mw.ustring.sub(get_text, head, tail)) + 1
	return "{{subst:" .. name .. "|".. tostring(counts) .."}}" ;
end

function p.sb_func()
	p.append("  ")
	return 0
end

function p.append(frame, category) --fork from [[Module:If_empty]]
	if mw.isSubsting() then return '' end
	local cat = ''
	if type(frame) == type('string') then cat = frame end
	if type((frame or {}).args) ~= type({}) then frame = mw.getCurrentFrame() end
	cat = mw.text.trim(cat or category or frame.args['1'] or frame.args[1] or frame.args.cat or '')
	if cat ~= '' then
		frame:extensionTag('ref', string.format('[[Category:%s]]', cat), {group = 'TrackingCategory'})
		frame:extensionTag('references', '', {group = 'TrackingCategory'})
	end
	return ''
end

local num_map, num_str = {1,2,3,4,5,6,7,8,9,[0]=0,[',']=',',['’']=','}, {"0123456789","₀₁₂₃₄₅₆₇₈₉","⁰¹²³⁴⁵⁶⁷⁸⁹"}; for i=1,#num_str do for j=1,mw.ustring.len(num_str[i])do num_map[mw.ustring.sub(num_str[i],j,j)]=j-1 end end 

function p._getPolyItem(entity)
	local poly_item_property = {527,1012,1552,2670,4330}
	local propertyValues = {}
	for it=1,#poly_item_property do
		local temp_data = entity:getAllStatements( 'P' .. poly_item_property[it] )
		for key,value in pairs(temp_data) do
			propertyValues[key] = mw.clone( temp_data[key] )
		end
	end
	return propertyValues
end

function p.loadDimItem(frame)
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {parentFirst=true})
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_title = args[1] or args['1']
    local input_dim = -1
    local entity
    input_title = mw.text.trim(mw.ustring.upper(input_title))
   
    if mw.ustring.find(input_title,"^Q%d+$") then
    	input_dim = tonumber(arg[2] or arg['2'] or '') or -2
    	entity = mw.wikibase.getEntity(input_title)
    elseif tonumber(input_title or '') then
    	input_dim = tonumber(input_title)
    	entity = mw.wikibase.getEntity()
    end
	local poly_item = {26382,3064117,2733626,4637223,18028552,18028565,18028567,18028569,18028571,
	[-1]={["item"]=27064338,["default"]=1},
	[0]={["item"]=26401},}
	local propertyValues = p._getPolyItem(entity)
	local body = ''
	local checkpoly = poly_item[tonumber(input_dim)or-2]
	local checkpoly_name = checkpoly
	local default_text = ''
	if type(checkpoly) == type({}) then
		checkpoly_name = checkpoly.item
		default_text = checkpoly.default or default_text
	end
    for it=1,#propertyValues do
    	local checker = ((propertyValues[it].mainsnak.datavalue or {}).value or {}).id
    	
    	if ('Q' .. tostring(checkpoly_name or '')) == (checker or '') then
			local getter = propertyValues[it].qualifiers["P1114"]
			if getter then
				body = body .. tonumber(getter[1].datavalue.value.amount)
			end
			break

    	end
    end
    if mw.text.trim(body) == '' then return default_text end
	return body
end

function p.loadSchlafli(frame)
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {parentFirst=true})
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_title = args[1] or args['1']
    local input_format = args.format or '$'
    local input_split = args.split or '、'
    local entity = mw.wikibase.getEntity(input_title or '') or mw.wikibase.getEntity()
    local propertyValues = entity:getAllStatements( 'P3228' )
    local body = ''
    for it=1,#propertyValues do
    	if body ~= '' then body = body .. input_split end
    	local sstr = propertyValues[it].mainsnak.datavalue.value
		sstr = mw.ustring.gsub(sstr, '([^,%{%}/]+)/([^,%{%}/]+)', '<sup>%1</sup>/<sub>%2</sub>')
		sstr = mw.ustring.gsub(sstr,"[,₁₂₃₄₅₆₇₈₉₀]+",function(nstr)
			if mw.ustring.len(nstr) <= 1 and mw.ustring.sub(nstr,1,1) == ',' then return nstr end
			return "<sub>"..mw.ustring.gsub(nstr,".",function(c) return num_map[c] end).."</sub>"
		end)
		sstr = mw.ustring.gsub(sstr,"[’⁰¹²³⁴⁵⁶⁷⁸⁹]+",function(nstr)
			if mw.ustring.len(nstr) <= 1 and mw.ustring.sub(nstr,1,1) == ',' then return nstr end
			return "<sup>"..mw.ustring.gsub(nstr,".",function(c) return num_map[c] end).."</sup>"
		end)
    	body = body .. mw.ustring.gsub(input_format,'%$',sstr)
    end
	return body
end

function p.templateArgWarp(frame, warpargs)
	local args, new_name
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        new_name = frame.args['1'] or frame.args[1] or frame.args.name or frame.args.Name
        local parent = frame:getParent() or {['args']={}}
        args = parent.args
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        if type(frame) == type('string') then new_name = frame
        elseif type(frame) == type({}) then new_name = frame['1'] or frame[1] or frame.name or frame.Name
        end
        args = warpargs or mw.getCurrentFrame().args
    end
    local mTemplateInvocation = require('Module:Template invocation')
	return require('Module:Template invocation').invocation(new_name, args)
end

function p.testFakeStrip()
	return mw.getCurrentFrame():preprocess( "{{{1\127'\"`UNIQ--item-00010000-QINU`\"'\127}}}\127'\"`UNIQ--item-00020000-QINU`\"'\127" )
end

function p.testBreakStrip()
	return mw.ustring.gsub(mw.getCurrentFrame():preprocess(mw.ustring.gsub(mw.getCurrentFrame().args[1] .. "<!--__UINQABCQNIU__-->",'\127','') ),'\127','')
end

local yesno = {}
function p.getJSON(frame)
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {parentFirst=true})
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_title = args[1] or args['1']
    local key_alias = args['key alias'] or args.key_alias
    if (key_alias or '') ~= '' then
		if type(yesno) ~= type(tonumber) then yesno = require('Module:Yesno') end
		key_alias = yesno(key_alias or 'no')
	end
    local default = args["default"] or ''
    local fullText = mw.title.new( input_title, "Template" ):getContent()
    local jsonText = ''
    if fullText then
    	local checker, begin = mw.ustring.find(fullText, "<%s*source%s*lang%s*=%s*['\"]?[Jj][Ss][Oo][Nn]['\"]?%s*>")
    	local finish, __noop = mw.ustring.find(fullText, "<%s*/%s*source[^>]*>")
    	if checker then
    		jsonText = mw.ustring.sub(fullText,begin+1,finish-1)
    	end
    end

    local JSONobj = mw.text.jsonDecode( jsonText )
    local iterator = 2
	while args[iterator] ~= nil do
		if key_alias then
			local check_key = JSONobj[args[iterator]]
			if type(check_key) == type("string") then
				local check_key_str, find_key_str = mw.ustring.gsub(check_key,"<%s*target%s*>(.-)<%s*/%s*target%s*>", '%1')
				mw.log(args[iterator] .. ', find:' .. check_key_str)
				while find_key_str > 0 and mw.text.trim(check_key_str) ~= '' and JSONobj[check_key_str] ~= nil do
					check_key = JSONobj[check_key_str]
					if type(check_key) == type("string") then
						check_key_str, find_key_str = mw.ustring.gsub(check_key,"<%starget%s>(.-)<%s/%starget%s>", '%1')
						mw.log('   =>\"' .. check_key_str .. '\"')
					else break end
				end
			end
			JSONobj = check_key
		else JSONobj = JSONobj[args[iterator]] end
		if JSONobj == nil and default ~= nil then
			return default
		end
		iterator = iterator + 1
	end
	
	if type(JSONobj) == type({}) then
		return default
	end
	
	return JSONobj
end

function p.loadTemplateValue(frame)
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {
        	parentFirst=true,
        	trim = false,
			removeBlanks = false
        })
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
	local input_title = args[1] or args['1']
	input_title = mw.text.trim(input_title or '')
	if input_title == '' then return '' end
	local input_template_name = args[2] or args['2']
	input_template_name = mw.text.trim(input_template_name or '')
	if input_template_name == '' then return '' end
    local title_obj = mw.title.new(input_title)
    local page_content = title_obj:getContent()
    if page_content then else return '' end
    local page_text = mw.text.decode(page_content)

	--store nowiki
	local nowikimap = {}
	page_text = mw.ustring.gsub(page_text, '<%s*([Nn][Oo][Ww][Ii][Kk][Ii][^>]*)>(.-)<%s*(/[Nn][Oo][Ww][Ii][Kk][Ii])%s*>', 
    	function(tag_head, tag_body, tag_tail)
    		nowikimap[#nowikimap + 1] = '<' .. tag_head .. '>' .. tag_body .. '<' .. tag_tail .. '>'
    		return '\127$__UINQ-NOWIKI-' .. #nowikimap ..'-QNIU__$\127'
    	end) 
	
	--store noinclude
	local noincludemap = {}
	local templatemap = {}
	local includemap = {}
    local paramap = {}

    local put_template_pattern, put_template_replacment = '%$__UINQ%-TEMP%-(%d+)%-QNIU__%$', function(map_id)
		local index = tonumber(map_id)
		return templatemap[index or ''] or ''
	end
    local put_noinclude_pattern, put_noinclude_replacment = '%$__UINQ%-NOINCLUDE%-(%d+)%-QNIU__%$', function(map_id)
		local index = tonumber(map_id)
		return noincludemap[index or ''] or ''
	end
    local put_include_pattern, put_include_replacment = '%$__UINQ%-INCLUDE%-(%d+)%-QNIU__%$', function(map_id)
		local index = tonumber(map_id)
		return includemap[index or ''] or ''
	end
    local put_para_pattern, put_para_replacment = '%$__UINQ%-PARA%-(%d+)%-QNIU__%$', function(map_id)
		local index = tonumber(map_id)
		return paramap[index or ''] or ''
	end
	
	page_text = mw.ustring.gsub(page_text, '<%s*([Nn][Oo][Ii][Nn][Cc][Ll][Uu][Dd][Ee][^>]*)>(.-)<%s*(/[Nn][Oo][Ii][Nn][Cc][Ll][Uu][Dd][Ee])%s*>', 
    	function(tag_head, tag_body, tag_tail)
    		noincludemap[#noincludemap + 1] = '<' .. tag_head .. '>' .. tag_body .. '<' .. tag_tail .. '>'
    		return '$__UINQ-NOINCLUDE-' .. #noincludemap ..'-QNIU__$'
    	end)
    --put back nowiki
	page_text = mw.ustring.gsub(page_text, '\127%$__UINQ%-NOWIKI%-(%d+)%-QNIU__%$\127', 
    	function(map_id)
    		local index = tonumber(map_id)
    		return nowikimap[index or ''] or ''
    	end)
    nowikimap = {}

    page_text = mw.ustring.gsub(page_text, '<%s*(/?)([a-zA-Z0-9_]-[Ii][Nn][Cc][Ll][Uu][Dd][Ee][a-zA-Z0-9_]-)([^>]*)>', 
    	function(end_tag,tag_name,tag_args)
    		includemap[#includemap + 1] = '<' .. end_tag .. tag_name .. tag_args .. '>'
    		return '$__UINQ-INCLUDE-' .. #includemap ..'-QNIU__$'
    	end)

    --使用自定義strip mark迴避subst階段完全無法unstrip的狀況
    local remap = {}
    local html_pattern, html_replacment = '<%s*(/?)([a-zA-Z0-9_]+)([^>]*)>',function(end_tag,tag_name,tag_args)
		remap[#remap + 1] = '<' .. end_tag .. tag_name .. tag_args .. '>'
		return '$__UINQ-HTML-' .. #remap ..'-QNIU__$'
	end
    page_text = mw.ustring.gsub(page_text, html_pattern, html_replacment)

	local deremap = {}
    local para_finder, para_replacment = '{{{[^{}]-}}}', function(paratext)
		paratext = mw.ustring.gsub(paratext, put_para_pattern, put_para_replacment)
		local check_name, check_name_end = mw.ustring.find(paratext, '{{{[^|}]-[|}]')
		if check_name then
			check_name = mw.ustring.sub(paratext, check_name+3, check_name_end-1)
			local check_org_name = mw.clone(check_name)
			local check_rep1,check_rep2 = 0,0
			check_org_name, check_rep1 = mw.ustring.gsub(check_name, put_include_pattern, put_include_replacment)
			check_org_name, check_rep2 = mw.ustring.gsub(check_org_name, put_noinclude_pattern, '')
			if (check_rep1+check_rep2) > 0 then
				deremap[check_name] = check_org_name
			end
		end
		paramap[#paramap + 1] = paratext
		return '$__UINQ-PARA-' .. #paramap ..'-QNIU__$'
	end
    for i=1,50 do
    	local replace_flag = 0
    	page_text, replace_flag = mw.ustring.gsub(page_text, para_finder,para_replacment)
    	if replace_flag <= 0 then break end
    end

    local template_finder, template_replacment = '{{[^{}]-}}', function(paratext)
    	--移除位於模板名稱中的include系列標籤,避免模板名稱錯誤
    	paratext = mw.ustring.gsub(paratext, '({{)([^|}]-)([|}])', function(template_head, template_name, template_tail)
    		local temp_name = mw.ustring.gsub(template_name, put_noinclude_pattern, '')
    		temp_name = mw.ustring.gsub(temp_name, put_include_pattern, '')
    		return template_head .. temp_name .. template_tail
    	end)
    	paratext = mw.ustring.gsub(paratext, put_para_pattern, put_para_replacment)
		paratext = mw.ustring.gsub(paratext, put_template_pattern, put_template_replacment)
		
		templatemap[#templatemap + 1] = paratext
		return '$__UINQ-TEMP-' .. #templatemap ..'-QNIU__$'
	end
    for i=1,50 do
    	local replace_flag = 0
    	page_text, replace_flag = mw.ustring.gsub(page_text, template_finder,template_replacment)
    	if replace_flag <= 0 then break end
    end

--			mw.log('deremap')
--			mw.logObject(deremap)
	
--    mw.log("paramap")
--mw.logObject(paramap)
--mw.log(table.concat( paramap,'\127'))
--mw.logObject(mw.text.split(table.concat( paramap,'\127'),'\127'))

--    mw.log("includemap")
--mw.logObject(includemap)

    mw.log("templatemap")
mw.logObject(templatemap)

--    mw.log("remap")
--mw.logObject(remap)
--    mw.log("noincludemap")
--mw.logObject(noincludemap)
	    		--mw.log("page_text")
	    		--mw.log(page_text)
	return ''
end

--{{#invoke:沙盒/a2569875/UserPageFunc|allSubst|subst|void}}
--失敗
function p.allSubst(frame)
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {
        	parentFirst=true,
        	trim = false,
			removeBlanks = false
        })
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_data = args[1] or args['1']
    local input_title = mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(input_data, 'msgnw:',''), 'safesubst:',''), 'subst:','')
    local check_i, start = mw.ustring.find(input_data, "#invoke:")
    local offset = 1
    if check_i then
    	input_title = 'Module' .. mw.ustring.sub(input_data,start)
    	offset = 2
    end
    local title_obj = mw.title.new(input_title, 'Template' )
    local page_content = title_obj:getContent()
    if page_content then else return "&#123;&#123;[[Help:替换引用#safesubst|safesubst]]:[[Help:替换引用#msgnw|msgnw]]:[["..
    	title_obj.fullText .. '|' .. title_obj.text .. "]]&#125;&#125;" end
    local page_text = mw.text.decode(page_content)

	--store nowiki
	local nowikimap = {}
	page_text = mw.ustring.gsub(page_text, '<%s*([Nn][Oo][Ww][Ii][Kk][Ii][^>]*)>(.-)<%s*(/[Nn][Oo][Ww][Ii][Kk][Ii])%s*>', 
    	function(tag_head, tag_body, tag_tail)
    		nowikimap[#nowikimap + 1] = '<' .. tag_head .. '>' .. tag_body .. '<' .. tag_tail .. '>'
    		return '\127$__UINQ-NOWIKI-' .. #nowikimap ..'-QNIU__$\127'
    	end) 
	
	--store noinclude
	local noincludemap = {}
	local templatemap = {}
	local includemap = {}
    local paramap = {}

    local put_template_pattern, put_template_replacment = '%$__UINQ%-TEMP%-(%d+)%-QNIU__%$', function(map_id)
		local index = tonumber(map_id)
		return templatemap[index or ''] or ''
	end
    local put_noinclude_pattern, put_noinclude_replacment = '%$__UINQ%-NOINCLUDE%-(%d+)%-QNIU__%$', function(map_id)
		local index = tonumber(map_id)
		return noincludemap[index or ''] or ''
	end
    local put_include_pattern, put_include_replacment = '%$__UINQ%-INCLUDE%-(%d+)%-QNIU__%$', function(map_id)
		local index = tonumber(map_id)
		return includemap[index or ''] or ''
	end
    local put_para_pattern, put_para_replacment = '%$__UINQ%-PARA%-(%d+)%-QNIU__%$', function(map_id)
		local index = tonumber(map_id)
		return paramap[index or ''] or ''
	end
	
	page_text = mw.ustring.gsub(page_text, '<%s*([Nn][Oo][Ii][Nn][Cc][Ll][Uu][Dd][Ee][^>]*)>(.-)<%s*(/[Nn][Oo][Ii][Nn][Cc][Ll][Uu][Dd][Ee])%s*>', 
    	function(tag_head, tag_body, tag_tail)
    		noincludemap[#noincludemap + 1] = '<' .. tag_head .. '>' .. tag_body .. '<' .. tag_tail .. '>'
    		return '$__UINQ-NOINCLUDE-' .. #noincludemap ..'-QNIU__$'
    	end)
    --put back nowiki
	page_text = mw.ustring.gsub(page_text, '\127%$__UINQ%-NOWIKI%-(%d+)%-QNIU__%$\127', 
    	function(map_id)
    		local index = tonumber(map_id)
    		return nowikimap[index or ''] or ''
    	end)
    nowikimap = {}

    page_text = mw.ustring.gsub(page_text, '<%s*(/?)([a-zA-Z0-9_]-[Ii][Nn][Cc][Ll][Uu][Dd][Ee][a-zA-Z0-9_]-)([^>]*)>', 
    	function(end_tag,tag_name,tag_args)
    		includemap[#includemap + 1] = '<' .. end_tag .. tag_name .. tag_args .. '>'
    		return '$__UINQ-INCLUDE-' .. #includemap ..'-QNIU__$'
    	end)

    --使用自定義strip mark迴避subst階段完全無法unstrip的狀況
    local remap = {}
    local html_pattern, html_replacment = '<%s*(/?)([a-zA-Z0-9_]+)([^>]*)>',function(end_tag,tag_name,tag_args)
		remap[#remap + 1] = '<' .. end_tag .. tag_name .. tag_args .. '>'
		return '$__UINQ-HTML-' .. #remap ..'-QNIU__$'
	end
    page_text = mw.ustring.gsub(page_text, html_pattern, html_replacment)

	local deremap = {}
    local para_finder, para_replacment = '{{{[^{}]-}}}', function(paratext)
		paratext = mw.ustring.gsub(paratext, put_para_pattern, put_para_replacment)
		local check_name, check_name_end = mw.ustring.find(paratext, '{{{[^|}]-[|}]')
		if check_name then
			check_name = mw.ustring.sub(paratext, check_name+3, check_name_end-1)
			local check_org_name = mw.clone(check_name)
			local check_rep1,check_rep2 = 0,0
			check_org_name, check_rep1 = mw.ustring.gsub(check_name, put_include_pattern, put_include_replacment)
			check_org_name, check_rep2 = mw.ustring.gsub(check_org_name, put_noinclude_pattern, '')
			if (check_rep1+check_rep2) > 0 then
				deremap[check_name] = check_org_name
			end
		end
		paramap[#paramap + 1] = paratext
		return '$__UINQ-PARA-' .. #paramap ..'-QNIU__$'
	end
    for i=1,50 do
    	local replace_flag = 0
    	page_text, replace_flag = mw.ustring.gsub(page_text, para_finder,para_replacment)
    	if replace_flag <= 0 then break end
    end

    local template_finder, template_replacment = '{{[^{}]-}}', function(paratext)
    	--移除位於模板名稱中的include系列標籤,避免模板名稱錯誤
    	paratext = mw.ustring.gsub(paratext, '({{)([^|}]-)([|}])', function(template_head, template_name, template_tail)
    		local temp_name = mw.ustring.gsub(template_name, put_noinclude_pattern, '')
    		temp_name = mw.ustring.gsub(temp_name, put_include_pattern, '')
    		return template_head .. temp_name .. template_tail
    	end)
    	paratext = mw.ustring.gsub(paratext, put_para_pattern, put_para_replacment)
		paratext = mw.ustring.gsub(paratext, put_template_pattern, put_template_replacment)
		
		templatemap[#templatemap + 1] = paratext
		return '$__UINQ-TEMP-' .. #templatemap ..'-QNIU__$'
	end
    for i=1,50 do
    	local replace_flag = 0
    	page_text, replace_flag = mw.ustring.gsub(page_text, template_finder,template_replacment)
    	if replace_flag <= 0 then break end
    end
    
    --將自訂strip mark放回去
    page_text = mw.ustring.gsub(page_text, put_template_pattern, put_template_replacment)
    page_text = mw.ustring.gsub(page_text, put_para_pattern, put_para_replacment)
    page_text = mw.ustring.gsub(page_text, put_include_pattern, put_include_replacment)
	page_text = mw.ustring.gsub(page_text, put_noinclude_pattern, put_noinclude_replacment)
	page_text = mw.ustring.gsub(page_text, html_pattern, html_replacment)
	
			mw.log('deremap')
			mw.logObject(deremap)
	
    mw.log("paramap")
mw.logObject(paramap)
--mw.log(table.concat( paramap,'\127'))
--mw.logObject(mw.text.split(table.concat( paramap,'\127'),'\127'))

    mw.log("includemap")
mw.logObject(includemap)

    mw.log("templatemap")
mw.logObject(templatemap)

    mw.log("remap")
mw.logObject(remap)
    mw.log("noincludemap")
mw.logObject(noincludemap)
	    		mw.log("page_text")
	    		mw.log(page_text)

    local num_args = {}
	for key,value in pairs(args) do
		local num = tonumber(key) or 0
		if num then
			if num > offset then
				num_args[key - offset] = value
			end
		else
			num_args[key] = value
		end
	end
	for key,value in pairs(deremap) do
		if num_args[value] then
			num_args[key] = num_args[value]
		end
		local id_value = tonumber(value)
		if num_args[id_value] then
			num_args[key] = num_args[id_value]
		end
	end
    mw.log("num_args")
mw.logObject(num_args)
    local subst_frame = working_frame:newChild{ title = title_obj.fullText, args = num_args }
    local result = subst_frame:preprocess( page_text )

    result = mw.ustring.gsub(result, '%$__UINQ%-HTML%-(%d+)%-QNIU__%$', 
		function(map_id)
	    	local index = tonumber(map_id)
	    	if index then
	    		return remap[index] or ''
	    	end
	    	return ''
	    end)
	    mw.log('result')
	    mw.log(result)
	if mw.isSubsting() then return result end
	result = mw.ustring.gsub(result, '<%s*/?[^>]*include[^>]*>', '')
	return result
end



function p.unnowiki(frame)
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {parentFirst=true})
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_n = args[1] or args['1']
	local input_data = "" .. mw.text.unstripNoWiki( input_n )
	if(mw.isSubsting())then 
		return mw.ustring.sub( input_n );
	end
	return working_frame:preprocess( input_data )
end

function p.getTemplateVar(frame)
    local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {parentFirst=true})
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local finding_frame = working_frame:newChild{ title = working_frame:getTitle() .. "/FINDER", working_frame.args }
    local input_n = args[1] or args['1']
    local finder_template = args[2] or args['2'] or ''
    local finder = args[3] or args['3'] or ''
    local it_times = args['times'] or 3
	local src = ' ' .. (finding_frame:newParserValue{ text = "{{msgnw:" .. input_n .. "}}" }):expand()
	if mw.text.trim(finder_template) ~= '' then
		if lib_para._getEscapeString == nil then lib_para = require('Module:TemplateParameters') end
		local unstripped = ' ' .. mw.ustring.gsub(src ,'(&#123;&#123;)%s-(' .. lib_para._getEscapeString(finder_template) .. ')', '%1[[%2]]' );
		unstripped = mw.ustring.gsub(unstripped ,'ref', '[[ref]]' );
		local reexpand = '. ' .. (finding_frame:newParserValue{ text = mw.text.decode( unstripped ) }):expand() .. ' .'

		template_data = mw.ustring.gmatch(reexpand , "{{%s-%[%[.-}}[^\}]")
		local result, iterator = '*', template_data()
		local replace_link = function(text)
			return mw.ustring.gsub(text ,'|', '{{!}}' )
		end
		while iterator do
			local pre_template_data = mw.ustring.gsub(iterator ,'%[%[.-%]%]', replace_link )
			local this_template = mw.text.split(pre_template_data, '|')  or { [1] = pre_template_data }
			local template_name = mw.text.trim(mw.ustring.gsub(this_template[1], '[%[%]{}\n\r\t]', ''))
			local arg_index = 0
			mw.log('pre_template_data',pre_template_data)
			mw.log('template_name',template_name)
			for arg_id, arg_full in pairs(this_template) do
				if arg_index == 0 then arg_index = 1 else
					local checker = mw.ustring.find( arg_full, "=")
					local arg_full_text = arg_full
					local arg_name = arg_index
					if checker then 
						arg_full_text = mw.text.trim(mw.ustring.sub(arg_full, checker+1, -1))
						arg_name = mw.text.trim(mw.ustring.sub(arg_full, 1, checker-1))
					else
						arg_index = arg_index + 1
					end 
					mw.log('arg_name='..arg_name..', '..'arg_value='..arg_full_text)
					
					local tmp_v = 'false';if(finder==arg_name)then tmp_v='true' end
					local tmp_t = 'false';if(finder_template==template_name)then tmp_t='true' end

					if mw.text.trim('' .. finder_template) == template_name and mw.text.trim('' .. finder) == arg_name then
						return mw.text.trim(arg_full_text or '')
					end
				end
			end
			--result = result .. iterator .. '\n*'
			iterator = template_data()
		end
		--return result
	end
	return ''
end
p['生成']=function()return 'jj生成' end
return p;