;: -*- emacs-lisp -*-
;:* $Id: html-sy.el,v 1.1 2003-10-17 23:45:30+10 steve Exp steve $

;:*=======================
;:* General HTML settings.
(setq
 html-helper-htmldtd-version 
 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
                      \"http://www.w3.org/TR/html4/loose.dtd\">\n"
 html-helper-use-expert-menu t
 hm--html-automatic-create-modified-line t
 hm--html-automatic-update-modified-line t
 hm--html-changed-comment-prefix "Changed with XEmacs, by: "
 hm--html-created-comment-prefix "Created with XEmacs, by: "
 hm--html-expert t
 hm--html-html-doctype-version 
 "-//W3C//DTD HTML 4.01 Transitional//EN\"
                     \"http://www.w3.org/TR/html4/loose.dtd"
 hm--html-log-date-format "%y-%m-%d"
 hm--html-template-dir 
 (file-name-as-directory 
  (expand-file-name "templates"
                    (locate-data-directory "hm--html-menus")))
 hm--html-frame-template-file 
 (expand-file-name "frame.html.tmpl" hm--html-template-dir)
 hm--html-title-date-format "%y-%m-%d"
 hm--html-username "Steve Youngs"
 html-sigusr1-signal-value 10)

;; SGML stuff.
(setq
 sgml-auto-activate-dtd t
 sgml-live-element-indicator t
 sgml-validate-command "onsgmls -s -m  %s %s")

(setq sgml-system-path 
      '("." 
        "/usr/local/share/sgml/dtd/docbook-3.0/" 
        "/usr/local/share/sgml/dtd/docbook-3.1/" 
        "/usr/local/share/sgml/dtd/docbook-4.0/" 
        "/usr/local/share/sgml/dtd/jade/" 
        "/usr/local/share/sgml/dtd/sgmltools/" 
        "/usr/local/share/sgml/entities/iso-entities-8879.1986/" 
        "/usr/local/share/sgml/stylesheets/docbook/" 
        "/usr/local/share/sgml/stylesheets/sgmltools/" 
        "/usr/local/lib/xemacs/xemacs-packages/etc/psgml-dtds/"))

;; Run HTML Tidy over the current buffer.  I don't use this yet, I've
;; only just installed HTML Tidy and haven't yet figured out how to
;; use it properly.  I snarfed this function from:
;;
;;      <http://www.hollenback.net/index.php/EmacsTidy>
(defun tidy-buffer ()
  "Run Tidy HTML parser on current buffer."
  (interactive)
  (if (get-buffer "tidy-errs") 
      (kill-buffer "tidy-errs"))
  (shell-command-on-region (point-min) (point-max)
                           "tidy -f /tmp/tidy-errs -q -i -wrap 72 -c" t)
  (find-file-other-window "/tmp/tidy-errs")
  (other-window 1)
  (delete-file "/tmp/tidy-errs")
  (message "buffer tidy'ed"))

