Module:Wikipedia ads/sandbox
![]() | This is the module sandbox page for Module:Wikipedia ads (diff). See also the companion subpage for test cases (run). |
This module displays banner-style adverts for various Wikipedia projects, processes and pages. It implements Template:Wikipedia ads.
==}}
Usage
From wikitext
From wikitext, usually this module should be used via Template:Wikipedia ads. However, it is also possible to use the syntax {{#invoke:wikipedia ads|main|...}}
, where "..." are the template arguments. See the template page for documentation and for a list of available parameters.
From Lua
First, load the module:
local mWikipediaAds = require('Module:Wikipedia ads')
Then you can generate Wikipedia ads using the "_main" function:
mWikipediaAds._main(args)
args is a table of arguments that can be used by the module. See Template:Wikipedia ads for a complete list of arguments and for other documentation.
Adding and removing ads
For instructions on creating the animated GIF files used in the ads, see Template:Wikipedia ads#Creating ads. For instructions on adding and removing images from the module, see Module:Wikipedia ads/list.
local yesno = require('Module:Yesno')
local p = {}
local function makeWikilink(page, display)
if display then
return string.format('[[%s|%s]]', page, display)
else
return string.format('[[%s]]', page)
end
end
local function makeUrlLink(url, display)
url = tostring(url)
return string.format('[%s %s]', url, display)
end
local function colorText(s, color)
return string.format('<span style="color:%s">%s</span>', color, s)
end
local function renderAd(imageData, args, title, showPurge)
local width = tonumber(args.width) or 468
local maxWidth = width + 9
local linkColor = args.linkcolor or '#002bb8'
-- Table root
local root = mw.html.create('table')
root
:addClass('plainlinks qxz-ads')
:css('color', args.color or '#555555')
:css('border', 'none')
:css('background', args.background)
:css('line-height', '1em')
:css('font-size', '90%')
:css('display', 'block')
:css('overflow', 'auto')
:css('max-width', maxWidth .. 'px')
if args.float then
root:css('float', args.float)
else
root:css('margin', '0 auto')
end
-- Image row
root
:tag('tr')
:tag('td')
:attr('colspan', 2)
:css('border', 'none')
:wikitext(string.format(
'[[File:%s|%dpx|alt=Wikipedia ad for %s|link=%s]]',
imageData.image,
width,
imageData.link,
imageData.link
))
-- Links row
if not yesno(args.nolinks) then
local linksRow = root:tag('tr')
-- Wikipedia ads link
linksRow
:tag('td')
:css('border', 'none')
:wikitext(makeWikilink(
'Template:Wikipedia ads',
colorText('Wikipedia ads', linkColor)
))
-- File info, purge and ID
local links = {}
links[#links + 1] = makeWikilink(
':File:' .. imageData.image,
colorText('file info', linkColor)
)
if showPurge then
links[#links + 1] = makeUrlLink(
title:fullUrl{action = 'purge'},
colorText('show another', linkColor)
)
end
links[#links + 1] = '#' .. imageData.id
linksRow
:tag('td')
:css('text-align', 'right')
:css('border', 'none')
:wikitext(table.concat(links, ' – '))
end
end
return p