本模块用于{{Shortcut}}


local export = {}

function export.show(frame)
	local output = {}
	local args = (frame.getParent and frame:getParent().args) or frame
	local demo = false

	local FULLPAGENAME = mw.title.getCurrentTitle().fullText
	local BASEPAGENAME, SUBPAGENAME = FULLPAGENAME:match("^(.-)/([^/]-)$")
	-- BASEPAGENAME = BASEPAGENAME or FULLPAGENAME
	
	if frame.getParent and FULLPAGENAME == frame:getParent():getTitle() then
		demo = true
	end
	output[#output + 1] = '<div class="noprint plainlinks shortcut-box" style="border: 1px solid #aaa; color: black; background: #f9f9f9; margin: 0 0 .5em 1em; text-align: center; padding: 5px; float: right; clear: both;">'

	if args.temp then
		output[#output + 1] = "暫時性[[Wiktionary:快捷方式|快捷方式]]:<br/>"
	else
		output[#output + 1] = "[[Wiktionary:快捷方式|快捷方式]]:<br/>"
	end
	
	local attn = false

	for i, shcut in ipairs(args) do
		local title = mw.title.new(shcut)
		
		if not title then
			output[#output + 1] = '<span class="attentionseeking">(<code>' .. mw.text.nowiki(shcut) .. '</code>)</span><br/>'
			attn = true
		else
			local iattn = false

			local item = '[' .. tostring(mw.uri.fullUrl(shcut, 'redirect=no')) .. ' ' .. shcut .. ']'
			if title.namespace == 10 then
				item = '<code>{{[' .. tostring(mw.uri.fullUrl(shcut, 'redirect=no')) .. ' ' .. title.text .. ']}}</code>'
			end

			if not title.exists then
				attn = true
				local query = 'action=edit&redlink=1&preload=Template:$1&preloadparams[]=' .. mw.uri.encode('#REDIRECT [[' .. FULLPAGENAME .. ']]')
				if title.namespace == 10 then
					item = '<code>{{[' .. tostring(mw.uri.fullUrl(shcut, query)) .. ' <span style="color:#c20;">' .. title.text .. '</span>]}}</code>'
				else
					item = '[' .. tostring(mw.uri.fullUrl(shcut, query)) .. ' <span style="color:#c20;">' .. shcut .. '</span>]'
				end
			elseif not demo then
				local content = title:getContent()
				local target = content:match("^#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[%s*:?%s*(.-)[#%]]")
				if not target then
					iattn = true
				else
					target = target:gsub("_", " ")
					local tns, tpg = target:match("%s*(.-)%s*:%s*(.*)%s*")
					if mw.site.namespaces[tns] then
						tns = mw.site.namespaces[tns].name
						target = tns .. ':' .. tpg
					end
					
					if title.namespace == 10 then
						if SUBPAGENAME == 'doc' then
							iattn = (target ~= BASEPAGENAME)
						else
							iattn = (target ~= FULLPAGENAME)
						end
					else
						if SUBPAGENAME == 'doc' then
							iattn = (target ~= BASEPAGENAME) and (target ~= FULLPAGENAME)
						else
							iattn = (target ~= FULLPAGENAME) and (target ~= (FULLPAGENAME .. '/doc'))
						end
					end
				end
			end

			if iattn then
				item = '<span class="attentionseeking">' .. item .. '</span>'
			end

			output[#output + 1] = item .. '<br/>'
			
			attn = attn or iattn
		end
	end

	if not args.nocat then
		if attn then
			output[#output + 1] = '[[Category:需要关注的快捷方式框]]'	
		end
		if SUBPAGENAME ~= 'doc' then
			output[#output + 1] = '[[Category:有快捷方式的维基词典页面]]'
		end
	end

	output[#output + 1] = '</div>'
	
	return table.concat(output)
end

return export