Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Navbar: Difference between revisions

Content added Content deleted
(handle expensive parser function limit without killing the box)
m (27 revisions imported: import clickable buttons 2)
 
(28 intermediate revisions by 15 users not shown)
Line 1: Line 1:
local p = {}
local p = {}


local getArgs
local HtmlBuilder = require('Module:HtmlBuilder')


function trim(s)
function p._navbar(args)
local titleArg = 1
return mw.ustring.match( s, "^%s*(.-)%s*$" )
end


if args.collapsible then
function error(s)
titleArg = 2
local span = HtmlBuilder.create('span')
if not args.plain then
args.mini = 1
end
if args.fontcolor then
args.fontstyle = 'color:' .. args.fontcolor .. ';'
end
args.style = 'float:left; text-align:left'
end


local titleText = args[titleArg] or (':' .. mw.getCurrentFrame():getParent():getTitle())
span
local title = mw.title.new(mw.text.trim(titleText), 'Template');
.addClass('error')
.css('float', 'left')
.css('white-space', 'nowrap')
.wikitext('Error: ' .. s)


if not title then
return tostring(span)
error('Invalid title ' .. titleText)
end
end


local talkpage = title.talkPageTitle and title.talkPageTitle.fullText or '';
function getTitle( pageName )
pageName = trim( pageName );
if mw.ustring.sub(pageName, 1, 1) == ':' then
return mw.title.new( mw.ustring.sub(pageName, 2) );
else
return mw.title.new( pageName, 'Template' );
end
end


local div = mw.html.create():tag('div')
function _navbar( args )
div
if not args[1] then
:addClass('plainlinks')
return error('No name provided')
:addClass('hlist')
end
:addClass('navbar')
:cssText(args.style)
local good, title;
good, title = pcall( getTitle, args[1] );
if not good then
return error('Expensive parser function limit exceeded');
end


if args.mini then div:addClass('mini') end
if not title then

return error('Page does not exist')
if not (args.mini or args.plain) then
end
div
:tag('span')
local mainpage = title.fullText;
:css('word-spacing', 0)
local talkpage = title.talkPageTitle and title.talkPageTitle.fullText or ''
:cssText(args.fontstyle)
local editurl = title:fullUrl( 'action=edit' );
:wikitext(args.text or 'This box:')
:wikitext(' ')
local viewLink, talkLink, editLink = 'view', 'talk', 'edit'
end
if args.mini then

viewLink, talkLink, editLink = 'v', 't', 'e'
if args.brackets then
end
div
:tag('span')
local div = HtmlBuilder.create( 'div' )
:css('margin-right', '-0.125em')
div
:cssText(args.fontstyle)
.addClass( 'noprint' )
:wikitext('[ ')
.addClass( 'plainlinks' )
end
.addClass( 'hlist' )

.addClass( 'navbar')
local ul = div:tag('ul');
.cssText( args.style )

ul
if args.mini then div.addClass('mini') end
:tag('li')
:addClass('nv-view')
if not (args.mini or args.plain) then
:wikitext('[[' .. title.fullText .. '|')
div
.tag( 'span' )
:tag(args.mini and 'abbr' or 'span')
:attr('title', 'View this template')
.css( 'word-spacing', 0 )
.cssText( args.fontstyle )
:cssText(args.fontstyle)
.wikitext( args.text or 'This box:' )
:wikitext(args.mini and 'v' or 'view')
:done()
.wikitext( ' ' )
:wikitext(']]')
end
:done()
:tag('li')
if args.brackets then
:addClass('nv-talk')
div
:wikitext('[[' .. talkpage .. '|')
.tag('span')
:tag(args.mini and 'abbr' or 'span')
.css('margin-right', '-0.125em')
:attr('title', 'Discuss this template')
.cssText( args.fontstyle )
:cssText(args.fontstyle)
.wikitext( '[' )
:wikitext(args.mini and 't' or 'talk')
.newline();
:done()
end
:wikitext(']]');

local ul = div.tag('ul');
if not args.noedit then
ul
ul
.tag( 'li' )
:tag('li')
.addClass( 'nv-view' )
:addClass('nv-edit')
.wikitext( '[[' .. mainpage .. '|' )
:wikitext('[' .. title:fullUrl('action=edit') .. ' ')
.tag( 'span ' )
:tag(args.mini and 'abbr' or 'span')
.attr( 'title', 'View this template' )
:attr('title', 'Edit this template')
.cssText( args.fontstyle or '' )
:cssText(args.fontstyle)
:wikitext(args.mini and 'e' or 'edit')
.wikitext( viewLink )
.done()
:done()
.wikitext( ']]' )
:wikitext(']');
end
.done()

