Module:Portal

-- This module implements

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('') tablerow.tag('td') .css('padding', '0 0.2em') .css('vertical-align', 'middle') .css('font-style', 'italic') .css('font-weight', 'bold') .wikitext('' .. v .. ((args['break'] == 'yes' and ' ') 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'})            .. ''        ) 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