Pastey Gallery Generator

-- Simple example program in Euphoria language to create a html
-- page with all the picture files in the current directory. 
  
-- Marco Achury, 2008 
-- Released to public domain.  Feel free to use and modify. 
-- Updated for Eu 4.1b2 by Irv Mullins, Mar 9, 2019 
-- Added input folder and output file validation, and -h help 
 
/* 

    Usage: eui img_tag [Input Directory] [Output file] 
 
    e.g. eui img_tag ~/Pictures ~/thumbs 
    This will create a 'thumbs.html' file in your home directory, 
    containing the images in your 'Pictures' folder. 
     
    eui img_tag Downloads ~/pix will create a 'pix.html' file  
    in your home folder containing the images in your Downloads folder. 
*/ 
 
include std/error.e 
include std/sort.e 
include std/filesys.e 
include std/text.e 
include std/console.e 
include std/graphics.e 
include std/sequence.e 
 
integer FN -- File number for output 
sequence DIR -- List of images found 
sequence CDate -- The current date 
sequence FDate -- Formated date for the foot note. 
sequence temp 
sequence output = "output.html" -- default 
sequence cmdline = command_line() 
 
if length(cmdline) >=3 then  
    if match("-h",cmdline[3]) then 
        text_color(YELLOW)  
        clear_screen() 
        display("\n* Program: img_tag.ex *\n\n\tUsage: eui img_tag [] []\n", 
            {"[Input Directory]","[Output file]"}) 
        display("\te.g. eui img_tag ~/Pictures ~/thumbs") 
        display("\tThis will create a 'thumbs.html' file in your home directory,") 
        display("\tcontaining the images in your 'Pictures' folder.\n\n") 
        abort(1) 
    end if 
    
    if not equal(current_dir(),cmdline[3]) and not chdir(cmdline[3]) then 
        crash("ERROR: Cannot find source directory %s",{cmdline[3]}) 
    else 
        chdir(cmdline[3]) 
    end if 
     
    if length(cmdline) >= 4 then  
        if atom(dir(pathname(cmdline[4]))) then 
            crash("ERROR: Destination directory %s does not exist.",{cmdline[4]}) 
        end if 
        output = canonical_path(cmdline[4]) 
        if not match(".html",output) then output &= ".html" end if 
    end if 
end if 
    
sequence image_paths = { -- wilcard strings for images 
	"*.jpg",  
	"*.gif", 
	"*.png", 
	"*.bmp", 
	"*.pic", 
	"*.img", 
	"*.dib", 
	"*.svg", -- add others if necessary 
    $} 
       
DIR = {} 
 
-- Look for files with image extension 
for i=1 to length(image_paths) do 
    DIR = DIR & dir(current_dir() & "/" & image_paths[i]) 
end for 
DIR = sort(DIR) 
 
-- Eliminate atoms returned when a  
-- dir() call was not succesfull  
 
temp={} 
for i=1  to length(DIR) do 
    if sequence(DIR[i]) then 
	temp=append(temp, DIR[i]) 
    end if 
end for 
DIR=temp 
 
FN = open (output, "w") -- Open output file 
if FN=-1 then 
    crash("ERROR: Cannot save output! %s",{output}) 
end if 
 
object fname 
 
---------------------------------------------------------------------------------- 
-- GENERATE HTML 
---------------------------------------------------------------------------------- 
puts (FN, "<HTML>\n") 
puts (FN, " <HEAD>\n") 
puts (FN, sprintf("  <TITLE>Images in %s</TITLE>\n",{current_dir()})) 
 
puts (FN, " </HEAD>\n\n") 
puts (FN, "<BODY>\n") 
 
puts (FN, "<H1>Image Gallery</H1>\n\n") 
puts (FN, sprintf("%d Files in %s\n<br>\n<br>\n<hr>",{length(DIR),cmdline[3]})) 
 
puts (FN, "<table border='1'\n") 
puts (FN, "<tr>\n") 
 
for i=1  to length(DIR) do 
    fname = quote(current_dir() & "/" & DIR[i][D_NAME]) 
    puts (FN, sprintf("<td>\n<IMG SRC=%s ALT=\"Picture: " &  
      DIR[i][1] & "\" HEIGTH=250 WIDTH=250>\n",{fname} ))  -- Generates <IMG> tag 
     
    puts (FN, "<br><b>File&nbsp;Name:</b> " & DIR[i][D_NAME] & "<br>\n") -- puts File Name 
     
    puts (FN, "<b>File&nbsp;Size:</b> " & sprint(DIR[i][D_SIZE]) &  
    " bytes <br>\n") -- puts File Size 
     
    puts (FN, "<b>Dated:</b> " & sprint(DIR[i][D_YEAR]) & "/" &  
	sprint(DIR[i][D_MONTH]) & "/" & 
	sprint(DIR[i][D_DAY]) & "\n</td>\n\n") -- Generates File Date 
       
    if remainder(i,4)=0 then   
    puts (FN, "</tr>\n\n<tr>\n") 
    end if 
     
end for 
puts (FN, "</table>\n") 
 
CDate = date() 
 
FDate = sprint(1900+CDate[1]) & "/" & sprint(CDate[2]) & "/" & sprint (CDate[3]) 
FDate = FDate & " " & sprint(CDate[4]) & ":" & sprint(CDate[5]) 
 
puts (FN, "<address><b>Page generated:</b> " & FDate) 
puts (FN, " by a Euphoria powered script</address>") 
puts (FN, "</BODY>\n</HTML>\n") 
 
display("\n\tOutput file: [] has [] images.\n",{canonical_path(output),length(DIR)}) 
 
abort(0)

1. Comment by irv 3 months ago

EuGTK version can be found here:

https://openeuphoria.org/pastey/304.wc