Jump to content
Menü aufrufen
Toggle preferences menu
Persönliches Menü aufrufen
Nicht angemeldet
Ihre IP-Adresse wird öffentlich sichtbar sein, wenn Sie Änderungen vornehmen.
Modul Dokumentation[view][edit][history][purge]
Diese Dokumentation wird von Modul:Gallery/doc aus übernommen. Änderungen können auf der Diskussionsseite vorgeschlagen werden.
Function list
L 26 — isFileName
L 32 — p._main
L 47 — p.main
L 62 — loadSmwData
L 83 — makeGallery
L 108 — p.output
L 136 — p.fromArgs

Dieses Modul setzt die Vorlage:Galerie um. Anweisungen zur Verwendung findest du auf der Vorlagenseite.


require('strict')

--- @module Gallery
--- Renders a horizontal row of thumbnails. Two interfaces share one module:
---
--- - `main` (upstream starcitizen.tools): numbered file-name arguments become
---   `[[File:<name>|frameless|120px]]` entries in a `div.template-gallery`; any
---   other argument passes through as wikitext.
--- - `fromArgs`/`output` (legacy star-citizen.wiki): the SMW-backed gallery the
---   legacy `{{Galerie}}`/`{{Gallery}}` invoke, kept verbatim so pages that rely
---   on the SMW auto-gallery keep working. `fromArgs` only recognizes
---   jpg/png/jpeg file names and the `desc1`-style caption arguments.

local getArgs = require('Module:Arguments').getArgs

local p = {}

local HEIGHT = 120

--- Whether a raw argument is usable as a bare file name. Upstream tested
--- `.[^{}<>]+%.%w+`, which misses stems shorter than three characters
--- (`A.png`); the trailing-extension test accepts any stem length.
---
--- @param value string
--- @return boolean
local function isFileName(value)
	return not value:match('[{}<>]') and value:match('%.%w+$') ~= nil
end

--- @param args table
--- @return string
function p._main(args)
	local filearg = ''
	for _, value in pairs(args) do
		if isFileName(value) then
			filearg = filearg .. '[[File:' .. value .. '|frameless|x' .. HEIGHT .. 'px]]'
		else
			filearg = filearg .. value
		end
	end

	return tostring(mw.html.create('div'):addClass('template-gallery citizen-overflow'):wikitext(filearg))
end

--- @param frame mw.frame
--- @return string
function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

--- Everything below is the legacy star-citizen.wiki module, kept so the SMW
--- auto-gallery keeps serving pages that transclude {{Galerie}} without
--- arguments. Do not extend it; add new behavior to main.

--- Queries SMW for images in Category:Galerie on the given page.
---
--- @param page string
--- @param depth number
--- @param limit number
--- @return table|nil
local function loadSmwData(page, depth, limit)
	depth = depth or 5
	limit = limit or 12

	local result = mw.smw.ask({
		'[[Kategorie:' .. page .. '|+depth=' .. depth .. ']][[Kategorie:Galerie]][[Datei:!~*.mp4]][[Datei:!~*.webm]]',
		'?#-=image',
		'?Beschreibung=caption',
		'+index=1',
		limit = limit,
		mainlabel = '-',
	})

	return result
end

--- Renders the gallery rows for the legacy entries; captions become alt text.
---
--- @param data table
--- @param withHeader boolean
--- @return string
local function makeGallery(data, withHeader)
	local div = mw.html.create('div'):addClass('template-gallery')
	local suffix = '|frameless|x' .. HEIGHT .. 'px'

	for _, image in pairs(data) do
		if image['caption'] == nil then
			div:wikitext('[[' .. image['image'] .. suffix .. ']]')
		else
			div:wikitext(
				'[[' .. image['image'] .. '|alt=' .. image['caption'] .. suffix .. '|' .. image['caption'] .. ']]'
			)
		end
	end

	if withHeader == false then
		return tostring(div)
	end

	return tostring(mw.html.create('h2'):wikitext('Galerie')) .. tostring(div)
end

--- Legacy template entry: loads images from SMW by page name.
---
--- @param frame mw.frame
--- @return string
function p.output(frame)
	local args = frame:getParent().args
	local page = mw.title.getCurrentTitle().subpageText

	if args.page ~= nil and #args.page > 0 then
		page = args.page
	end

	if args.noGallery ~= nil then
		return ''
	end

	local images = loadSmwData(page, args.depth or 0, args['Bilderanzahl'] or 12)

	if images == nil then
		return ''
	end

	return makeGallery(images, true)
end

--- Legacy template entry: all-bare-filename calls render through main so any
--- extension is wrapped; calls with named arguments (descN captions, page,
--- noGallery, depth, Bilderanzahl) keep the legacy behavior, including the SMW
--- fallback when no arguments are given.
---
--- @param frame mw.frame
--- @return string
function p.fromArgs(frame)
	local args = getArgs(frame)

	local numbered, named = {}, 0
	local allFiles = true
	for k, arg in pairs(args) do
		if type(k) == 'number' then
			numbered[#numbered + 1] = arg
			allFiles = allFiles and isFileName(arg)
		else
			named = named + 1
		end
	end

	if named == 0 and allFiles and #numbered > 0 then
		return p._main(numbered)
	end

	local images = {}

	for k, arg in pairs(args) do
		local ending = mw.ustring.sub(arg, -3)

		if ending == 'png' or ending == 'jpg' or ending == 'peg' then
			local title = mw.title.new(arg, 6)
			if title ~= nil then
				arg = title.prefixedText
			end
		end

		local sub = mw.ustring.sub(arg, 0, 4)

		if sub == 'File' or sub == 'Date' then
			table.insert(images, tonumber(k) or #images + 1, { ['image'] = arg })
		else
			local prev = tonumber(k)
			if prev ~= nil then
				prev = prev - 1
			else
				-- desc1 addresses the image directly instead of the next one
				prev = tonumber(mw.ustring.match(k, '^desc(%d+)$'))
			end
			if prev ~= nil and images[prev] ~= nil then
				images[prev] = { ['image'] = images[prev]['image'], ['caption'] = arg }
			end
		end
	end

	if #images == 0 then
		return p.output(frame)
	end

	return makeGallery(images, false)
end
return p
Cookies helfen uns bei der Bereitstellung dieses Wikis. Durch die Nutzung des Star Citizen Wiki erklärst du dich damit einverstanden, dass wir Cookies speichern.