Module:shortcut box

Revision as of 10:31, 25 March 2021 by Sware (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


local export = {}

function export.show(frame)
	local output = {}
	-- Hacky way to allow function to be called from other modules.
	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="plainlinks" style="border: 1px solid #aaa; color: black; background: #f9f9f9;
	margin: 0 0 .5em 1em; text-align: center; padding: 5px; float: right; clear: both; font-weight: bold; font-size: smaller;">]]

	if args.temp then
		output[#output + 1] = "Temporary [[wikt:Wiktionary:Shortcut|shortcut" .. (args[2] and 's' or '') .. "]]:<br/>"
	else
		output[#output + 1] = "[[wikt:Wiktionary:Shortcut|Shortcut" .. (args[2] and 's' or '') .. "]]:<br/>"
	end
	
	local attn = false

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

			local item

			if not title.exists then
				attn = true
				local query = 'action=edit&redlink=1&preloadtext=' .. mw.uri.encode('#REDIRECT [[' .. FULLPAGENAME .. ']]')
				if title.nsText == "Template" then
					item = '<code>{{[' .. tostring(mw.uri.fullUrl(shortcut, query)) .. ' <span style="color:#c20;">' .. title.text .. '</span>]}}</code>'
				else
					item = '[' .. tostring(mw.uri.fullUrl(shortcut, query)) .. ' <span style="color:#c20;">' .. shortcut .. '</span>]'
				end
			else
				if title.nsText == "Template" then
					item = '<code>{{[' .. tostring(mw.uri.fullUrl(shortcut, 'redirect=no')) .. ' ' .. title.text .. ']}}</code>'
				else
					item = '[' .. tostring(mw.uri.fullUrl(shortcut, 'redirect=no')) .. ' ' .. shortcut .. ']'
				end
				
				if not demo then
					local redirectTarget = title.redirectTarget
					if redirectTarget then
						redirectTarget.fragment = '' -- remove fragment, if present
						local targetText = redirectTarget.fullText
						if title.nsText == "Template" then
							if SUBPAGENAME == 'doc' then
								iattn = (targetText ~= BASEPAGENAME)
							else
								iattn = (targetText ~= FULLPAGENAME)
							end
						else
							if SUBPAGENAME == 'doc' then
								iattn = not (targetText == BASEPAGENAME
									or targetText == FULLPAGENAME)
							else
								iattn = not (targetText == FULLPAGENAME
									or targetText == FULLPAGENAME .. '/doc')
							end
						end
					end
					
					if not (redirectTarget and redirectTarget.exists) then
						iattn = true
					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:Shortcut boxes needing attention]]'	
		end
		if SUBPAGENAME ~= 'doc' and FULLPAGENAME ~= 'Template:Shortcut' then
			output[#output + 1] = '[[Category:Templates with shortcuts]]'
		end
	end

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

return export