Module:Sandbox/Synpath

local templatestyles = 'Module:Sandbox/Synpath/styles.css'
local getArgs = require('Module:Arguments').getArgs
local tracking, preview = {}, {}
local p = {}

local function isImage(input_string)
	--adapted from Module:Gallery 09-02-2025
    local input_string = input_string:lower() -- Case insensitive check

    -- Check if it starts with "File:", "Image:", or "Media:"
    local _, prefix = input_string:match("^(%[%[)?(%a-):")
    if prefix and (prefix == "file" or prefix == "image" or prefix == "media") then
        return true
    end
    
    local valid_extensions = {
        "apng", "djvu", "flac", "gif", "jfi", "jfif", "jif", "jpe", "jpeg", "jpg",
        "m1a", "m1v", "m2a", "m2v", "mid", "mp1", "mp2", "mp3", "mpa", "mpe", "mpeg", "mpg",
        "mpv", "oga", "ogg", "ogv", "opus", "pdf", "png", "stl", "svg", "svgz", "tif", "tiff",
        "wav", "wave", "webm", "webp", "xcf"
    }
    
    -- Extract file extension, of 3 or 4 characters only
    local ext = file:match("%.(%w%w%w%w?)$")
    
    -- Check if the extension is in the valid list
    if ext then
        for _, valid_ext in ipairs(valid_extensions) do
            if ext == valid_ext then
            	table.insert(tracking, '[[Category:Pages using gallery without a media namespace prefix]]')
                return true
            end
        end
    end
    return false
end

local function checkarg(k,v,previouskind)
	--I'm sure there's a better way to do this, but just make it work for now
	if k and type(k) == 'string' and v and v ~= '' then
		if k:match('^%d+$') then
			--unparameterized values are treated as captions when following an image
			--unless they have a valid file extension or namespace
			if previouskind == 'image' or previouskind == 'unparameterized_image' 
				and isImage(v) == false then
				return 'unparameterized_caption'
			else
				--assume here that the user has entered wikitext of an image
				return 'unparameterized_image'
			end
		elseif k:match('^image%d+$') then
			--assume here that the user has entered wikitext of an image
			return 'image'
		elseif k:match('^caption%d+$') then
			--just for clarity/readability
			return 'caption'
		elseif k == 'containerclass' or k == 'itemclass' or k == 'imageclass' 
			or k == 'captionclass' or k == 'widths' or k == 'heights' 
			or k == 'itemstyles' or k == 'imagestyles' or k =='captionstyles' 
			or k == 'mode' or k:match('^class%d+$') then
			return 'string'
		--else
			-- invalid, omit error tracking categorization for now
			--adapted from Module:Gallery 09-02-2025
			--local vlen = mw.ustring.len(k)
			--k = mw.ustring.sub(k, 1, (vlen < 25) and vlen or 25)
			--k = mw.ustring.gsub(k, '[^%w%-_ ]', '?')
			--table.insert(tracking, '[[Category:Pages using gallery with unknown parameters|' .. k .. ']]')
			--table.insert(preview, '"' .. k .. '"')
		end
	end
	return 'invalid'
end

local function file_affixes(file_wikitext)
	--[[
	prepend two open brackets or "File:" and append two closing brackets as needed
	]]--
	local prefix5 = file_wikitext:sub(1,5):lower()
	local prefix6 = file_wikitext:sub(1,6):lower()
	
	if file_wikitext:sub(1,2) == "[[" then
		return file_wikitext --just assume the editor wrote their wikitext right
	elseif prefix5 == "file:" or prefix6 == "media:" or prefix6 == "image:" then
		return "[[" .. file_wikitext .. "]]"
	else
		return "[[File:" .. file_wikitext .. "]]"
	end
end

local function add_gallery_flex_item(args, tbl, image, caption)
	tbl
		:tag('div')
		:addClass('mod-gallery-flex-item')
		:addClass(args.itemclass)
		:css('width', args.widths)
		:css('style', args.itemstyles) --for one off exceptions
		:tag('div')
			:addClass('mod-gallery-flexbox-image')
			:addClass(args.imageclass)
			:css('height', args.heights)
			:css('style', args.imagestyles) --for one off exceptions
			:wikitext(image)
			:done()
		:tag('div')
			:addClass('mod-gallery-flexbox-caption')
			:addClass(args.captionclass)
			:css('style', args.captionstyles) --for one off exceptions
			:wikitext(caption)
			:done()
		:done() --end div; flex-item
