;; 07-html-sy.el --- HTML settings

;; Copyright (C) 2007 - 2020 Steve Youngs

;;     Author: Steve Youngs <steve@sxemacs.org>
;; Maintainer: Steve Youngs <steve@sxemacs.org>
;;    Created: <2007-12-02>
;; Time-stamp: <Wednesday Apr 15, 2020 18:03:47 steve>
;;   Download: <https://downloads.sxemacs.org/SYinits/>
;;   HTMLised: <https://www.sxemacs.org/SYinits/07-html.html>
;;   Git Repo: git clone https://git.sxemacs.org/syinit
;;   Keywords: init, compile

;; This file is part of SYinit

;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;;
;; 1. Redistributions of source code must retain the above copyright
;;    notice, this list of conditions and the following disclaimer.
;;
;; 2. Redistributions in binary form must reproduce the above copyright
;;    notice, this list of conditions and the following disclaimer in the
;;    documentation and/or other materials provided with the distribution.
;;
;; 3. Neither the name of the author nor the names of any contributors
;;    may be used to endorse or promote products derived from this
;;    software without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;; DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

;;; Commentary:
;;
;;   Sets up things for writing HTML.

;;; Credits:
;;
;;   The HTML version of this file was created with Hrvoje Niksic's
;;   htmlize.el which is part of the XEmacs "text-modes" package.
;;

;;; Todo:
;;
;;     

;;; Code:
;:*=======================
;:* General HTML settings.
(setq
 html-helper-htmldtd-version
 "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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 SXEmacs, by: "
 hm--html-created-comment-prefix "Created with SXEmacs, by: "
 hm--html-expert t
 hm--html-html-doctype-version
 "\"-//W3C//DTD XHTML 1.0 Transitional//EN\"
 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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")

(defvar sy-sgml-dirs
  (directory-files-recur "/usr/share/sgml" 'full nil nil 'subdir)
  "List of directories for `sgml-system-path'.")

(setq sgml-system-path
      (append '("." 
                "/usr/share/sxemacs/xemacs-packages/etc/psgml-dtds/")
              sy-sgml-dirs))

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

(add-hook 'html-mode-hook #'hm--html-minor-mode)
(eval-when-compile (defvar ispell-extra-args))
(add-hook 'html-mode-hook
          #'(lambda ()
              (make-local-variable 'ispell-extra-args)
              (push "-H" ispell-extra-args)))
(add-hook 'html-helper-timestamp-hook #'html-helper-default-insert-timestamp)

;:*=======================
;:* Hrvoje Niksic's htmlize
;;
;;  `htmlize.el' comes with the "text-modes" SXEmacs package.
;;
;;  `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))

