Jump to content

Module:Portal

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 17:54, 30 March 2013 (copy from Module:User:Mr. Stradivarius/sandbox). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- This module implements {{Portal}}

local p = {}
 
local HtmlBuilder = require('Module:HtmlBuilder')
local HtmlError = require('Module:Error')

local function _portal(args)

    local root = HtmlBuilder.create('div')
    
    root
        .addClass('noprint')
        .addClass((args.left == 'yes' and 'tleft') or 'tright')
        .addClass('portal')
        .css('border', 'solid #aaa 1px')
        .css('margin', args.margin or (args.left == 'yes' and '0.5em 1em 0.5em 0') or '0.5em 0 0.5em 1em')
        .newline()

    -- start of the wikitext table
    tableroot = root.tag('table')
        .css('background', '#f9f9f9')
        .css('font-size', '85%')
        .css('line-height', '110%')
        .css('max-width', '175px')
        .css(args.boxsize and 'width', args.boxsize and (args.boxsize .. 'px'))
    
    local anyPortals = false -- Track whether any portals have been specified.
    for k,v in pairs(args) do
        if type(k) == "number" then
            anyPortals = true
            
            v = mw.ustring.match(v, '^%s*(.*%S)') or ''  -- Trim whitespace.
            
            -- Portal image names are stored in subtemplates of [[Template:Portal/Images]].
            -- The name of the subtemplate is the portal name in all lower case, but with
            -- the first character in upper case.
            
            -- Work out the image subtemplate location.
            local vfirst, vbody = mw.ustring.match(v, "^(.)(.*)")
            local imageloc = 'Portal/Images/' .. mw.ustring.upper(vfirst or "") .. mw.ustring.lower(vbody or "")

            -- If the image subtemplate exists, expand it to get the name.
            -- Otherwise, use the default of Portal-puzzle.svg.
            local imagename
            if mw.title.new(imageloc, 'Template').exists then
                imagename = frame:expandTemplate{ title = imageloc }
            else
                imagename = 'Portal-puzzle.svg'
            end
            
            tablerow = tableroot.newline().tag('tr').attr('valign', 'middle')
                tablerow.tag('td')
                    .css('text-align', 'center')
                    .wikitext('[[File:' .. imagename .. '|32x28px|alt=Portal icon]]')
                tablerow.tag('td')
                    .css('padding', '0 0.2em')
                    .css('vertical-align', 'middle')
                    .css('font-style', 'italic')
                    .css('font-weight', 'bold')
                    .wikitext('[[Portal:' .. v .. '|' .. v .. ((args['break'] == 'yes' and '<br />') or " ") .. 'portal]]')

        end
    end

    -- If no portals have been specified, throw an error and add a tracking category.
    if anyPortals == false then
        tableroot.wikitext(
            tostring(HtmlError.error{'No portals specified: please specify at least one portal'})
            .. '[[Category:Portal templates without a parameter]]'
        )
    end

    return tostring(root)
end

function p.portal(frame)
    local origArgs
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. If the invoking template passed any args, use
        -- them. Otherwise, use the args that were passed into the template.
        origArgs = frame:getParent().args
        for k, v in pairs(frame.args) do
            origArgs = frame.args
            break
        end
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        origArgs = frame
    end
    
        -- ParserFunctions considers the empty string to be false, so to preserve the previous 
        -- behavior of the template, change any empty arguments to nil, so Lua will consider
        -- them false too.
    local args = {}
    for k, v in pairs(origArgs) do
        if mw.ustring.match(v, "%S") then
            args[k] = v
        end
    end

    return _portal(args)
end

return p