;;FILE:               html-update-index.el
;;LANGUAGE:           emacs lisp
;;SYSTEM:             POSIX
;;    Builds an index of picture thumb nails.
;;    ( used for example by: /data/pascal/pjb-bg/index.template )
;;    <PJB> Pascal Bourguignon <pjb@informatimago.com>
;;    2003-12-04 <PJB> Added this comment.
;;    GPL
;;    Copyright Pascal Bourguignon 2003 - 2003
;;    This program is free software; you can redistribute it and/or
;;    modify it under the terms of the GNU General Public License
;;    as published by the Free Software Foundation; either version
;;    2 of the License, or (at your option) any later version.
;;    This program is distributed in the hope that it will be
;;    useful, but WITHOUT ANY WARRANTY; without even the implied
;;    PURPOSE.  See the GNU General Public License for more details.
;;    You should have received a copy of the GNU General Public
;;    License along with this program; if not, write to the Free
;;    Software Foundation, Inc., 59 Temple Place, Suite 330,
;;    Boston, MA 02111-1307 USA

(defun flow-table (number-of-columns table-attributes cell-list)
RETURN: A string with the table filled with the cells from
        cell-list (a list of strings), over the specified
    (if table-attributes
        (printf nil "<table %s>\n" table-attributes)
      (printf nil "<table>\n"))
    (loop for cell in cell-list
          for i = 0 then (1+ i)
          for close-tag = "" then "</tr>\n"
          (when (= (mod i number-of-columns) 0)
            (printf nil "%s<tr>\n" close-tag))
          (printf nil "<td>%s</td>\n" cell)
    (printf nil "</tr>\n")
    (printf nil "</table>\n")

(defun get-thumb-nail-table ()
RETURN: A string containing the thumb-nails table
  (let ((thumb-nail-dir "small"))
    (flow-table 5 "width=80%"
                 (lambda (file)
                   (let ((nail (format "%s/%s" thumb-nail-dir file))
                         (name (replace-regexp-in-string "pjb-bg-\\(.*\\).jpg" "\\1" file t nil)))
                       (printf nil (concatenate 'string
                                     "<a href=\"%s\">"
                                     "<img src=\"%s\" alt=\"%s\">"
                                     "</a>\n  ") file nail file)
                       (printf nil (concatenate 'string
                                     "<br><a href=\"%s\">%s</a>\n ") file name)
                 (file-expand-wildcards "*.jpg") ))

(defun update-index (directory)
DO:   Generates a new index.html from index.template and the .jpg in the
      given directory.
  (find-file (format "%s/index.template" directory))
  (kill-ring-save (point-min) (point-max))
  (kill-buffer (current-buffer))
  (find-file (format "%s/index.html" directory))
  (printf :stderr "%s\n" (format "%s/index.html" directory))
  (let ((table (get-thumb-nail-table)))
    (when (re-search-forward "<!-- INSERT-THUMB-NAILS-HERE -->" nil t)
      (replace-match table t t))
  (save-buffer 0)
  (kill-buffer (current-buffer))

;;;; html-update-index.el             -- 2003-12-04 04:12:20 -- pascal   ;;;;