home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / photoalbum < prev    next >
Text File  |  2020-01-01  |  16KB  |  406 lines

  1. #!/usr/local/bin/kermit +
  2. #
  3. # IN UNIX CHANGE TOP LINE TO INDICATE ACTUAL PATH OF C-KERMIT
  4. # and give this file execute permission.
  5. #
  6. # Usage: photoalbum.ksc [ keyword=value [ keyword=value [ ... ] ] ]
  7. #
  8. # Make web site of pictures, which must be jpg's.
  9. # To use, put the desired jpg's in an empty directory,
  10. # cd to the directory, and run this script with the desired arguments:
  11. #
  12. # Makes an index page with thumbnails (or picture filenames if there are
  13. # no thumbnails) and then a page for each picture with forward, back, and
  14. # index links, with the ability to click on each picture to see the original.
  15. # Page can be in Spanish or English.
  16. #
  17. # Options (keywords):
  18. #   title="This will be the title" (enclose in quotes if multiple words)
  19. #   color=xxx      (foreground text color, default dargmagenta)
  20. #   background=yyy (background color, default #fff0f0)
  21. #   language=zzz   (english or spanish, default english)
  22. #   height=nnn     (height for thumbnails in pixels, default 120)
  23. #   noscale=n      (0 to scale photos, 1 not to scale, default 0)
  24. #   resize=n       (resize originals, n = max width or height, pixels)
  25. #   arrows         (put Unicode triangles for navigation instead of words)
  26. #   shorttitle="short title for photos" (default is full title)
  27. #
  28. # NOTE: For the present, keywords must be spelled out in full.
  29. #
  30. # The shorttitle is used in the alt= and title= clause of each photo.
  31. # If no shorttitle is given, the regular title is used.
  32. #
  33. # Overwrites any previous versions of the .html pages each time you run it.
  34. #
  35. # If ImageMagick is available, it is used to generate thumbnails for the
  36. # index page.  Otherwise the index page just contains a list of links to
  37. # the individual HTML pages.  ImageMagick can be found here:
  38. #
  39. #   http://www.imagemagick.org/
  40. #
  41. # Alternatively if you can make thumbnails some other way, they will be used
  42. # if they are already there.  For each file xxx.jpg, the corresponding
  43. # thumbnail must be called xxx-t.jpg; all thumbnails should be the same height.
  44. #
  45. # Frank da Cruz, Columbia University, 9 July 2006
  46. #
  47. # Updates:
  48. # 10 Jul 2006: Fixed some typos and fixed the test for Windows.
  49. # 11 Jul 2006: Added RESIZE option (RESIZE=n, pixels, default=760).
  50. # 11 Jul 2006: Reduced disk size of thumbnails by factor of 10.
  51. # 12 Jul 2006: Don't give HTML HEIGHT or WIDTH directives for resized images.
  52. # 12 Jul 2006: Keep and link to original when resizing rather than replacing.
  53. # 15 Sep 2006: Fix NOSCALE result; add "arrows" option for navigation links.
  54. # 16 Nov 2006: Colors for links; fix help text; allow "-help".
  55. # 12 Dec 2009: Nicer default colors; give usage message if no arguments.
  56. # 07 Aug 2010: Put alts and better titles on images; new default colors
  57.  
  58. .myname := \fbasename(\%0)              # Filename of this script
  59.  
  60. if eq "\v(system)" "unix" {             # Check prerequisites
  61.     local msg
  62.     .msg = C-Kermit 8.0.212 Dev.20 or later required.
  63.     if llt \v(version) 800212 exit 1 \m(msg)
  64.     if < \v(buildid) 20060707 exit 1 \m(msg)
  65. }
  66. if eq "\v(system)" "windows" {
  67.     if <= \v(xversion) 2103 {
  68.         echo
  69.         echo WARNING:
  70.         echo Certain functions in this script won't work in Windows but a fully
  71.         echo functional website should still result. Ignore any error messages.
  72.         echo This script will operate correctly in the next release of K95.
  73.         echo
  74.     }
  75. }
  76. def USAGE {
  77.     echo
  78.     echo { Usage: \m(myname) keyword=value [ keyword=value [ ... ] ]}
  79.     echo
  80.     echo { Makes a website in a directory containing .jpg images.}
  81.     echo { Index page shows thumbnails of the images.}
  82.     echo
  83.     echo { Options (keywords):}
  84.     echo {   title="This will be the title"}
  85.     echo {   color=xxx      (foreground text color, default darkmagenta)}
  86.     echo {   background=yyy (background color, default \#fff0f0)}
  87.     echo {   language=zzz   (english or spanish, default english)}
  88.     echo {   height=nnn     (height for thumbnails in pixels, default 72)}
  89.     echo {   noscale=n      (0 to scale photos, 1 not to scale, default 0)}
  90.     echo {   resize=n       (nonzero n means resize pictures to n pixels)}
  91.     echo {   arrows         (use arrows instead "next" and "previous")}
  92.     echo {   help           (print this message)}
  93.     echo {   shorttitle="short title for photos" (default is full title)}
  94.     echo
  95.     exit 0
  96. }
  97. if < \v(argc) 2 usage
  98.  
  99. .kerbang = 0                            # How we exit depends on how invoked
  100. if kerbang incr kerbang                 # (This only works at top level)
  101. define FATAL {                          # Fatal error macro
  102.     fclose all                          # Close all open files
  103.     echo                                # and exit or stop with given message
  104.     if \m(kerbang) exit 1 FATAL - \%*
  105.     else stop 1 FATAL - \%*
  106. }
  107. def STYLE {                # Write <style>..</style> stanza
  108.     fwrite /line \%1 <style type="text/css">
  109.     if \m(arrows) {
  110.     fwrite /line \%1 "  a { text-decoration:none }"
  111.     }
  112.     fwrite /line \%1 "  a:link { color:\m(color) }"
  113.     fwrite /line \%1 "  a:visited { color:\m(color) }"
  114.     fwrite /line \%1 "  a:hover { color:\m(hover) }"
  115.     fwrite /line \%1 "  a:active { color:\m(hover) }"
  116.     fwrite /line \%1 "  img { border:2px solid \m(color) }"
  117.     fwrite /line \%1 </style>
  118. }
  119. # Command-line processing - title and options
  120.  
  121. .height = 120                # Default thumbnail height
  122. .language = English            # Default text language
  123. .color = darkmagenta                       # Default text language
  124. .background = \#fff0f0            # Default background color
  125.  
  126. .lp = (                    # Left paren
  127. .rp = )                    # Right paren
  128.  
  129. for \%i 1 \v(argc)-1 1 {                # Parse each keyword option
  130.     void \fkeywordvalue(\&_[\%i])    # This function does it
  131.     if eq "\v(lastkwval)" "help" usage    # If keyword was "help" give help
  132.     if eq "\v(lastkwval)" "-help" usage    # If keyword was "-help" give help
  133.     if eq "\v(lastkwval)" "-h" usage    # If keyword was "-h" give help
  134.     if eq "\v(lastkwval)" "resize" {    # If "resize" given without argument
  135.         if not def resize .resize = 720 # supply default.
  136.     }
  137.     if eq "\v(lastkwval)" "noscale" {    # If "noscale" given without argument
  138.         if not def noscale .noscale = 1 # supply default.
  139.     }
  140.     if eq "\v(lastkwval)" "arrows" {    # If "arrows" given without argument
  141.         if not def arrows .arrows = 1   # supply default.
  142.     }
  143. }
  144. stop
  145.  
  146. if not def resize .resize = 0
  147. if not def noscale .noscale = 0
  148. if not def arrows .arrows = 0
  149. if not numeric \m(height) exit 1 Fatal - Height not numeric: \m(height)
  150. if not numeric \m(noscale) exit 1 Fatal - Noscale not numeric: \m(noscale)
  151.  
  152. if match english \m(language)* .language = English
  153. else if match spanish \m(language)* .language = Spanish
  154. else exit 1 Fatal - Language "\m(language)" not supported
  155.  
  156. .hover := \m(color)
  157. switch \m(color) {
  158.   :darkmagenta, .hover = fuchsia, break
  159.   :deeppink,    .hover = pink, break
  160.   :gold,  .hover = yellow, break
  161.   :black, .hover = lightgrey, break
  162.   :blue,  .hover = lightblue, break
  163.   :red,   .hover = pink
  164. }
  165. show macro title color background hover language height noscale resize arrows
  166.  
  167. undef \%h \%w                # Height and width clauses for HTML
  168. if not \m(noscale) {
  169.     .\%h = { height="90%"}        # Portrait
  170.     .\%w = { width="100%"}        # Landscape
  171. }
  172. pwd
  173.  
  174. # Use Image Magick 'convert' (if available) to create resize originals
  175. # and/or create thumbnails.
  176.  
  177. dir /array:&t /except:*-[tr].jpg *.jpg  # Get list of original images
  178. if not \fdimension(&t) fatal No original JPGs found - nothing to do.
  179.  
  180. if eq \v(system) unix chmod 664 *.jpg
  181.  
  182. .need_thumbnails = 0                    # Assume we don't need thumbnails
  183. if def height .need_thumbnails = 1      # If height given maybe we do
  184. if not \m(need_thumbnails) {            # Or...
  185.     .\%t := \ffiles(*-t.jpg)            # if we don't have a thumbnail for
  186.     if > \&t[0] \%t .need_thumbnails = 1 # every non-thumbnail
  187. }
  188. .have_convert = 0                       # Assume we don't have 'convert'
  189. if \m(need_thumbnails) {                # Need thumbnails.
  190.     !convert -version > /dev/null       # Do we have Image Magick convert?
  191.     if fail {
  192.     echo Image Magick 'convert' not found - can't make thumbnails...
  193.         .need_thumbnails = 0            # No - can't make thumbnails
  194.     } else {
  195.         .have_convert = 1
  196.     }
  197. }
  198. if ( \m(resize) && \m(have_convert) ) { # Resizing originals
  199.     echo Resizing...
  200.     for i 1 \&t[0] 1 {                  # Go through non-thumbnail jpgs...
  201.         .t := \fstripx(\fbasename(\&t[i]),.)-r.jpg # Name for resized version
  202.         if not exist \m(t) {            # If it doesn't exist
  203.             copy \&t[i] \m(t)           # copy original
  204.         }
  205.         .\%9 := \fpictureinfo(\m(t),&d) # Get current size
  206.         if == \m(resize) \&d[\%9] {     # If it's already the desired size
  207.             echo \m(t) SKIPPED          # don't resize it.
  208.             continue
  209.         }
  210.         xecho \m(t): \fsize(\m(t))...   # Resize the copy
  211.         !convert \m(t) -resize \m(resize)x\m(resize) \m(t)
  212.         if fail echo PROBLEM RESIZING \&t[i] - Continuing...
  213.         echo \fsize(\m(t))
  214.         chmod 644 \m(t)
  215.     }
  216.     echo                                # Done with thumbnails
  217. }
  218. if \m(need_thumbnails) {                # Need to and can make thumbnails...
  219.     echo Checking thumbnails...
  220.     for i 1 \&t[0] 1 {                  # Go through non-thumbnail jpgs...
  221.         .t := \fstripx(\fbasename(\&t[i]),.)
  222.         .tt := \m(t)-t.jpg              # Make name for thumbnail
  223.         if exist \m(tt) {               # Already exists?
  224.             if \fpictureinfo(\m(tt),&d) { # get its height
  225.                 if ( == \m(height) \&d[2] ) { # Same as requested height?
  226.                     continue                  # Nothing to do
  227.                 }
  228.             }
  229.             del /quiet \m(tt)           # Delete existing thumbnail
  230.         }
  231.         xecho " \&t[i]"                 # Create new thumbnail
  232.         !convert \&t[i] -thumbnail x\m(height) \m(tt)
  233.         if fail echo PROBLEM MAKING THUMBNAIL FROM \&t[i] - Continuing...
  234.         chmod 644 \m(tt)
  235.     }
  236.     echo                                # Done with thumbnails
  237. }
  238. .\%t := \ffiles(*-t.jpg)                # Now see how many thumbnails exist
  239.  
  240. # Localization (Spanish or English)...
  241.  
  242. .charset = iso-8859-1                   # Character set is ISO 8859-1
  243. undef langtag                           # No HMTL language tag yet
  244.  
  245. switch \m(language) {                   # Text messages for selected language
  246.   :spanish
  247.     .langtag = { lang="es"}             # HTML language tag
  248.     if not def title .title = Fotos     # Default title
  249.     .photo = Foto                       # Individual page title
  250.     .next  = Adelante                   # Forward
  251.     .prev  = Atrßs                      # Back
  252.     .index = Indice                     # Index
  253.     .up    = Arriba
  254.     .msg1  = Haz clic en cualquier nombre para ver fotos # Legend for index
  255.     if \%t .msg1 = Haz clic en cualquier foto para entrar
  256.     .msg2  = (Haz clic para ver en tama±o completo)
  257.     break
  258.   :english
  259.     .langtag = { lang="en"}             # HTML language tag
  260.     if not def title .title = Photos    # Default title
  261.     .photo = Photo                      # Individual page title
  262.     .next  = Next                       # Forward
  263.     .prev  = Prev                       # Back
  264.     .index = Index                      # Index
  265.     .up    = Up
  266.     .msg1  = Click on any name to see photos # Legend for index
  267.     if \%t .msg1 = Click on any photo to enter
  268.     .msg2  = (Click to enlarge)
  269.     break
  270.   :default
  271.     fatal "No language!"
  272. }
  273. if not def shorttitle .shorttitle := \m(title)
  274.  
  275. if \m(noscale) undef msg2
  276.  
  277. if \m(arrows) {
  278.     undef lp rp
  279.     .index = <span style="font-size:40px;padding-top:16">▲</span>
  280.     .up := \m(index)
  281.     .next = <span style="font-size:36px;padding-top:16">▶</span>
  282.     .prev = <span style="font-size:36px;padding-top:16">◀</span>
  283. }
  284.  
  285. echo Creating Web pages...
  286.  
  287. fopen /write \%o index.html             # Create the index page
  288. if fail fatal FOPEN index failed
  289.  
  290. # Write HTML prologue...
  291.  
  292. fwrite /line \%o <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  293. fwrite /line \%o <html\m(langtag)>
  294. fwrite /line \%o <head>
  295. fwrite /line \%o <title>\m(title)</title>
  296. fwrite /line \%o -
  297.   <META http-equiv="Content-Type" content="text/html; charset=\m(charset)">
  298. do style \%o
  299. fwrite /line \%o </head>
  300. fwrite /line \%o -
  301.  <body style="background:\m(background);color:\m(color);font-size:13pt">
  302. fwrite /line \%o <h1>\m(title)</h1>
  303. fwrite /line \%o {\m(lp) <a href="..">\m(up)</a> \m(rp)  }
  304. fwrite /line \%o <i>(\m(msg1))</i><br>
  305.  
  306. .\%n := \&t[0]                          # How many non-thumbnail JPGs
  307. set flag off
  308. .\%r := \ffiles(*-r.jpg)
  309. if == \%r \%n set flag on               # We have resized images
  310.  
  311. for \%i 1 \%n 1 {                       # Loop through all non-thumbnails
  312.     xecho "FILENAME \&t[\%i] => "       # Start to say what we're going
  313.     .\%b := \fstripx(\&t[\%i])          # Filename without ".jpg"
  314.     .\%f := \%b.html                    # Filename with ".html"
  315.     .\%a := \&t[\%i]                    # Filename of image file
  316.     if flag .\%a := \%b-r.jpg           # Change to this for resized image
  317.     .orientation := \fpictureinfo(\%a)  # Get picture orientation
  318.  
  319.     undef \%q                           # Scaling command for HTML
  320.     switch \m(orientation) {
  321.       :1, .\%p = "Landscape", .\%q := \%w, break
  322.       :2, .\%p = "Portrait",  .\%q := \%h, break
  323.       :default, .\%p = "Unknown"
  324.     }
  325.     if \m(resize) undef \%q             # No HTML scale if resizing ourselves
  326.     echo \%f (\%p)                      # Finish saying what we're doing
  327.     fopen /write \%c \%f                # Create page for this picture
  328.     if fail exit 1 OPEN failed
  329.     fwrite /line \%c -                  # Write HTML prologue
  330.       <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  331.     fwrite /line \%c <html\m(langtag)>
  332.     fwrite /line \%c <head>
  333.     fwrite /line \%c <title>\m(photo) \#\%i - \m(title)</title>
  334.     fwrite /line \%c -
  335.       <META http-equiv="Content-Type" content="text/html; charset=\m(charset)">
  336.     do style \%c
  337.     fwrite /line \%c </head>
  338.     fwrite /line \%c -
  339.       <body style="background:\m(background);color:\m(color);font-size:13pt">
  340.     fwrite /line \%c <h2>\m(title) - \m(photo) \#\%i</h2>
  341.     fwrite /line \%c
  342.     fwrite /line \%c <div>
  343.  
  344.     if == \%i \%n {                     # If last picture there is no next.
  345.         .\%x := index.html
  346.         fwrite /line \%c \m(lp) <a href="index.html">\m(index)</a> \m(rp)
  347.     } else {                            # Not last picture
  348.     .\%x := \fstripx(\&t[\%i+1]).html # So make link to next.
  349.         fwrite /line \%c \m(lp) <a href="\%x">\m(next)</a> \m(rp)
  350.     }
  351.     .xnext := \%x            # Remember this for later
  352.     if != \%i 1 {                       # Not first picture?
  353.       .\%x := \fstripx(\&t[\%i-1]).html # Not first so link to previous
  354.       fwrite /line \%c {  \m(lp) <a href='\%x'>\m(prev)</a> \m(rp)  }
  355.     }
  356.     if != \%i \%n {                     # Not last picture?
  357.       # Link to index
  358.       fwrite /line \%c \m(lp) <a href='index.html'>\m(index)</a> \m(rp)
  359.     }
  360.     if def msg2 fwrite /line \%c <small><i>  \m(msg2)</i></small><br>
  361.     .\%x := \%b
  362.     .\%z = \fcontents(\%b)
  363.     if exist \%x.jpg .\%z = \fcontents(\%x)
  364.  
  365.     fwrite /line \%c </div>
  366.  
  367.     if \m(noscale) {
  368.         fwrite /line \%c <a href='\m(xnext)'><img -
  369. title="\m(shorttitle) \m(photo) \#\%i" -
  370. alt="\m(shorttitle) \m(photo) \#\%i" -
  371. src="\%a"\%q></a>
  372.     } else {
  373.         fwrite /line \%c <a href='\%z.jpg'><img -
  374. title="\m(shorttitle) \m(photo) \#\%i" -
  375. alt="\m(shorttitle) \m(photo) \#\%i" -
  376. src="\%a"\%q></a>
  377.     }
  378.     fwrite /line \%c <p>
  379.     fwrite /line \%c <hr color="\m(color)"> # End of page
  380.     fwrite /line \%c </body>
  381.     fwrite /line \%c </html>
  382.     fclose \%c
  383.     if eq \v(system) unix chmod 664 \%f
  384.     .\%x := \fstripx(\%f)-t             # Make entry in index page.
  385.     if exist \%x.jpg {
  386.         fwrite /line \%o {<a href='\%f'><img -
  387. title="\m(shorttitle) \m(photo) \#\%i" -
  388. alt="\m(shorttitle) \m(photo) \#\%i" -
  389. src="\%x.jpg"></a>}
  390.     } else {
  391.         fwrite /line \%o [ <a href='\%f'>\%f</a> ]
  392.     }
  393. }
  394. # Finish and close Index page
  395.  
  396. fwrite /line \%o <hr color="\m(color)"> # End of page
  397. fwrite /line \%o Created \v(date) by -
  398.  <a href="http://www.columbia.edu/kermit/photoalbum.html">photoalbum</a>
  399. fwrite /line \%o </body>
  400. fwrite /line \%o </html>
  401. fclose \%o
  402. if eq \v(system) unix {
  403.     chmod 664 index.html
  404.     exit
  405. }
  406.