40d121dd786cb6e56b1c7a39d1fc913bc2b21c30
[syinit] / 07-html.el
1 ;; 07-html-sy.el --- HTML settings
2
3 ;; Copyright (C) 2007 - 2020 Steve Youngs
4
5 ;;     Author: Steve Youngs <steve@sxemacs.org>
6 ;; Maintainer: Steve Youngs <steve@sxemacs.org>
7 ;;    Created: <2007-12-02>
8 ;; Time-stamp: <Tuesday Apr 14, 2020 17:04:47 steve>
9 ;;   Download: <https://downloads.sxemacs.org/SYinits/>
10 ;;   HTMLised: <https://www.sxemacs.org/SYinits/07-html.html>
11 ;;   Git Repo: git clone https://git.sxemacs.org/syinit
12 ;;   Keywords: init, compile
13
14 ;; This file is part of SYinit
15
16 ;; Redistribution and use in source and binary forms, with or without
17 ;; modification, are permitted provided that the following conditions
18 ;; are met:
19 ;;
20 ;; 1. Redistributions of source code must retain the above copyright
21 ;;    notice, this list of conditions and the following disclaimer.
22 ;;
23 ;; 2. Redistributions in binary form must reproduce the above copyright
24 ;;    notice, this list of conditions and the following disclaimer in the
25 ;;    documentation and/or other materials provided with the distribution.
26 ;;
27 ;; 3. Neither the name of the author nor the names of any contributors
28 ;;    may be used to endorse or promote products derived from this
29 ;;    software without specific prior written permission.
30 ;;
31 ;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
32 ;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 ;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 ;; DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 ;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
38 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
39 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
40 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
41 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42
43 ;;; Commentary:
44 ;;
45 ;;   Sets up things for writing HTML.
46
47 ;;; Credits:
48 ;;
49 ;;   The HTML version of this file was created with Hrvoje Niksic's
50 ;;   htmlize.el which is part of the XEmacs "text-modes" package.
51 ;;
52
53 ;;; Todo:
54 ;;
55 ;;     
56
57 ;;; Code:
58 ;:*=======================
59 ;:* General HTML settings.
60 (setq
61  html-helper-htmldtd-version
62  "<?xml version=\"1.0\" encoding=\"utf-8\"?>
63 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
64  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
65  html-helper-use-expert-menu t
66  hm--html-automatic-create-modified-line t
67  hm--html-automatic-update-modified-line t
68  hm--html-changed-comment-prefix "Changed with SXEmacs, by: "
69  hm--html-created-comment-prefix "Created with SXEmacs, by: "
70  hm--html-expert t
71  hm--html-html-doctype-version
72  "\"-//W3C//DTD XHTML 1.0 Transitional//EN\"
73  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\""
74  hm--html-log-date-format "%y-%m-%d"
75  hm--html-template-dir 
76  (file-name-as-directory 
77   (expand-file-name "templates"
78                     (locate-data-directory "hm--html-menus")))
79  hm--html-frame-template-file 
80  (expand-file-name "frame.html.tmpl" hm--html-template-dir)
81  hm--html-title-date-format "%y-%m-%d"
82  hm--html-username "Steve Youngs"
83  html-sigusr1-signal-value 10)
84
85 ;; SGML stuff.
86 (setq
87  sgml-auto-activate-dtd t
88  sgml-live-element-indicator t
89  sgml-validate-command "onsgmls -s -m  %s %s")
90
91 (defvar sy-sgml-dirs
92   (directory-files-recur "/usr/share/sgml" 'full nil nil 'subdir)
93   "List of directories for `sgml-system-path'.")
94
95 (setq sgml-system-path
96       (append '("." 
97                 "/usr/share/sxemacs/xemacs-packages/etc/psgml-dtds/")
98               sy-sgml-dirs))
99
100 (autoload 'html-mode "psgml-html" "Load psgml-html-mode" t)
101 (autoload 'css-mode "css-mode")
102
103 (add-hook 'html-mode-hook #'hm--html-minor-mode)
104 (eval-when-compile (defvar ispell-extra-args))
105 (add-hook 'html-mode-hook
106           #'(lambda ()
107               (make-local-variable 'ispell-extra-args)
108               (push "-H" ispell-extra-args)))
109 (add-hook 'html-helper-timestamp-hook #'html-helper-default-insert-timestamp)
110
111 ;:*=======================
112 ;:* Hrvoje Niksic's htmlize
113 ;;
114 ;;  `htmlize.el' comes with the "text-modes" SXEmacs package.
115 ;;
116 ;;  `htmlize.el' is a lovely piece of code for converting Emacs buffers
117 ;;  into HTML.  The default settings should be fine for most people most
118 ;;  of the time in most situations.  But, by now, I'm sure that you've
119 ;;  come to the realisation that I don't fit into that category.  So
120 ;;  lets hack... :-)
121 (eval-and-compile
122   (require 'htmlize))
123
124 ;; Force XHTML 1.0 Transitional
125 (defun htmlize-default-doctype ()
126   nil
127   "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
128  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">")
129
130 ;; File-local variables can cause problems so this advice simply
131 ;; disables them when htmlize runs.
132 (defadvice htmlize-buffer (around nolocals (&rest args) activate)
133   "Turn off file local variables during htmlize-buffer."
134   (let ((enable-local-variables nil)
135         buf)
136     (setq buf ad-do-it)
137     (when (interactive-p) (switch-to-buffer buf))))
138
139 ;; By default htmlize would turn 'file.el' into 'file.el.html', I
140 ;; prefer that it turned it into 'file.html'.  That's what this advice
141 ;; does.
142 (defadvice htmlize-make-file-name (before filename activate)
143   "Prefer filenames \"file.ext\" -> \"file.html\"."
144   (setq file (file-name-sans-extension file)))
145
146 ;; This function, run from `htmlize-after-hook' lets me embed HTML
147 ;; comments inside normal source comments and also allows me to set up
148 ;; relative hyperlinks.
149 (defun sy-htmlize-set-links ()
150   "Function run from `htmlize-after-hook'.
151
152 It takes care of any relative hyperlinks and also dequotes any HTML
153 comments that were incorrectly quoted.
154
155 Relative hyperlinks need to be prepared in a particular way in the
156 file that is to be converted to HTML.  For example, in an emacs lisp
157 file you might see:
158
159     Get the latest rls=/src/foo.el;rld=version here;rle.
160
161 Which would be converted to:
162
163     Get the latest <a href=\"/src/foo.el\">version here</a>."
164   ;; LOL, we actually need to _NOT_ do it in _this_ file so the doc
165   ;; string for this function looks right.
166   (unless (string-match "07-html" (buffer-name htmlbuf))
167     (save-excursion
168       (goto-char (point-min))
169       (mapcar
170        '(lambda (x) (save-excursion (eval x)))
171        '((replace-string "rls=" "<a href=\"")
172          (replace-string ";rld=" "\">")
173          (replace-string ";rle" "</a>")
174          (replace-string "&lt;!--" "<!--")
175          (replace-string "--&gt;" "-->"))))))
176
177 (defun sy-htmlize-add-xmlns ()
178   "Adds a xmlns property to the html tag.
179
180 Because we are trying to output XHTML we need to add the xmlns to the
181 html tag.  Done via `htmlize-after-hook'."
182   (save-excursion
183     (goto-char (point-min))
184     (re-search-forward "<html>" nil t)
185     (replace-match
186      "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">")))
187
188 ;; I know that the pages generated by `htmlize.el' are valid HTML/CSS and
189 ;; are created by SXEmacs.  So I announce the fact by adding the appropriate
190 ;; logos to the bottom of the page, together with a copyright notice.  This
191 ;; is the raw HTML for that.
192 (defconst sy-htmlize-appendix
193   (concat "<!--  SXEmacs Logo -->
194     <div class=\"img\">
195       <a href=\"https://www.sxemacs.org/\">
196         <img style=\"padding:0px 5px 0px 0px;border:0;width:88px;height:31px\"
197           src=\"/Images/cbsx.png\"
198           title=\"This page was created entirely in SXEmacs\"
199           alt=\"Created with SXEmacs\" />
200       </a>
201 <!--  End SXEmacs Logo -->
202
203 <!--  Valid XHTML 1.0 / CSS -->
204       <a href=\"http://validator.w3.org/check?uri=referer\">
205         <img style=\"padding:0px 5px 0px 10px;border:0;width:88px;height:31px\"
206           src=\"/Images/valid-xhtml10.png\"
207           title=\"Valid XHTML 1.0 Transitional!\"
208           alt=\"Valid XHTML 1.0 Transitional!\" />
209       </a>
210       <a href=\"http://jigsaw.w3.org/css-validator/check/referer\">
211         <img style=\"padding:0px 0px 0px 5px;border:0;width:88px;height:31px\"
212           src=\"/Images/vcss.png\"
213           title=\"Valid CSS!\" alt=\"Valid CSS!\" />
214       </a>
215     </div>
216
217 <!--  End Valid HTML / CSS-->
218
219     <h6>Copyright &#169; "
220           (format-time-string "%Y") " Steve Youngs<br />
221      Verbatim copying and distribution is permitted in any medium,
222     providing this notice is preserved.<br />
223 <!-- hhmts start -->
224 <!-- hhmts end -->
225    </h6>
226
227 "
228 ))
229
230 ;; Appends the above to each page.  Run from `htmlize-after-hook'.
231 (defun sy-htmlize-append ()
232   "Append some things to the end of files produced by `htmlize.el'."
233   (goto-char (point-max))
234   (re-search-backward "</body>" nil t)
235   (insert sy-htmlize-appendix))
236
237 ;; Set a couple of basic things.
238 (setq
239  htmlize-html-charset nil
240  htmlize-html-major-mode 'html-mode
241  htmlize-head-tags
242  "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
243     <meta name=\"author\" content=\"Steve Youngs\" />
244     <meta name=\"owner\" content=\"steve@sxemacs.org\" />
245 ")
246
247 (setq htmlize-hyperlink-style "
248       a {
249         color: #FF0000;
250         background-color: inherit;
251         font: inherit;
252         font-weight: bold;
253         text-decoration: underline;
254       }
255       a:hover {
256         color: #FFFFFF;
257         background-color: #FD00FD;
258         text-decoration: underline;
259       }
260       .img a:hover {
261         background-color: #000000;
262         text-decoration: none;
263       }
264 ")
265
266 ;; Hooks
267 (defvar sy-htmlize-this-file nil
268   "The name of the file we htmlize'd.")
269 (defun sy-htmlize-before-hook ()
270   "Prepares the buffer for htmlising.
271 Turns off howm-mode as it can play havoc with the font-locking, and we
272 force a fontify of the buffer."
273   (setq sy-htmlize-this-file (buffer-file-name))
274   (and howm-mode (howm-mode 0))
275   (font-lock-fontify-buffer))
276
277 (defun sy-htmlize-after-hook ()
278   (sy-htmlize-set-links)
279   (sy-htmlize-append)
280   (sy-htmlize-add-xmlns)
281   (with-current-buffer (get-file-buffer sy-htmlize-this-file)
282     (normal-mode)) ; Will turn howm-mode back on if it was off
283   (setq sy-htmlize-this-file nil))
284
285 (add-hook 'htmlize-before-hook #'sy-htmlize-before-hook)
286 (add-hook 'htmlize-after-hook #'sy-htmlize-after-hook)
287 ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*
288 (message "HTML settings initialised")