Documentation for this module may be created at Module:FileSD/sandbox/doc

Code

local yesno = require('Module:Yesno')
local sdc = require ('Module:Structured Data')
local getArgs = require('Module:Arguments').getArgs

--------------------------------------------------------------------------------
-- Builder
--------------------------------------------------------------------------------
local file = {}

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.text.trim(w or '0')
	if w:find('^x') then
		-- [[File:Example.svg|x1500px]] syntax, i.e. the image will be 1500px high
		h = w:gsub('^x', '')
		w = '0'
	end
	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, dir)
	if c == 'thumb' or c == 'thumbnail' or c == 'frame' or c == 'border' then
		c = s
	elseif dir and c ~= '' then
		c = (dir == 'rtl' and '‪' or '‫') .. 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', '0 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', '0 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 file.wideimage(image, width, height, caption, boxwidth, float, alt, border, capalign, dir)
	if image then
		image = getfilename(image)
		local iwidth = getwidth(image, width or '0', height or '0')
		if width == nil then
			width = iwidth .. 'px'
		end
	
		if dir ~= 'ltr' and dir ~= 'rtl' then
			dir = nil
		end
		local noborder = border and border == 'no' or nil
		local maxwidth = noborder and (iwidth .. 'px') or ((iwidth + 8) .. 'px')
		local r,d = getcontainers(noborder, float or '', boxwidth or 'auto', maxwidth)
		
		if tonumber(width) then
			width = width .. 'px'
		end
		
		d:tag('div')
			:addClass('noresize')
			:css('overflow', 'auto')
			:css('direction', dir)
			:wikitext(getimage(image,width,alt,caption or '',dir))
		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
	return ''
end

local function get(val)
	local text = {}
	local text2 = ''
	for i, v in ipairs(val) do
    	text[#text+1] = mw.wikibase.getLabel(v.mainsnak.datavalue.value.id)
	end
	
	if not text[1] then
		return ''
	elseif not text[2] then 
		text2 = text[1]:gsub("^%l", string.upper)
	else
		text[1] = text[1]:gsub("^%l", string.upper)
		text2 = table.concat(text, ', ')
	end
	return text2
end

function file.new(data, pid, name, pref, alt, position, size)
	local val = mw.wikibase.getBestStatements(data, pid)
	local file = ''

	if name then
		file = '[[File:' .. name .. '|' .. pref .. '|' .. position .. '|' .. size .. '|alt=' .. alt .. '|' 
		if mw.wikibase.mediainfo.getCaption(data) then
			file = file .. mw.wikibase.mediainfo.getCaption(data) .. '. '
		end
		if val[1] then
			local icon = ' [[File:Blue pencil.svg|frameless|text-top|5px|alt=Edit on Wikimedia Commons|link=c:File:' .. name .. '#ooui-php-8' .. '|Edit on Wikimedia Commons]]'
			file = file .. mw.wikibase.getLabel(pid):gsub("^%l", string.upper) .. ': ' .. get(val) .. '.' .. icon
		end
		if string.lower(pref) == 'wide' then
			return file.wideimage('Helsinki z00.jpg', nil, 'caption thiss!', '1800px')
			--{{wide image|Helsinki z00.jpg|1800px|alt=|}}
		else
			return file .. ']]'
		end
	end
	return nil

end
	
--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------
local p = {}

function p.main(frame)
	local title = ''
	local name = getArgs(frame, {
	wrappers = 'Template:FileSD'
	})
	local pid = name['pid'] or 'P275'
	local pref = name['format'] or 'thumb'
	local alt = name['alt'] or ''
	local position =  name['position'] or 'right'
	local size = name['size'] or '220'
	
	if tonumber(size) ~= nil then
		size = size .. 'px' -- if 'px' is not included
	end;
	if mw.title.getCurrentTitle().namespace == 6 then
		name[1] = mw.title.getCurrentTitle().text
		title = sdc.getMID()
	elseif name then
		title = mw.wikibase.mediainfo.getEntityIdForTitle('File:' .. getfilename(name[1]))
	else
		return ''
	end
	
	return file.new(title, pid, name[1], pref, alt, position, size)
end

return p