Zum Inhalt springen

Modul:Scroll Gallery

Aus Wikivoyage
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: 2025-12-10 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 = '2025-12-10',
		item   = 87862715
	},

	-- miscellaneous messages
	texts = {
		doubleUpright  = '<span class="error">Upright doppelt spezifiziert</span>[[Category:Bildergalerie: Upright doppelt spezifiziert]]',
		fileNS         = { '[Ff]ile', '[Ii]mage', '[Dd]atei', '[Bb]ild' },
		missingFile    = '<span class="error">Fehlender Dateiname</span>[[Category:Bildergalerie: fehlender Dateiname]] ',
		insufficientImages = '<span class="error">Kein/nur ein Bild definiert</span>[[Category:Bildergalerie: kein oder nur ein Bild definiert]] ',
		outsized       = '[[Category:Bildergalerie: Übergroße Bilder spezifiziert]]',
		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]] ',
		wrongSize      = '<span class="error">Fehlerhafte Größenangabe</span>[[Category:Bildergalerie: fehlerhafte Größenangabe]] ',
		wrongUpright   = '<span class="error">Fehlerhafter Hochkant-Wert</span>[[Category:Bildergalerie: fehlerhafter Hochkant-Wert]] ',
	},

	-- possible argument identifiers
	params = {
		align          = { 'align', 'Ausrichtung', default = 'right' },
		height         = { 'height', 'heights', 'maxHeight', 'Höhe' },
		hideUnitHeader = { 'hideUnitHeader', default = 'no' },
		loop           = { 'loop', 'Schleife', default = 'yes' },
		timeDependent  = { 'timeDependent', default = 'no' },
		title          = { 'title', 'caption', 'Titel', 'Überschrift' },
		upright        = { 'upright', 'hochkant', 'Hochkant', default = '1' },
		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' },

	-- Possible upright parameter names
	upright = {
		upright  = 1,
		hochkant = 1
	},

	-- proposed maximum image size
	maxSize = 251 -- size in pixels
}

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()
	if #errorMsgs > 0 then
		return table.concat( errorMsgs, ' ' ) .. ' '
	else
		return ''
	end
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

	-- handle args.upright
	args.upright = args.upright:gsub( ',', '.' ) 
	args.upright = tonumber( args.upright )
	if not args.upright or args.upright > 1.5 or args.upright < 0.5 then
		args.upright = 1
		addErrorMsg( sg.texts.wrongUpright )
	end

	return args
end

-- create image attributes: args.imgParams and args.offset
local function makeImageParameters( args )
	local function checkSizeValue( value )
		if isSet( value ) then
			local val = tonumber( value )
			if not val or val < 10 or val > 1000 then
				addErrorMsg( sg.texts.wrongSize )
			else
				return val
			end
		end
		return nil
	end

	args.width = isSet( args.width ) and args.width:gsub( 'px$', '' ) or ''
	args.height = isSet( args.height ) and args.height:gsub( 'px$', '' ) or ''
	if args.width .. args.height ~= '' then
		addErrorMsg( sg.texts.sizeUsed )
	end

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

	local wValue = checkSizeValue( args.width )
	args.imgParams = wValue and args.width or ''
	local hValue = checkSizeValue( args.height )
	args.imgParams = args.imgParams .. ( hValue and ( 'x' .. args.height ) or '' )

	if math.max( wValue or 0, hValue or 0 ) >= sg.maxSize then
		addErrorMsg( sg.texts.outsized )
	end
	args.imgParams = ( args.imgParams ~= '' ) and ( '|' .. args.imgParams .. 'px' ) or ''

	args.offset = 0
	if sg.upright[ args[ 1 ]:ulower() ] then
		args.offset = 1
		args.imgParams = args.imgParams .. '|upright'
		if args.upright ~= 1 then
			addErrorMsg( sg.texts.doubleUpright )
		end
	end

	if args.offset == 0 and args.upright ~= 1 then
		args.imgParams = args.imgParams .. '|upright=' .. tostring( args.upright )
	end

	-- add messages
	-- no or only one image
	if not isSet( args[ args.offset + 1 ] ) or not isSet( args[ args.offset + 3 ] ) then
		addErrorMsg( sg.texts.insufficientImages )
	end
end

-- generate output: outer div and title
local function makeGalleryWrapper( args )
	local gallery = mw.html.create( 'div' )
		:addClass( 'voy-ImageGroup' )
	if args.width .. args.height == '' then
		gallery:addClass( 'voy-ImageGroupNoSize' )
	end
	local align = sg.align[ args.align:ulower() ] 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 header
	if isSet( args.title ) then
		gallery:node( mw.html.create( 'div' )
			:addClass( 'voy-ImageGroupHeader' )
			:wikitext( args.title )
		)
	end

	return gallery
end

-- add image groups and images
local function addImages( gallery, args )
	local i = args.offset + 1
	local ok = true
	local fileOk, imgNode, node
	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
		imgNode = mw.html.create( 'div' )
			:addClass( 'voy-ImageGrUnitInner' )
			:wikitext( mw.ustring.format( '[[File:%s|thumb|center%s%s]]',
				args[ i ], args.imgParams, args[ i + 1 ] ) )
		-- create image-container div for scroll icons and image
		node = mw.html.create( 'div' )
			:addClass( 'voy-ImageGrUnit' )
			:node( imgNode )
		if i > args.offset + 1 then
			node:css( 'display', 'none' )
		end
		gallery:node( node )

		i = i + 2
	end

	-- add error messages
	if not isSet( args[ i ] ) and isSet( args[ i + 1 ] ) then
		addErrorMsg( sg.texts.missingFile )
	end
	if not ok then
		addErrorMsg( sg.texts.unknownFiles )
	end
end

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

	-- create image attributes
	makeImageParameters( args )

	-- generate output: outer div and title
	local gallery = makeGalleryWrapper( args )

	-- add image groups and images
	addImages( gallery, args )

	return getErrorMsgs() .. tostring( gallery )
end

return sg