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-html-style-sheet
53 "<style type=\"text/css\">
55 background: white; color: black;
56 margin-left: 3%; margin-right: 7%;
60 p.verse { margin-left: 3% }
62 .example { margin-left: 3% }
68 h3 { margin-bottom: 0px; }
70 "Store your stylesheet definitions here.
71 This is used in `muse-html-header'.
72 You can put raw CSS in here or a <link> tag to an external stylesheet.
73 This text may contain <lisp> markup tags.
75 An example of using <link> is as follows.
77 <link rel=\"stylesheet\" type=\"text/css\" charset=\"utf-8\" media=\"all\" href=\"/default.css\">
79 If you are using XHTML, make sure to close the tag properly, as
80 shown in the following example.
82 <link rel=\"stylesheet\" type=\"text/css\" charset=\"utf-8\" media=\"all\" href=\"/default.css\" />"
86 (defcustom muse-html-header
87 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">
91 (concat (muse-publishing-directive \"title\")
92 (let ((author (muse-publishing-directive \"author\")))
93 (if (not (string= author (user-full-name)))
94 (concat \" (by \" author \")\"))))</lisp></title>
95 <meta name=\"generator\" content=\"muse.el\">
96 <meta http-equiv=\"<lisp>muse-html-meta-http-equiv</lisp>\"
97 content=\"<lisp>muse-html-meta-content-type</lisp>\">
99 (let ((maintainer (muse-style-element :maintainer)))
101 (concat \"<link rev=\\\"made\\\" href=\\\"\" maintainer \"\\\">\")))
103 <lisp>muse-html-style-sheet</lisp>
107 (concat (muse-publishing-directive \"title\")
108 (let ((author (muse-publishing-directive \"author\")))
109 (if (not (string= author (user-full-name)))
110 (concat \" (by \" author \")\"))))</lisp></h1>
111 <!-- Page published by Emacs Muse begins here -->\n"
112 "Header used for publishing HTML files."
113 :type
'(choice string file
)
116 (defcustom muse-html-footer
"
117 <!-- Page published by Emacs Muse ends here -->
120 "Footer used for publishing HTML files."
121 :type
'(choice string file
)
124 (defcustom muse-xhtml-header
125 "<?xml version=\"1.0\" encoding=\"<lisp>
126 (muse-html-encoding)</lisp>\"?>
127 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
128 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
129 <html xmlns=\"http://www.w3.org/1999/xhtml\">
132 (concat (muse-publishing-directive \"title\")
133 (let ((author (muse-publishing-directive \"author\")))
134 (if (not (string= author (user-full-name)))
135 (concat \" (by \" author \")\"))))</lisp></title>
136 <meta name=\"generator\" content=\"muse.el\" />
137 <meta http-equiv=\"<lisp>muse-html-meta-http-equiv</lisp>\"
138 content=\"<lisp>muse-html-meta-content-type</lisp>\" />
140 (let ((maintainer (muse-style-element :maintainer)))
142 (concat \"<link rev=\\\"made\\\" href=\\\"\" maintainer \"\\\" />\")))
144 <lisp>muse-html-style-sheet</lisp>
148 (concat (muse-publishing-directive \"title\")
149 (let ((author (muse-publishing-directive \"author\")))
150 (if (not (string= author (user-full-name)))
151 (concat \" (by \" author \")\"))))</lisp></h1>
152 <!-- Page published by Emacs Muse begins here -->\n"
153 "Header used for publishing XHTML files."
154 :type
'(choice string file
)
157 (defcustom muse-xhtml-footer
"
158 <!-- Page published by Emacs Muse ends here -->
161 "Footer used for publishing XHTML files."
162 :type
'(choice string file
)
165 (defcustom muse-html-anchor-on-word nil
166 "When true, anchors surround the closest word. This allows you
167 to select them in a browser (i.e. for pasting), but has the
168 side-effect of marking up headers in multiple colors if your
169 header style is different from your link style."
173 (defcustom muse-html-table-attributes
174 "class=\"muse-table\" border=\"2\" cellpadding=\"5\""
175 "The attribute to be used with HTML <table> tags.
176 Note that since Muse supports direct insertion of HTML tags, you
177 can easily create any kind of table you want, as long as each
178 line begins at column 0 (to prevent it from being blockquoted).
179 To make such a table, use this idiom:
183 [... contents of my table, in raw HTML ...]
186 It may look strange to have the tags out of sequence, but this is
187 because the Muse verbatim tag is handled during a different pass
188 than the HTML table tag."
192 (defcustom muse-html-markup-regexps
193 `(;; join together the parts of a list or table
194 (10000 "</\\([oud]l\\)>\\s-*<\\1>\\s-*" 0 "")
195 (10100 ,(concat " </t\\(body\\|head\\|foot\\)>\\s-*</table>\\s-*"
196 "<table[^>]*>\\s-*<t\\1>\n") 0 "")
197 (10200 "</table>\\s-*<table[^>]*>\n" 0 "")
199 ;; beginning of doc, end of doc, or plain paragraph separator
200 (10300 ,(concat "\\(\n</\\(blockquote\\|center\\)>\\)?"
203 "]*\n\\)+\\|\\`\\s-*\\|\\s-*\\'\\)"
204 "\\(<\\(blockquote\\|center\\)>\n\\)?")
205 0 muse-html-markup-paragraph
))
206 "List of markup rules for publishing a Muse page to HTML.
207 For more on the structure of this list, see `muse-publish-markup-regexps'."
208 :type
'(repeat (choice
209 (list :tag
"Markup rule"
211 (choice regexp symbol
)
213 (choice string function symbol
))
217 (defcustom muse-html-markup-functions
218 '((anchor . muse-html-markup-anchor
)
219 (table . muse-html-markup-table
)
220 (footnote . muse-html-markup-footnote
))
221 "An alist of style types to custom functions for that kind of text.
222 For more on the structure of this list, see
223 `muse-publish-markup-functions'."
224 :type
'(alist :key-type symbol
:value-type function
)
227 (defcustom muse-html-markup-strings
228 '((image-with-desc .
"<img src=\"%s\" alt=\"%s\">")
229 (image-link .
"<img src=\"%s\">")
230 (url-with-image .
"<a class=\"image-link\" href=\"%s\"><img src=\"%s\"></a>")
231 (url-link .
"<a href=\"%s\">%s</a>")
232 (email-addr .
"<a href=\"mailto:%s\">%s</a>")
233 (emdash .
" — ")
239 (section-end .
"</h2>")
240 (subsection .
"<h3>")
241 (subsection-end .
"</h3>")
242 (subsubsection .
"<h4>")
243 (subsubsection-end .
"</h4>")
244 (begin-underline .
"<u>")
245 (end-underline .
"</u>")
246 (begin-literal .
"<code>")
247 (end-literal .
"</code>")
248 (begin-emph .
"<em>")
250 (begin-more-emph .
"<strong>")
251 (end-more-emph .
"</strong>")
252 (begin-most-emph .
"<strong><em>")
253 (end-most-emph .
"</em></strong>")
254 (begin-verse .
"<p class=\"verse\">\n")
255 (verse-space .
" ")
256 (end-verse-line .
"<br>")
257 (last-stanza-end .
"<br>")
258 (empty-verse-line .
"<br>")
260 (begin-example .
"<pre class=\"example\">")
261 (end-example .
"</pre>")
262 (begin-center .
"<center>\n")
263 (end-center .
"\n</center>")
264 (begin-quote .
"<blockquote>\n")
265 (end-quote .
"\n</blockquote>")
266 (begin-uli .
"<ul>\n<li>")
267 (end-uli .
"</li>\n</ul>")
268 (begin-oli .
"<ol>\n<li>")
269 (end-oli .
"</li>\n</ol>")
270 (begin-ddt .
"<dl>\n<dt><strong>")
271 (start-dde .
"</strong></dt>\n<dd>")
272 (end-ddt .
"</dd>\n</dl>"))
273 "Strings used for marking up text as HTML.
274 These cover the most basic kinds of markup, the handling of which
275 differs little between the various styles."
276 :type
'(alist :key-type symbol
:value-type string
)
279 (defcustom muse-xhtml-markup-strings
280 '((image-with-desc .
"<img src=\"%s\" alt=\"%s\" />")
281 (image-link .
"<img src=\"%s\" alt=\"\" />")
282 (url-with-image .
"<a class=\"image-link\" href=\"%s\"><img src=\"%s\" alt=\"\" /></a>")
284 (fn-sep .
"<hr />\n")
285 (begin-underline .
"<span style=\"text-decoration: underline;\">\n")
286 (end-underline .
"</span>")
287 (begin-center .
"<span style=\"text-align: center;\">\n")
288 (end-verse-line .
"<br />")
289 (last-stanza-end .
"<br />")
290 (empty-verse-line .
"<br />")
291 (end-center .
"\n</span>"))
292 "Strings used for marking up text as XHTML.
293 These cover the most basic kinds of markup, the handling of which
294 differs little between the various styles.
296 If a markup rule is not found here, `muse-html-markup-strings' is
298 :type
'(alist :key-type symbol
:value-type string
)
301 (defcustom muse-html-markup-tags
302 '(("class" t t muse-html-class-tag
))
303 "A list of tag specifications, for specially marking up HTML."
304 :type
'(repeat (list (string :tag
"Markup tag")
305 (boolean :tag
"Expect closing tag" :value t
)
306 (boolean :tag
"Parse attributes" :value nil
)
310 (defcustom muse-html-markup-specials
315 "A table of characters which must be represented specially."
316 :type
'(alist :key-type character
:value-type string
)
319 (defcustom muse-html-meta-http-equiv
"Content-Type"
320 "The http-equiv attribute used for the HTML <meta> tag."
324 (defcustom muse-html-meta-content-type
"text/html"
325 "The content type used for the HTML <meta> tag.
326 If you are striving for XHTML 1.1 compliance, you may want to
327 change this to \"application/xhtml+xml\"."
331 (defcustom muse-html-meta-content-encoding
(if (featurep 'mule
)
334 "The charset to append to the HTML <meta> tag.
335 If set to the symbol 'detect, use `muse-html-encoding-map' to try
336 and determine the HTML charset from emacs's coding. If set to a
337 string, this string will be used to force a particular charset"
338 :type
'(choice string symbol
)
341 (defcustom muse-html-charset-default
"iso-8859-1"
342 "The default HTML meta charset to use if no translation is found in
343 `muse-html-encoding-map'."
347 (defcustom muse-html-encoding-default
'iso-8859-1
348 "The default Emacs buffer encoding to use in published files.
349 This will be used if no special characters are found."
353 (defcustom muse-html-encoding-map
354 '((iso-8859-1 .
"iso-8859-1")
355 (iso-2022-jp .
"iso-2022-jp")
357 (japanese-iso-8bit .
"euc-jp")
358 (chinese-big5 .
"big5")
359 (mule-utf-8 .
"utf-8")
360 (chinese-iso-8bit .
"gb2312")
361 (chinese-gbk .
"gbk"))
362 "An alist mapping emacs coding systems to appropriate HTML charsets.
363 Use the base name of the coding system (i.e. without the -unix)."
364 :type
'(alist :key-type coding-system
:value-type string
)
367 (defun muse-html-transform-content-type (content-type)
368 "Using `muse-html-encoding-map', try and resolve an emacs coding
369 system to an associated HTML coding system. If no match is found,
370 `muse-html-charset-default' is used instead."
371 (let ((match (assoc (coding-system-base content-type
)
372 muse-html-encoding-map
)))
375 muse-html-charset-default
)))
377 (defun muse-html-insert-anchor (anchor)
378 "Insert an anchor, either around the word at point, or within a tag."
379 (skip-chars-forward muse-regexp-space
)
380 (if (looking-at "<\\([^ />]+\\)>")
381 (let ((tag (match-string 1)))
382 (goto-char (match-end 0))
383 (insert "<a name=\"" anchor
"\" id=\"" anchor
"\">")
384 (when muse-html-anchor-on-word
385 (or (and (search-forward (format "</%s>" tag
)
386 (muse-line-end-position) t
)
387 (goto-char (match-beginning 0)))
390 (insert "<a name=\"" anchor
"\" id=\"" anchor
"\">")
391 (when muse-html-anchor-on-word
395 (defun muse-html-markup-paragraph ()
396 (let ((end (copy-marker (match-end 0) t
)))
397 (goto-char (match-beginning 0))
401 (and (re-search-backward (concat
403 "div class=\"image-link\"\\)[ >]")
405 (not (string-equal (match-string 1) "/"))
406 (if (string-equal (match-string 2) "p")
415 ((eq (char-after) ?\
<)
417 ((looking-at "<\\(em\\|strong\\|code\\|span\\)[ >]")
420 (if (looking-at "<a[^>]+><img")
421 (insert "<div class=\"image-link\">")
423 ((looking-at "<img[ >]")
424 (insert "<div class=\"image-link\">"))))
425 ((muse-looking-back "\\(</h[1-4]>\\|<hr>\\)\n\n")
426 (insert "<p class=\"first\">"))
427 ((muse-looking-back "<\\(blockquote\\|center\\)>\n")
428 (insert "<p class=\"quoted\">"))
432 (defun muse-html-markup-anchor ()
434 (muse-html-insert-anchor (match-string 1))) "")
436 (defun muse-html-escape-string (str)
437 "Convert to character entities any non-alphanumeric characters
438 outside a few punctuation symbols, that risk being misinterpreted
443 (while (setq pos
(string-match (concat "[^-"
447 (setq code
(int-to-string
448 (cond ((fboundp 'char-to-ucs
)
449 (char-to-ucs (aref str pos
)))
450 ((fboundp 'char-to-int
)
451 (char-to-int (aref str pos
)))
454 str
(concat (substring str
0 pos
)
456 (when (< pos
(length str
))
457 (substring str
(1+ pos
) nil
)))
461 (defun muse-html-markup-footnote ()
462 (if (/= (muse-line-beginning-position) (match-beginning 0))
463 "<sup><a name=\"fnr.\\1\" href=\"#fn.\\1\">\\1</a></sup>"
465 "<p class=\"footnote\"><a name=\"fn.\\1\" href=\"#fnr.\\1\">\\1.</a>"
468 (let* ((beg (goto-char (match-end 0)))
469 (end (and (search-forward "\n\n" nil t
)
471 (copy-marker (match-beginning 0))
473 (while (re-search-forward (concat "^["
477 (replace-match "\\1" t
))))))))
479 (defun muse-html-markup-table ()
482 (delete-region (match-beginning 0) (match-end 0))))
483 (fields (split-string str
"\\s-*|+\\s-*"))
484 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str
)
485 (length (match-string 1 str
))))
486 (part (cond ((= type
1) "tbody")
488 ((= type
3) "tfoot")))
489 (col (cond ((= type
1) "td")
492 (insert "<table " muse-html-table-attributes
">\n"
495 (dolist (field fields
)
496 (insert " <" col
">" field
"</" col
">\n"))
501 ;; Handling of tags for HTML
503 (defun muse-html-insert-contents (depth)
504 (let ((max-depth (or depth
2))
508 (goto-char (point-min))
509 (search-forward "Page published by Emacs Muse begins here" nil t
)
511 (while (re-search-forward "^<h\\([0-9]+\\)>\\(.+?\\)</h\\1>" nil t
)
512 (unless (get-text-property (point) 'read-only
)
513 (setq l
(1- (string-to-number (match-string 1))))
518 (when (<= l max-depth
)
519 (setq contents
(cons (cons l
(muse-match-string-no-properties 2))
521 (goto-char (match-beginning 2))
522 (muse-html-insert-anchor (concat "sec" (int-to-string index
)))
523 (setq index
(1+ index
)))))))
524 (setq index
1 contents
(reverse contents
))
525 (let ((depth 1) (sub-open 0) (p (point)))
526 (insert "<dl class=\"contents\">\n")
528 (insert "<dt class=\"contents\">\n")
529 (insert "<a href=\"#sec" (int-to-string index
) "\">"
530 (muse-publish-strip-tags (cdar contents
))
532 (setq index
(1+ index
))
534 (setq depth
(caar contents
)
535 contents
(cdr contents
))
538 ((< (caar contents
) depth
)
539 (let ((idx (caar contents
)))
541 (insert "</dl>\n</dd>\n")
542 (setq sub-open
(1- sub-open
)
544 ((> (caar contents
) depth
) ; can't jump more than one ahead
545 (insert "<dd>\n<dl class=\"contents\">\n")
546 (setq sub-open
(1+ sub-open
))))))
547 (while (> sub-open
0)
548 (insert "</dl>\n</dd>\n")
549 (setq sub-open
(1- sub-open
)))
551 (muse-publish-mark-read-only p
(point)))))
553 (defun muse-html-class-tag (beg end attrs
)
555 (insert "<span class=\"" (cdr (assoc "name" attrs
)) "\">")
559 ;; Register the Muse HTML Publisher
561 (defun muse-html-browse-file (file)
562 (browse-url (concat "file:" file
)))
564 (defun muse-html-encoding ()
565 (if (stringp muse-html-meta-content-encoding
)
566 muse-html-meta-content-encoding
567 (muse-html-transform-content-type
568 (or buffer-file-coding-system
569 muse-html-encoding-default
))))
571 (defun muse-html-prepare-buffer ()
572 (set (make-local-variable 'muse-publish-url-transforms
)
573 (cons 'muse-html-escape-string muse-publish-url-transforms
))
574 (make-local-variable 'muse-html-meta-http-equiv
)
575 (set (make-local-variable 'muse-html-meta-content-type
)
576 (concat muse-html-meta-content-type
"; charset="
577 (muse-html-encoding))))
579 (defun muse-html-finalize-buffer ()
580 (when muse-publish-generate-contents
581 (goto-char (car muse-publish-generate-contents
))
582 (muse-html-insert-contents (cdr muse-publish-generate-contents
)))
583 (when (memq buffer-file-coding-system
'(no-conversion undecided-unix
))
584 ;; make it agree with the default charset
585 (setq buffer-file-coding-system muse-html-encoding-default
)))
587 (unless (assoc "html" muse-publishing-styles
)
588 (muse-define-style "html"
589 :suffix
'muse-html-extension
590 :regexps
'muse-html-markup-regexps
591 :functions
'muse-html-markup-functions
592 :strings
'muse-html-markup-strings
593 :tags
'muse-html-markup-tags
594 :specials
'muse-html-markup-specials
595 :before
'muse-html-prepare-buffer
596 :after
'muse-html-finalize-buffer
597 :header
'muse-html-header
598 :footer
'muse-html-footer
599 :browser
'muse-html-browse-file
)
601 (muse-derive-style "xhtml" "html"
602 :strings
'muse-xhtml-markup-strings
603 :header
'muse-xhtml-header
604 :footer
'muse-xhtml-footer
))
608 ;;; muse-html.el ends here