Jump to content

Module:FreedImg/sandbox

From Wikisource
-- Implements [[Template:FreedImg]]
-- Arguments to the functions are as described on [[Template:FreedImg]] and [[Template:FreedImg/span]].

require('strict')

local p = {} --p stands for package
local getArgs = require('Module:Arguments').getArgs
local error_message = require('Module:Error')['error']

--[=[
	This is a limit on the max default image size
	to avoid multi-MB full-size images being loaded.
	If the image is smaller than this, it is loaded at full resolution.
]=]
local max_image_size = 1024

local function construct_image_markup(img_name, args)
	-- Get the image info.
	local img_title = mw.title.makeTitle('File', img_name)
	
	-- Whatever we were given, it was not a valid file name.
	if not img_title then
		local error = 'The specified image (' .. img_name .. ') is invalid'
		return error_message({message = error})
	end
	
	-- The filename given does not exist.
	-- This is an expensive function.
	if not img_title.file.exists then
		local error = 'The specified image (' .. img_name .. ') does not exist'
		return error_message({message = error})
	end
	
	local img_width_px
	
	if args['imgwidth'] then
		img_width_px = string.match(args['imgwidth'], '(%d+)px$')
		img_width_px = tonumber(img_width_px)
	end
	
	--[=[
		If the width parameter is in px, use that to get the image
		as the image is at most the size of the container.
	]=]
	if not img_width_px and args['width'] then
		img_width_px = string.match(args['width'], '(%d+)px$')
		img_width_px = tonumber(img_width_px)
	end
	
	-- If nil, use the default size.
	-- This is an expensive function.
	if not img_width_px then
		img_width_px = img_title.file['width']
	end
	
	-- Limit to the max size.
	img_width_px = math.min(img_width_px, max_image_size)
	
	-- construct the image markup we will use
	local img_markup = '[[File:' .. img_name .. '|' .. img_width_px .. 'px'
	for i, param in ipairs({'alt', 'link', 'page'}) do
		if args[param] then
			img_markup = img_markup .. '|' .. param .. '=' .. args[param]
		end
	end
	if args['imgclass'] then
		img_markup = img_markup .. '|class=' .. args['imgclass']
	end
	img_markup = img_markup .. ']]'
	return img_markup
end

local function construct_caption(is_div, args, caption, class)
	local tag = is_div and 'div' or 'span'
	
	local pstyle = {
		['text-align'] = args['talign'],
		['margin-right'] = args['tmright'],
		['margin-left'] = args['tmleft'],
		['text-indent'] = args['indent']
	}
	
	local para = mw.html.create(tag)
		:css(pstyle)
		:cssText(args['tstyle'])
		:addClass('imgCaption wst-freedimg-caption ' .. class)
		:wikitext(caption)
	
	return para
end

--[=[
	Main function
]=]
local function freedImg(is_div, args)
	local cats = {}
	
	local img_markup
	-- construct the image markup we will use
	if args['type'] == 'math' or args['type'] == 'score' or args['type'] == 'user' then
		-- math, score and user just place the markup directly
		img_markup = args['file']
	elseif not args.file then
		local error = 'The file parameter must be given (use \"missing\" if the image needs to be added later)'
		return error_message({message = error})
	elseif args.file == 'missing' then
		img_markup = mw.html.create('span')
			:addClass('wst-freedimg-missing')
			:wikitext('An image should appear at this position in the text.')
		img_markup = tostring(img_markup)
		table.insert(cats, 'Pages with missing images')
	elseif args.file == 'removed' then
		img_markup = mw.html.create('span')
			:addClass('wst-freedimg-missing')
			:wikitext('A non free image has been removed.<br>It can be viewed in the original at '.. args['srcdoc'] .. '.' )
		img_markup = tostring(img_markup)
		table.insert(cats, 'Pages with redacted images')
	else
		img_markup = construct_image_markup(args['file'], args)
	end
	
	local outer_tag = is_div and 'div' or 'span'
	local caption_tag = is_div and 'p' or 'span'
	
	local outer_div_class = {
		'wst-freedimg',
		args['cclass']
	}
	
	local outer_div_style = {
		['width'] = args['width'],
		['margin-right'] = args['margin-right'],
		['margin-left'] = args['margin-left'],
		['float'] = args['float'],
		['clear'] = args['clear']
	}
	
	local outer = mw.html.create(outer_tag)
	outer
		:addClass(table.concat(outer_div_class, ' '))
		:css(outer_div_style)
		:cssText(args['cstyle'])
	
	-- add the top caption, if there is one, to the outer div
	if args['top-caption'] then
		outer:node(construct_caption(is_div, args, args['top-caption'], 'wst-freedimg-caption-top'))
	end
	
	outer:wikitext(img_markup)
	
	-- add the bottom caption, if there is one, to the outer div
	if args['caption'] then
		outer:node(construct_caption(is_div, args, args['caption'], 'wst-freedimg-caption-bottom'))
	end
	
	outer = tostring(outer)
	
	for k, v in pairs(cats) do
		outer = outer .. '[[Category:' .. v .. ']]'
	end
	
	return outer
end

--[=[
	Make FreedImg accessible from other modules
]=]
function p._freedImg(is_div, args)
	return freedImg(is_div, args)
end

--[=[
	Construct a 'block' FreedImg
]=]
function p.freedImg(frame)
	local args = getArgs(frame)
	return freedImg(true, args)
end

--[=[
	Construct an 'inline' FreedImg
]=]
function p.freedImg_span(frame)
	local args = getArgs(frame)
	return freedImg(false, args)
end

return p