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."
46 (defcustom muse-html-extension
".html"
47 "Default file extension for publishing HTML files."
51 (defcustom muse-xhtml-extension
".html"
52 "Default file extension for publishing XHTML files."
56 (defcustom muse-html-style-sheet
57 "<style type=\"text/css\">
59 background: white; color: black;
60 margin-left: 3%; margin-right: 7%;
64 p.verse { margin-left: 3% }
66 .example { margin-left: 3% }
72 h3 { margin-bottom: 0px; }
74 "Store your stylesheet definitions here.
75 This is used in `muse-html-header'.
76 You can put raw CSS in here or a <link> tag to an external stylesheet.
77 This text may contain <lisp> markup tags.
79 An example of using <link> is as follows.
81 <link rel=\"stylesheet\" type=\"text/css\" charset=\"utf-8\" media=\"all\" href=\"/default.css\">"
85 (defcustom muse-xhtml-style-sheet
86 "<style type=\"text/css\">
88 background: white; color: black;
89 margin-left: 3%; margin-right: 7%;
93 p.verse { margin-left: 3% }
95 .example { margin-left: 3% }
101 h3 { margin-bottom: 0px; }
103 "Store your stylesheet definitions here.
104 This is used in `muse-xhtml-header'.
105 You can put raw CSS in here or a <link> tag to an external stylesheet.
106 This text may contain <lisp> markup tags.
108 An example of using <link> is as follows.
110 <link rel=\"stylesheet\" type=\"text/css\" charset=\"utf-8\" media=\"all\" href=\"/default.css\" />"
114 (defcustom muse-html-header
115 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
119 (concat (muse-publishing-directive \"title\")
120 (let ((author (muse-publishing-directive \"author\")))
121 (if (not (string= author (user-full-name)))
122 (concat \" (by \" author \")\"))))</lisp></title>
123 <meta name=\"generator\" content=\"muse.el\">
124 <meta http-equiv=\"<lisp>muse-html-meta-http-equiv</lisp>\"
125 content=\"<lisp>muse-html-meta-content-type</lisp>\">
127 (let ((maintainer (muse-style-element :maintainer)))
129 (concat \"<link rev=\\\"made\\\" href=\\\"\" maintainer \"\\\">\")))
131 <lisp>muse-html-style-sheet</lisp>
135 (concat (muse-publishing-directive \"title\")
136 (let ((author (muse-publishing-directive \"author\")))
137 (if (not (string= author (user-full-name)))
138 (concat \" (by \" author \")\"))))</lisp></h1>
139 <!-- Page published by Emacs Muse begins here -->\n"
140 "Header used for publishing HTML files. This may be text or a filename."
144 (defcustom muse-html-footer
"
145 <!-- Page published by Emacs Muse ends here -->
148 "Footer used for publishing HTML files. This may be text or a filename."
152 (defcustom muse-xhtml-header
153 "<?xml version=\"1.0\" encoding=\"<lisp>
154 (muse-html-encoding)</lisp>\"?>
155 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
156 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
157 <html xmlns=\"http://www.w3.org/1999/xhtml\">
160 (concat (muse-publishing-directive \"title\")
161 (let ((author (muse-publishing-directive \"author\")))
162 (if (not (string= author (user-full-name)))
163 (concat \" (by \" author \")\"))))</lisp></title>
164 <meta name=\"generator\" content=\"muse.el\" />
165 <meta http-equiv=\"<lisp>muse-html-meta-http-equiv</lisp>\"
166 content=\"<lisp>muse-html-meta-content-type</lisp>\" />
168 (let ((maintainer (muse-style-element :maintainer)))
170 (concat \"<link rev=\\\"made\\\" href=\\\"\" maintainer \"\\\" />\")))
172 <lisp>muse-xhtml-style-sheet</lisp>
176 (concat (muse-publishing-directive \"title\")
177 (let ((author (muse-publishing-directive \"author\")))
178 (if (not (string= author (user-full-name)))
179 (concat \" (by \" author \")\"))))</lisp></h1>
180 <!-- Page published by Emacs Muse begins here -->\n"
181 "Header used for publishing XHTML files. This may be text or a filename."
185 (defcustom muse-xhtml-footer
"
186 <!-- Page published by Emacs Muse ends here -->
189 "Footer used for publishing XHTML files. This may be text or a filename."
193 (defcustom muse-html-anchor-on-word nil
194 "When true, anchors surround the closest word. This allows you
195 to select them in a browser (i.e. for pasting), but has the
196 side-effect of marking up headers in multiple colors if your
197 header style is different from your link style."
201 (defcustom muse-html-table-attributes
202 "class=\"muse-table\" border=\"2\" cellpadding=\"5\""
203 "The attribute to be used with HTML <table> tags.
204 Note that since Muse supports direct insertion of HTML tags, you
205 can easily create any kind of table you want, as long as each
206 line begins at column 0 (to prevent it from being blockquoted).
207 To make such a table, use this idiom:
211 [... contents of my table, in raw HTML ...]
214 It may look strange to have the tags out of sequence, but this is
215 because the Muse verbatim tag is handled during a different pass
216 than the HTML table tag."
220 (defcustom muse-html-markup-regexps
221 `(;; Join together the parts of a table
222 (10000 ,(concat " </t\\(body\\|head\\|foot\\)>\\s-*</table>"
223 "\\([" muse-regexp-blank
"]*\n\\)\\{0,2\\}"
224 "[" muse-regexp-blank
"]*"
225 "<table[^>]*>\\s-*<t\\1>\n") 0 "")
226 (10100 ,(concat "</table>"
227 "\\([" muse-regexp-blank
"]*\n\\)\\{0,2\\}"
228 "[" muse-regexp-blank
"]*"
232 ;; Join together the parts of a list
233 (10200 ,(concat "</\\([oud]l\\)>"
234 "\\([" muse-regexp-blank
"]*\n\\)\\{0,2\\}"
235 "[" muse-regexp-blank
"]*"
239 ;; Beginning of doc, end of doc, or plain paragraph separator
240 (10300 ,(concat "\\(\n</\\(blockquote\\|center\\)>\\)?"
243 "]*\n\\)+\\)\\|\\`\\s-*\\|\\s-*\\'\\)"
244 "\\(<\\(blockquote\\|center\\)>\n\\)?")
245 0 muse-html-markup-paragraph
))
246 "List of markup rules for publishing a Muse page to HTML.
247 For more on the structure of this list, see `muse-publish-markup-regexps'."
248 :type
'(repeat (choice
249 (list :tag
"Markup rule"
251 (choice regexp symbol
)
253 (choice string function symbol
))
257 (defcustom muse-html-markup-functions
258 '((anchor . muse-html-markup-anchor
)
259 (table . muse-html-markup-table
)
260 (footnote . muse-html-markup-footnote
))
261 "An alist of style types to custom functions for that kind of text.
262 For more on the structure of this list, see
263 `muse-publish-markup-functions'."
264 :type
'(alist :key-type symbol
:value-type function
)
267 (defcustom muse-html-markup-strings
268 '((image-with-desc .
"<img src=\"%s\" alt=\"%s\">")
269 (image-link .
"<img src=\"%s\" alt=\"\">")
270 (url-with-image .
"<a class=\"image-link\" href=\"%s\"><img src=\"%s\"></a>")
271 (url-link .
"<a href=\"%s\">%s</a>")
272 (internal-link .
"<a href=\"#%s\">%s</a>")
273 (email-addr .
"<a href=\"mailto:%s\">%s</a>")
274 (emdash .
" — ")
275 (comment-begin .
"<!-- ")
276 (comment-end .
" -->")
282 (section-end .
"</h2>")
283 (subsection .
"<h3>")
284 (subsection-end .
"</h3>")
285 (subsubsection .
"<h4>")
286 (subsubsection-end .
"</h4>")
287 (section-other .
"<h5>")
288 (section-other-end .
"</h5>")
289 (begin-underline .
"<u>")
290 (end-underline .
"</u>")
291 (begin-literal .
"<code>")
292 (end-literal .
"</code>")
293 (begin-emph .
"<em>")
295 (begin-more-emph .
"<strong>")
296 (end-more-emph .
"</strong>")
297 (begin-most-emph .
"<strong><em>")
298 (end-most-emph .
"</em></strong>")
299 (begin-verse .
"<p class=\"verse\">\n")
300 (verse-space .
" ")
301 (end-verse-line .
"<br>")
302 (end-last-stanza-line .
"<br>")
303 (empty-verse-line .
"<br>")
305 (begin-example .
"<pre class=\"example\">")
306 (end-example .
"</pre>")
307 (begin-center .
"<center>\n")
308 (end-center .
"\n</center>")
309 (begin-quote .
"<blockquote>\n")
310 (end-quote .
"\n</blockquote>")
311 (begin-uli .
"<ul>\n<li>")
312 (end-uli .
"</li>\n</ul>")
313 (begin-oli .
"<ol>\n<li>")
314 (end-oli .
"</li>\n</ol>")
315 (begin-ddt .
"<dl>\n<dt><strong>")
316 (start-dde .
"</strong></dt>\n<dd>")
317 (end-ddt .
"</dd>\n</dl>"))
318 "Strings used for marking up text as HTML.
319 These cover the most basic kinds of markup, the handling of which
320 differs little between the various styles."
321 :type
'(alist :key-type symbol
:value-type string
)
324 (defcustom muse-xhtml-markup-strings
325 '((image-with-desc .
"<img src=\"%s\" alt=\"%s\" />")
326 (image-link .
"<img src=\"%s\" alt=\"\" />")
327 (url-with-image .
"<a class=\"image-link\" href=\"%s\"><img src=\"%s\" alt=\"\" /></a>")
329 (fn-sep .
"<hr />\n")
330 (begin-underline .
"<span style=\"text-decoration: underline;\">")
331 (end-underline .
"</span>")
332 (begin-center .
"<span style=\"text-align: center;\">\n")
333 (end-center .
"\n</span>")
334 (end-verse-line .
"<br />")
335 (end-last-stanza-line .
"<br />")
336 (empty-verse-line .
"<br />"))
337 "Strings used for marking up text as XHTML.
338 These cover the most basic kinds of markup, the handling of which
339 differs little between the various styles.
341 If a markup rule is not found here, `muse-html-markup-strings' is
343 :type
'(alist :key-type symbol
:value-type string
)
346 (defcustom muse-html-markup-tags
347 '(("class" t t muse-html-class-tag
))
348 "A list of tag specifications, for specially marking up HTML."
349 :type
'(repeat (list (string :tag
"Markup tag")
350 (boolean :tag
"Expect closing tag" :value t
)
351 (boolean :tag
"Parse attributes" :value nil
)
355 (defcustom muse-html-markup-specials
360 "A table of characters which must be represented specially."
361 :type
'(alist :key-type character
:value-type string
)
364 (defcustom muse-html-meta-http-equiv
"Content-Type"
365 "The http-equiv attribute used for the HTML <meta> tag."
369 (defcustom muse-html-meta-content-type
"text/html"
370 "The content type used for the HTML <meta> tag.
371 If you are striving for XHTML 1.1 compliance, you may want to
372 change this to \"application/xhtml+xml\"."
376 (defcustom muse-html-meta-content-encoding
(if (featurep 'mule
)
379 "The charset to append to the HTML <meta> tag.
380 If set to the symbol 'detect, use `muse-html-encoding-map' to try
381 and determine the HTML charset from emacs's coding. If set to a
382 string, this string will be used to force a particular charset"
383 :type
'(choice string symbol
)
386 (defcustom muse-html-charset-default
"iso-8859-1"
387 "The default HTML meta charset to use if no translation is found in
388 `muse-html-encoding-map'."
392 (defcustom muse-html-encoding-default
'iso-8859-1
393 "The default Emacs buffer encoding to use in published files.
394 This will be used if no special characters are found."
398 (defcustom muse-html-encoding-map
399 '((iso-8859-1 .
"iso-8859-1")
400 (iso-2022-jp .
"iso-2022-jp")
402 (japanese-iso-8bit .
"euc-jp")
403 (chinese-big5 .
"big5")
404 (mule-utf-8 .
"utf-8")
405 (chinese-iso-8bit .
"gb2312")
406 (chinese-gbk .
"gbk"))
407 "An alist mapping emacs coding systems to appropriate HTML charsets.
408 Use the base name of the coding system (i.e. without the -unix)."
409 :type
'(alist :key-type coding-system
:value-type string
)
412 (defun muse-html-transform-content-type (content-type)
413 "Using `muse-html-encoding-map', try and resolve an emacs coding
414 system to an associated HTML coding system. If no match is found,
415 `muse-html-charset-default' is used instead."
416 (let ((match (and (fboundp 'coding-system-base
)
417 (assoc (coding-system-base content-type
)
418 muse-html-encoding-map
))))
421 muse-html-charset-default
)))
423 (defun muse-html-insert-anchor (anchor)
424 "Insert an anchor, either around the word at point, or within a tag."
425 (skip-chars-forward muse-regexp-space
)
426 (if (looking-at "<\\([^ />]+\\)>")
427 (let ((tag (match-string 1)))
428 (goto-char (match-end 0))
429 (insert "<a name=\"" anchor
"\" id=\"" anchor
"\">")
430 (when muse-html-anchor-on-word
431 (or (and (search-forward (format "</%s>" tag
)
432 (muse-line-end-position) t
)
433 (goto-char (match-beginning 0)))
436 (insert "<a name=\"" anchor
"\" id=\"" anchor
"\">")
437 (when muse-html-anchor-on-word
441 (defun muse-html-markup-anchor ()
443 (muse-html-insert-anchor (match-string 2)))
446 (defun muse-html-markup-paragraph ()
447 (let ((end (copy-marker (match-end 0) t
)))
448 (goto-char (match-beginning 0))
449 (when (save-excursion
451 (and (re-search-backward "<\\(/?\\)p[ >]" nil t
)
452 (not (string-equal (match-string 1) "/")))))
459 ((eq (char-after) ?\
<)
461 ((looking-at "<\\(em\\|strong\\|code\\|span\\)[ >]")
464 (if (looking-at "<a[^>]+><img")
465 (insert "<p class=\"image-link\">")
467 ((looking-at "<img[ >]")
468 (insert "<p class=\"image-link\">"))))
469 ((muse-looking-back "\\(</h[1-4]>\\|<hr>\\)\n\n")
470 (insert "<p class=\"first\">"))
471 ((muse-looking-back "<\\(blockquote\\|center\\)>\n")
472 (insert "<p class=\"quoted\">"))
476 (defun muse-html-escape-string (str &rest ignored
)
477 "Convert to character entities any non-alphanumeric characters
478 outside a few punctuation symbols, that risk being misinterpreted
481 (let (pos code len ch
)
483 (while (setq pos
(string-match (concat "[^-"
485 "/:._=@\\?~#\"<>&;]")
487 (setq ch
(aref str pos
)
490 (cond ((fboundp 'char-to-ucs
)
492 ((fboundp 'char-to-int
)
497 str
(concat (substring str
0 pos
)
499 (when (< pos
(length str
))
500 (substring str
(1+ pos
) nil
)))
504 (defun muse-html-markup-footnote ()
505 (if (/= (muse-line-beginning-position) (match-beginning 0))
506 "<sup><a name=\"fnr.\\1\" href=\"#fn.\\1\">\\1</a></sup>"
508 "<p class=\"footnote\"><a name=\"fn.\\1\" href=\"#fnr.\\1\">\\1.</a>"
511 (let* ((beg (goto-char (match-end 0)))
512 (end (and (search-forward "\n\n" nil t
)
514 (copy-marker (match-beginning 0))
516 (while (re-search-forward (concat "^["
520 (replace-match "\\1" t
))))))))
522 (defun muse-html-markup-table ()
525 (delete-region (match-beginning 0) (match-end 0))))
526 (fields (split-string str
"\\s-*|+\\s-*"))
527 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str
)
528 (length (match-string 1 str
))))
529 (part (cond ((= type
1) "tbody")
531 ((= type
3) "tfoot")))
532 (col (cond ((= type
1) "td")
535 (insert "<table " muse-html-table-attributes
">\n"
538 (dolist (field fields
)
539 (insert " <" col
">" field
"</" col
">\n"))
544 ;; Handling of tags for HTML
546 (defun muse-html-insert-contents (depth)
547 (let ((max-depth (or depth
2))
551 (goto-char (point-min))
552 (search-forward "Page published by Emacs Muse begins here" nil t
)
554 (while (re-search-forward "^<h\\([0-9]+\\)>\\(.+?\\)</h\\1>" nil t
)
555 (unless (get-text-property (point) 'read-only
)
556 (setq l
(1- (string-to-number (match-string 1))))
561 (when (<= l max-depth
)
562 (setq contents
(cons (cons l
(muse-match-string-no-properties 2))
564 (goto-char (match-beginning 2))
565 (muse-html-insert-anchor (concat "sec" (int-to-string index
)))
566 (setq index
(1+ index
)))))))
567 (setq index
1 contents
(reverse contents
))
568 (let ((depth 1) (sub-open 0) (p (point)))
569 (insert "<dl class=\"contents\">\n")
571 (insert "<dt class=\"contents\">\n")
572 (insert "<a href=\"#sec" (int-to-string index
) "\">"
573 (muse-publish-strip-tags (cdar contents
))
575 (setq index
(1+ index
))
577 (setq depth
(caar contents
)
578 contents
(cdr contents
))
581 ((< (caar contents
) depth
)
582 (let ((idx (caar contents
)))
584 (insert "</dl>\n</dd>\n")
585 (setq sub-open
(1- sub-open
)
587 ((> (caar contents
) depth
) ; can't jump more than one ahead
588 (insert "<dd>\n<dl class=\"contents\">\n")
589 (setq sub-open
(1+ sub-open
))))))
590 (while (> sub-open
0)
591 (insert "</dl>\n</dd>\n")
592 (setq sub-open
(1- sub-open
)))
594 (muse-publish-mark-read-only p
(point)))))
596 (defun muse-html-class-tag (beg end attrs
)
598 (insert "<span class=\"" (cdr (assoc "name" attrs
)) "\">")
602 ;; Register the Muse HTML Publisher
604 (defun muse-html-browse-file (file)
605 (browse-url (concat "file:" file
)))
607 (defun muse-html-encoding ()
608 (if (stringp muse-html-meta-content-encoding
)
609 muse-html-meta-content-encoding
610 (muse-html-transform-content-type
611 (or (and (boundp 'buffer-file-coding-system
)
612 buffer-file-coding-system
)
613 muse-html-encoding-default
))))
615 (defun muse-html-prepare-buffer ()
616 (set (make-local-variable 'muse-publish-url-transforms
)
617 (cons 'muse-html-escape-string muse-publish-url-transforms
))
618 (make-local-variable 'muse-html-meta-http-equiv
)
619 (set (make-local-variable 'muse-html-meta-content-type
)
621 (string-match "charset=" muse-html-meta-content-type
))
622 muse-html-meta-content-type
623 (concat muse-html-meta-content-type
"; charset="
624 (muse-html-encoding)))))
626 (defun muse-html-fixup-tables ()
628 (goto-char (point-min))
630 (while (re-search-forward "^<table[^>]*>$" nil t
)
631 (unless (get-text-property (point) 'read-only
)
635 (narrow-to-region beg
(and (re-search-forward "^</table>"
637 (match-beginning 0))))
638 (goto-char (point-min))
639 (let ((inhibit-read-only t
))
643 (if (re-search-forward
644 "^\\s-*<t\\(head\\|body\\|foot\\)>$" nil t
)
645 (goto-char (match-beginning 0))
646 (goto-char (point-max)))))
649 (if (re-search-forward
650 "^\\s-*</t\\(head\\|body\\|foot\\)>$" nil t
)
651 (goto-char (match-end 0))
652 (goto-char (point-max)))))
655 (looking-at "\\s-*<t\\(head\\|body\\|foot\\)>")
656 (cond ((string= (match-string 1) "head") 1)
657 ((string= (match-string 1) "foot") 2)
660 (defun muse-html-finalize-buffer ()
661 (when muse-publish-generate-contents
662 (goto-char (car muse-publish-generate-contents
))
663 (muse-html-insert-contents (cdr muse-publish-generate-contents
)))
664 (when (and (boundp 'buffer-file-coding-system
)
665 (memq buffer-file-coding-system
'(no-conversion undecided-unix
)))
666 ;; make it agree with the default charset
667 (setq buffer-file-coding-system muse-html-encoding-default
)))
669 (unless (assoc "html" muse-publishing-styles
)
670 (muse-define-style "html"
671 :suffix
'muse-html-extension
672 :regexps
'muse-html-markup-regexps
673 :functions
'muse-html-markup-functions
674 :strings
'muse-html-markup-strings
675 :tags
'muse-html-markup-tags
676 :specials
'muse-html-markup-specials
677 :before
'muse-html-prepare-buffer
678 :before-end
'muse-html-fixup-tables
679 :after
'muse-html-finalize-buffer
680 :header
'muse-html-header
681 :footer
'muse-html-footer
682 :browser
'muse-html-browse-file
)
684 (muse-derive-style "xhtml" "html"
685 :suffix
'muse-xhtml-extension
686 :strings
'muse-xhtml-markup-strings
687 :header
'muse-xhtml-header
688 :footer
'muse-xhtml-footer
))
692 ;;; muse-html.el ends here