(autoload 'html-mode "psgml-html" "Load psgml-html-mode" t)
(autoload 'css-mode "css-mode")

(add-hook 'html-mode-hook 'hm--html-minor-mode)
(add-hook 'html-mode-hook
          '(lambda ()
             (setq ispell-extra-args "-H")))
(add-hook 'html-helper-timestamp-hook 'html-helper-default-insert-timestamp)

;:*=======================
;:* Hrvoje Niksic's htmlize
;;
;;  `htmlize.el' comes with the "text-modes" XEmacs package, or you
;;  can get the latest version from:
;;
;;      <http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el>.
;;
;;  `htmlize.el' is a lovely piece of code for converting Emacs buffers
;;  into HTML.  The default settings should be fine for most people most
;;  of the time in most situations.  But, by now, I'm sure that you've
;;  come to the realisation that I don't fit into that category.  So
;;  lets hack... :-)
(eval-and-compile
  (require 'htmlize))

;; Those "local-variables" declarations you sometimes see at the bottom
;; of source files can cause quite a bit of havoc when it comes to
;; "htmlize'ing" them.  Basically what happens is that when you go to
;; save the resulting HTML buffer XEmacs goes into an infinite loop
;; complaining about bad local vars.  I get around it with this next
;; function which just a simple wrapper for `htmlize-buffer'.
(defun sy-htmlize-buffer (&optional buffer)
  "This is just a simple wrapper for `htmlize-buffer'.

All it does is scan the buffer to be converted for any local variable
declarations and replaces them with a place holder.  This prevents an
infloop when you go to save the resulting HTML buffer.

Original `htmlize-buffer' doc string:

Convert buffer to HTML, preserving the font-lock colorization.
The generated HTML is available in a new buffer, which is returned.
When invoked interactively, the new buffer is selected in the
current window."
  (interactive)
  (let ((htmlbuf (with-current-buffer (or buffer (current-buffer))
                   (goto-char (point-min))
                   (while (re-search-forward
                           ;; This concat is deliberately split accross
                           ;; 4 lines for a reason.  Don't change it!
                           (concat "^" 
                                   comment-start 
                                   "Local"
                                   " Variables:$") nil t)
                     (replace-match 
                      (concat comment-start
                              " Put the normal local variables declaration here")))
                   (htmlize-buffer-1)
                   ;; Undo that replace-match.
                   (condition-case nil
                       (undo)
                     (error nil)))))
    (when (interactive-p)
      (switch-to-buffer htmlbuf))
    htmlbuf))

;; I prefer to use this doctype.  Hrvoje, shouldn't this be customisable?
(defun sy-htmlize-css-doctype ()
  nil                                   ; no doc-string
  "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
    \"http://www.w3.org/TR/html4/loose.dtd\">")

;; Use my functions by default.
(defalias 'htmlize-buffer 'sy-htmlize-buffer)
(defalias 'htmlize-css-doctype 'sy-htmlize-css-doctype)

;; This function, run from `htmlize-after-hook' lets me embed HTML
;; comments inside normal source comments and also allows me to set up
;; relative hyperlinks.  It has been hand-massaged after htmlizing to
;; get the desired results for its doc string.
(defun sy-htmlize-after-hook ()
  "Function run from `htmlize-after-hook'.

It takes care of any relative hyperlinks and also dequotes any HTML
comments that were incorrectly quoted.

Relative hyperlinks need to be prepared in a particular way in the
file that is to be converted to HTML.  For example, in an emacs lisp
file you might see:

Get the latest rls=/src/foo.el;rld=version here;rle.

Which would be converted to:

Get the latest <a href="/src/foo.el">version here</a>."
  (save-excursion
    (goto-char (point-min))
    (mapcar
     '(lambda (x) (save-excursion (eval x)))
     '((replace-string "rls=" "<a href=\"")
       (replace-string ";rld=" "\">")
       (replace-string ";rle" "</a>")
       (replace-string "&lt;!--" "<!--")
       (replace-string "--&gt;" "-->")))))

;; I know that the pages generated by `htmlize.el' are valid HTML/CSS and
;; are created by XEmacs.  So I announce the fact by adding the appropriate
;; logos to the bottom of the page, together with a copyright notice.  This
;; is the raw HTML for that.
(defconst sy-htmlize-appendix
  "<!--  Logos -->
<br />
<!--  XEmacs Logo -->
    <a href=\"http://www.xemacs.org/\">
<img src=\"/images/created-xemacs.png\" 
     border=\"0\"
     width=\"88\"
     height=\"31\" 
     alt=\"Created with XEmacs\" /></a>
<!--  End XEmacs Logo -->

<!--  Valid HTML 4.0.1 / CSS -->
      <a href=\"http://validator.w3.org/check/referer\"><img border=\"0\"
          src=\"/images/valid-html401.png\"
          alt=\"Valid HTML 4.01!\" height=\"31\" width=\"88\"></a>
 <a href=\"http://jigsaw.w3.org/css-validator/check/referer\">
  <img style=\"border:0;width:88px;height:31px\"
       src=\"/images/vcss.png\" 
       alt=\"Valid CSS!\">
 </a>
<!--  End Valid HTML / CSS-->

    <h6>Copyright &#169; 2003 Steve Youngs<br />
     Verbatim copying and distribution is permitted in any medium,
    providing this notice is preserved.<br />
<!-- hhmts start -->
<!-- hhmts end -->
   </h6>

")

;; Appends the above to each page.  Run from `htmlize-after-hook'.
(defun sy-htmlize-append ()
  "Append some things to the end of files produced by `htmlize.el'."
  (goto-char (point-max))
  (re-search-backward "</body>" nil t)
  (insert sy-htmlize-appendix))

;; I have web pages at various sites around the place and unfortunately
;; the logo image files are in different locations at the different
;; sites.  This sorts that mess out.
(defun sy-htmlize-set-footer-image-locations ()
  "Set the \"<img src=\" tags for the images at the bottom of the page."
  (let ((location (read-string "Location (bigpond,eicq,local-p,local-s): "))
        (bigpond-replace "/sryoungs/images/")
        (eicq-replace "./images/")
        (local-p-replace "/~steve/images/")
        (local-s-replace "/images/")
        (replace-this "/images/")
        replacement)
    (cond
     ((string= location "bigpond")
      (setq replacement bigpond-replace))
     ((string= location "eicq")
      (setq replacement eicq-replace))
     ((string= location "local-p")
      (setq replacement local-p-replace))
     ((string= location "local-s")
      (setq replacement local-s-replace)))
    (save-excursion
      (goto-char (point-min))
      (re-search-forward "<!--  Logos -->" nil t)
      (save-restriction
        (narrow-to-region (point) (point-max))
        (goto-char (point-min))
        (while (re-search-forward replace-this nil t)
          (replace-match replacement nil nil))))))

;; Set a couple of basic things.
(setq
 htmlize-html-charset "iso-8859-1"
 htmlize-html-major-mode 'html-mode
 htmlize-head-tags
 "<meta http-equiv=\"author\" content=\"Steve Youngs\">
<meta http-equiv=\"generator\" content=\"Slackware GNU/Linux + XEmacs + htmlize.el\">

")

;; Hooks
(add-hook 'htmlize-before-hook 'font-lock-fontify-buffer)
(add-hook 'htmlize-after-hook 
          '(lambda ()
             (sy-htmlize-after-hook)
             (sy-htmlize-append)
             (sy-htmlize-set-footer-image-locations)
             (font-lock-fontify-buffer)))
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*
(message "hm--html-mode initialised")

Created with XEmacs Valid HTML 4.01! Valid CSS!
Copyright © 2003 Steve Youngs
Verbatim copying and distribution is permitted in any medium, providing this notice is preserved.
Last modified: Thu Oct 30 10:11:31 EST 2003