;; Force XHTML 1.0 Transitional
(defun htmlize-default-doctype ()
  nil
  "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">")

;; File-local variables can cause problems so this advice simply
;; disables them when htmlize runs.
(defadvice htmlize-buffer (around nolocals (&rest args) activate)
  "Turn off file local variables during htmlize-buffer."
  (let ((enable-local-variables nil)
        buf)
    (setq buf ad-do-it)
    (when (interactive-p) (switch-to-buffer buf))))

;; By default htmlize would turn 'file.el' into 'file.el.html', I
;; prefer that it turned it into 'file.html'.  That's what this advice
;; does.
(defadvice htmlize-make-file-name (before filename activate)
  "Prefer filenames \"file.ext\" -> \"file.html\"."
  (setq file (file-name-sans-extension file)))

;; 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.
(defun sy-htmlize-set-links ()
  "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>."
  ;; LOL, we actually need to _NOT_ do it in _this_ file so the doc
  ;; string for this function looks right.
  (unless (string-match "07-html" (buffer-name htmlbuf))
    (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;" "-->"))))))

(defun sy-htmlize-add-xmlns ()
  "Adds a xmlns property to the html tag.

Because we are trying to output XHTML we need to add the xmlns to the
html tag.  Done via `htmlize-after-hook'."
  (save-excursion
    (goto-char (point-min))
    (re-search-forward "<html>" nil t)
    (replace-match
     "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">")))

;; I know that the pages generated by `htmlize.el' are valid HTML and
;; are created by SXEmacs.  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.
;; Used to have valid CSS as well but that is way too much mucking
;; about to get the validator to work from within an iframe.  In case
;; you are wondering, the CSS produced from htmlize is definitely
;; valid.
;; <a href=\"http://jigsaw.w3.org/css-validator/check/referer\" target=\"_blank\">
;;   <img style=\"padding:0px 0px 0px 5px;border:0;width:88px;height:31px\"
;;     src=\"/Images/vcss.png\"
;;     title=\"Valid CSS!\" alt=\"Valid CSS!\" />
;; </a>
(defvar sy-htmlize-appendix
  (concat "<!--  SXEmacs Logo -->
    <div class=\"img\">
      <a href=\"https://www.sxemacs.org/\" target=\"_parent\">
        <img style=\"padding:0px 5px 0px 0px;border:0;width:88px;height:31px\"
          src=\"/Images/cbsx.png\"
          title=\"This page was created entirely in SXEmacs\"
          alt=\"Created with SXEmacs\" />
      </a>
<!--  End SXEmacs Logo -->
<!--  Valid XHTML 1.0 -->
      <a href=\"http://validator.w3.org/check?uri=" "%s" "\" target=\"_blank\">
       <img style=\"padding:0px 5px 0px 10px;border:0;width:88px;height:31px\"
          src=\"/Images/valid-xhtml10.png\"
          title=\"Valid XHTML 1.0 Transitional!\"
          alt=\"Valid XHTML 1.0 Transitional!\" />
      </a>
    </div>
<!--  End Valid XHTML 1.0 -->

    <h6>Copyright &#169; "
          (format-time-string "%Y") " 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'.
(autoload 'url-hexify-string "url-util")
(defun sy-htmlize-append ()
  "Append some things to the end of files produced by `htmlize.el'."
  (let ((html (url-hexify-string
               (concat "https://www.sxemacs.org/SYinits/"
                       (buffer-name)))))
    (goto-char (point-max))
    (re-search-backward "</body>" nil t)
    (insert (format sy-htmlize-appendix html))))

;; Set a couple of basic things.
(setq
 htmlize-html-charset nil
 htmlize-html-major-mode 'html-mode
 htmlize-head-tags
 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
    <meta name=\"author\" content=\"Steve Youngs\" />
    <meta name=\"owner\" content=\"steve@sxemacs.org\" />
")

(setq htmlize-hyperlink-style "
      a {
        color: #FF0000;
        background-color: inherit;
        font: inherit;
        font-weight: bold;
        text-decoration: underline;
      }
      a:hover {
        color: #FFFFFF;
        background-color: #FD00FD;
        text-decoration: underline;
      }
      .img a:hover {
        background-color: #000000;
        text-decoration: none;
      }
")

;; Hooks
(defvar sy-htmlize-this-file nil
  "The name of the file we htmlize'd.")
(defun sy-htmlize-before-hook ()
  "Prepares the buffer for htmlising.
Turns off howm-mode as it can play havoc with the font-locking, and we
force a fontify of the buffer."
  (setq sy-htmlize-this-file (buffer-file-name))
  (and howm-mode (howm-mode 0))
  (font-lock-fontify-buffer))

(defun sy-htmlize-after-hook ()
  (sy-htmlize-set-links)
  (sy-htmlize-append)
  (sy-htmlize-add-xmlns)
  (with-current-buffer (get-file-buffer sy-htmlize-this-file)
    (normal-mode)) ; Will turn howm-mode back on if it was off
  (setq sy-htmlize-this-file nil))

(add-hook 'htmlize-before-hook #'sy-htmlize-before-hook)
(add-hook 'htmlize-after-hook #'sy-htmlize-after-hook)
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*
(message "HTML settings initialised")
Created with SXEmacs Valid XHTML 1.0 Transitional!
Copyright © 2020 Steve Youngs
Verbatim copying and distribution is permitted in any medium, providing this notice is preserved.
Last modified: Wed Apr 15 18:13:12 AEST 2020