end

local function add_gallery_flex_item_no_caption(args, tbl, image)
	tbl
		:tag('div')
		:addClass('mod-gallery-flex-item')
		:addClass(args.itemclass)
		:css('width', args.widths)
		:css('style', args.itemstyles) --for one off exceptions
		:tag('div')
			:addClass('mod-gallery-flexbox-image')
			:addClass(args.imageclass)
			:css('height', args.heights)
			:css('style', args.imagestyles) --for one off exceptions
			:wikitext(image)
			:done()
		:done() --end div; flex-item
end

local function _galleryflexbox(passedargs, frame)
    -- check parameters and reassign those that have values
    error(error(table.concat(passedargs, ',')))
    local args = {}
    local unparameterized_images = {}
    local unparameterized_captions = {}
    local N_images = 0
    local N_un_img = 0
    local argkind = 'invalid'
    local prevkind = 'invalid'
    
    for k, v in pairs(passedargs) do
    	argkind = checkarg(k,v) --also adds invalid parameter tracking
    	
    	if argkind == 'image' then
    		prevkind = 'image'
    		N_images = N_images + 1
    		args[k] = file_affixes(v)
    	elseif argkind == 'unparameterized_image' then
    		prevkind = 'unparameterized_image'
    		N_un_img = N_un_img + 1
    		unparameterized_images[N_un_img] = file_affixes(v)
    	elseif argkind == 'caption' then
    		prevkind = 'caption'
    		args[k] = v
    	elseif argkind == 'unparameterized_caption' and prevkind == 'unparameterized_image'  then
    		prevkind = 'unparameterized_caption'
    		unparameterized_captions[N_un_img] = v
    	elseif argkind == 'string' then --valid, named parameters
    		args[k] = v
    	end
	end
	
	-- generate flexbox html
	local tbl = mw.html.create('div')
	tbl:addClass('mod-gallery-flexbox')
	
	if args.containerclass == nil then
		tbl:addClass('mod-gallery-flexbox-default')
	elseif args.mode then
		tbl:addClass('mod-gallery-flexbox-' .. args.mode)
	else
		tbl:addClass(args.containerclass)
	end

	for i = 1, N_images do --repeat for each named image (i.e. image(n))
		add_gallery_flex_item(args, tbl, args['image' .. i], args['caption' .. i])
		--[[
		tbl
			:tag('div')
			:addClass('mod-gallery-flex-item')
			:addClass(args.itemclass)
			:css('width', args.widths)
			:css('style', args.itemstyles) --for one off exceptions
			:tag('div')
				:addClass('mod-gallery-flexbox-image')
				:addClass(args.imageclass)
				:css('height', args.heights)
				:css('style', args.imagestyles) --for one off exceptions
				:wikitext(args['image' .. i])
				:done()
			:tag('div')
				:addClass('mod-gallery-flexbox-caption')
				:addClass(args.captionclass)
				:css('style', args.captionstyles) --for one off exceptions
				:wikitext(args['caption' .. i])
				:done()
			:done() --end div; flex-item
		]]--
	end
	for i = 1, N_un_img do --repeat for each unnamed image (i.e. image(n))
		add_gallery_flex_item(args, tbl, unparameterized_images[i], unparameterized_captions[i])
	end
	
	-- error tracking 
	local trackstr = (#tracking > 0) and table.concat(tracking, '') or ''
	if #preview > 0 then
		trackstr = require('Module:If preview')._warning({
			'Unknown parameters ' .. table.concat(preview, '; ') .. '.'
		}) .. trackstr
	end
	
	return frame:extensionTag{ name = 'templatestyles', args = { src = templatestyles} } .. tostring(tbl) .. trackstr
end

function p.main(frame)
	local args = getArgs(frame)
	return _galleryflexbox(args, frame)
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.