#!/bin/sh --login
string=? ; exec /usr/bin/env mzscheme -gfmv- $0 `dirname $0` "$@"

(current-directory (vector-ref argv 0))

(require (lib "ssax.ss" "ssax")
         (lib "myenv.ss" "ssax")
         (lib "util.ss" "ssax")
         (lib "sxml-tools.ss" "sxml")
         (lib "list.ss")
         (lib "file.ss")
         (lib "pregexp.ss"))

;; TODO: how to turn these into MzScheme modules?
(load (find-library "SXML-tree-trans.scm" "ssax/sourceforge/lib"))
(load "SXML-to-XML.scm")

;; ============================================================================
;; Utility functions and file manipulations
;; ============================================================================

(define month-names
  (vector "January" "February" "March" "April" "May" "June" "July"
          "August" "September" "October" "November" "December"))

;; modified-string : string -> string
;; creates a readable string for the last modified date of a given file
(define (modified-string filename)
  (let ([modified (seconds->date (file-or-directory-modify-seconds filename))])
    (format "~a ~a, ~a"
            (vector-ref month-names (sub1 (date-month modified)))
            (date-day modified)
            (date-year modified))))

;; save-xhtml-file : string string -> void
;; saves an XHTML file, overwriting the file if it exists
(define (save-xhtml-file filename contents)
  (with-output-to-file filename
    (lambda ()
      (display "<!-- This page was automatically generated from a stylesheet. ")
      (display "Please see the \"view source\" links for details. -->\n")
      (display "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" ")
      (display "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n")
      (display contents))

;; transform : string string -> void
;; applies the stylesheet to an input file and writes it to an output file
(define (transform in out)
  (let ([input-data (read (open-input-file in))])
    (save-xhtml-file out
     (SXML->XML (pictures-stylesheet
                 (number-contents input-data)
                 (modified-string in))))))

;; ============================================================================
;; Preprocessing functions for the SXML tree
;; ============================================================================

;; TODO: name these three functions better

(define (number-contents doc)
      . ,(lambda (tag . contents)
           `(contents ,(number-images contents 0))))
     (*default* . ,(lambda x x))
     (*text* . ,(lambda (tag str) str)))))

(define (number-images contents count)
  (if (null? contents)
      (let ([elt (car contents)])
          [(eq? (car elt) 'paragraph)
           (cons elt (number-images (cdr contents) count))]
          [(eq? (car elt) 'gallery)
           (let ([len (length (cdr elt))])
             (cons (cons 'gallery (number-gallery (cdr elt) count))
                   (number-images (cdr contents) (+ count len))))]))))

(define (number-gallery images last-index)
  (if (null? images)
      (let ([image (car images)]
            [index (add1 last-index)])
        (cons (list 'image
                    (list (caadr image) (number->string index))
                    (caddr image))
              (number-gallery (cdr images) index)))))

;; ============================================================================
;; Stylesheets
;; ============================================================================

;; pictures-stylesheet : sxml string string -> sxhtml
;; main stylesheet that produces sxhtml
(define (pictures-stylesheet doc source modified)
   `((pictures *preorder*
      . ,(lambda (tag title file created contents . rest)
             (@ (xmlns "http://www.w3.org/1999/xhtml")
                (xml:lang "en")
                (lang "en"))
              (title "timshel.org - pictures (" ,(sxml:content title) ")")
               (@ (http-equiv "Content-type")
                  (content "text/html; charset='ISO-8859-1'")))
              (link (@ (rel "stylesheet")
                       (type "text/css")
                       (href "../../general.css")))
              (link (@ (rel "stylesheet")
                       (type "text/css")
                       (href "../../header.css")))
              (link (@ (rel "stylesheet")
                       (type "text/css")
                       (href "../../pictures.css"))))
              (script (@ (language "php"))
              (div (@ (class "contents"))
               (div (@ (class "padded"))
                (div (@ (class "title")) ,(sxml:content title))
                 ,(pictures-contents-stylesheet (sxml:content file)
                  (a (@ (href "http://validator.w3.org/check/referer"))
                   (img (@ (src "../../valid-xhtml10.png")
                           (height "31")
                           (width "88")
                           (id "validatorImage")
                           (alt "Valid XHTML 1.0")
                           (title ""))))
                  "created " ,(sxml:content created) (br)
                  "last modified " ,modified (br)
                  (span (@ (class "source"))
                   "view source ("
                   (a (@ (href ,source
                               ,(sxml:content file)
                      "PHP") ", "
                   (a (@ (href ,source "?/data/pictures/pictures.scm"))
                      "SXSLT") ", "
                   (a (@ (href ,source
                               ,(sxml:content file)
                      "SXML") ")"))))))))
     (*default* *preorder*
      . ,(lambda x x))
      . ,(lambda (tag str) str)))))

;; TODO: simplify this with post-order

;; pictures-contents-stylesheet : string string string sxml -> sxml
;; stylesheet for the body of the SXML tree (the "contents" section)
(define (pictures-contents-stylesheet file contents)
      . ,(lambda (tag . rest) rest))
      . ,(lambda (tag . contents)
           `(p ,contents)))
      . ,(lambda (tag . contents)
           `(p (@ (class "gallery"))
      . ,(lambda (tag attrs filename)
           `((a (@ (href "/pictures/" ,file "/" ,filename ".jpg"))
              (img (@ (class ,(car attrs))
                      (alt "[" ,(cadr attrs) "]")
                      (title "")
                      (src "/pictures/" ,file "/" ,filename "_sm.jpg"))))
             " ")))
      . ,(lambda (esc . contents)
           (string-append "&" (apply string-append contents) ";")))
      . ,(lambda x x))
      . ,(lambda (tag str)
           (escape-string str)))

;; escape-string : string -> string
;; LaTeX-izes quotes and escapes special XML characters
(define (escape-string str)
  (pregexp-replace* "'"
    (pregexp-replace* "`"
      (pregexp-replace* "''"
        (pregexp-replace* "``"
          (string->goodXML str)

;; ============================================================================
;; Run the script on the command-line arguments.
;; ============================================================================

(define (print-usage)
  (parameterize ((current-output-port (current-error-port)))
    (display "usage: pictures.scm sxml html\n")
    (display "    sxml   SXML input file\n")
    (display "    html   XHTML output file\n")))

(if (not (= (vector-length argv) 3))
      (exit 1))
    (transform (vector-ref argv 1) (vector-ref argv 2)))