Dokumentation für das Modul Scroll Gallery[Ansicht] [Bearbeiten] [Versionsgeschichte] [Aktualisieren]

Anwendung

Das Modul wird direkt von der Vorlage {{Scroll Gallery}} aufgerufen. Parameterbeschreibung siehe dort. Die Modulbeschreibung befindet sich in Wikivoyage:Scroll Gallery, die Stilanweisungen in Vorlage:Scroll Gallery/styles.css.

Versionsbezeichnung auf Wikidata: 2023-05-03 Ok!

Globale Funktionen

function sg.gallery( frame )
  • Vorgaben:
    • frame
      • tabelle: Parametertabelle, die vom #invoke-Aufruf übergeben wird.
  • Zurückgelieferter Wert:
    • string: MediaWiki-Quellcode der Bildergalerie (Scroll Gallery).

Benötigte weitere Module

Dieses Modul benötigt folgende weitere Module: Yesno
Hinweise
-- module import
local yn = require( 'Module:Yesno' )

-- module variable
local sg = {
	-- administration
	moduleInterface = {
		suite  = 'Scroll Gallery',
		serial = '2023-05-03',
		item   = 87862715
	},

	-- miscellaneous messages
	texts = {
		badSize        = '<span class="error">Fehlerhafte Größenangabe</span>[[Category:Bildergalerie: fehlerhafte Größenangabe]] ',
		fileNS         = { '[Ff]ile', '[Ii]mage', '[Dd]atei', '[Bb]ild' },
		missingFile    = '<span class="error">Fehlender Dateiname</span>[[Category:Bildergalerie: fehlender Dateiname]] ',
		noImage        = '<span class="error">Kein Bild definiert</span>[[Category:Bildergalerie: kein Bild definiert]] ',
		sizeUsed       = '[[Category:Bildergalerie: Größenangaben verwendet]]',
		unknownFiles   = '<span class="error">Fehlerhafte(r) Dateiname(n)</span>[[Category:Bildergalerie: fehlerhafte Dateinamen]] ',
		unknownParams  = '<span class="error">Fehlerhafte(r) Parameter</span>[[Category:Bildergalerie: fehlerhafte Parameter]] '
	},

	-- possible argument identifiers
	params = {
		align          = { 'align', 'Ausrichtung', default = 'right' },
		background     = { 'background', 'Hintergrund' },
		border         = { 'border', 'Rahmen' },
		headerStyles   = 'headerStyles',
		height         = { 'height', 'heights', 'maxHeight', 'Höhe' },
		hideUnitHeader = { 'hideUnitHeader', default = 'no' },
		imageGrStyles  = 'imageGrStyles',
		loop           = { 'loop', 'Schleife', default = 'yes' },
		styles         = 'styles',
		timeDependent  = { 'timeDependent', default = 'no' },
		title          = { 'title', 'Titel' },
		width          = { 'width', 'Breite', 'Größe' }
	},

	-- possible alignment values
	align = {
		left      = 'left',
		links     = 'left',
		right     = 'right',
		rechts    = 'right',
		center    = 'center',
		mitte     = 'center',
		zentriert = 'center'
	},

	-- graphics and video extensions excluding audio extensions
	extensions = { 'tif', 'tiff', 'png', 'gif', 'jpg', 'jpeg', 'jpe', 'webp',
		'xcf', 'ogg', 'ogv', 'svg', 'pdf', 'djvu', 'stl', 'webm', 'mpg', 'mpeg' }
}

local errorMsgs = {}

-- add error message to errorMsgs table
local function addErrorMsg( msg )
	table.insert( errorMsgs, msg )
end

-- get errorMsgs table as string
local function getErrorMsgs()
	local result = table.concat( errorMsgs, ' ' )
	if result ~= '' then
		result = result .. ' '
	end
	return result
end

-- check if param is set: not nil or empty
local function isSet( param )
	return param and param ~= '';
end

-- check for possible arguments against list table
local function checkParams( frameArgs, list )
	local complete = {}
	local args = {}

	-- named arguments
	for key, value in pairs( list ) do
		if type( value ) == 'table' then
			for key2, value2 in ipairs( value ) do
				complete[ value2 ] = key
				args[ key ] = args[ key ] or frameArgs[ value2 ]
			end
			args[ key ] = args[ key ] or ''
		elseif value ~= '' then
			complete[ value ] = key
			args[ key ] = frameArgs[ value ] or ''
		else
			complete[ key ] = key
			args[ key ] = frameArgs[ key ] or ''
		end
		if args[ key ] == '' and type( value ) == 'table' and value.default then
			args[ key ] = value.default
		end
	end

	local ok = true
	for key, value in pairs( frameArgs ) do
		-- numbered arguments
		if tonumber( key ) then
			-- frameArgs[ key ] cannot be nil
			args[ key ] = mw.text.trim( frameArgs[ key ] )
		end
		if not complete[ key ] and not tonumber( key ) then
			ok = false
		end
	end
	if not ok then
		addErrorMsg( sg.texts.unknownParams )
	end
	return args
