1 ;;; muse-html.el --- Publish to HTML and XHTML.
3 ;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 ;; This file is not part of GNU Emacs.
7 ;; This is free software; you can redistribute it and/or modify it under
8 ;; the terms of the GNU General Public License as published by the Free
9 ;; Software Foundation; either version 2, or (at your option) any later
12 ;; This is distributed in the hope that it will be useful, but WITHOUT
13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; 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.
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.
33 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 ;; Muse HTML Publishing
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 (require 'muse-publish
)
40 (require 'muse-regexps
)
42 (defgroup muse-html nil
43 "Options controlling the behavior of Muse HTML publishing.
44 See `muse-html' for more information."
47 (defcustom muse-html-extension
".html"
48 "Default file extension for publishing HTML files."
52 (defcustom muse-xhtml-extension
".html"
53 "Default file extension for publishing XHTML files."
57 (defcustom muse-html-style-sheet
58 "<style type=\"text/css\">
60 background: white; color: black;
61 margin-left: 3%; margin-right: 7%;
65 p.verse { margin-left: 3% }
67 .example { margin-left: 3% }
73 h3 { margin-bottom: 0px; }
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\">
84 If you are using XHTML, make sure to close the tag properly, as
85 shown in the following example.
87 <link rel=\"stylesheet\" type=\"text/css\" charset=\"utf-8\" media=\"all\" href=\"/default.css\" />"
91 (defcustom muse-html-header
92 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
96 (concat (muse-publishing-directive \"title\")
97 (let ((author (muse-publishing-directive \"author\")))
98 (if (not (string= author (user-full-name)))
99 (concat \" (by \" author \")\"))))</lisp></title>
100 <meta name=\"generator\" content=\"muse.el\">
101 <meta http-equiv=\"<lisp>muse-html-meta-http-equiv</lisp>\"
102 content=\"<lisp>muse-html-meta-content-type</lisp>\">
104 (let ((maintainer (muse-style-element :maintainer)))
106 (concat \"<link rev=\\\"made\\\" href=\\\"\" maintainer \"\\\">\")))
108 <lisp>muse-html-style-sheet</lisp>
112 (concat (muse-publishing-directive \"title\")
113 (let ((author (muse-publishing-directive \"author\")))
114 (if (not (string= author (user-full-name)))
115 (concat \" (by \" author \")\"))))</lisp></h1>
116 <!-- Page published by Emacs Muse begins here -->\n"
117 "Header used for publishing HTML files. This may be text or a filename."
121 (defcustom muse-html-footer
"
122 <!-- Page published by Emacs Muse ends here -->
125 "Footer used for publishing HTML files. This may be text or a filename."
129 (defcustom muse-xhtml-header
130 "<?xml version=\"1.0\" encoding=\"<lisp>
131 (muse-html-encoding)</lisp>\"?>
132 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
133 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
134 <html xmlns=\"http://www.w3.org/1999/xhtml\">
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></title>
141 <meta name=\"generator\" content=\"muse.el\" />
142 <meta http-equiv=\"<lisp>muse-html-meta-http-equiv</lisp>\"
143 content=\"<lisp>muse-html-meta-content-type</lisp>\" />
145 (let ((maintainer (muse-style-element :maintainer)))
147 (concat \"<link rev=\\\"made\\\" href=\\\"\" maintainer \"\\\" />\")))
149 <lisp>muse-html-style-sheet</lisp>
153 (concat (muse-publishing-directive \"title\")
154 (let ((author (muse-publishing-directive \"author\")))
155 (if (not (string= author (user-full-name)))
156 (concat \" (by \" author \")\"))))</lisp></h1>
157 <!-- Page published by Emacs Muse begins here -->\n"
158 "Header used for publishing XHTML files. This may be text or a filename."
162 (defcustom muse-xhtml-footer
"
163 <!-- Page published by Emacs Muse ends here -->
166 "Footer used for publishing XHTML files. This may be text or a filename."
170 (defcustom muse-html-anchor-on-word nil
171 "When true, anchors surround the closest word. This allows you
172 to select them in a browser (i.e. for pasting), but has the
173 side-effect of marking up headers in multiple colors if your
174 header style is different from your link style."
178 (defcustom muse-html-table-attributes
179 "class=\"muse-table\" border=\"2\" cellpadding=\"5\""
180 "The attribute to be used with HTML <table> tags.
181 Note that since Muse supports direct insertion of HTML tags, you
182 can easily create any kind of table you want, as long as each
183 line begins at column 0 (to prevent it from being blockquoted).
184 To make such a table, use this idiom:
188 [... contents of my table, in raw HTML ...]
191 It may look strange to have the tags out of sequence, but this is
192 because the Muse verbatim tag is handled during a different pass
193 than the HTML table tag."
197 (defcustom muse-html-markup-regexps
198 `(;; Join together the parts of a table
199 (10000 ,(concat " </t\\(body\\|head\\|foot\\)>\\s-*</table>\\s-*"
200 "<table[^>]*>\\s-*<t\\1>\n") 0 "")
201 (10100 "</table>\\s-*<table[^>]*>\n" 0 "")
203 ;; Join together the parts of a list
204 (10200 "</\\([oud]l\\)>\\s-*<\\1>\\s-*" 0 "")
206 ;; Beginning of doc, end of doc, or plain paragraph separator
207 (10300 ,(concat "\\(\n</\\(blockquote\\|center\\)>\\)?"
210 "]*\n\\)+\\)\\|\\`\\s-*\\|\\s-*\\'\\)"
211 "\\(<\\(blockquote\\|center\\)>\n\\)?")
212 0 muse-html-markup-paragraph
))
213 "List of markup rules for publishing a Muse page to HTML.
214 For more on the structure of this list, see `muse-publish-markup-regexps'."
215 :type
'(repeat (choice
216 (list :tag
"Markup rule"
218 (choice regexp symbol
)
220 (choice string function symbol
))
224 (defcustom muse-html-markup-functions
225 '((anchor . muse-html-markup-anchor
)
226 (table . muse-html-markup-table
)
227 (footnote . muse-html-markup-footnote
))
228 "An alist of style types to custom functions for that kind of text.
229 For more on the structure of this list, see
230 `muse-publish-markup-functions'."
231 :type
'(alist :key-type symbol
:value-type function
)
234 (defcustom muse-html-markup-strings
235 '((image-with-desc .
"<img src=\"%s\" alt=\"%s\">")
236 (image-link .
"<img src=\"%s\" alt=\"\">")
237 (url-with-image .
"<a class=\"image-link\" href=\"%s\"><img src=\"%s\"></a>")
238 (url-link .
"<a href=\"%s\">%s</a>")
239 (internal-link .
"<a href=\"#%s\">%s</a>")
240 (email-addr .
"<a href=\"mailto:%s\">%s</a>")
241 (emdash .
" — ")
247 (section-end .
"</h2>")
248 (subsection .
"<h3>")
249 (subsection-end .
"</h3>")
250 (subsubsection .
"<h4>")
251 (subsubsection-end .
"</h4>")
252 (section-other .
"<h5>")
253 (section-other-end .
"</h5>")
254 (begin-underline .
"<u>")
255 (end-underline .
"</u>")
256 (begin-literal .
"<code>")
257 (end-literal .
"</code>")
258 (begin-emph .
"<em>")
260 (begin-more-emph .
"<strong>")
261 (end-more-emph .
"</strong>")
262 (begin-most-emph .
"<strong><em>")
263 (end-most-emph .
"</em></strong>")
264 (begin-verse .
"<p class=\"verse\">\n")
265 (verse-space .
" ")
266 (end-verse-line .
"<br>")
267 (end-last-stanza-line .
"<br>")
268 (empty-verse-line .
"<br>")
270 (begin-example .
"<pre class=\"example\">")
271 (end-example .
"</pre>")
272 (begin-center .
"<center>\n")
273 (end-center .
"\n</center>")
274 (begin-quote .
"<blockquote>\n")
275 (end-quote .
"\n</blockquote>")
276 (begin-uli .
"<ul>\n<li>")
277 (end-uli .
"</li>\n</ul>")
278 (begin-oli .
"<ol>\n<li>")
279 (end-oli .
"</li>\n</ol>")
280 (begin-ddt .
"<dl>\n<dt><strong>")
281 (start-dde .
"</strong></dt>\n<dd>")
282 (end-ddt .
"</dd>\n</dl>"))
283 "Strings used for marking up text as HTML.
284 These cover the most basic kinds of markup, the handling of which
285 differs little between the various styles."
286 :type
'(alist :key-type symbol
:value-type string
)
289 (defcustom muse-xhtml-markup-strings
290 '((image-with-desc .
"<img src=\"%s\" alt=\"%s\" />")
291 (image-link .
"<img src=\"%s\" alt=\"\" />")
292 (url-with-image .
"<a class=\"image-link\" href=\"%s\"><img src=\"%s\" alt=\"\" /></a>")
294 (fn-sep .
"<hr />\n")
295 (begin-underline .
"<span style=\"text-decoration: underline;\">")
296 (end-underline .
"</span>")
297 (begin-center .
"<span style=\"text-align: center;\">\n")
298 (end-center .
"\n</span>")
299 (end-verse-line .
"<br />")
300 (end-last-stanza-line .
"<br />")
301 (empty-verse-line .
"<br />"))
302 "Strings used for marking up text as XHTML.
303 These cover the most basic kinds of markup, the handling of which
304 differs little between the various styles.
306 If a markup rule is not found here, `muse-html-markup-strings' is
308 :type
'(alist :key-type symbol
:value-type string
)
311 (defcustom muse-html-markup-tags
312 '(("class" t t muse-html-class-tag
))
313 "A list of tag specifications, for specially marking up HTML."
314 :type
'(repeat (list (string :tag
"Markup tag")
315 (boolean :tag
"Expect closing tag" :value t
)
316 (boolean :tag
"Parse attributes" :value nil
)
320 (defcustom muse-html-markup-specials
325 "A table of characters which must be represented specially."
326 :type
'(alist :key-type character
:value-type string
)
329 (defcustom muse-html-meta-http-equiv
"Content-Type"
330 "The http-equiv attribute used for the HTML <meta> tag."
334 (defcustom muse-html-meta-content-type
"text/html"
335 "The content type used for the HTML <meta> tag.
336 If you are striving for XHTML 1.1 compliance, you may want to
337 change this to \"application/xhtml+xml\"."
341 (defcustom muse-html-meta-content-encoding
(if (featurep 'mule
)
344 "The charset to append to the HTML <meta> tag.
345 If set to the symbol 'detect, use `muse-html-encoding-map' to try
346 and determine the HTML charset from emacs's coding. If set to a
347 string, this string will be used to force a particular charset"
348 :type
'(choice string symbol
)
351 (defcustom muse-html-charset-default
"iso-8859-1"
352 "The default HTML meta charset to use if no translation is found in
353 `muse-html-encoding-map'."
357 (defcustom muse-html-encoding-default
'iso-8859-1
358 "The default Emacs buffer encoding to use in published files.
359 This will be used if no special characters are found."
363 (defcustom muse-html-encoding-map
364 '((iso-8859-1 .
"iso-8859-1")
365 (iso-2022-jp .
"iso-2022-jp")
367 (japanese-iso-8bit .
"euc-jp")
368 (chinese-big5 .
"big5")
369 (mule-utf-8 .
"utf-8")
370 (chinese-iso-8bit .
"gb2312")
371 (chinese-gbk .
"gbk"))
372 "An alist mapping emacs coding systems to appropriate HTML charsets.
373 Use the base name of the coding system (i.e. without the -unix)."
374 :type
'(alist :key-type coding-system
:value-type string
)
377 (defun muse-html-transform-content-type (content-type)
378 "Using `muse-html-encoding-map', try and resolve an emacs coding
379 system to an associated HTML coding system. If no match is found,
380 `muse-html-charset-default' is used instead."
381 (let ((match (and (fboundp 'coding-system-base
)
382 (assoc (coding-system-base content-type
)
383 muse-html-encoding-map
))))
386 muse-html-charset-default
)))
388 (defun muse-html-insert-anchor (anchor)
389 "Insert an anchor, either around the word at point, or within a tag."
390 (skip-chars-forward muse-regexp-space
)
391 (if (looking-at "<\\([^ />]+\\)>")
392 (let ((tag (match-string 1)))
393 (goto-char (match-end 0))
394 (insert "<a name=\"" anchor
"\" id=\"" anchor
"\">")
395 (when muse-html-anchor-on-word
396 (or (and (search-forward (format "</%s>" tag
)
397 (muse-line-end-position) t
)
398 (goto-char (match-beginning 0)))
401 (insert "<a name=\"" anchor
"\" id=\"" anchor
"\">")
402 (when muse-html-anchor-on-word
406 (defun muse-html-markup-anchor ()
408 (muse-html-insert-anchor (match-string 1))) "")
410 (defun muse-html-markup-paragraph ()
411 (let ((end (copy-marker (match-end 0) t
)))
412 (goto-char (match-beginning 0))
413 (when (save-excursion
415 (and (re-search-backward "<\\(/?\\)p[ >]" nil t
)
416 (not (string-equal (match-string 1) "/")))))
423 ((eq (char-after) ?\
<)
425 ((looking-at "<\\(em\\|strong\\|code\\|span\\)[ >]")
428 (if (looking-at "<a[^>]+><img")
429 (insert "<p class=\"image-link\">")
431 ((looking-at "<img[ >]")
432 (insert "<p class=\"image-link\">"))))
433 ((muse-looking-back "\\(</h[1-4]>\\|<hr>\\)\n\n")
434 (insert "<p class=\"first\">"))
435 ((muse-looking-back "<\\(blockquote\\|center\\)>\n")
436 (insert "<p class=\"quoted\">"))
440 (defun muse-html-escape-string (str &rest ignored
)
441 "Convert to character entities any non-alphanumeric characters
442 outside a few punctuation symbols, that risk being misinterpreted
445 (let (pos code len ch
)
447 (while (setq pos
(string-match (concat "[^-"
449 "/:._=@\\?~#]\"<>&;")
451 (setq ch
(aref str pos
)
454 (cond ((fboundp 'char-to-ucs
)
456 ((fboundp 'char-to-int
)
461 str
(concat (substring str
0 pos
)
463 (when (< pos
(length str
))
464 (substring str
(1+ pos
) nil
)))
468 (defun muse-html-markup-footnote ()
469 (if (/= (muse-line-beginning-position) (match-beginning 0))
470 "<sup><a name=\"fnr.\\1\" href=\"#fn.\\1\">\\1</a></sup>"
472 "<p class=\"footnote\"><a name=\"fn.\\1\" href=\"#fnr.\\1\">\\1.</a>"
475 (let* ((beg (goto-char (match-end 0)))
476 (end (and (search-forward "\n\n" nil t
)
478 (copy-marker (match-beginning 0))
480 (while (re-search-forward (concat "^["
484 (replace-match "\\1" t
))))))))
486 (defun muse-html-markup-table ()
489 (delete-region (match-beginning 0) (match-end 0))))
490 (fields (split-string str
"\\s-*|+\\s-*"))
491 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str
)
492 (length (match-string 1 str
))))
493 (part (cond ((= type
1) "tbody")
495 ((= type
3) "tfoot")))
496 (col (cond ((= type
1) "td")
499 (insert "<table " muse-html-table-attributes
">\n"
502 (dolist (field fields
)
503 (insert " <" col
">" field
"</" col
">\n"))
508 ;; Handling of tags for HTML
510 (defun muse-html-insert-contents (depth)
511 (let ((max-depth (or depth
2))
515 (goto-char (point-min))
516 (search-forward "Page published by Emacs Muse begins here" nil t
)
518 (while (re-search-forward "^<h\\([0-9]+\\)>\\(.+?\\)</h\\1>" nil t
)
519 (unless (get-text-property (point) 'read-only
)
520 (setq l
(1- (string-to-number (match-string 1))))
525 (when (<= l max-depth
)
526 (setq contents
(cons (cons l
(muse-match-string-no-properties 2))
528 (goto-char (match-beginning 2))
529 (muse-html-insert-anchor (concat "sec" (int-to-string index
)))
530 (setq index
(1+ index
)))))))
531 (setq index
1 contents
(reverse contents
))
532 (let ((depth 1) (sub-open 0) (p (point)))
533 (insert "<dl class=\"contents\">\n")
535 (insert "<dt class=\"contents\">\n")
536 (insert "<a href=\"#sec" (int-to-string index
) "\">"
537 (muse-publish-strip-tags (cdar contents
))
539 (setq index
(1+ index
))
541 (setq depth
(caar contents
)
542 contents
(cdr contents
))
545 ((< (caar contents
) depth
)
546 (let ((idx (caar contents
)))
548 (insert "</dl>\n</dd>\n")
549 (setq sub-open
(1- sub-open
)
551 ((> (caar contents
) depth
) ; can't jump more than one ahead
552 (insert "<dd>\n<dl class=\"contents\">\n")
553 (setq sub-open
(1+ sub-open
))))))
554 (while (> sub-open
0)
555 (insert "</dl>\n</dd>\n")
556 (setq sub-open
(1- sub-open
)))
558 (muse-publish-mark-read-only p
(point)))))
560 (defun muse-html-class-tag (beg end attrs
)
562 (insert "<span class=\"" (cdr (assoc "name" attrs
)) "\">")
566 ;; Register the Muse HTML Publisher
568 (defun muse-html-browse-file (file)
569 (browse-url (concat "file:" file
)))
571 (defun muse-html-encoding ()
572 (if (stringp muse-html-meta-content-encoding
)
573 muse-html-meta-content-encoding
574 (muse-html-transform-content-type
575 (or (and (boundp 'buffer-file-coding-system
)
576 buffer-file-coding-system
)
577 muse-html-encoding-default
))))
579 (defun muse-html-prepare-buffer ()
580 (set (make-local-variable 'muse-publish-url-transforms
)
581 (cons 'muse-html-escape-string muse-publish-url-transforms
))
582 (make-local-variable 'muse-html-meta-http-equiv
)
583 (set (make-local-variable 'muse-html-meta-content-type
)
585 (string-match "charset=" muse-html-meta-content-type
))
586 muse-html-meta-content-type
587 (concat muse-html-meta-content-type
"; charset="
588 (muse-html-encoding)))))
590 (defun muse-html-fixup-tables ()
592 (goto-char (point-min))
594 (while (re-search-forward "^<table[^>]*>$" nil t
)
595 (unless (get-text-property (point) 'read-only
)
599 (narrow-to-region beg
(and (re-search-forward "^</table>"
601 (match-beginning 0))))
602 (goto-char (point-min))
603 (let ((inhibit-read-only t
))
607 (if (re-search-forward
608 "^\\s-*<t\\(head\\|body\\|foot\\)>$" nil t
)
609 (goto-char (match-beginning 0))
610 (goto-char (point-max)))))
613 (if (re-search-forward
614 "^\\s-*</t\\(head\\|body\\|foot\\)>$" nil t
)
615 (goto-char (match-end 0))
616 (goto-char (point-max)))))
619 (looking-at "\\s-*<t\\(head\\|body\\|foot\\)>")
620 (cond ((string= (match-string 1) "head") 1)
621 ((string= (match-string 1) "foot") 2)
624 (defun muse-html-finalize-buffer ()
625 (when muse-publish-generate-contents
626 (goto-char (car muse-publish-generate-contents
))
627 (muse-html-insert-contents (cdr muse-publish-generate-contents
)))
628 (when (and (boundp 'buffer-file-coding-system
)
629 (memq buffer-file-coding-system
'(no-conversion undecided-unix
)))
630 ;; make it agree with the default charset
631 (setq buffer-file-coding-system muse-html-encoding-default
)))
633 (unless (assoc "html" muse-publishing-styles
)
634 (muse-define-style "html"
635 :suffix
'muse-html-extension
636 :regexps
'muse-html-markup-regexps
637 :functions
'muse-html-markup-functions
638 :strings
'muse-html-markup-strings
639 :tags
'muse-html-markup-tags
640 :specials
'muse-html-markup-specials
641 :before
'muse-html-prepare-buffer
642 :before-end
'muse-html-fixup-tables
643 :after
'muse-html-finalize-buffer
644 :header
'muse-html-header
645 :footer
'muse-html-footer
646 :browser
'muse-html-browse-file
)
648 (muse-derive-style "xhtml" "html"
649 :suffix
'muse-xhtml-extension
650 :strings
'muse-xhtml-markup-strings
651 :header
'muse-xhtml-header
652 :footer
'muse-xhtml-footer
))
656 ;;; muse-html.el ends here