Merged from mwolson@gnu.org--2006 (patch 132-135)
[muse-el.git] / lisp / muse-html.el
blob2f2b0637579bea67626b5bd2dac182dca064354b
1 ;;; muse-html.el --- publish to HTML and XHTML
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
7 ;; Emacs Muse is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published
9 ;; by the Free Software Foundation; either version 2, or (at your
10 ;; option) any later version.
12 ;; Emacs Muse is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Emacs Muse; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;; Zhiqiang Ye (yezq AT mail DOT cbi DOT pku DOT edu DOT cn) suggested
27 ;; appending an 'encoding="..."' fragment to the first line of the
28 ;; sample publishing header so that when editing the resulting XHTML
29 ;; file, Emacs would use the proper encoding.
31 ;;; Code:
33 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 ;; Muse HTML Publishing
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 (require 'muse-publish)
40 (require 'muse-regexps)
41 (require 'muse-xml-common)
43 (defgroup muse-html nil
44 "Options controlling the behavior of Muse HTML publishing."
45 :group 'muse-publish)
47 (defcustom muse-html-extension ".html"
48 "Default file extension for publishing HTML files."
49 :type 'string
50 :group 'muse-html)
52 (defcustom muse-xhtml-extension ".html"
53 "Default file extension for publishing XHTML files."
54 :type 'string
55 :group 'muse-html)
57 (defcustom muse-html-style-sheet
58 "<style type=\"text/css\">
59 body {
60 background: white; color: black;
61 margin-left: 3%; margin-right: 7%;
64 p { margin-top: 1% }
65 p.verse { margin-left: 3% }
67 .example { margin-left: 3% }
69 h2 {
70 margin-top: 25px;
71 margin-bottom: 0px;
73 h3 { margin-bottom: 0px; }
74 </style>"
75 "Store your stylesheet definitions here.
76 This is used in `muse-html-header'.
77 You can put raw CSS in here or a <link> tag to an external stylesheet.
78 This text may contain <lisp> markup tags.
80 An example of using <link> is as follows.
82 <link rel=\"stylesheet\" type=\"text/css\" charset=\"utf-8\" media=\"all\" href=\"/default.css\">"
83 :type 'string
84 :group 'muse-html)
86 (defcustom muse-xhtml-style-sheet
87 "<style type=\"text/css\">
88 body {
89 background: white; color: black;
90 margin-left: 3%; margin-right: 7%;
93 p { margin-top: 1% }
94 p.verse { margin-left: 3% }
96 .example { margin-left: 3% }
98 h2 {
99 margin-top: 25px;
100 margin-bottom: 0px;
102 h3 { margin-bottom: 0px; }
103 </style>"
104 "Store your stylesheet definitions here.
105 This is used in `muse-xhtml-header'.
106 You can put raw CSS in here or a <link> tag to an external stylesheet.
107 This text may contain <lisp> markup tags.
109 An example of using <link> is as follows.
111 <link rel=\"stylesheet\" type=\"text/css\" charset=\"utf-8\" media=\"all\" href=\"/default.css\" />"
112 :type 'string
113 :group 'muse-html)
115 (defcustom muse-html-header
116 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
117 <html>
118 <head>
119 <title><lisp>
120 (concat (muse-publishing-directive \"title\")
121 (let ((author (muse-publishing-directive \"author\")))
122 (if (not (string= author (user-full-name)))
123 (concat \" (by \" author \")\"))))</lisp></title>
124 <meta name=\"generator\" content=\"muse.el\">
125 <meta http-equiv=\"<lisp>muse-html-meta-http-equiv</lisp>\"
126 content=\"<lisp>muse-html-meta-content-type</lisp>\">
127 <lisp>
128 (let ((maintainer (muse-style-element :maintainer)))
129 (when maintainer
130 (concat \"<link rev=\\\"made\\\" href=\\\"\" maintainer \"\\\">\")))
131 </lisp><lisp>
132 (muse-style-element :style-sheet muse-publishing-current-style)
133 </lisp>
134 </head>
135 <body>
136 <h1><lisp>
137 (concat (muse-publishing-directive \"title\")
138 (let ((author (muse-publishing-directive \"author\")))
139 (if (not (string= author (user-full-name)))
140 (concat \" (by \" author \")\"))))</lisp></h1>
141 <!-- Page published by Emacs Muse begins here -->\n"
142 "Header used for publishing HTML files. This may be text or a filename."
143 :type 'string
144 :group 'muse-html)
146 (defcustom muse-html-footer "
147 <!-- Page published by Emacs Muse ends here -->
148 </body>
149 </html>\n"
150 "Footer used for publishing HTML files. This may be text or a filename."
151 :type 'string
152 :group 'muse-html)
154 (defcustom muse-xhtml-header
155 "<?xml version=\"1.0\" encoding=\"<lisp>
156 (muse-html-encoding)</lisp>\"?>
157 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
158 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
159 <html xmlns=\"http://www.w3.org/1999/xhtml\">
160 <head>
161 <title><lisp>
162 (concat (muse-publishing-directive \"title\")
163 (let ((author (muse-publishing-directive \"author\")))
164 (if (not (string= author (user-full-name)))
165 (concat \" (by \" author \")\"))))</lisp></title>
166 <meta name=\"generator\" content=\"muse.el\" />
167 <meta http-equiv=\"<lisp>muse-html-meta-http-equiv</lisp>\"
168 content=\"<lisp>muse-html-meta-content-type</lisp>\" />
169 <lisp>
170 (let ((maintainer (muse-style-element :maintainer)))
171 (when maintainer
172 (concat \"<link rev=\\\"made\\\" href=\\\"\" maintainer \"\\\" />\")))
173 </lisp><lisp>
174 (muse-style-element :style-sheet muse-publishing-current-style)
175 </lisp>
176 </head>
177 <body>
178 <h1><lisp>
179 (concat (muse-publishing-directive \"title\")
180 (let ((author (muse-publishing-directive \"author\")))
181 (if (not (string= author (user-full-name)))
182 (concat \" (by \" author \")\"))))</lisp></h1>
183 <!-- Page published by Emacs Muse begins here -->\n"
184 "Header used for publishing XHTML files. This may be text or a filename."
185 :type 'string
186 :group 'muse-html)
188 (defcustom muse-xhtml-footer "
189 <!-- Page published by Emacs Muse ends here -->
190 </body>
191 </html>\n"
192 "Footer used for publishing XHTML files. This may be text or a filename."
193 :type 'string
194 :group 'muse-html)
196 (defcustom muse-html-anchor-on-word nil
197 "When true, anchors surround the closest word. This allows you
198 to select them in a browser (i.e. for pasting), but has the
199 side-effect of marking up headers in multiple colors if your
200 header style is different from your link style."
201 :type 'boolean
202 :group 'muse-html)
204 (defcustom muse-html-table-attributes
205 " class=\"muse-table\" border=\"2\" cellpadding=\"5\""
206 "The attribute to be used with HTML <table> tags.
207 Note that since Muse supports insertion of raw HTML tags, as long
208 as you wrap the region in <literal></literal>."
209 :type 'string
210 :group 'muse-html)
212 (defcustom muse-html-markup-regexps
213 `(;; Beginning of doc, end of doc, or plain paragraph separator
214 (10000 ,(concat "\\(\\(\n\\([" muse-regexp-blank "]*\n\\)+\\)"
215 "\\|\\`\\s-*\\|\\s-*\\'\\)")
216 0 muse-html-markup-paragraph))
217 "List of markup rules for publishing a Muse page to HTML.
218 For more on the structure of this list, see `muse-publish-markup-regexps'."
219 :type '(repeat (choice
220 (list :tag "Markup rule"
221 integer
222 (choice regexp symbol)
223 integer
224 (choice string function symbol))
225 function))
226 :group 'muse-html)
228 (defcustom muse-html-markup-functions
229 '((anchor . muse-html-markup-anchor)
230 (table . muse-html-markup-table)
231 (footnote . muse-html-markup-footnote))
232 "An alist of style types to custom functions for that kind of text.
233 For more on the structure of this list, see
234 `muse-publish-markup-functions'."
235 :type '(alist :key-type symbol :value-type function)
236 :group 'muse-html)
238 (defcustom muse-html-markup-strings
239 '((image-with-desc . "<img src=\"%s\" alt=\"%s\">")
240 (image-link . "<img src=\"%s\" alt=\"\">")
241 (url-with-image . "<a class=\"image-link\" href=\"%s\"><img src=\"%s\"></a>")
242 (anchor-ref . "<a href=\"#%s\">%s</a>")
243 (url . "<a href=\"%s\">%s</a>")
244 (link . "<a href=\"%s\">%s</a>")
245 (link-and-anchor . "<a href=\"%s#%s\">%s</a>")
246 (email-addr . "<a href=\"mailto:%s\">%s</a>")
247 (emdash . "%s&mdash;%s")
248 (comment-begin . "<!-- ")
249 (comment-end . " -->")
250 (rule . "<hr>")
251 (fn-sep . "<hr>\n")
252 (no-break-space . "&nbsp;")
253 (enddots . "....")
254 (dots . "...")
255 (section . "<h2>")
256 (section-end . "</h2>")
257 (subsection . "<h3>")
258 (subsection-end . "</h3>")
259 (subsubsection . "<h4>")
260 (subsubsection-end . "</h4>")
261 (section-other . "<h5>")
262 (section-other-end . "</h5>")
263 (begin-underline . "<u>")
264 (end-underline . "</u>")
265 (begin-literal . "<code>")
266 (end-literal . "</code>")
267 (begin-emph . "<em>")
268 (end-emph . "</em>")
269 (begin-more-emph . "<strong>")
270 (end-more-emph . "</strong>")
271 (begin-most-emph . "<strong><em>")
272 (end-most-emph . "</em></strong>")
273 (begin-verse . "<p class=\"verse\">\n")
274 (verse-space . "&nbsp;&nbsp;")
275 (end-verse-line . "<br>")
276 (end-last-stanza-line . "<br>")
277 (empty-verse-line . "<br>")
278 (end-verse . "</p>")
279 (begin-example . "<pre class=\"example\">")
280 (end-example . "</pre>")
281 (begin-center . "<center>\n<p>")
282 (end-center . "</p>\n</center>")
283 (begin-quote . "<blockquote>\n")
284 (end-quote . "\n</blockquote>")
285 (begin-quote-item . "<p class=\"quoted\">")
286 (end-quote-item . "</p>")
287 (begin-uli . "<ul>\n")
288 (end-uli . "\n</ul>")
289 (begin-uli-item . "<li>")
290 (end-uli-item . "</li>")
291 (begin-oli . "<ol>\n")
292 (end-oli . "\n</ol>")
293 (begin-oli-item . "<li>")
294 (end-oli-item . "</li>")
295 (begin-dl . "<dl>\n")
296 (end-dl . "\n</dl>")
297 (begin-ddt . "<dt><strong>")
298 (end-ddt . "</strong></dt>")
299 (begin-dde . "<dd>")
300 (end-dde . "</dd>")
301 (begin-table . "<table%s>\n")
302 (end-table . "</table>")
303 (begin-table-row . " <tr>\n")
304 (end-table-row . " </tr>\n")
305 (begin-table-entry . " <%s>")
306 (end-table-entry . "</%s>\n"))
307 "Strings used for marking up text as HTML.
308 These cover the most basic kinds of markup, the handling of which
309 differs little between the various styles."
310 :type '(alist :key-type symbol :value-type string)
311 :group 'muse-html)
313 (defcustom muse-xhtml-markup-strings
314 '((image-with-desc . "<img src=\"%s\" alt=\"%s\" />")
315 (image-link . "<img src=\"%s\" alt=\"\" />")
316 (url-with-image . "<a class=\"image-link\" href=\"%s\"><img src=\"%s\" alt=\"\" /></a>")
317 (rule . "<hr />")
318 (fn-sep . "<hr />\n")
319 (begin-underline . "<span style=\"text-decoration: underline;\">")
320 (end-underline . "</span>")
321 (begin-center . "<p style=\"text-align: center;\">\n")
322 (end-center . "\n</p>")
323 (end-verse-line . "<br />")
324 (end-last-stanza-line . "<br />")
325 (empty-verse-line . "<br />"))
326 "Strings used for marking up text as XHTML.
327 These cover the most basic kinds of markup, the handling of which
328 differs little between the various styles.
330 If a markup rule is not found here, `muse-html-markup-strings' is
331 searched."
332 :type '(alist :key-type symbol :value-type string)
333 :group 'muse-html)
335 (defcustom muse-html-markup-tags
336 '(("class" t t muse-html-class-tag))
337 "A list of tag specifications, for specially marking up HTML."
338 :type '(repeat (list (string :tag "Markup tag")
339 (boolean :tag "Expect closing tag" :value t)
340 (boolean :tag "Parse attributes" :value nil)
341 function))
342 :group 'muse-html)
344 (defcustom muse-html-meta-http-equiv "Content-Type"
345 "The http-equiv attribute used for the HTML <meta> tag."
346 :type 'string
347 :group 'muse-html)
349 (defcustom muse-html-meta-content-type "text/html"
350 "The content type used for the HTML <meta> tag.
351 If you are striving for XHTML 1.1 compliance, you may want to
352 change this to \"application/xhtml+xml\"."
353 :type 'string
354 :group 'muse-html)
356 (defcustom muse-html-meta-content-encoding (if (featurep 'mule)
357 'detect
358 "iso-8859-1")
359 "The charset to append to the HTML <meta> tag.
360 If set to the symbol 'detect, use `muse-html-encoding-map' to try
361 and determine the HTML charset from emacs's coding. If set to a
362 string, this string will be used to force a particular charset"
363 :type '(choice string symbol)
364 :group 'muse-html)
366 (defcustom muse-html-encoding-default 'iso-8859-1
367 "The default Emacs buffer encoding to use in published files.
368 This will be used if no special characters are found."
369 :type 'symbol
370 :group 'muse-html)
372 (defcustom muse-html-charset-default "iso-8859-1"
373 "The default HTML meta charset to use if no translation is found in
374 `muse-html-encoding-map'."
375 :type 'string
376 :group 'muse-html)
378 (defun muse-html-insert-anchor (anchor)
379 "Insert an anchor, either around the word at point, or within a tag."
380 (skip-chars-forward (concat muse-regexp-blank "\n"))
381 (if (looking-at (concat "<\\([^" muse-regexp-blank "/>\n]+\\)>"))
382 (let ((tag (match-string 1)))
383 (goto-char (match-end 0))
384 (muse-insert-markup "<a name=\"" anchor "\" id=\"" anchor "\">")
385 (when muse-html-anchor-on-word
386 (or (and (search-forward (format "</%s>" tag)
387 (muse-line-end-position) t)
388 (goto-char (match-beginning 0)))
389 (forward-word 1)))
390 (muse-insert-markup "</a>"))
391 (muse-insert-markup "<a name=\"" anchor "\" id=\"" anchor "\">")
392 (when muse-html-anchor-on-word
393 (forward-word 1))
394 (muse-insert-markup "</a>\n")))
396 (defun muse-html-markup-anchor ()
397 (unless (get-text-property (match-end 1) 'muse-link)
398 (save-match-data
399 (muse-html-insert-anchor (match-string 2)))
400 (match-string 1)))
402 (defun muse-html-markup-paragraph ()
403 (let ((end (copy-marker (match-end 0) t)))
404 (goto-char (match-beginning 0))
405 (when (save-excursion
406 (save-match-data
407 (and (re-search-backward "<\\(/?\\)p[ >]" nil t)
408 (not (string-equal (match-string 1) "/")))))
409 (when (get-text-property (1- (point)) 'end-list)
410 (goto-char (previous-single-property-change (1- (point)) 'end-list)))
411 (muse-insert-markup "</p>"))
412 (goto-char end))
413 (cond
414 ((eobp)
415 (unless (bolp)
416 (insert "\n")))
417 ((eq (char-after) ?\<)
418 (cond
419 ((looking-at "<\\(em\\|strong\\|code\\|span\\)[ >]")
420 (muse-insert-markup "<p>"))
421 ((looking-at "<a ")
422 (if (looking-at "<a[^>\n]+><img")
423 (muse-insert-markup "<p class=\"image-link\">")
424 (muse-insert-markup "<p>")))
425 ((looking-at "<img[ >]")
426 (muse-insert-markup "<p class=\"image-link\">"))
428 (forward-char 1)
429 nil)))
430 ((muse-looking-back "\\(</h[1-4]>\\|<hr>\\)\n\n")
431 (muse-insert-markup "<p class=\"first\">"))
433 (muse-insert-markup "<p>"))))
435 (defun muse-html-markup-footnote ()
436 (cond
437 ((get-text-property (match-beginning 0) 'muse-link)
438 nil)
439 ((= (muse-line-beginning-position) (match-beginning 0))
440 (prog1
441 (let ((text (match-string 1)))
442 (muse-insert-markup
443 (concat "<p class=\"footnote\">"
444 "<a name=\"fn." text "\" href=\"#fnr." text "\">"
445 text ".</a>")))
446 (save-excursion
447 (save-match-data
448 (let* ((beg (goto-char (match-end 0)))
449 (end (and (search-forward "\n\n" nil t)
450 (prog1
451 (copy-marker (match-beginning 0))
452 (goto-char beg)))))
453 (while (re-search-forward (concat "^["
454 muse-regexp-blank
455 "]+\\([^\n]\\)")
456 end t)
457 (replace-match "\\1" t)))))
458 (replace-match "")))
459 (t (let ((text (match-string 1)))
460 (muse-insert-markup
461 (concat "<sup><a name=\"fnr." text "\" href=\"#fn." text "\">"
462 text "</a></sup>")))
463 (replace-match ""))))
465 (defun muse-html-markup-table ()
466 (muse-xml-markup-table muse-html-table-attributes))
468 ;; Handling of tags for HTML
470 (defun muse-html-insert-contents (depth)
471 (let ((max-depth (or depth 2))
472 (index 1)
473 base contents l)
474 (save-excursion
475 (goto-char (point-min))
476 (search-forward "Page published by Emacs Muse begins here" nil t)
477 (catch 'done
478 (while (re-search-forward "^<h\\([0-9]+\\)>\\(.+?\\)</h\\1>" nil t)
479 (unless (get-text-property (point) 'read-only)
480 (setq l (1- (string-to-number (match-string 1))))
481 (if (null base)
482 (setq base l)
483 (if (< l base)
484 (throw 'done t)))
485 (when (<= l max-depth)
486 (setq contents (cons (cons l (muse-match-string-no-properties 2))
487 contents))
488 (goto-char (match-beginning 2))
489 (muse-html-insert-anchor (concat "sec" (int-to-string index)))
490 (setq index (1+ index)))))))
491 (setq index 1 contents (reverse contents))
492 (let ((depth 1) (sub-open 0) (p (point)))
493 (muse-insert-markup "<dl class=\"contents\">\n")
494 (while contents
495 (muse-insert-markup "<dt class=\"contents\">\n"
496 "<a href=\"#sec" (int-to-string index) "\">"
497 (muse-publish-strip-tags (cdar contents))
498 "</a>\n"
499 "</dt>\n")
500 (setq index (1+ index)
501 depth (caar contents)
502 contents (cdr contents))
503 (when contents
504 (cond
505 ((< (caar contents) depth)
506 (let ((idx (caar contents)))
507 (while (< idx depth)
508 (muse-insert-markup "</dl>\n</dd>\n")
509 (setq sub-open (1- sub-open)
510 idx (1+ idx)))))
511 ((> (caar contents) depth) ; can't jump more than one ahead
512 (muse-insert-markup "<dd>\n<dl class=\"contents\">\n")
513 (setq sub-open (1+ sub-open))))))
514 (while (> sub-open 0)
515 (muse-insert-markup "</dl>\n</dd>\n")
516 (setq sub-open (1- sub-open)))
517 (muse-insert-markup "</dl>\n")
518 (muse-publish-mark-read-only p (point)))))
520 (defun muse-html-class-tag (beg end attrs)
521 (goto-char beg)
522 (muse-insert-markup "<span class=\"" (cdr (assoc "name" attrs)) "\">")
523 (goto-char end)
524 (muse-insert-markup "</span>"))
526 ;; Register the Muse HTML Publisher
528 (defun muse-html-browse-file (file)
529 (browse-url (concat "file:" file)))
531 (defun muse-html-encoding ()
532 (if (stringp muse-html-meta-content-encoding)
533 muse-html-meta-content-encoding
534 (muse-xml-transform-content-type
535 (or (and (boundp 'buffer-file-coding-system)
536 buffer-file-coding-system)
537 muse-html-encoding-default)
538 muse-html-charset-default)))
540 (defun muse-html-prepare-buffer ()
541 (make-local-variable 'muse-html-meta-http-equiv)
542 (set (make-local-variable 'muse-html-meta-content-type)
543 (if (save-match-data
544 (string-match "charset=" muse-html-meta-content-type))
545 muse-html-meta-content-type
546 (concat muse-html-meta-content-type "; charset="
547 (muse-html-encoding)))))
549 (defun muse-html-finalize-buffer ()
550 (when muse-publish-generate-contents
551 (goto-char (car muse-publish-generate-contents))
552 (muse-html-insert-contents (cdr muse-publish-generate-contents)))
553 (when (and (boundp 'buffer-file-coding-system)
554 (memq buffer-file-coding-system '(no-conversion undecided-unix)))
555 ;; make it agree with the default charset
556 (setq buffer-file-coding-system muse-html-encoding-default)))
558 (unless (assoc "html" muse-publishing-styles)
559 (muse-define-style "html"
560 :suffix 'muse-html-extension
561 :regexps 'muse-html-markup-regexps
562 :functions 'muse-html-markup-functions
563 :strings 'muse-html-markup-strings
564 :tags 'muse-html-markup-tags
565 :specials 'muse-xml-decide-specials
566 :before 'muse-html-prepare-buffer
567 :after 'muse-html-finalize-buffer
568 :header 'muse-html-header
569 :footer 'muse-html-footer
570 :style-sheet 'muse-html-style-sheet
571 :browser 'muse-html-browse-file)
573 (muse-derive-style "xhtml" "html"
574 :suffix 'muse-xhtml-extension
575 :strings 'muse-xhtml-markup-strings
576 :header 'muse-xhtml-header
577 :footer 'muse-xhtml-footer
578 :style-sheet 'muse-xhtml-style-sheet))
580 (provide 'muse-html)
582 ;;; muse-html.el ends here