Jump to content

Module:Sandbox/Mr. Stradivarius/sandbox3

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 20:19, 6 August 2013 (WikiProject banner code). 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)

local htmlBuilder = require( 'Module:HtmlBuilder' )


-- Define a custom error function for this module.
local function err( obj, msg, cat )
    if type( obj ) ~= 'table' then
        error( 'no object specified to pass errors to' )
    end
    if not msg then
        error( 'no error message specified' )
    end
    obj.errors = obj.errors or {}
    table.insert( obj.errors, msg )
    if cat then
        obj.errorcats = obj.errorcats or {}
        table.insert( obj.errorcats, cat )
    end
end

local function yesno( val, default )
    val = type( val ) == 'string' and mw.ustring.lower( val ) or val -- put in lower case
    if not val or val == 'no' or val == 'n' or val == 'false' or tonumber( val ) == 0 then
        return false
    elseif val == true or val == 'yes' or val == 'y' or val == 'true' or tonumber( val ) == 1 then
        return true
    else
        return default
    end
end

local banner = {}
banner.__index = banner

function banner.new( data )
    data = data or {}
    local obj = {}
    function obj:err( msg, cat )
        return err( obj, msg, cat )
    end
    if not data.project then
        obj:err( 'no project specified' )
    end
    obj.project = data.project or ''
    obj.banner_name = data.banner_name or 'Template:WikiProject ' .. obj.project
    obj.project_name = data.project_name or 'WikiProject ' .. obj.project
    obj.project_link = data.project_link or 'Wikipedia:WikiProject ' .. obj.project
    obj.image_left = data.image_left
    obj.image_right = data.image_right
    obj.small = yesno( data.small, false )
    if obj.small then
        obj.image_left_size = data.image_left_small or '40px'
        obj.image_right_size = data.image_right_small or '40px'
    else
        obj.image_left_size = data.image_left_large or '80px'
        obj.image_right_size = data.image_right_large or '80px'
    end
    obj.main_article = data.main_article
    obj.main_text = data.main_text
    if not obj.main_text then
        obj.main_text = mw.ustring.format( 
            [=[This %s is within the scope of '''[[%s|%s]]''', a collaborative effort to improve the coverage of %s on Wikipedia.]=],
            'page', obj.project_link, obj.project_name, obj.main_article or '[[' .. obj.project .. ']]'
        )
        if not obj.small then
            obj.main_text = obj.main_text .. mw.ustring.format(
                [=[If you would like to participate, please visit the project page, where you can join the [[%s|discussion]] and see a list of open tasks.]=],
                'Wikipedia talk:WikiProject ' .. obj.project
            )
        end
    end
    obj.portal = data.portal
    setmetatable( obj, banner )
    return obj
end

local p = {}

function p.main( frame )
    local myBanner = banner.new{ project = "Foo" }
    return myBanner.main_text
end

return p