Jump to content

Module:Wide image/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
sync
some slight refactor
Line 79: Line 79:
function wideimage(image, width, height, caption, boxwidth, float, alt, border, capalign, dir)
function wideimage(image, width, height, caption, boxwidth, float, alt, border, capalign, dir)
if image then
if not image then return '' end
image = getfilename(image)
image = getfilename(image)
local iwidth = getwidth(image, width or '0', height or '0')
local imagewidth = getwidth(image, width or '0', height or '0')
if width == nil then
width = iwidth .. 'px'
if width == nil then
width = imagewidth .. 'px'
end
end
local rtl = dir and dir == 'rtl' or nil
local noborder = border and border == 'no' or nil
if tonumber(width) then
width = width .. 'px'
local maxwidth = noborder and (iwidth .. 'px') or ((iwidth + 8) .. 'px')
end
local r,d = getcontainers(noborder, float or '', boxwidth or 'auto', maxwidth)
local rtl = dir and dir == 'rtl' or nil
local noborder = border and border == 'no' or nil
if tonumber(width) then
width = width .. 'px'
local maxwidth = noborder and (imagewidth .. 'px') or ((imagewidth + 8) .. 'px')
end
local r,d = getcontainers(noborder, float or '', boxwidth or 'auto', maxwidth)
d:tag('div')
:addClass('noresize')
d:tag('div')
:css('overflow', 'auto')
:addClass('noresize')
:css('direction', rtl and 'rtl' or nil)
:css('overflow', 'auto')
:wikitext(getimage(image,width,alt,caption or '',rtl))
:css('direction', rtl and 'rtl' or nil)
if caption then
:wikitext(getimage(image,width,alt,caption or '',rtl))
d = d:tag('div')
if caption then
:addClass('thumbcaption')
d = d:tag('div')
:css('text-align', capalign)
:addClass('thumbcaption')
if noborder == nil then
d:tag('div')
:css('text-align', capalign)
if noborder == nil then
:addClass('magnify')
d:tag('div')
:wikitext('[[:File:' .. image .. '| ]]')
:addClass('magnify')
end
d:wikitext(caption)
:wikitext('[[:File:' .. image .. '| ]]')
end
end
d:wikitext(caption)
return tostring(r)
end
end
return ''
return tostring(r)
end
end


function p.main(frame)
function p.main(frame)
local getArgs = require('Module:Arguments').getArgs
local args = require('Module:Arguments').getArgs(frame)
local args = getArgs(frame)
return wideimage(
return wideimage(
args['image'] or args[1], args[2] or nil, -- width
args['image'] or args[1],
args[2] or nil, -- width
args['height'] or nil, args['caption'] or args[3],
args['height'] or nil,
args['caption'] or args[3],
args['width'] or args[4], args['align'] or args[5],
args['width'] or args[4],
args['align'] or args[5],
args['alt'], args['border'], args['align-cap'], args['dir']
args['alt'],
args['border'],
args['align-cap'],
args['dir']
)
)
end
end



Revision as of 18:18, 1 December 2020

-- This module implements [[template:wide image]] and [[template:panorama]]
local p = {}

local function getfilename(s)
	s = mw.ustring.gsub(s or '', '^%s*[Ff][Ii][Ll][Ee]%s*:%s*', '')
	s = mw.ustring.gsub(s or '', '^%s*[Ii][Mm][Aa][Gg][Ee]%s*:%s*', '')
	return s
end

local function getwidth(s, w, h)
	w = mw.ustring.gsub(w or '0', '^%s*(%d+)%s*[Pp][Xx]*%s*$', '%1')
	h = mw.ustring.gsub(h or '0', '^%s*(%d+)%s*[Pp][Xx]*%s*$', '%1')
	w = tonumber(w) or 0
	h = tonumber(h) or 0
	if w > 0 then
		return w
	end
	
	local file = s and mw.title.new('File:' .. mw.uri.decode(mw.ustring.gsub(s,'%|.*$',''), 'WIKI'))
	file = file and file.file or {width = 0, height = 0}

	if h > 0 then
		w = math.floor(h * (tonumber(file.width) or 0)/(tonumber(file.height) or 1) + 0.5)
		if w > 0 then
			return w
		end
	end
	
	w = tonumber(file.width) or 0
	return w
end

local function getimage(s, w, a, c, rtl)
	if c == 'thumb' or c == 'thumbnail' or c == 'frame' or c == 'border' then
		c = s
	elseif rtl and c ~= '' then
		c = '‪' .. c .. '‬'
	end
	return '[[File:' .. (s or '') .. '|' .. (w or '') .. '|alt=' .. (a or '') 
		.. '|' .. mw.text.unstrip(c or '') .. ']]'
end

local function getcontainers(noborder, float, width, maxwidth)
	local r = mw.html.create('div')
	if noborder then
		if float == 'left' then
			r:addClass('floatleft')
		elseif float == 'right' then
			r:addClass('floatright')
		elseif float == 'none' then
			r:addClass('floatnone')
		else -- center is default
			r:addClass('floatnone')
			r:css('margin-left', 'auto')
			r:css('margin-right', 'auto')
			r:css('overflow', 'hidden')
		end
	else
		r:addClass('thumb')
		if float == 'left' then
			r:addClass('tleft')
		elseif float == 'right' then
			r:addClass('tright')
		elseif float == 'none' then
			r:addClass('tnone')
		else -- center is default
			r:addClass('tnone')
			r:css('margin-left', 'auto')
			r:css('margin-right', 'auto')
			r:css('overflow', 'hidden')
		end
	end
	r:css('width', width)
	r:css('max-width', maxwidth)
	local d = noborder and r or r:tag('div'):addClass('thumbinner')
	
	return r,d
end
	
function wideimage(image, width, height, caption, boxwidth, float, alt, border, capalign, dir)
	if not image then return '' end
	
	image = getfilename(image)
	local imagewidth = getwidth(image, width or '0', height or '0')
	if width == nil then
		width = imagewidth .. 'px'
	end
	
	if tonumber(width) then
		width = width .. 'px'
	end
	
	local rtl = dir and dir == 'rtl' or nil
	local noborder = border and border == 'no' or nil
	
	local maxwidth = noborder and (imagewidth .. 'px') or ((imagewidth + 8) .. 'px')
	
	local r,d = getcontainers(noborder, float or '', boxwidth or 'auto', maxwidth)
	
	d:tag('div')
		:addClass('noresize')
		:css('overflow', 'auto')
		:css('direction', rtl and 'rtl' or nil)
		:wikitext(getimage(image,width,alt,caption or '',rtl))
	if caption then
		d = d:tag('div')
				:addClass('thumbcaption')
				:css('text-align', capalign)
		if noborder == nil then
			d:tag('div')
				:addClass('magnify')
				:wikitext('[[:File:' .. image .. '| ]]')
		end
		d:wikitext(caption)
	end
	return tostring(r)
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	return wideimage(
			args['image'] or args[1],
			args[2] or nil, -- width
			args['height'] or nil,
			args['caption'] or args[3], 
			args['width'] or args[4],
			args['align'] or args[5], 
			args['alt'],
			args['border'],
			args['align-cap'],
			args['dir']
	)
end

return p