Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Chart display/sandbox

From Wikipedia, the free encyclopedia
local p = {}

local notblank = function(v) return (v or '') ~= '' end
local ifblank = function (v, alt) if notblank(v) then return v else return alt end end
local getArgs = require('Module:Arguments').getArgs

function p._chart(args)
	local values = {}
	local chartArgs = {}
	local templateArgs = {
		right = 1, left = 1, center = 1, none = 1,
		thumb = 1, thumbnail = 1, border = 1
	}
	local alignClasses = {center = 'none', none = 'none', left = 'left', right = 'right'}
	
	for k, v in pairs(args) do --ipairs fails with blank positional arguments
		if tonumber(k) then
			local lv = mw.ustring.lower(v)
			if v:sub(1,4) == 'arg:' then
				chartArgs.insert(v)
			elseif mw.ustring.match(lv, "%d+%s*px%s*$") then
				values.width = lv
			elseif templateArgs[lv] then
				values[lv] = lv
			elseif v ~= '' then
				values.caption = v
			end
		end
	end
	local opts = {
		align = ifblank(args.align, values.right or values.left or values.center or values.none),
		thumb = notblank(args.thumb) or values.thumb or values.thumbnail,
		width = ifblank(args.width, values.width),
		border = notblank(args.border) or values.border,
		chart = ifblank(args.chart, args.definition)
	}
	assert(notblank(opts.chart), 'Must specify chart= or definition=')

	local caption = ifblank(args.caption, ifblank(
		values.caption, notblank(args.title) and (args.title .. ".") or ""
	))
	if not notblank(args.hideSource) then
		caption = caption .. ' See or edit [[c:Data:'
		if notblank(ifblank(args.source, args.data)) then
			caption = caption .. ifblank(args.source, args.data) .. '|source data]].'
		else
			caption = caption .. opts.chart .. '|chart definition]].'
		end
	end

	local html = mw.html.create('div'):addClass((opts.align == 'center') and 'center' or nil)
	if opts.thumb then
		html = html:tag('div'):addClass('thumb'):addClass('t' .. (alignClasses[opts.align] or 'right'))
			:css('width', (not opts.width) and '100%' or nil)
			:tag('div'):addClass('thumbinner')
				:css('width', opts.width and 'fit-content' or nil)
	end
	html = html:tag('div'):addClass('enwiki-chart')
		:addClass(opts.thumb and 'thumbimage' or ('float' .. (alignClasses[opts.align] or 'none')))
		:css('width', opts.width)
		:css('border-style', opts.border and '1' or nil)
		:cssText(ifblank(args.style))
		:tag('div'):addClass('enwiki-chart-content')
			:wikitext(mw.getCurrentFrame():preprocess(
				"{{#chart:" .. 
				opts.chart ..
				(notblank(args.data) and ("|data=" .. args.data) or "") ..
				((#chartArgs > 0) and ("|" .. table.concat(chartArgs, '|')) or "") ..
				"}}"
			))
			:done()
	if opts.thumb then
		html = html:done():tag('div'):addClass('thumbcaption')
	else
		html = html:tag('div'):addClass('center')
		:tag('small')
	end

	return tostring(html:wikitext(caption):allDone()) ..
		(opts.thumb and '' or '<div style="clear:both;"></div>')
end
function p.chart(frame)
	return p._chart(getArgs(frame))
end

return p