Module:YouTube

From Uncyclopedia, the content-free encyclopedia
Jump to navigation Jump to search

local p = {}

-- Allowed float values
local FLOAT_VALUES = { left=true, right=true, center=true, none=true }

local function lc(v)
	return v and mw.ustring.lower(v) or nil
end

local function isFloatValue(v)
	return v and FLOAT_VALUES[lc(v)] or false
end

function p.main(frame)
	local args = frame:getParent().args

	-- Core parameters
	local id      = args[1] or args.id or "dQw4w9WgXcQ"
	local p2      = args[2]
	local p3      = args[3]
	local float   = args.float
	local caption = args.caption

	-- Determine float and caption
	local floatVal, captionVal
	if isFloatValue(p2) and not isFloatValue(p3) then
		floatVal = lc(p2)
		captionVal = p3 or caption or ""
	elseif isFloatValue(p3) and not isFloatValue(p2) then
		floatVal = lc(p3)
		captionVal = p2 or caption or ""
	elseif isFloatValue(p2) and isFloatValue(p3) then
		floatVal = lc(p3)
		captionVal = p2
	else
		floatVal = lc(float) or "right"
		captionVal = p2 or caption or ""
	end

	-- Normalize float for thumb class
	local thumbFloat
	if floatVal == "left" then
		thumbFloat = "left"
	elseif floatVal == "center" or floatVal == "none" then
		thumbFloat = "none"
	else
		thumbFloat = "right"
	end

	local out = {}
	
	-- Add TemplateStyles
	table.insert(out, frame:extensionTag{
		name = 'templatestyles',
		args = { src = 'YouTube/styles.css' }
	})

	-- Center wrapper
	if floatVal == "center" then
		table.insert(out, '<div class="center">')
	end

	table.insert(out,
		string.format('<div class="thumb t%s"><div class="thumbinner" style="width:min-content">', thumbFloat)
	)

	-- Forward all additional parameters to YouTube embed
	local embedArgs = { [1] = id }
	for k, v in pairs(args) do
		if k ~= "1" and k ~= "2" and k ~= "3" and k ~= "caption" and k ~= "float" then
			embedArgs[k] = v
		end
	end

	table.insert(out,
		frame:expandTemplate{
			title = "YouTube embed",
			args = embedArgs
		}
	)

	-- Caption
	table.insert(out, string.format('<div class="thumbcaption">%s</div>', captionVal))
	table.insert(out, '</div></div>')

	if floatVal == "center" then
		table.insert(out, '</div>')
	end

	return table.concat(out)
end

return p