Jump to content

Module:Wikipedia ads/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 04:17, 10 November 2014 (make a start at converting this to mw.html). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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