.tag( 'li' )
if args.brackets then
.addClass( 'nv-talk' )
div
.wikitext( '[[' .. talkpage .. '|' )
.tag( 'span ' )
:tag('span')
:css('margin-left', '-0.125em')
.attr( 'title', 'Discuss this template' )
.cssText( args.fontstyle or '' )
:cssText(args.fontstyle)
.wikitext( talkLink )
:wikitext(' ]')
end
.done()

.wikitext( ']]' );
if args.collapsible then
div
if not args.noedit then
:done()
ul
.tag( 'li' )
:tag('div')
:css('font-size', '114%')
.addClass( 'nv-edit' )
:css('margin', args.mini and '0 4em' or '0 7em')
.wikitext( '[' .. editurl .. ' ' )
:cssText(args.fontstyle)
.tag( 'span ' )
:wikitext(args[1])
.attr( 'title', 'Edit this template' )
end
.cssText( args.fontstyle or '' )

.wikitext( editLink )
return tostring(div:done())
.done()
.wikitext( ']' );
end
if args.brackets then
div
.tag('span')
.css('margin-left', '-0.125em')
.cssText( args.fontstyle or '' )
.wikitext( ']' )
.newline();
end
return tostring(div)
end
end


function p.navbar(frame)
function p.navbar(frame)
if not getArgs then
local origArgs
getArgs = require('Module:Arguments').getArgs
-- If called via #invoke, use the args passed into the invoking template.
end
-- Otherwise, for testing purposes, assume args are being passed directly in.
return p._navbar(getArgs(frame))
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
else
origArgs = frame
end
-- ParserFunctions considers the empty string to be false, so to preserve the previous
-- behavior of {{navbar}}, change any empty arguments to nil, so Lua will consider
-- them false too.
args = {}
for k, v in pairs(origArgs) do
if v ~= '' then
args[k] = v
end
end
return _navbar(args)
end
end

return p
return p

Latest revision as of 00:30, 28 March 2020

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

local p = {}

local getArgs

function p._navbar(args)
	local titleArg = 1

	if args.collapsible then
		titleArg = 2
		if not args.plain then
			args.mini = 1
		end
		if args.fontcolor then
			args.fontstyle = 'color:' .. args.fontcolor .. ';'
		end
		args.style = 'float:left; text-align:left'
	end

	local titleText = args[titleArg] or (':' .. mw.getCurrentFrame():getParent():getTitle())
	local title = mw.title.new(mw.text.trim(titleText), 'Template');

	if not title then
		error('Invalid title ' .. titleText)
	end

	local talkpage = title.talkPageTitle and title.talkPageTitle.fullText or '';

	local div = mw.html.create():tag('div')
	div
		:addClass('plainlinks')
		:addClass('hlist')
		:addClass('navbar')
		:cssText(args.style)

	if args.mini then div:addClass('mini') end

	if not (args.mini or args.plain) then
		div
			:tag('span')
				:css('word-spacing', 0)
				:cssText(args.fontstyle)
				:wikitext(args.text or 'This box:')
				:wikitext(' ')
	end

	if args.brackets then
		div
			:tag('span')
				:css('margin-right', '-0.125em')
				:cssText(args.fontstyle)
				:wikitext('[ ')
	end

	local ul = div:tag('ul');

	ul
		:tag('li')
			:addClass('nv-view')
			:wikitext('[[' .. title.fullText .. '|')
			:tag(args.mini and 'abbr' or 'span')
				:attr('title', 'View this template')
				:cssText(args.fontstyle)
				:wikitext(args.mini and 'v' or 'view')
				:done()
			:wikitext(']]')
			:done()
		:tag('li')
			:addClass('nv-talk')
			:wikitext('[[' .. talkpage .. '|')
			:tag(args.mini and 'abbr' or 'span')
				:attr('title', 'Discuss this template')
				:cssText(args.fontstyle)
				:wikitext(args.mini and 't' or 'talk')
				:done()
			:wikitext(']]');

	if not args.noedit then
		ul
			:tag('li')
				:addClass('nv-edit')
				:wikitext('[' .. title:fullUrl('action=edit') .. ' ')
				:tag(args.mini and 'abbr' or 'span')
					:attr('title', 'Edit this template')
					:cssText(args.fontstyle)
					:wikitext(args.mini and 'e' or 'edit')
					:done()
				:wikitext(']');
	end

	if args.brackets then
		div
			:tag('span')
				:css('margin-left', '-0.125em')
				:cssText(args.fontstyle)
				:wikitext(' ]')
	end

	if args.collapsible then
		div
			:done()
		:tag('div')
			:css('font-size', '114%')
			:css('margin', args.mini and '0 4em' or '0 7em')
			:cssText(args.fontstyle)
			:wikitext(args[1])
	end

	return tostring(div:done())
end

function p.navbar(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	return p._navbar(getArgs(frame))
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.