end

-- make image-size parameter string from width and height with check
local function makeSize( width, height )
	if width ~= '' then
		width = width:gsub( 'px$', '' )
	end
	if height ~= '' then
		height = height:gsub( 'px$', '' )
	end

	local at = width:find( 'x' )
	if at then
		height = width:sub( at + 1, #width )
		width = width:sub( 1, at - 1 )
	end

	local value = tonumber( width )
	if isSet( width ) and not value then
		addErrorMsg( sg.texts.badSize )
	end
	local size = ''
	if value and value > 10 and value <= 1000 then
		size = width
	end
	value = tonumber( height )
	if isSet( height ) and not value then
		addErrorMsg( sg.texts.badSize )
	end
	if value and value > 10 and value <= 1000 then
		size = size .. 'x' .. height
	end
	if size ~= '' then
		return '|' .. size .. 'px'
	else
		return ''
	end
end

-- main function for Scroll Gallery template
function sg.gallery( frame )
	local args = checkParams( frame:getParent().args, sg.params )

	-- generate output: outer div and title
	local gallery = mw.html.create( 'div' )
		:addClass( 'voy-ImageGroup' )
	local align = sg.align[ args.align:lower() ] or ''
	if align == 'left' then
		gallery:addClass( 'voy-ImageGroupLeft' )
	elseif align == 'center' then
		gallery:addClass( 'voy-ImageGroupCenter' )
	else -- default: right
		gallery:addClass( 'voy-ImageGroupRight' )
	end
	if yn( args.timeDependent, false ) then
		gallery:addClass( 'voy-timeDependent' )
	end
	if not yn( args.loop, true ) then
		gallery:addClass( 'voy-noLoop' )
	end
	if yn( args.hideUnitHeader, false ) then
		gallery:addClass( 'voy-hideUnitHeader' )
	end

	-- add styles
	local node
	if args.border ~= '' then
		gallery:css( 'border-color', args.border )
	end
	if args.background ~= '' then
		gallery:css( 'background-color', args.background )
	end
	gallery:cssText( args.styles )
	if args.title ~= '' then
		node = mw.html.create( 'div' )
			:addClass( 'voy-ImageGroupHeader' )
			:wikitext( args.title )
		if args.headerStyles ~= '' then
			node:cssText( args.headerStyles )
		end
		gallery:node( node )
	end

	-- prepare size attribute
	local size = makeSize( args.width, args.height )

	-- add image groups and images
	local i = 1
	local ok = true
	local fileOk, imNode
	while isSet( args[ i ] ) do
		-- remove LTR and RTL marks
		args[ i ] = mw.ustring.gsub( args[ i ], '[‎‏]+', '' )

		-- delete namespace
		for j, ns in ipairs( sg.texts.fileNS ) do
			args[ i ] = mw.ustring.gsub( args[ i ], '^' .. ns .. ':', '' )
		end

		-- check files for possible file extensions
		fileOk = false
		if ok then
			for j, ext in ipairs( sg.extensions ) do
				if args[ i ]:lower():find( '^.+%.' .. ext .. '$' ) then
					fileOk = true
					break
				end
			end
		end
		if not fileOk then
			ok = false
		end

		-- prepare image caption
		if isSet( args[ i + 1 ] ) then
			args[ i + 1 ] = '|' .. args[ i + 1 ]
		else
			args[ i + 1 ] = ''
		end

		-- generate image syntax and create container for additional img styles
		imNode = mw.html.create( 'div' )
			:addClass( 'voy-ImageGrUnitInner' )
			:wikitext( mw.ustring.format( '[[File:%s|thumb|center%s%s]]',
				args[ i ], size, args[ i + 1 ] ) )
		if args.imageGrStyles ~= '' then
			imNode:cssText( args.imageGrStyles )
		end
		-- create image-container div for scroll icons and image
		node = mw.html.create( 'div' )
			:addClass( 'voy-ImageGrUnit' )
			:node( imNode )
		if i > 1 then
			node:css( 'display', 'none' )
		end
		gallery:node( node )

		i = i + 2
	end

	-- add error and hint messages
	if not isSet( args[ i ] ) and isSet( args[ i + 1 ] ) then
		addErrorMsg( sg.texts.missingFile )
	end
	if not isSet( args[ 1 ] ) then
		addErrorMsg( sg.texts.noImage )
	end
	if not ok then
		addErrorMsg( sg.texts.unknownFiles )
	end
	if args.width .. args.height ~= '' then
		addErrorMsg( sg.texts.sizeUsed )
	end
	return getErrorMsgs() .. tostring( gallery )
end

return sg