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 "\\([" 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
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"
226 (choice regexp symbol
)
228 (choice string function symbol
))
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
)
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—%s")
252 (comment-begin .
"<!-- ")
253 (comment-end .
" -->")
256 (no-break-space .
" ")
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>")
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 .
" ")
279 (end-verse-line .
"<br>")
280 (end-last-stanza-line .
"<br>")
281 (empty-verse-line .
"<br>")
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")
301 (begin-ddt .
"<dt><strong>")
302 (end-ddt .
"</strong></dt>")
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
)
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>")
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
336 :type
'(alist :key-type symbol
:value-type string
)
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
)
348 (defcustom muse-html-meta-http-equiv
"Content-Type"
349 "The http-equiv attribute used for the HTML <meta> tag."
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\"."
360 (defcustom muse-html-meta-content-encoding
(if (featurep 'mule
)
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
)
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."
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'."
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)))
394 (muse-insert-markup "</a>"))
395 (muse-insert-markup "<a name=\"" anchor
"\" id=\"" anchor
"\">")
396 (when muse-html-anchor-on-word
398 (muse-insert-markup "</a>\n")))
400 (defun muse-html-markup-anchor ()
401 (unless (get-text-property (match-end 1) 'muse-link
)
403 (muse-html-insert-anchor (match-string 2)))
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
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>"))
421 ((eq (char-after) ?\
<)
423 ((looking-at "<\\(em\\|strong\\|code\\|span\\)[ >]")
424 (muse-insert-markup "<p>"))
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\">"))
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 ()
441 ((get-text-property (match-beginning 0) 'muse-link
)
443 ((= (muse-line-beginning-position) (match-beginning 0))
445 (let ((text (match-string 1)))
447 (concat "<p class=\"footnote\">"
448 "<a name=\"fn." text
"\" href=\"#fnr." text
"\">"
452 (let* ((beg (goto-char (match-end 0)))
453 (end (and (search-forward "\n\n" nil t
)
455 (copy-marker (match-beginning 0))
457 (while (re-search-forward (concat "^["
461 (replace-match "\\1" t
)))))
463 (t (let ((text (match-string 1)))
465 (concat "<sup><a name=\"fnr." text
"\" href=\"#fn." text
"\">"
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))
479 (goto-char (point-min))
480 (search-forward "Page published by Emacs Muse begins here" nil t
)
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))))
489 (when (<= l max-depth
)
490 (setq contents
(cons (cons l
(muse-match-string-no-properties 2))
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")
499 (muse-insert-markup "<dt>\n"
500 "<a href=\"#sec" (int-to-string index
) "\">"
501 (muse-publish-strip-tags (cdar contents
))
504 (setq index
(1+ index
)
505 depth
(caar contents
)
506 contents
(cdr contents
))
509 ((< (caar contents
) depth
)
510 (let ((idx (caar contents
)))
512 (muse-insert-markup "</dl>\n</dd>\n")
513 (setq sub-open
(1- sub-open
)
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
)
526 (muse-insert-markup "<span class=\"" (cdr (assoc "name" attrs
)) "\">")
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
)
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
))
586 ;;; muse-html.el ends here