Difference between revisions of "Module:Team roster navbox"

From blackwiki
Jump to navigation Jump to search
blackwiki>The Earwig
m (Protected "Module:Team roster navbox": High-risk Lua module: over 20,000 mainspace transclusions ([Edit=Allow only template editors and admins] (indefinite) [Move=Allow only template editors and admins] (indefinite)))
blackwiki>Frietjes
(now that we have nowrapitems=yes in navbox, let's make this module use that, and add automatic link coloring to make it more useful (will revert if I see any problems))
Line 1: Line 1:
 
+
-- This module implements {{team roster navbox}}
 
local me = { }
 
local me = { }
  
 
local Navbox = require('Module:Navbox')
 
local Navbox = require('Module:Navbox')
 +
 +
local function colorlinks(v, s)
 +
if v and v ~= '' and s and s ~= '' then
 +
if not mw.ustring.match(v, '<span style') then
 +
v = mw.ustring.gsub(v, '%[%[([^%[%]|]*)%]%]',
 +
'[[%1|<span style="' .. s .. '>%1</span>]]')
 +
v = mw.ustring.gsub(v, '%[%[([^%[%]|]*)|([^%[%]|]*)%]%]',
 +
'[[%1|<span style="' .. s .. '>%2</span>]]')
 +
end
 +
end
 +
return v
 +
end
  
 
function me.generateRosterNavbox(frame)
 
function me.generateRosterNavbox(frame)
 
     local args = { }
 
     local args = { }
 
     local parentArgs = frame:getParent().args
 
     local parentArgs = frame:getParent().args
 +
   
 +
    -- Massage the styles for coloring the links
 +
    local titlestyle = parentArgs['titlestyle'] or ''
 +
    local abovestyle = parentArgs['abovestyle'] or ''
 +
    local groupstyle = parentArgs['groupstyle'] or ''
 +
    local belowstyle = parentArgs['belowstyle'] or ''
 +
    local basestyle  = parentArgs['basestyle'] or ''
 +
   
 +
    if basestyle ~= '' then
 +
    titlestyle = basestyle .. ';' .. titlestyle
 +
    abovestyle = basestyle .. ';' .. abovestyle
 +
    groupstyle = basestyle .. ';' .. groupstyle
 +
    belowstyle = basestyle .. ';' .. belowstyle
 +
    end
  
     -- Massage the arguments before passing them to the Navbox helper function
+
     -- Color links before passing them to the Navbox helper function
 
 
 
     for argName, value in pairs(parentArgs) do
 
     for argName, value in pairs(parentArgs) do
         if value ~= '' then
+
         if value ~= '' and type(argName) == 'string' then
            if type(argName) == 'string' and mw.ustring.find(argName, '^list') then
+
            if argName == 'title' then
                 local listValues = { }
+
                 value = colorlinks(value, titlestyle)
                 for entry in mw.text.gsplit(value, '[\n\r]+') do
+
            elseif argName == 'above' then
                  entry = mw.ustring.gsub(entry, '([^%*])%s+', '%1&nbsp;')
+
                 value = colorlinks(value, abovestyle)
                  table.insert(listValues, entry)
+
            elseif mw.ustring.find(argName, '^group[0-9]') then
                 end
+
                value = colorlinks(value, groupstyle)
            value = table.concat(listValues, '\n')
+
            elseif argName == 'below' then
 +
                 value = colorlinks(value, belowstyle)
 
             end
 
             end
            args[argName] = value
 
 
         end
 
         end
 
     end
 
     end
 +
    args['nowrapitems'] = 'yes'
  
 
     -- Note Navbox.navbox() has a kludge to order the parent frame's args
 
     -- Note Navbox.navbox() has a kludge to order the parent frame's args

Revision as of 13:00, 3 June 2017

This module is used to implement {{Team roster navbox}}, a helper template used to implement the individual team roster navboxes for the Major League Baseball teams.

At present, it parses any list<number> arguments to the template and replaces spaces in each list item with &nbsp; in order to prevent wrapping. It then calls Module:Navbox._navbox() to create the navbox. (Note the preprocessing done by Module::Navbox.navbox() has not been copied to this module and so is skipped.)


-- This module implements {{team roster navbox}}
local me = { }

local Navbox = require('Module:Navbox')

local function colorlinks(v, s)
	if v and v ~= '' and s and s ~= '' then
		if not mw.ustring.match(v, '<span style') then
			v = mw.ustring.gsub(v, '%[%[([^%[%]|]*)%]%]', 
				'[[%1|<span style="' .. s .. '>%1</span>]]')
			v = mw.ustring.gsub(v, '%[%[([^%[%]|]*)|([^%[%]|]*)%]%]', 
				'[[%1|<span style="' .. s .. '>%2</span>]]')
		end
	end
	return v
end

function me.generateRosterNavbox(frame)
    local args = { }
    local parentArgs = frame:getParent().args
    
    -- Massage the styles for coloring the links
    local titlestyle = parentArgs['titlestyle'] or ''
    local abovestyle = parentArgs['abovestyle'] or ''
    local groupstyle = parentArgs['groupstyle'] or ''
    local belowstyle = parentArgs['belowstyle'] or ''
    local basestyle  = parentArgs['basestyle'] or ''
    
    if basestyle ~= '' then
    	titlestyle = basestyle .. ';' .. titlestyle
    	abovestyle = basestyle .. ';' .. abovestyle
    	groupstyle = basestyle .. ';' .. groupstyle
    	belowstyle = basestyle .. ';' .. belowstyle
    end

    -- Color links before passing them to the Navbox helper function
    for argName, value in pairs(parentArgs) do
        if value ~= '' and type(argName) == 'string' then
            if argName == 'title' then
                value = colorlinks(value, titlestyle)
            elseif argName == 'above' then
                value = colorlinks(value, abovestyle)
            elseif mw.ustring.find(argName, '^group[0-9]') then
                value = colorlinks(value, groupstyle)
            elseif argName == 'below' then
                value = colorlinks(value, belowstyle)
            end
        end
    end
    args['nowrapitems'] = 'yes'

    -- Note Navbox.navbox() has a kludge to order the parent frame's args
    -- into a specific order. For now, this is omitted from this module.

    return Navbox._navbox(args)

end  -- function me.generateRosterNavbox

return me