Diferencia entre revisiones de «Módulo:Navbar»

De Familia Sanchez Arjona
Saltar a: navegación, buscar
m (1 revisión importada)
(navbar code split from Module:Navbox)
(No se muestran 29 ediciones intermedias de 13 usuarios)
Línea 1: Línea 1:
 
local p = {}
 
local p = {}
  
local getArgs
 
 
local HtmlBuilder = require('Module:HtmlBuilder')
 
local HtmlBuilder = require('Module:HtmlBuilder')
  
local function trim(s)
+
function trim(s)
return mw.ustring.match(s, "^%s*(.-)%s*$")
+
    return (mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1"))
 
end
 
end
  
local function htmlError(s)
+
function _navbar( args )
local span = HtmlBuilder.create('span')
+
    if not args[1] then
 
+
        local span = HtmlBuilder.create('span')
span
+
.addClass('error')
+
        span
.css('float', 'left')
+
            .addClass('error')
.css('white-space', 'nowrap')
+
            .css('float', 'left')
.wikitext('Error: ' .. s)
+
            .css('white-space', 'nowrap')
 
+
            .wikitext('Error: No name provided')
return tostring(span)
+
end
+
        return span
 
+
    end
local function getTitle(pageName)
+
pageName = trim(pageName);
+
    local title;
local page_title, talk_page_title;
+
    local pageName = trim(args[1])
 
+
    if mw.ustring.sub(pageName, 1, 1) == ':' then
if mw.ustring.sub(pageName, 1, 1) == ':' then
+
        title = mw.title.new( mw.ustring.sub(pageName, 2) );
page_title = mw.title.new( mw.ustring.sub(pageName, 2));
+
    else
else
+
        title = mw.title.new( pageName );
page_title = mw.title.new(pageName, 'Template');
+
        if title.namespace == 0 then
end
+
            title = mw.title.makeTitle( 'Template', pageName );
 
+
        end  
if page_title then
+
    end      
talk_page_title = page_title.talkPageTitle;
+
else
+
    local mainpage = title.fullText;
talk_page_title = nil;
+
    local talkpage = title.talkPageTitle and title.talkPageTitle.fullText or ''
end
+
    local editurl = title:fullUrl( 'action=edit' );  
 
+
return page_title, talk_page_title;
+
    local viewLink, talkLink, editLink = 'view', 'talk', 'edit'
end
+
    if args.mini then
 
+
        viewLink, talkLink, editLink = 'v', 't', 'e'
function p._navbar(args)
+
    end
local good, title, talk_title;
+
local titleArg = 1
+
    local div = HtmlBuilder.create( 'div' )
+
    div
if args.collapsible then
+
        .addClass( 'noprint' )
titleArg = 2
+
        .addClass( 'plainlinks' )
if not args.plain then
+
        .addClass( 'hlist' )
args.mini = 1
+
        .addClass( 'navbar')
end
+
        .cssText( args.style )
if args.fontcolor then
+
args.fontstyle = 'color:' .. args.fontcolor .. ';'
+
    if args.mini then div.addClass('mini') end
end
+
args.style = 'float:left; text-align:left; width:6em;'
+
    if not (args.mini or args.plain) then
end
+
        div
+
            .tag( 'span' )
good, title, talk_title = pcall(getTitle, args[titleArg] or (':' .. mw.getCurrentFrame():getParent():getTitle()));
+
                .css( 'word-spacing', 0 )
if not good then
+
                .cssText( args.fontstyle )
return htmlError(title);
+
                .wikitext( args.text or 'This box:' )
end
+
                .wikitext( ' ' )
 
+
    end
if not title then
+
return htmlError('Page does not exist')
+
    if args.brackets then
end
+
        div
 
+
            .tag('span')
local mainpage = title.fullText;
+
                .css('margin-right', '-0.125em')
local talkpage = talk_title and talk_title.fullText or ''
+
                .cssText( args.fontstyle )
local editurl = title:fullUrl('action=edit');
+
                .wikitext( '[' )
 
+
                .newline();
local viewLink, talkLink, editLink = 'view', 'talk', 'edit'
+
    end
if args.mini then
+
viewLink, talkLink, editLink = 'v', 't', 'e'
+
    local ul = div.tag('ul');
end
+
 
+
    ul
local div = HtmlBuilder.create().tag('div')
+
        .tag( 'li' )
div
+
            .addClass( 'nv-view' )
.addClass('plainlinks')
+
            .wikitext( '[[' .. mainpage .. '|' )
.addClass('hlist')
+
            .tag( 'span ' )
.addClass('navbar')
+
                .attr( 'title', 'View this template' )
.cssText(args.style)
+
                .cssText( args.fontstyle or '' )
 
+
                .wikitext( viewLink )
if args.mini then div.addClass('mini') end
+
                .done()
 
+
            .wikitext( ']]' )
if not (args.mini or args.plain) then
+
            .done()
div
+
        .tag( 'li' )
.tag('span')
+
            .addClass( 'nv-talk' )
.css('word-spacing', 0)
+
            .wikitext( '[[' .. talkpage .. '|' )
.cssText(args.fontstyle)
+
            .tag( 'span ' )
.wikitext(args.text or 'This box:')
+
                .attr( 'title', 'Discuss this template' )
.wikitext(' ')
+
                .cssText( args.fontstyle or '' )
end
+
                .wikitext( talkLink )
 
+
                .done()
if args.brackets then
+
            .wikitext( ']]' );
div
+
.tag('span')
+
    if not args.noedit then  
.css('margin-right', '-0.125em')
+
        ul
.cssText(args.fontstyle)
+
            .tag( 'li' )
.wikitext('[')
+
                .addClass( 'nv-edit' )
.newline();
+
                .wikitext( '[' .. editurl .. ' ' )
end
+
                .tag( 'span ' )
 
+
                    .attr( 'title', 'Edit this template' )
local ul = div.tag('ul');
+
                    .cssText( args.fontstyle or '' )
 
+
                    .wikitext( editLink )
ul
+
                    .done()
.tag('li')
+
                .wikitext( ']' );
.addClass('nv-view')
+
    end
.wikitext('[[' .. mainpage .. '|')
+
.tag('span')
+
    if args.brackets then
.attr('title', 'View this template')
+
        div
.cssText(args.fontstyle or '')
+
            .tag('span')
.wikitext(viewLink)
+
                .css('margin-left', '-0.125em')
.done()
+
                .cssText( args.fontstyle or '' )
.wikitext(']]')
+
                .wikitext( ']' )
.done()
+
                .newline();
.tag('li')
+
    end
.addClass('nv-talk')
+
.wikitext('[[' .. talkpage .. '|')
+
    return div
.tag('span')
 
.attr('title', 'Discuss this template')
 
.cssText(args.fontstyle or '')
 
.wikitext(talkLink)
 
.done()
 
.wikitext(']]');
 
 
 
if not args.noedit then
 
ul
 
.tag('li')
 
.addClass('nv-edit')
 
.wikitext('[' .. editurl .. ' ')
 
.tag('span')
 
.attr('title', 'Edit this template')
 
.cssText(args.fontstyle or '')
 
.wikitext(editLink)
 
.done()
 
.wikitext(']');
 
end
 
 
 
if args.brackets then
 
div
 
.tag('span')
 
.css('margin-left', '-0.125em')
 
.cssText( args.fontstyle or '')
 
.wikitext(']')
 
.newline();
 
end
 
 
if args.collapsible then
 
div
 
.done()
 
.tag('span')
 
.css('font-size', '110%')
 
.cssText(args.fontstyle or '')
 
.wikitext(args[1])
 
end
 
 
 
return tostring(div.allDone())
 
 
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

Revisión del 04:45 16 mar 2013

Uso


local p = {}

local HtmlBuilder = require('Module:HtmlBuilder')

function trim(s)
    return (mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1"))
end

function _navbar( args )
    if not args[1] then
        local span = HtmlBuilder.create('span')
 
        span
            .addClass('error')
            .css('float', 'left')
            .css('white-space', 'nowrap')
            .wikitext('Error: No name provided')
 
        return span
    end
 
    local title;
    local pageName = trim(args[1])
    if mw.ustring.sub(pageName, 1, 1) == ':' then
        title = mw.title.new( mw.ustring.sub(pageName, 2) );
    else
        title = mw.title.new( pageName );
        if title.namespace == 0 then
            title = mw.title.makeTitle( 'Template', pageName );
        end    
    end       
 
    local mainpage = title.fullText;
    local talkpage = title.talkPageTitle and title.talkPageTitle.fullText or ''
    local editurl = title:fullUrl( 'action=edit' ); 
 
    local viewLink, talkLink, editLink = 'view', 'talk', 'edit'
    if args.mini then
        viewLink, talkLink, editLink = 'v', 't', 'e'
    end
 
    local div = HtmlBuilder.create( 'div' )
    div
        .addClass( 'noprint' )
        .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( '[' )
                .newline();
    end
 
    local ul = div.tag('ul');
 
    ul
        .tag( 'li' )
            .addClass( 'nv-view' )
            .wikitext( '[[' .. mainpage .. '|' )
            .tag( 'span ' )
                .attr( 'title', 'View this template' )
                .cssText( args.fontstyle or '' )
                .wikitext( viewLink )
                .done()
            .wikitext( ']]' )
            .done()
        .tag( 'li' )
            .addClass( 'nv-talk' )
            .wikitext( '[[' .. talkpage .. '|' )
            .tag( 'span ' )
                .attr( 'title', 'Discuss this template' )
                .cssText( args.fontstyle or '' )
                .wikitext( talkLink )
                .done()
            .wikitext( ']]' );
 
    if not args.noedit then 
        ul
            .tag( 'li' )
                .addClass( 'nv-edit' )
                .wikitext( '[' .. editurl .. ' ' )
                .tag( 'span ' )
                    .attr( 'title', 'Edit this template' )
                    .cssText( args.fontstyle or '' )
                    .wikitext( editLink )
                    .done()
                .wikitext( ']' );
    end
 
    if args.brackets then
        div
            .tag('span')
                .css('margin-left', '-0.125em')
                .cssText( args.fontstyle or '' )
                .wikitext( ']' )
                .newline();
    end
 
    return div
end

function p.navbar(frame)
    local origArgs
    -- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
    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
 
return p