New file.
authortomo <tomo>
Fri, 19 May 2006 10:42:36 +0000 (10:42 +0000)
committertomo <tomo>
Fri, 19 May 2006 10:42:36 +0000 (10:42 +0000)
page.cgi [new file with mode: 0755]
www-page.el [new file with mode: 0644]

diff --git a/page.cgi b/page.cgi
new file mode 100755 (executable)
index 0000000..ec5a639
--- /dev/null
+++ b/page.cgi
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+xemacs -q -batch -l www-page.elc -f www-page-batch-get "$QUERY_STRING"
diff --git a/www-page.el b/www-page.el
new file mode 100644 (file)
index 0000000..1682d09
--- /dev/null
@@ -0,0 +1,418 @@
+;;; www-page.el --- Album page generator for page.cgi.
+
+;; Copyright (C) 2005,2006 MORIOKA Tomohiko
+
+;; Keywords: Photo, image, album, HTML, WWW
+
+;; This file is part of Album.
+
+;; Album 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, or (at your option)
+;; any later version.
+
+;; Album is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;; It requires `convert' and `identify' of ImageMagick.
+
+;;; Code:
+
+(defvar www-page-coding-system
+  (if (featurep 'chise)
+      'utf-8-jp-er
+    'utf-8))
+
+(defvar www-page-default-base-directory
+  "../pub/pages/")
+
+(defvar www-page-size-specs
+  '((thumbnail  160  160)
+    (QVGA       320  240)
+    (VGA        640  480)
+    (SVGA       800  600)
+    (XGA       1024  768)
+    (WXGA      1280  768)
+    (SXGA      1280 1024)
+    (SXGA+     1400 1050)
+    (WSXGA+    1680 1050)
+    (UXGA      1600 1200)
+    (WUXGA     1920 1200)
+    (QXGA      2048 1536)
+    (WQXGA     2560 1600)
+    ))
+
+(defun decode-url-string (string &optional coding-system)
+  (if (> (length string) 0)
+      (let ((i 0)
+           dest)
+       (while (string-match "%\\([0-9A-F][0-9A-F]\\)" string i)
+         (setq dest (concat dest
+                            (substring string i (match-beginning 0))
+                            (char-to-string
+                             (int-char
+                              (string-to-int (match-string 1 string) 16))))
+               i (match-end 0)))
+       (decode-coding-string
+        (concat dest (substring string i))
+        coding-system))))
+
+(defun www-page-display-thumbnails (url-dir &optional size image-root
+                                            lang title parent-url)
+  (setq url-dir (file-name-as-directory url-dir))
+  (let* ((desc-file
+         (expand-file-name "dir.desc"
+                           (expand-file-name url-dir image-root)))
+        (params
+         (with-temp-buffer
+           (when (file-exists-p desc-file)
+             (insert-file-contents desc-file)
+             (read (current-buffer)))))
+        source-images
+        file
+        note)
+    (when (setq title (assq 'title params))
+      (setq title (cdr title)))
+    (unless title
+      (setq title
+           (file-name-nondirectory
+            (substring url-dir 0 (1- (length url-dir))))))
+    (when (setq source-images (assq 'files params))
+      (setq source-images (cdr source-images)))
+    (when (setq note (assq 'note params))
+      (setq note (cdr note)))
+    (when (setq parent-url (assq 'exit params))
+      (setq parent-url (cdr parent-url)))
+    (with-temp-buffer
+      (insert
+       "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
+            \"http://www.w3.org/TR/html4/loose.dtd\">\n")
+      (insert "<html")
+      (if lang
+         (insert (format " lang=\"%s\"" lang)))
+      (insert ">\n")
+      (insert "<head>\n")
+      (insert (format "<title>%s</title>\n" title))
+      (insert "</head>\n")
+      (insert "<body>\n")
+      (insert (format "<h1>%s</h1>\n" title))
+
+      (insert "
+<hr>
+")
+      (dolist (image-file source-images)
+       (setq file (file-name-nondirectory image-file))
+       (insert "<a href=\"page.cgi?")
+       (insert url-dir)
+       (insert file)
+       (insert (format ".html.%s&size=%s\">"
+                       (or lang 'en)
+                       (or size 'VGA)))
+       (insert (format "<img alt=\"%s\" src=\"page.cgi?%s%s&size=thumbnail\">"
+                       file url-dir file))
+       (insert "</a>\n"))
+
+      (when note
+       (insert "<p>")
+       (insert note))
+
+      (insert "
+
+<hr>
+")
+      (if parent-url
+         (insert (format "[<a href=\"%s\">Return</a>]\n" parent-url)))
+      
+      (insert "
+</body>
+</html>
+")
+      (encode-coding-region (point-min)(point-max) www-page-coding-system)
+      (princ "Content-Type: text/html; charset=UTF-8
+
+")
+      (princ (buffer-string))
+      )))
+
+(defun www-page-display-page (file &optional size image-root
+                                   lang prev-file next-file)
+  (if (stringp size)
+      (setq size (intern size)))
+  (if (stringp lang)
+      (setq lang (intern lang)))
+  (princ "Content-Type: text/html; charset=UTF-8
+
+")
+  (with-temp-buffer
+    (insert
+     "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
+            \"http://www.w3.org/TR/html4/loose.dtd\">\n")
+    (insert "<html")
+    (if lang
+       (insert (format " lang=\"%s\"" lang)))
+    (insert " />\n")
+    (insert "<head>\n")
+    (insert (format "<title>%s</title>\n" file))
+    (insert "</head>\n")
+    (insert "<body>\n")
+    ;; (insert (format "<h1>%s</h1>\n" file))
+
+    (let* ((desc-file (expand-file-name (concat file ".desc") image-root))
+          (params
+           (with-temp-buffer
+             (when (file-exists-p desc-file)
+               (insert-file-contents desc-file)
+               (read (current-buffer)))))
+          dir-desc-file
+          prev-file next-file
+          prev-grade next-grade
+          rest spec ret)
+      (unless params
+       (setq dir-desc-file
+             (expand-file-name "dir.desc"
+                               (expand-file-name (file-name-directory file)
+                                                 image-root)))
+       (setq params
+             (with-temp-buffer
+               (when (file-exists-p dir-desc-file)
+                 (insert-file-contents dir-desc-file)
+                 (read (current-buffer)))))
+       (when (setq rest (assq 'files params))
+         (setq rest (cdr rest)))
+       (setq ret (file-name-nondirectory file))
+       (while (and rest
+                   (not (string= (car rest) ret)))
+         (setq prev-file (car rest)
+               rest (cdr rest)))
+       (setq next-file (car (cdr rest)))
+       (if prev-file
+           (setq params (list (cons 'prev-file prev-file))))
+       (if next-file
+           (setq params (cons (cons 'next-file next-file)
+                              params)))
+       (with-temp-buffer
+         (insert (format "%S" params))
+          ;; (princ "X-XEmacs-Message: ")
+         (write-region (point-min)(point-max) desc-file)
+          ;; (princ "\n")
+         ))
+      (if (setq prev-file (assq 'prev-file params))
+         (setq prev-file (cdr prev-file)))
+      (if (setq next-file (assq 'next-file params))
+         (setq next-file (cdr next-file)))
+      (setq rest www-page-size-specs)
+      (while (and rest
+                 (setq spec (car rest))
+                 (not (eq (car spec) size)))
+       (setq prev-grade (car spec)
+             rest (cdr rest)))
+      (setq next-grade (car (car (cdr rest))))
+      (if prev-file
+         (insert (format "<a href=\"page.cgi?%s%s.html.%s&size=%s\">"
+                         (file-name-directory file) prev-file
+                         (or lang 'en)
+                         size)))
+      (cond ((eq lang 'ja)
+            (insert "[\e$BA0\e(B]")
+            )
+           (t
+            (insert "[Previous]")
+            ))
+      (if prev-file
+         (insert "</a>"))
+      (insert "\n")
+
+      (if next-file
+         (insert (format "<a href=\"page.cgi?%s%s.html.%s&size=%s\">"
+                         (file-name-directory file) next-file
+                         (or lang 'en)
+                         size)))
+      (cond ((eq lang 'ja)
+            (insert "[\e$B<!\e(B]")
+            )
+           (t
+            (insert "[Next]")
+            ))
+      (if next-file
+         (insert "</a>"))
+      (insert "\n")
+
+      (if prev-grade
+         (insert (format "<a href=\"page.cgi?%s.html.%s&size=%s\">"
+                         file
+                         (or lang 'en)
+                         prev-grade)))
+      (cond ((eq lang 'ja)
+            (insert "[\e$B=L>.\e(B]")
+            )
+           (t
+            (insert "[Smaller]")
+            ))
+      (if prev-grade
+         (insert "</a>"))
+      (insert "\n")
+
+      (if next-grade
+         (insert (format "<a href=\"page.cgi?%s.html.%s&size=%s\">"
+                         file
+                         (or lang 'en)
+                         next-grade)))
+      (cond ((eq lang 'ja)
+            (insert "[\e$B3HBg\e(B]")
+            )
+           (t
+            (insert "[Larger]")
+            ))
+      (if next-grade
+         (insert "</a>"))
+      (insert "\n")
+
+      (insert "
+<hr>
+")
+      (if next-file
+         (insert (format "<a href=\"page.cgi?%s%s.html.%s&size=%s\">"
+                         (file-name-directory file) next-file
+                         (or lang 'en)
+                         size)))
+      (insert (format "<img alt=\"%s\" src=\"page.cgi?%s&size=%s\">"
+                     file file size))
+      (if next-file
+         (insert "</a>"))
+      (insert "
+<hr>
+
+")
+      (insert
+       (format "<a href=\"page.cgi?%s&lang=%s&size=%s\">[index]</a>"
+              (file-name-directory file)
+              (or lang 'en)
+              size))
+      (insert "
+</body>
+</html>
+"))
+    (encode-coding-region (point-min)(point-max) www-page-coding-system)
+    (princ (buffer-string))
+    ))
+
+(defun www-page-display-image (file &optional size image-root)
+  (setq file (expand-file-name file image-root))
+  (let (file-dir file-name
+       resized-file resized-dir
+       spec)
+    (cond
+     (size
+      (setq file-dir (file-name-directory file)
+           file-name (file-name-nondirectory file))
+      (setq resized-file
+           (expand-file-name
+            file-name
+            (setq resized-dir
+                  (expand-file-name
+                   size file-dir))))
+      (unless (file-exists-p resized-file)
+       (setq size (intern size))
+       (if (setq spec (assq size www-page-size-specs))
+           (progn
+             (condition-case nil
+                 (unless (file-exists-p resized-dir)
+                   (make-directory resized-dir))
+               (error nil))
+             (call-process
+              "convert" nil nil nil
+              "-resize" (format "%dx%d>" (nth 1 spec)(nth 2 spec))
+              file resized-file)
+             )
+         (setq resized-file file)))
+      ;; (princ resized-file)
+      (setq file resized-file)
+      )
+     (t
+      ;; (princ file)
+      ))
+    (princ (format "Content-Type: %s"
+                  (with-temp-buffer
+                    (call-process
+                     "file"
+                     nil t t
+                     "-b" "--mime" file)
+                    (insert "\n")
+                    (let ((coding-system-for-read 'binary)
+                          (coding-system-for-write 'binary))
+                      (insert-file-contents-literally file))
+                    (buffer-string))))))
+
+(defun www-page-batch-get ()
+  ;; (set-coding-priority-list
+  ;;  '(iso-7 iso-8-2 utf-8 big5 shift-jis
+  ;;          iso-8-designate iso-8-1 iso-lock-shift no-conversion))
+  ;; (set-coding-category-system 'utf-8 'utf-8-jp)
+  (let (target params method size key image-root lang ret)
+    (let ((rest (car command-line-args-left))
+         arg val)
+      (if rest
+         (setq rest (split-string rest "&")))
+      (setq target (car rest)
+           rest (cdr rest))
+      (while rest
+       (when (and (string-match "=" (setq arg (car rest)))
+                  (> (length (setq val (substring arg (match-end 0)))) 0))
+         (setq key (substring arg 0 (match-beginning 0)))
+         (set-alist 'params
+                    key
+                    (cons (decode-url-string val www-page-coding-system)
+                          (cdr (assoc key params)))))
+       (setq rest (cdr rest))))
+    (setq method
+         (cond
+          ((string-match "\\.html\\(\\.\\([a-z]+\\)\\)?$" target)
+           (setq lang (match-string 2 target))
+           (setq target (substring target 0 (match-beginning 0)))
+           (cond ((string= (file-name-nondirectory
+                            ;; (substring target 0 (match-beginning 0))
+                            target
+                            )
+                           "index")
+                  (setq target (file-name-directory target))
+                  'dir)
+                 (t
+                  'page))
+           )
+          ((or (string= (file-name-nondirectory target) "")
+               (null (file-name-extension target)))
+           'dir)))
+    ;; (setq file (car (cdr (assoc "file" params))))
+    (setq size (car (cdr (assoc "size" params))))
+    (if (setq ret (cdr (assoc "lang" params)))
+       (setq lang (car ret)))
+    (setq command-line-args-left (cdr command-line-args-left))
+    (setq image-root (or (car command-line-args-left)
+                        (expand-file-name
+                         www-page-default-base-directory
+                         default-directory)))
+    (setq command-line-args-left (cdr command-line-args-left))
+    (cond ((eq method 'dir)
+          (www-page-display-thumbnails target size image-root lang)
+          )
+         ((eq method 'page)
+          (www-page-display-page target size image-root lang)
+          )
+         (t
+          (www-page-display-image target size image-root)
+          )
+         )))
+
+
+(provide 'www-page)
+
+;;; www-page.el ends here