Module:Tnavbar: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
Armadyl Godsword>Shayani
(don't assign to global variable)
 
m (1 revision imported)
 
(No difference)

Latest revision as of 19:30, 23 March 2024

Documentation for this module may be created at Module:Tnavbar/doc

-- <nowiki>
--
-- Implements {{tnavbar}} and variants
--
-- @todo move the hardcoded css to [[MediaWiki:Common.css]] given how many pages it's found on
--
require('strict')

local p = {}
local yesno = require( 'Module:Yesno' )

function p._navbar( args )
	local navbarstyle = args.style or ''
	local fontstyle = args.fontstyle or ''
	local view, talk, edit = (args.view or true), (args.talk or true), (args.edit or true)

	local desc = {
		view = 'view',
		talk = 'talk',
		edit = 'edit'
	}

	local tag = mw.html.create( 'div' )
		:addClass( 'navbar' )
		:addClass( 'plainlinks' )
		:addClass( 'noprint' )
		:css( {
			['white-space'] = 'nowrap',
			['font-weight'] = 'normal',
			['font-size'] = 'x-small'
		} )
		:cssText( navbarstyle )

	if yesno( args.mini ) then
		desc = {
			view = 'v',
			talk = 't',
			edit = 'e'
		}
		tag:addClass( 'navbar-mini' )
	end

	local viewSpan = mw.html.create( 'span' )
		:attr( 'title', 'View this template' )
		:cssText( fontstyle )
		:wikitext( desc.view )

	local talkSpan = mw.html.create( 'span' )
		:attr( 'title', 'Discussion about this template' )
		:cssText( fontstyle )
		:wikitext( desc.talk )

	local editSpan = mw.html.create( 'span' )
		:attr( 'title', 'Edit this template' )
		:cssText( fontstyle )
		:wikitext( desc.edit )

	local title = args[1] and mw.text.trim( args[1] ) or error('No page title given')
	local ns, titleTbl, pagelink, talklink

	if mw.ustring.sub( title, 1, 1 ) == ':' then
		-- mainspace
		title = mw.ustring.sub( title, 2 )
		pagelink = title
		talklink = 'Talk:' .. title

	elseif mw.ustring.match( title, ':' ) then
		-- split title to see if it has a valid namespace
		titleTbl = mw.text.split( title, ':' )
		ns = mw.site.namespaces[titleTbl[1]]

		if ns ~= nil then
			pagelink = ns.name .. ':' .. table.concat( titleTbl, '', 2 )

			if ns.isTalk then
				talklink = page
			else
				talklink = ns.talk.name .. ':' .. table.concat( titleTbl, '', 2 )
			end
		end
	end

	-- this happens if there's no semi-colons in title
	-- or if there is semi-colons but it didn't have valid ns name
	if not pagelink then
		pagelink = 'Template:' .. title
		talklink = 'Template talk:' .. title
	end

	tag:wikitext( '[[' .. pagelink .. '|' .. tostring( viewSpan ) .. ']]' )
		:wikitext( '&nbsp;' )
		:tag( 'span' )
			:css( 'font-size', '80%' )
			:wikitext( '&bull;' )
			:done()
		:wikitext( '&nbsp;' )

	if talk == 'autoconfirmed' then
		tag
			:tag( 'span' )
				:addClass( 'autoconfirmed-show' )
				:css( 'display', 'none' )
				:wikitext( '[' .. tostring( mw.uri.fullUrl( talklink ) ) .. ' ' .. tostring( talkSpan ) .. ']' )
				:wikitext( '&nbsp;' )
				:tag( 'span' )
					:css( 'font-size', '80%' )
					:wikitext( '&bull;' )
					:done()
				:wikitext( '&nbsp;' )
				:done()
	elseif yesno(talk) then
		tag:wikitext( '[' .. tostring( mw.uri.fullUrl( talklink ) ) .. ' ' .. tostring( talkSpan ) .. ']' )
			:wikitext( '&nbsp;' )
			:tag( 'span' )
				:css( 'font-size', '80%' )
				:wikitext( '&bull;' )
				:done()
			:wikitext( '&nbsp;' )
			:done()
	end

	tag:wikitext( '[' .. tostring( mw.uri.fullUrl( pagelink, 'action=edit' ) ) .. ' ' .. tostring( editSpan ) .. ']' )

	return tostring( tag )
end

function p.navbar( frame )
	return p._navbar( frame:getParent().args )
end

return p