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