Publish captions for images that have descriptions.
[muse-el.git] / lisp / muse-html.el
blobc5a960c8b671e67b9c69ef0ea87149a2869f7b12
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 "\\([" muse-regexp-blank "]*\n\\)\\)"
216 "\\|\\`\\s-*\\|\\s-*\\'\\)")
217 ;; this is somewhat repetitive because we only require the
218 ;; line just before the paragraph beginning to be not
219 ;; read-only
220 3 muse-html-markup-paragraph))
221 "List of markup rules for publishing a Muse page to HTML.
222 For more on the structure of this list, see `muse-publish-markup-regexps'."
223 :type '(repeat (choice
224 (list :tag "Markup rule"
225 integer
226 (choice regexp symbol)
227 integer
228 (choice string function symbol))
229 function))
230 :group 'muse-html)
232 (defcustom muse-html-markup-functions
233 '((anchor . muse-html-markup-anchor)
234 (table . muse-html-markup-table)
235 (footnote . muse-html-markup-footnote))
236 "An alist of style types to custom functions for that kind of text.
237 For more on the structure of this list, see
238 `muse-publish-markup-functions'."
239 :type '(alist :key-type symbol :value-type function)
240 :group 'muse-html)
242 (defcustom muse-html-markup-strings
243 '((image-with-desc . "<table align=\"center\" class=\"image\">
244 <tr><td><img src=\"%1%.%2%\" alt=\"%3%\"></td></tr>
245 <tr><td align=\"center\" class=\"image-caption\">%3%</td></tr>
246 </table>")
247 (image . "<img src=\"%s.%s\" alt=\"\">")
248 (image-link . "<a class=\"image-link\" href=\"%s\">
249 <img src=\"%s.%s\"></a>")
250 (anchor-ref . "<a href=\"#%s\">%s</a>")
251 (url . "<a href=\"%s\">%s</a>")
252 (link . "<a href=\"%s\">%s</a>")
253 (link-and-anchor . "<a href=\"%s#%s\">%s</a>")
254 (email-addr . "<a href=\"mailto:%s\">%s</a>")
255 (emdash . "%s&mdash;%s")
256 (comment-begin . "<!-- ")
257 (comment-end . " -->")
258 (rule . "<hr>")
259 (fn-sep . "<hr>\n")
260 (no-break-space . "&nbsp;")
261 (enddots . "....")
262 (dots . "...")
263 (section . "<h2>")
264 (section-end . "</h2>")
265 (subsection . "<h3>")
266 (subsection-end . "</h3>")
267 (subsubsection . "<h4>")
268 (subsubsection-end . "</h4>")
269 (section-other . "<h5>")
270 (section-other-end . "</h5>")
271 (begin-underline . "<u>")
272 (end-underline . "</u>")
273 (begin-literal . "<code>")
274 (end-literal . "</code>")
275 (begin-emph . "<em>")
276 (end-emph . "</em>")
277 (begin-more-emph . "<strong>")
278 (end-more-emph . "</strong>")
279 (begin-most-emph . "<strong><em>")
280 (end-most-emph . "</em></strong>")
281 (begin-verse . "<p class=\"verse\">\n")
282 (verse-space . "&nbsp;&nbsp;")
283 (end-verse-line . "<br>")
284 (end-last-stanza-line . "<br>")
285 (empty-verse-line . "<br>")
286 (end-verse . "</p>")
287 (begin-example . "<pre class=\"example\">")
288 (end-example . "</pre>")
289 (begin-center . "<center>\n<p>")
290 (end-center . "</p>\n</center>")
291 (begin-quote . "<blockquote>\n")
292 (end-quote . "\n</blockquote>")
293 (begin-quote-item . "<p class=\"quoted\">")
294 (end-quote-item . "</p>")
295 (begin-uli . "<ul>\n")
296 (end-uli . "\n</ul>")
297 (begin-uli-item . "<li>")
298 (end-uli-item . "</li>")
299 (begin-oli . "<ol>\n")
300 (end-oli . "\n</ol>")
301 (begin-oli-item . "<li>")
302 (end-oli-item . "</li>")
303 (begin-dl . "<dl>\n")
304 (end-dl . "\n</dl>")
305 (begin-ddt . "<dt><strong>")
306 (end-ddt . "</strong></dt>")
307 (begin-dde . "<dd>")
308 (end-dde . "</dd>")
309 (begin-table . "<table%s>\n")
310 (end-table . "</table>")
311 (begin-table-row . " <tr>\n")
312 (end-table-row . " </tr>\n")
313 (begin-table-entry . " <%s>")
314 (end-table-entry . "</%s>\n"))
315 "Strings used for marking up text as HTML.
316 These cover the most basic kinds of markup, the handling of which
317 differs little between the various styles."
318 :type '(alist :key-type symbol :value-type string)
319 :group 'muse-html)
321 (defcustom muse-xhtml-markup-strings
322 '((image-with-desc . "<table class=\"image\">
323 <tr><td><img src=\"%1%.%2%\" alt=\"%3%\" /></td></tr>
324 <tr><td align=\"center\" class=\"image-caption\">%3%</td></tr>
325 </table>")
326 (image . "<img src=\"%s.%s\" alt=\"\" />")
327 (image-link . "<a class=\"image-link\" href=\"%s\">
328 <img src=\"%s.%s\" alt=\"\" /></a>")
329 (rule . "<hr />")
330 (fn-sep . "<hr />\n")
331 (begin-underline . "<span style=\"text-decoration: underline;\">")
332 (end-underline . "</span>")
333 (begin-center . "<p style=\"text-align: center;\">\n")
334 (end-center . "\n</p>")
335 (end-verse-line . "<br />")
336 (end-last-stanza-line . "<br />")
337 (empty-verse-line . "<br />"))
338 "Strings used for marking up text as XHTML.
339 These cover the most basic kinds of markup, the handling of which
340 differs little between the various styles.
342 If a markup rule is not found here, `muse-html-markup-strings' is
343 searched."
344 :type '(alist :key-type symbol :value-type string)
345 :group 'muse-html)
347 (defcustom muse-html-markup-tags
348 '(("class" t t muse-html-class-tag))
349 "A list of tag specifications, for specially marking up HTML."
350 :type '(repeat (list (string :tag "Markup tag")
351 (boolean :tag "Expect closing tag" :value t)
352 (boolean :tag "Parse attributes" :value nil)
353 function))
354 :group 'muse-html)
356 (defcustom muse-html-meta-http-equiv "Content-Type"
357 "The http-equiv attribute used for the HTML <meta> tag."
358 :type 'string
359 :group 'muse-html)
361 (defcustom muse-html-meta-content-type "text/html"
362 "The content type used for the HTML <meta> tag.
363 If you are striving for XHTML 1.1 compliance, you may want to
364 change this to \"application/xhtml+xml\"."
365 :type 'string
366 :group 'muse-html)
368 (defcustom muse-html-meta-content-encoding (if (featurep 'mule)
369 'detect
370 "iso-8859-1")
371 "The charset to append to the HTML <meta> tag.
372 If set to the symbol 'detect, use `muse-html-encoding-map' to try
373 and determine the HTML charset from emacs's coding. If set to a
374 string, this string will be used to force a particular charset"
375 :type '(choice string symbol)
376 :group 'muse-html)
378 (defcustom muse-html-encoding-default 'iso-8859-1
379 "The default Emacs buffer encoding to use in published files.
380 This will be used if no special characters are found."
381 :type 'symbol
382 :group 'muse-html)
384 (defcustom muse-html-charset-default "iso-8859-1"
385 "The default HTML meta charset to use if no translation is found in
386 `muse-html-encoding-map'."
387 :type 'string
388 :group 'muse-html)
390 (defun muse-html-insert-anchor (anchor)
391 "Insert an anchor, either around the word at point, or within a tag."
392 (skip-chars-forward (concat muse-regexp-blank "\n"))
393 (if (looking-at (concat "<\\([^" muse-regexp-blank "/>\n]+\\)>"))
394 (let ((tag (match-string 1)))
395 (goto-char (match-end 0))
396 (muse-insert-markup "<a name=\"" anchor "\" id=\"" anchor "\">")
397 (when muse-html-anchor-on-word
398 (or (and (search-forward (format "</%s>" tag)
399 (muse-line-end-position) t)
400 (goto-char (match-beginning 0)))
401 (forward-word 1)))
402 (muse-insert-markup "</a>"))
403 (muse-insert-markup "<a name=\"" anchor "\" id=\"" anchor "\">")
404 (when muse-html-anchor-on-word
405 (forward-word 1))
406 (muse-insert-markup "</a>\n")))
408 (defun muse-html-markup-anchor ()
409 (unless (get-text-property (match-end 1) 'muse-link)
410 (save-match-data
411 (muse-html-insert-anchor (match-string 2)))
412 (match-string 1)))
414 (defun muse-html-markup-paragraph ()
415 (let ((end (copy-marker (match-end 0) t)))
416 (goto-char (match-beginning 0))
417 (when (save-excursion
418 (save-match-data
419 (and (re-search-backward "<\\(/?\\)p[ >]" nil t)
420 (not (string-equal (match-string 1) "/")))))
421 (when (get-text-property (1- (point)) 'end-list)
422 (goto-char (previous-single-property-change (1- (point)) 'end-list)))
423 (muse-insert-markup "</p>"))
424 (goto-char end))
425 (cond
426 ((eobp)
427 (unless (bolp)
428 (insert "\n")))
429 ((eq (char-after) ?\<)
430 (cond
431 ((looking-at "<\\(em\\|strong\\|code\\|span\\)[ >]")
432 (muse-insert-markup "<p>"))
433 ((looking-at "<a ")
434 (if (looking-at "<a[^>\n]+><img")
435 (muse-insert-markup "<p class=\"image-link\">")
436 (muse-insert-markup "<p>")))
437 ((looking-at "<img[ >]")
438 (muse-insert-markup "<p class=\"image\">"))
440 (forward-char 1)
441 nil)))
442 ((muse-looking-back "\\(</h[1-4]>\\|<hr>\\)\n\n")
443 (muse-insert-markup "<p class=\"first\">"))
445 (muse-insert-markup "<p>"))))
447 (defun muse-html-markup-footnote ()
448 (cond
449 ((get-text-property (match-beginning 0) 'muse-link)
450 nil)
451 ((= (muse-line-beginning-position) (match-beginning 0))
452 (prog1
453 (let ((text (match-string 1)))
454 (muse-insert-markup
455 (concat "<p class=\"footnote\">"
456 "<a name=\"fn." text "\" href=\"#fnr." text "\">"
457 text ".</a>")))
458 (save-excursion
459 (save-match-data
460 (let* ((beg (goto-char (match-end 0)))
461 (end (and (search-forward "\n\n" nil t)
462 (prog1
463 (copy-marker (match-beginning 0))
464 (goto-char beg)))))
465 (while (re-search-forward (concat "^["
466 muse-regexp-blank
467 "]+\\([^\n]\\)")
468 end t)
469 (replace-match "\\1" t)))))
470 (replace-match "")))
471 (t (let ((text (match-string 1)))
472 (muse-insert-markup
473 (concat "<sup><a name=\"fnr." text "\" href=\"#fn." text "\">"
474 text "</a></sup>")))
475 (replace-match ""))))
477 (defun muse-html-markup-table ()
478 (muse-xml-markup-table muse-html-table-attributes))
480 ;; Handling of tags for HTML
482 (defun muse-html-insert-contents (depth)
483 (let ((max-depth (or depth 2))
484 (index 1)
485 base contents l)
486 (save-excursion
487 (goto-char (point-min))
488 (search-forward "Page published by Emacs Muse begins here" nil t)
489 (catch 'done
490 (while (re-search-forward "^<h\\([0-9]+\\)>\\(.+?\\)</h\\1>" nil t)
491 (unless (get-text-property (point) 'read-only)
492 (setq l (1- (string-to-number (match-string 1))))
493 (if (null base)
494 (setq base l)
495 (if (< l base)
496 (throw 'done t)))
497 (when (<= l max-depth)
498 (setq contents (cons (cons l (muse-match-string-no-properties 2))
499 contents))
500 (goto-char (match-beginning 2))
501 (muse-html-insert-anchor (concat "sec" (int-to-string index)))
502 (setq index (1+ index)))))))
503 (setq index 1 contents (nreverse contents))
504 (let ((depth 1) (sub-open 0) (p (point)))
505 (muse-insert-markup "<div class=\"contents\">\n<dl>\n")
506 (while contents
507 (muse-insert-markup "<dt>\n"
508 "<a href=\"#sec" (int-to-string index) "\">"
509 (muse-publish-strip-tags (cdar contents))
510 "</a>\n"
511 "</dt>\n")
512 (setq index (1+ index)
513 depth (caar contents)
514 contents (cdr contents))
515 (when contents
516 (cond
517 ((< (caar contents) depth)
518 (let ((idx (caar contents)))
519 (while (< idx depth)
520 (muse-insert-markup "</dl>\n</dd>\n")
521 (setq sub-open (1- sub-open)
522 idx (1+ idx)))))
523 ((> (caar contents) depth) ; can't jump more than one ahead
524 (muse-insert-markup "<dd>\n<dl>\n")
525 (setq sub-open (1+ sub-open))))))
526 (while (> sub-open 0)
527 (muse-insert-markup "</dl>\n</dd>\n")
528 (setq sub-open (1- sub-open)))
529 (muse-insert-markup "</dl>\n</div>\n")
530 (muse-publish-mark-read-only p (point)))))
532 (defun muse-html-class-tag (beg end attrs)
533 (goto-char beg)
534 (muse-insert-markup "<span class=\"" (cdr (assoc "name" attrs)) "\">")
535 (goto-char end)
536 (muse-insert-markup "</span>"))
538 ;; Register the Muse HTML Publisher
540 (defun muse-html-browse-file (file)
541 (browse-url (concat "file:" file)))
543 (defun muse-html-encoding ()
544 (if (stringp muse-html-meta-content-encoding)
545 muse-html-meta-content-encoding
546 (muse-xml-transform-content-type
547 (or (and (boundp 'buffer-file-coding-system)
548 buffer-file-coding-system)
549 muse-html-encoding-default)
550 muse-html-charset-default)))
552 (defun muse-html-prepare-buffer ()
553 (make-local-variable 'muse-html-meta-http-equiv)
554 (set (make-local-variable 'muse-html-meta-content-type)
555 (if (save-match-data
556 (string-match "charset=" muse-html-meta-content-type))
557 muse-html-meta-content-type
558 (concat muse-html-meta-content-type "; charset="
559 (muse-html-encoding)))))
561 (defun muse-html-finalize-buffer ()
562 (when muse-publish-generate-contents
563 (goto-char (car muse-publish-generate-contents))
564 (muse-html-insert-contents (cdr muse-publish-generate-contents)))
565 (when (and (boundp 'buffer-file-coding-system)
566 (memq buffer-file-coding-system '(no-conversion undecided-unix)))
567 ;; make it agree with the default charset
568 (setq buffer-file-coding-system muse-html-encoding-default)))
570 (unless (assoc "html" muse-publishing-styles)
571 (muse-define-style "html"
572 :suffix 'muse-html-extension
573 :regexps 'muse-html-markup-regexps
574 :functions 'muse-html-markup-functions
575 :strings 'muse-html-markup-strings
576 :tags 'muse-html-markup-tags
577 :specials 'muse-xml-decide-specials
578 :before 'muse-html-prepare-buffer
579 :after 'muse-html-finalize-buffer
580 :header 'muse-html-header
581 :footer 'muse-html-footer
582 :style-sheet 'muse-html-style-sheet
583 :browser 'muse-html-browse-file)
585 (muse-derive-style "xhtml" "html"
586 :suffix 'muse-xhtml-extension
587 :strings 'muse-xhtml-markup-strings
588 :header 'muse-xhtml-header
589 :footer 'muse-xhtml-footer
590 :style-sheet 'muse-xhtml-style-sheet))
592 (provide 'muse-html)
594 ;;; muse-html.el ends here