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.
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
)
41 (require 'muse-xml-common
)
43 (defgroup muse-html nil
44 "Options controlling the behavior of Muse HTML publishing."
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\">"
86 (defcustom muse-xhtml-style-sheet
87 "<style type=\"text/css\">
89 background: white; color: black;
90 margin-left: 3%; margin-right: 7%;
94 p.verse { margin-left: 3% }
96 .example { margin-left: 3% }
102 h3 { margin-bottom: 0px; }
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\" />"
115 (defcustom muse-html-header
116 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
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>\">
128 (let ((maintainer (muse-style-element :maintainer)))
130 (concat \"<link rev=\\\"made\\\" href=\\\"\" maintainer \"\\\">\")))
132 (muse-style-element :style-sheet muse-publishing-current-style)
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."
146 (defcustom muse-html-footer
"
147 <!-- Page published by Emacs Muse ends here -->
150 "Footer used for publishing HTML files. This may be text or a filename."
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\">
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>\" />
170 (let ((maintainer (muse-style-element :maintainer)))
172 (concat \"<link rev=\\\"made\\\" href=\\\"\" maintainer \"\\\" />\")))
174 (muse-style-element :style-sheet muse-publishing-current-style)
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."
188 (defcustom muse-xhtml-footer
"
189 <!-- Page published by Emacs Muse ends here -->
192 "Footer used for publishing XHTML files. This may be text or a filename."
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."
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>."
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 "\\|\\`\\s-*\\|\\s-*\\'\\)")
216 0 muse-html-markup-paragraph
))
217 "List of markup rules for publishing a Muse page to HTML.
218 For more on the structure of this list, see `muse-publish-markup-regexps'."
219 :type
'(repeat (choice
220 (list :tag
"Markup rule"
222 (choice regexp symbol
)
224 (choice string function symbol
))
228 (defcustom muse-html-markup-functions
229 '((anchor . muse-html-markup-anchor
)
230 (table . muse-html-markup-table
)
231 (footnote . muse-html-markup-footnote
))
232 "An alist of style types to custom functions for that kind of text.
233 For more on the structure of this list, see
234 `muse-publish-markup-functions'."
235 :type
'(alist :key-type symbol
:value-type function
)
238 (defcustom muse-html-markup-strings
239 '((image-with-desc .
"<img src=\"%s\" alt=\"%s\">")
240 (image-link .
"<img src=\"%s\" alt=\"\">")
241 (url-with-image .
"<a class=\"image-link\" href=\"%s\"><img src=\"%s\"></a>")
242 (anchor-ref .
"<a href=\"#%s\">%s</a>")
243 (url .
"<a href=\"%s\">%s</a>")
244 (link .
"<a href=\"%s\">%s</a>")
245 (link-and-anchor .
"<a href=\"%s#%s\">%s</a>")
246 (email-addr .
"<a href=\"mailto:%s\">%s</a>")
247 (emdash .
"%s—%s")
248 (comment-begin .
"<!-- ")
249 (comment-end .
" -->")
252 (no-break-space .
" ")
256 (section-end .
"</h2>")
257 (subsection .
"<h3>")
258 (subsection-end .
"</h3>")
259 (subsubsection .
"<h4>")
260 (subsubsection-end .
"</h4>")
261 (section-other .
"<h5>")
262 (section-other-end .
"</h5>")
263 (begin-underline .
"<u>")
264 (end-underline .
"</u>")
265 (begin-literal .
"<code>")
266 (end-literal .
"</code>")
267 (begin-emph .
"<em>")
269 (begin-more-emph .
"<strong>")
270 (end-more-emph .
"</strong>")
271 (begin-most-emph .
"<strong><em>")
272 (end-most-emph .
"</em></strong>")
273 (begin-verse .
"<p class=\"verse\">\n")
274 (verse-space .
" ")
275 (end-verse-line .
"<br>")
276 (end-last-stanza-line .
"<br>")
277 (empty-verse-line .
"<br>")
279 (begin-example .
"<pre class=\"example\">")
280 (end-example .
"</pre>")
281 (begin-center .
"<center>\n<p>")
282 (end-center .
"</p>\n</center>")
283 (begin-quote .
"<blockquote>\n")
284 (end-quote .
"\n</blockquote>")
285 (begin-quote-item .
"<p class=\"quoted\">")
286 (end-quote-item .
"</p>")
287 (begin-uli .
"<ul>\n")
288 (end-uli .
"\n</ul>")
289 (begin-uli-item .
"<li>")
290 (end-uli-item .
"</li>")
291 (begin-oli .
"<ol>\n")
292 (end-oli .
"\n</ol>")
293 (begin-oli-item .
"<li>")
294 (end-oli-item .
"</li>")
295 (begin-dl .
"<dl>\n")
297 (begin-ddt .
"<dt><strong>")
298 (end-ddt .
"</strong></dt>")
301 (begin-table .
"<table%s>\n")
302 (end-table .
"</table>")
303 (begin-table-row .
" <tr>\n")
304 (end-table-row .
" </tr>\n")
305 (begin-table-entry .
" <%s>")
306 (end-table-entry .
"</%s>\n"))
307 "Strings used for marking up text as HTML.
308 These cover the most basic kinds of markup, the handling of which
309 differs little between the various styles."
310 :type
'(alist :key-type symbol
:value-type string
)
313 (defcustom muse-xhtml-markup-strings
314 '((image-with-desc .
"<img src=\"%s\" alt=\"%s\" />")
315 (image-link .
"<img src=\"%s\" alt=\"\" />")
316 (url-with-image .
"<a class=\"image-link\" href=\"%s\"><img src=\"%s\" alt=\"\" /></a>")
318 (fn-sep .
"<hr />\n")
319 (begin-underline .
"<span style=\"text-decoration: underline;\">")
320 (end-underline .
"</span>")
321 (begin-center .
"<p style=\"text-align: center;\">\n")
322 (end-center .
"\n</p>")
323 (end-verse-line .
"<br />")
324 (end-last-stanza-line .
"<br />")
325 (empty-verse-line .
"<br />"))
326 "Strings used for marking up text as XHTML.
327 These cover the most basic kinds of markup, the handling of which
328 differs little between the various styles.
330 If a markup rule is not found here, `muse-html-markup-strings' is
332 :type
'(alist :key-type symbol
:value-type string
)
335 (defcustom muse-html-markup-tags
336 '(("class" t t muse-html-class-tag
))
337 "A list of tag specifications, for specially marking up HTML."
338 :type
'(repeat (list (string :tag
"Markup tag")
339 (boolean :tag
"Expect closing tag" :value t
)
340 (boolean :tag
"Parse attributes" :value nil
)
344 (defcustom muse-html-meta-http-equiv
"Content-Type"
345 "The http-equiv attribute used for the HTML <meta> tag."
349 (defcustom muse-html-meta-content-type
"text/html"
350 "The content type used for the HTML <meta> tag.
351 If you are striving for XHTML 1.1 compliance, you may want to
352 change this to \"application/xhtml+xml\"."
356 (defcustom muse-html-meta-content-encoding
(if (featurep 'mule
)
359 "The charset to append to the HTML <meta> tag.
360 If set to the symbol 'detect, use `muse-html-encoding-map' to try
361 and determine the HTML charset from emacs's coding. If set to a
362 string, this string will be used to force a particular charset"
363 :type
'(choice string symbol
)
366 (defcustom muse-html-encoding-default
'iso-8859-1
367 "The default Emacs buffer encoding to use in published files.
368 This will be used if no special characters are found."
372 (defcustom muse-html-charset-default
"iso-8859-1"
373 "The default HTML meta charset to use if no translation is found in
374 `muse-html-encoding-map'."
378 (defun muse-html-insert-anchor (anchor)
379 "Insert an anchor, either around the word at point, or within a tag."
380 (skip-chars-forward (concat muse-regexp-blank
"\n"))
381 (if (looking-at (concat "<\\([^" muse-regexp-blank
"/>\n]+\\)>"))
382 (let ((tag (match-string 1)))
383 (goto-char (match-end 0))
384 (muse-insert-markup "<a name=\"" anchor
"\" id=\"" anchor
"\">")
385 (when muse-html-anchor-on-word
386 (or (and (search-forward (format "</%s>" tag
)
387 (muse-line-end-position) t
)
388 (goto-char (match-beginning 0)))
390 (muse-insert-markup "</a>"))
391 (muse-insert-markup "<a name=\"" anchor
"\" id=\"" anchor
"\">")
392 (when muse-html-anchor-on-word
394 (muse-insert-markup "</a>\n")))
396 (defun muse-html-markup-anchor ()
397 (unless (get-text-property (match-end 1) 'muse-link
)
399 (muse-html-insert-anchor (match-string 2)))
402 (defun muse-html-markup-paragraph ()
403 (let ((end (copy-marker (match-end 0) t
)))
404 (goto-char (match-beginning 0))
405 (when (save-excursion
407 (and (re-search-backward "<\\(/?\\)p[ >]" nil t
)
408 (not (string-equal (match-string 1) "/")))))
409 (when (get-text-property (1- (point)) 'end-list
)
410 (goto-char (previous-single-property-change (1- (point)) 'end-list
)))
411 (muse-insert-markup "</p>"))
417 ((eq (char-after) ?\
<)
419 ((looking-at "<\\(em\\|strong\\|code\\|span\\)[ >]")
420 (muse-insert-markup "<p>"))
422 (if (looking-at "<a[^>\n]+><img")
423 (muse-insert-markup "<p class=\"image-link\">")
424 (muse-insert-markup "<p>")))
425 ((looking-at "<img[ >]")
426 (muse-insert-markup "<p class=\"image-link\">"))
430 ((muse-looking-back "\\(</h[1-4]>\\|<hr>\\)\n\n")
431 (muse-insert-markup "<p class=\"first\">"))
433 (muse-insert-markup "<p>"))))
435 (defun muse-html-markup-footnote ()
437 ((get-text-property (match-beginning 0) 'muse-link
)
439 ((= (muse-line-beginning-position) (match-beginning 0))
441 (let ((text (match-string 1)))
443 (concat "<p class=\"footnote\">"
444 "<a name=\"fn." text
"\" href=\"#fnr." text
"\">"
448 (let* ((beg (goto-char (match-end 0)))
449 (end (and (search-forward "\n\n" nil t
)
451 (copy-marker (match-beginning 0))
453 (while (re-search-forward (concat "^["
457 (replace-match "\\1" t
)))))
459 (t (let ((text (match-string 1)))
461 (concat "<sup><a name=\"fnr." text
"\" href=\"#fn." text
"\">"
463 (replace-match ""))))
465 (defun muse-html-markup-table ()
466 (muse-xml-markup-table muse-html-table-attributes
))
468 ;; Handling of tags for HTML
470 (defun muse-html-insert-contents (depth)
471 (let ((max-depth (or depth
2))
475 (goto-char (point-min))
476 (search-forward "Page published by Emacs Muse begins here" nil t
)
478 (while (re-search-forward "^<h\\([0-9]+\\)>\\(.+?\\)</h\\1>" nil t
)
479 (unless (get-text-property (point) 'read-only
)
480 (setq l
(1- (string-to-number (match-string 1))))
485 (when (<= l max-depth
)
486 (setq contents
(cons (cons l
(muse-match-string-no-properties 2))
488 (goto-char (match-beginning 2))
489 (muse-html-insert-anchor (concat "sec" (int-to-string index
)))
490 (setq index
(1+ index
)))))))
491 (setq index
1 contents
(reverse contents
))
492 (let ((depth 1) (sub-open 0) (p (point)))
493 (muse-insert-markup "<dl class=\"contents\">\n")
495 (muse-insert-markup "<dt class=\"contents\">\n"
496 "<a href=\"#sec" (int-to-string index
) "\">"
497 (muse-publish-strip-tags (cdar contents
))
500 (setq index
(1+ index
)
501 depth
(caar contents
)
502 contents
(cdr contents
))
505 ((< (caar contents
) depth
)
506 (let ((idx (caar contents
)))
508 (muse-insert-markup "</dl>\n</dd>\n")
509 (setq sub-open
(1- sub-open
)
511 ((> (caar contents
) depth
) ; can't jump more than one ahead
512 (muse-insert-markup "<dd>\n<dl class=\"contents\">\n")
513 (setq sub-open
(1+ sub-open
))))))
514 (while (> sub-open
0)
515 (muse-insert-markup "</dl>\n</dd>\n")
516 (setq sub-open
(1- sub-open
)))
517 (muse-insert-markup "</dl>\n")
518 (muse-publish-mark-read-only p
(point)))))
520 (defun muse-html-class-tag (beg end attrs
)
522 (muse-insert-markup "<span class=\"" (cdr (assoc "name" attrs
)) "\">")
524 (muse-insert-markup "</span>"))
526 ;; Register the Muse HTML Publisher
528 (defun muse-html-browse-file (file)
529 (browse-url (concat "file:" file
)))
531 (defun muse-html-encoding ()
532 (if (stringp muse-html-meta-content-encoding
)
533 muse-html-meta-content-encoding
534 (muse-xml-transform-content-type
535 (or (and (boundp 'buffer-file-coding-system
)
536 buffer-file-coding-system
)
537 muse-html-encoding-default
)
538 muse-html-charset-default
)))
540 (defun muse-html-prepare-buffer ()
541 (make-local-variable 'muse-html-meta-http-equiv
)
542 (set (make-local-variable 'muse-html-meta-content-type
)
544 (string-match "charset=" muse-html-meta-content-type
))
545 muse-html-meta-content-type
546 (concat muse-html-meta-content-type
"; charset="
547 (muse-html-encoding)))))
549 (defun muse-html-finalize-buffer ()
550 (when muse-publish-generate-contents
551 (goto-char (car muse-publish-generate-contents
))
552 (muse-html-insert-contents (cdr muse-publish-generate-contents
)))
553 (when (and (boundp 'buffer-file-coding-system
)
554 (memq buffer-file-coding-system
'(no-conversion undecided-unix
)))
555 ;; make it agree with the default charset
556 (setq buffer-file-coding-system muse-html-encoding-default
)))
558 (unless (assoc "html" muse-publishing-styles
)
559 (muse-define-style "html"
560 :suffix
'muse-html-extension
561 :regexps
'muse-html-markup-regexps
562 :functions
'muse-html-markup-functions
563 :strings
'muse-html-markup-strings
564 :tags
'muse-html-markup-tags
565 :specials
'muse-xml-decide-specials
566 :before
'muse-html-prepare-buffer
567 :after
'muse-html-finalize-buffer
568 :header
'muse-html-header
569 :footer
'muse-html-footer
570 :style-sheet
'muse-html-style-sheet
571 :browser
'muse-html-browse-file
)
573 (muse-derive-style "xhtml" "html"
574 :suffix
'muse-xhtml-extension
575 :strings
'muse-xhtml-markup-strings
576 :header
'muse-xhtml-header
577 :footer
'muse-xhtml-footer
578 :style-sheet
'muse-xhtml-style-sheet
))
582 ;;; muse-html.el ends here