Module:FileTools

From Sagan 4 Beta Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:FileTools/doc

local p = {}

local extensions = {
	["png"] = true, ["jpg"] = true, ["jpeg"] = true, ["gif"] = true, ["djvu"] = true,
	["ico"] = true, ["ogg"] = true, ["pdf"] = true, ["stl"] = true, ["svg"] = true,
	["webp"] = true, ["flac"] = true, ["mkv"] = true, ["mov"] = true, ["mp3"] = true,
	["oga"] = true, ["ogv"] = true, ["wav"] = true, ["webm"] = true, ["opus"] = true,
	["mp4"] = true, ["midi"] = true, ["mid"] = true, ["mpg"] = true, ["mpeg"] = true
}

function p.main(frame)
	return "Error: There is no main file tool. Please see [[Module:FileTools]] for documentation of every function provided by this module."
end


function p.gallery(frame)
	--Takes a series of parameters and converts it to a pseudogallery
	--Numbered params: File, caption, and link
	--Named params: Width, alignment
	--Named params apply to all. Numbered params are put together to make the gallery, using params that contain extensions as boundaries.
	
	--set named param defaults and override them with user input if applicable
	local size = "200px"
	local alignment = "center"
	if frame.args["alignment"] then
		alignment = frame.args["alignment"]
	end
	if frame.args["size"] then
		size = frame.args["size"]
		if not size:match("px") then
			size = size.."px"
		end
	end
	
	local currentImage = ""
	local currentCaption = ""
	local currentLink = ""
	local currentString = ""
	local finalString = ""
	local imageFound = false
	local captionFound = false
	local linkFound = false
	--Params edited in the loop
	for i,param in pairs (frame.args) do
		--please Chimlopzock tell me it does these in order
		
		if imageFound == true then
			--act accordingly
			if not param:match("%.(%w*)") and type(i) == "number" then
				if captionFound == false then
					currentCaption = "|"..param
					local captionFound = true
				elseif linkFound == false then
					currentLink = "|link="..param
					linkFound = true
				end
			end
			
		end
		
		--check if an image is found on the current line, and if so, save the string and reset
		if param:match("%.(%w*)") then if extensions[string.lower(param:match("%.(%w*)"))] then
			if imageFound == true then
				currentString = '<li style="display: inline-block; vertical-align: '..alignment..';">[['..currentImage.."|thumb|"..size..currentLink..currentCaption.."]]</li>"
			end
			
			finalString = finalString..currentString
			currentImage = param
			if not currentImage:find(":") then currentImage = "File:"..currentImage end
			currentLink = ""
			currentCaption = ""
			imageFound = true
			captionFound = false
			linkFound = false
		end end
		
	end
	
	if imageFound == true then finalString = finalString..'<li style="display: inline-block; vertical-align: '..alignment..';">[['..currentImage.."|thumb|"..size..currentLink..currentCaption.."]]</li>" end
	
	if finalString == "" then
		finalString = "Error: No images found. Be sure to format them correctly![[Category:Pages containing an empty FileTools gallery]]"
	else
		finalString = finalString.."[[Category:Pages using FileTools gallery]]"
	end
	
	
	return finalString
	
end




return p