Editing Module:Navbar

Jump to: navigation, search

Warning: You are not logged in.

Your IP address will be recorded in this page's edit history.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
 
local p = {}
 
local p = {}
  
local getArgs
+
local HtmlBuilder = require('Module:HtmlBuilder')
  
function p._navbar(args)
+
function trim(s)
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; width:6em;'
+
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 );
 +
    local page_title, talk_page_title;
 +
   
 +
    if mw.ustring.sub(pageName, 1, 1) == ':' then
 +
        page_title = mw.title.new( mw.ustring.sub(pageName, 2) );
 +
    else
 +
        page_title = mw.title.new( pageName, 'Template' );
 +
    end   
 +
   
 +
    if page_title then
 +
        talk_page_title = page_title.talkPageTitle;
 +
    else
 +
        talk_page_title = nil;
 +
    end
 +
   
 +
    return page_title, talk_page_title;  
 +
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, talk_title;
 +
    good, title, talk_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 = talk_title and talk_title.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' )
:newline();
+
        .addClass( 'hlist' )
end
+
        .addClass( 'navbar')
 
+
        .cssText( args.style )
local ul = div:tag('ul');
+
 
+
    if args.mini then div.addClass('mini') end
ul
+
:tag('li')
+
    if not (args.mini or args.plain) then
:addClass('nv-view')
+
        div
:wikitext('[[' .. title.fullText .. '|')
+
            .tag( 'span' )
:tag('span')
+
                .css( 'word-spacing', 0 )
:attr('title', 'View this template')
+
                .cssText( args.fontstyle )
:cssText(args.fontstyle)
+
                .wikitext( args.text or 'This box:' )
:wikitext(args.mini and 'v' or 'view')
+
                .wikitext( ' ' )
:done()
+
    end
:wikitext(']]')
+
:done()
+
    if args.brackets then
:tag('li')
+
        div
:addClass('nv-talk')
+
            .tag('span')
:wikitext('[[' .. talkpage .. '|')
+
                .css('margin-right', '-0.125em')
:tag('span')
+
                .cssText( args.fontstyle )
:attr('title', 'Discuss this template')
+
                .wikitext( '[' )
:cssText(args.fontstyle)
+
                .newline();
:wikitext(args.mini and 't' or 'talk')
+
    end
:done()
+
: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('span')
+
                .attr( 'title', 'View this template' )
:attr('title', 'Edit this template')
+
                .cssText( args.fontstyle or '' )
:cssText(args.fontstyle)
+
                .wikitext( viewLink )
:wikitext(args.mini and 'e' or 'edit')
+
                .done()
:done()
+
            .wikitext( ']]' )
:wikitext(']');
+
            .done()
end
+
        .tag( 'li' )
 
+
            .addClass( 'nv-talk' )
if args.brackets then
+
            .wikitext( '[[' .. talkpage .. '|' )
div
+
            .tag( 'span ' )
:tag('span')
+
                .attr( 'title', 'Discuss this template' )
:css('margin-left', '-0.125em')
+
                .cssText( args.fontstyle or '' )
:cssText(args.fontstyle)
+
                .wikitext( talkLink )
:wikitext(']')
+
                .done()
:newline();
+
            .wikitext( ']]' );
end
+
 
+
    if not args.noedit then  
if args.collapsible then
+
        ul
div
+
            .tag( 'li' )
:done()
+
                .addClass( 'nv-edit' )
:tag('span')
+
                .wikitext( '[' .. editurl .. ' ' )
:css('font-size', '110%')
+
                .tag( 'span ' )
:cssText(args.fontstyle)
+
                    .attr( 'title', 'Edit this template' )
:wikitext(args[1])
+
                    .cssText( args.fontstyle or '' )
end
+
                    .wikitext( editLink )
 
+
                    .done()
return tostring(div: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
 

Please note that all contributions to The Crowdsourced Resource-Based Economy Knowledgebase are considered to be released under the Creative Commons Zero (Public Domain) (see RBEMWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox
Share