Moduł:Listen

Z Wikipedii, wolnej encyklopedii
 Dokumentacja modułu [stwórz] [odśwież]
local p = {}

function p.main(frame)
	-- Trim the args
	local origArgs = frame:getParent().args
	local args = {}
	for k, v in pairs(origArgs) do
		v = v:match('^%s*(.-)%s*$')
		if v ~= '' then
			args[k] = v
		end
	end

	-- Get the parameters
	local header = args['nagłówek'] or args.header
	local playerType = args.typ or args['type'] or 'default'

	local files = {}
	local playerWidth = 240

	-- Determine the image
	local images = {
		music = 'Gnome-mime-audio-openclipart.svg',
		muzyka = 'Gnome-mime-audio-openclipart.svg',
		speech = 'Audio-input-microphone.svg',
		mowa = 'Audio-input-microphone.svg',
		sound = 'Gnome-mime-sound-openclipart.svg',
		['dźwięk'] = 'Gnome-mime-sound-openclipart.svg',
		default = 'Gnome-mime-sound-openclipart.svg'
	}
	local image = images[playerType] or images.default
	image = '[[File:' .. image .. '|x32px|link=]]'

	-- Get the files
	local file = getFileData(args, '')
	while file ~= nil do
		table.insert(files, file)
		file = getFileData(args, #files + 1)
	end

	-- For single-file boxes use file title as header if not specified
	if header == nil then
		if #files == 1 and files[1].title ~= nil then
			header = files[1].title
			files[1].title = nil -- Don't display the title twice
		else
			header = "'''Odsłuchaj'''"
		end
	end

	return renderBox(header, image, files, playerWidth)
end

function getFileData(args, number)
	local numberSp = ''
	if number ~= '' then
		numberSp = ' ' .. number
	end

	local file = args['plik' .. numberSp] or args['filename' .. number]
	local title = args['tytuł' .. numberSp] or args['title' .. number]
	local description = args['opis' .. numberSp] or args['description' .. number]
	local alt = args['alt' .. numberSp] or args['alt' .. number]

	-- If no file is specified, return nil
	if file == nil then
		return nil
	end

	if title ~= nil then
		title = '[[:File:' .. file .. '|' .. title .. ']]'
	end

	return {
		name = file,
		title = title,
		description = description,
		alt = alt
	}
end

function renderBox(header, image, files, playerWidth)
	local box = mw.html.create('div'):addClass('listen-box')

	box:tag('div')
		:addClass('listen-header')
		:wikitext(image .. '<span>' .. header .. '</span>')

	for i, file in ipairs(files) do
		box:node(renderRow(file.name, file.title, file.description, file.alt, playerWidth))
	end

	local theseFiles = (#files == 1) and 'tego pliku' or 'tych plików'
	box:tag('div')
		:addClass('listen-footer')
		:wikitext("''Problem z odtwarzaniem " .. theseFiles .. "? Zobacz&nbsp;[[:commons:Commons:Pomoc - multimedia|strony&nbsp;pomocy]].''")

	return tostring(box)
end

function renderRow(fileName, title, description, alt, playerWidth)
	local row = mw.html.create('div'):addClass('listen-file')

	-- Display the title (if provided)
	if title then
		row:tag('span'):wikitext(title)
	end

	-- Insert the media player
	if not alt or alt == '' then
		alt = ''
	else
		alt = '|alt='..alt
	end
	row:wikitext('[[File:' .. fileName .. '|' .. playerWidth .. 'px|class=listen-player' .. alt ..']]')

	-- Display the description (if provided)
	if description then
		row:tag('div'):wikitext("\n" .. mw.text.trim(description))
	end

	return row
end

return p