Fix #5477: muse-replace-regexp-in-string throws error in XEmacs 21.4
[muse-el.git] / lisp / muse-html.el
blob03be68cc7050eed847ab5a1604a4449ff7abed50
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.
22 ;;; Commentary:
24 ;;; Contributors:
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.
31 ;;; Code:
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."
45 :group 'muse-publish)
47 (defcustom muse-html-extension ".html"
48 "Default file extension for publishing HTML files."
49 :type 'string
50 :group 'muse-html)
52 (defcustom muse-xhtml-extension ".html"
53 "Default file extension for publishing XHTML files."
54 :type 'string
55 :group 'muse-html)
57 (defcustom muse-html-style-sheet
58 "<style type=\"text/css\">
59 body {
60 background: white; color: black;
61 margin-left: 3%; margin-right: 7%;
64 p { margin-top: 1% }
65 p.verse { margin-left: 3% }
67 .example { margin-left: 3% }
69 h2 {
70 margin-top: 25px;
71 margin-bottom: 0px;
73 h3 { margin-bottom: 0px; }
74 </style>"
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\">"
83 :type 'string
84 :group 'muse-html)
86 (defcustom muse-xhtml-style-sheet
87 "<style type=\"text/css\">
88 body {
89 background: white; color: black;
90 margin-left: 3%; margin-right: 7%;
93 p { margin-top: 1% }
94 p.verse { margin-left: 3% }
96 .example { margin-left: 3% }
98 h2 {
99 margin-top: 25px;
100 margin-bottom: 0px;
102 h3 { margin-bottom: 0px; }
103 </style>"
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\" />"
112 :type 'string
113 :group 'muse-html)
115 (defcustom muse-html-header
116 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
117 <html>
118 <head>
119 <title><lisp>
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>\">
127 <lisp>
128 (let ((maintainer (muse-style-element :maintainer)))
129 (when maintainer
130 (concat \"<link rev=\\\"made\\\" href=\\\"\" maintainer \"\\\">\")))
131 </lisp>
132 <lisp>muse-html-style-sheet</lisp>
133 </head>
134 <body>
135 <h1><lisp>
136 (concat (muse-publishing-directive \"title\")
137 (let ((author (muse-publishing-directive \"author\")))
138 (if (not (string= author (user-full-name)))
139 (concat \" (by \" author \")\"))))</lisp></h1>
140 <!-- Page published by Emacs Muse begins here -->\n"
141 "Header used for publishing HTML files. This may be text or a filename."
142 :type 'string
143 :group 'muse-html)
145 (defcustom muse-html-footer "
146 <!-- Page published by Emacs Muse ends here -->
147 </body>
148 </html>\n"
149 "Footer used for publishing HTML files. This may be text or a filename."
150 :type 'string
151 :group 'muse-html)
153 (defcustom muse-xhtml-header
154 "<?xml version=\"1.0\" encoding=\"<lisp>
155 (muse-html-encoding)</lisp>\"?>
156 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
157 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
158 <html xmlns=\"http://www.w3.org/1999/xhtml\">
159 <head>
160 <title><lisp>
161 (concat (muse-publishing-directive \"title\")
162 (let ((author (muse-publishing-directive \"author\")))
163 (if (not (string= author (user-full-name)))
164 (concat \" (by \" author \")\"))))</lisp></title>
165 <meta name=\"generator\" content=\"muse.el\" />
166 <meta http-equiv=\"<lisp>muse-html-meta-http-equiv</lisp>\"
167 content=\"<lisp>muse-html-meta-content-type</lisp>\" />
168 <lisp>
169 (let ((maintainer (muse-style-element :maintainer)))
170 (when maintainer
171 (concat \"<link rev=\\\"made\\\" href=\\\"\" maintainer \"\\\" />\")))
172 </lisp>
173 <lisp>muse-xhtml-style-sheet</lisp>
174 </head>
175 <body>
176 <h1><lisp>
177 (concat (muse-publishing-directive \"title\")
178 (let ((author (muse-publishing-directive \"author\")))
179 (if (not (string= author (user-full-name)))
180 (concat \" (by \" author \")\"))))</lisp></h1>
181 <!-- Page published by Emacs Muse begins here -->\n"
182 "Header used for publishing XHTML files. This may be text or a filename."
183 :type 'string
184 :group 'muse-html)
186 (defcustom muse-xhtml-footer "
187 <!-- Page published by Emacs Muse ends here -->
188 </body>
189 </html>\n"
190 "Footer used for publishing XHTML files. This may be text or a filename."
191 :type 'string
192 :group 'muse-html)
194 (defcustom muse-html-anchor-on-word nil
195 "When true, anchors surround the closest word. This allows you
196 to select them in a browser (i.e. for pasting), but has the
197 side-effect of marking up headers in multiple colors if your
198 header style is different from your link style."
199 :type 'boolean
200 :group 'muse-html)
202 (defcustom muse-html-table-attributes
203 " class=\"muse-table\" border=\"2\" cellpadding=\"5\""
204 "The attribute to be used with HTML <table> tags.
205 Note that since Muse supports insertion of raw HTML tags, as long
206 as you wrap the region in <literal></literal>."
207 :type 'string
208 :group 'muse-html)
210 (defcustom muse-html-markup-regexps
211 `(;; Beginning of doc, end of doc, or plain paragraph separator
212 (10000 ,(concat "\\(\\(\n\\([" muse-regexp-blank "]*\n\\)+\\)"
213 "\\|\\`\\s-*\\|\\s-*\\'\\)")
214 0 muse-html-markup-paragraph))
215 "List of markup rules for publishing a Muse page to HTML.
216 For more on the structure of this list, see `muse-publish-markup-regexps'."
217 :type '(repeat (choice
218 (list :tag "Markup rule"
219 integer
220 (choice regexp symbol)
221 integer
222 (choice string function symbol))
223 function))
224 :group 'muse-html)
226 (defcustom muse-html-markup-functions
227 '((anchor . muse-html-markup-anchor)
228 (table . muse-html-markup-table)
229 (footnote . muse-html-markup-footnote))
230 "An alist of style types to custom functions for that kind of text.
231 For more on the structure of this list, see
232 `muse-publish-markup-functions'."
233 :type '(alist :key-type symbol :value-type function)
234 :group 'muse-html)
236 (defcustom muse-html-markup-strings
237 '((image-with-desc . "<img src=\"%s\" alt=\"%s\">")
238 (image-link . "<img src=\"%s\" alt=\"\">")
239 (url-with-image . "<a class=\"image-link\" href=\"%s\"><img src=\"%s\"></a>")
240 (anchor-ref . "<a href=\"#%s\">%s</a>")
241 (url . "<a href=\"%s\">%s</a>")
242 (link . "<a href=\"%s\">%s</a>")
243 (link-and-anchor . "<a href=\"%s#%s\">%s</a>")
244 (email-addr . "<a href=\"mailto:%s\">%s</a>")
245 (emdash . " &mdash; ")
246 (comment-begin . "<!-- ")
247 (comment-end . " -->")
248 (rule . "<hr>")
249 (fn-sep . "<hr>\n")
250 (no-break-space . "&nbsp;")
251 (enddots . "....")
252 (dots . "...")
253 (section . "<h2>")
254 (section-end . "</h2>")
255 (subsection . "<h3>")
256 (subsection-end . "</h3>")
257 (subsubsection . "<h4>")
258 (subsubsection-end . "</h4>")
259 (section-other . "<h5>")
260 (section-other-end . "</h5>")
261 (begin-underline . "<u>")
262 (end-underline . "</u>")
263 (begin-literal . "<code>")
264 (end-literal . "</code>")
265 (begin-emph . "<em>")
266 (end-emph . "</em>")
267 (begin-more-emph . "<strong>")
268 (end-more-emph . "</strong>")
269 (begin-most-emph . "<strong><em>")
270 (end-most-emph . "</em></strong>")
271 (begin-verse . "<p class=\"verse\">\n")
272 (verse-space . "&nbsp;&nbsp;")
273 (end-verse-line . "<br>")
274 (end-last-stanza-line . "<br>")
275 (empty-verse-line . "<br>")
276 (end-verse . "</p>")
277 (begin-example . "<pre class=\"example\">")
278 (end-example . "</pre>")
279 (begin-center . "<center>\n<p>")
280 (end-center . "</p>\n</center>")
281 (begin-quote . "<blockquote>\n")
282 (end-quote . "\n</blockquote>")
283 (begin-quote-item . "<p class=\"quoted\">")
284 (end-quote-item . "</p>")
285 (begin-uli . "<ul>\n")
286 (end-uli . "\n</ul>")
287 (begin-uli-item . "<li>")
288 (end-uli-item . "</li>")
289 (begin-oli . "<ol>\n")
290 (end-oli . "\n</ol>")
291 (begin-oli-item . "<li>")
292 (end-oli-item . "</li>")
293 (begin-dl . "<dl>\n")
294 (end-dl . "\n</dl>")
295 (begin-ddt . "<dt><strong>")
296 (end-ddt . "</strong></dt>")
297 (begin-dde . "<dd>")
298 (end-dde . "</dd>")
299 (begin-table . "<table%s>\n")
300 (end-table . "</table>")
301 (begin-table-row . " <tr>\n")
302 (end-table-row . " </tr>\n")
303 (begin-table-entry . " <%s>")
304 (end-table-entry . "</%s>\n"))
305 "Strings used for marking up text as HTML.
306 These cover the most basic kinds of markup, the handling of which
307 differs little between the various styles."
308 :type '(alist :key-type symbol :value-type string)
309 :group 'muse-html)
311 (defcustom muse-xhtml-markup-strings
312 '((image-with-desc . "<img src=\"%s\" alt=\"%s\" />")
313 (image-link . "<img src=\"%s\" alt=\"\" />")
314 (url-with-image . "<a class=\"image-link\" href=\"%s\"><img src=\"%s\" alt=\"\" /></a>")
315 (rule . "<hr />")
316 (fn-sep . "<hr />\n")
317 (begin-underline . "<span style=\"text-decoration: underline;\">")
318 (end-underline . "</span>")
319 (begin-center . "<p style=\"text-align: center;\">\n")
320 (end-center . "\n</p>")
321 (end-verse-line . "<br />")
322 (end-last-stanza-line . "<br />")
323 (empty-verse-line . "<br />"))
324 "Strings used for marking up text as XHTML.
325 These cover the most basic kinds of markup, the handling of which
326 differs little between the various styles.
328 If a markup rule is not found here, `muse-html-markup-strings' is
329 searched."
330 :type '(alist :key-type symbol :value-type string)
331 :group 'muse-html)
333 (defcustom muse-html-markup-tags
334 '(("class" t t muse-html-class-tag))
335 "A list of tag specifications, for specially marking up HTML."
336 :type '(repeat (list (string :tag "Markup tag")
337 (boolean :tag "Expect closing tag" :value t)
338 (boolean :tag "Parse attributes" :value nil)
339 function))
340 :group 'muse-html)
342 (defcustom muse-html-meta-http-equiv "Content-Type"
343 "The http-equiv attribute used for the HTML <meta> tag."
344 :type 'string
345 :group 'muse-html)
347 (defcustom muse-html-meta-content-type "text/html"
348 "The content type used for the HTML <meta> tag.
349 If you are striving for XHTML 1.1 compliance, you may want to
350 change this to \"application/xhtml+xml\"."
351 :type 'string
352 :group 'muse-html)
354 (defcustom muse-html-meta-content-encoding (if (featurep 'mule)
355 'detect
356 "iso-8859-1")
357 "The charset to append to the HTML <meta> tag.
358 If set to the symbol 'detect, use `muse-html-encoding-map' to try
359 and determine the HTML charset from emacs's coding. If set to a
360 string, this string will be used to force a particular charset"
361 :type '(choice string symbol)
362 :group 'muse-html)
364 (defcustom muse-html-encoding-default 'iso-8859-1
365 "The default Emacs buffer encoding to use in published files.
366 This will be used if no special characters are found."
367 :type 'symbol
368 :group 'muse-html)
370 (defcustom muse-html-charset-default "iso-8859-1"
371 "The default HTML meta charset to use if no translation is found in
372 `muse-html-encoding-map'."
373 :type 'string
374 :group 'muse-html)
376 (defun muse-html-insert-anchor (anchor)
377 "Insert an anchor, either around the word at point, or within a tag."
378 (skip-chars-forward (concat muse-regexp-blank "\n"))
379 (if (looking-at (concat "<\\([^" muse-regexp-blank "/>\n]+\\)>"))
380 (let ((tag (match-string 1)))
381 (goto-char (match-end 0))
382 (muse-insert-markup "<a name=\"" anchor "\" id=\"" anchor "\">")
383 (when muse-html-anchor-on-word
384 (or (and (search-forward (format "</%s>" tag)
385 (muse-line-end-position) t)
386 (goto-char (match-beginning 0)))
387 (forward-word 1)))
388 (muse-insert-markup "</a>"))
389 (muse-insert-markup "<a name=\"" anchor "\" id=\"" anchor "\">")
390 (when muse-html-anchor-on-word
391 (forward-word 1))
392 (muse-insert-markup "</a>\n")))
394 (defun muse-html-markup-anchor ()
395 (unless (get-text-property (match-end 1) 'noemphasis)
396 (save-match-data
397 (muse-html-insert-anchor (match-string 2)))
398 (match-string 1)))
400 (defun muse-html-markup-paragraph ()
401 (let ((end (copy-marker (match-end 0) t)))
402 (goto-char (match-beginning 0))
403 (when (save-excursion
404 (save-match-data
405 (and (re-search-backward "<\\(/?\\)p[ >]" nil t)
406 (not (string-equal (match-string 1) "/")))))
407 (when (get-text-property (1- (point)) 'end-list)
408 (goto-char (previous-single-property-change (1- (point)) 'end-list)))
409 (muse-insert-markup "</p>"))
410 (goto-char end))
411 (cond
412 ((eobp)
413 (unless (bolp)
414 (insert "\n")))
415 ((eq (char-after) ?\<)
416 (cond
417 ((looking-at "<\\(em\\|strong\\|code\\|span\\)[ >]")
418 (muse-insert-markup "<p>"))
419 ((looking-at "<a ")
420 (if (looking-at "<a[^>\n]+><img")
421 (muse-insert-markup "<p class=\"image-link\">")
422 (muse-insert-markup "<p>")))
423 ((looking-at "<img[ >]")
424 (muse-insert-markup "<p class=\"image-link\">"))
426 (forward-char 1)
427 nil)))
428 ((muse-looking-back "\\(</h[1-4]>\\|<hr>\\)\n\n")
429 (muse-insert-markup "<p class=\"first\">"))
431 (muse-insert-markup "<p>"))))
433 (defun muse-html-markup-footnote ()
434 (cond
435 ((get-text-property (match-beginning 0) 'noemphasis)
436 nil)
437 ((= (muse-line-beginning-position) (match-beginning 0))
438 (prog1
439 (let ((text (match-string 1)))
440 (muse-insert-markup
441 (concat "<p class=\"footnote\">"
442 "<a name=\"fn." text "\" href=\"#fnr." text "\">"
443 text ".</a>")))
444 (save-excursion
445 (save-match-data
446 (let* ((beg (goto-char (match-end 0)))
447 (end (and (search-forward "\n\n" nil t)
448 (prog1
449 (copy-marker (match-beginning 0))
450 (goto-char beg)))))
451 (while (re-search-forward (concat "^["
452 muse-regexp-blank
453 "]+\\([^\n]\\)")
454 end t)
455 (replace-match "\\1" t)))))
456 (replace-match "")))
457 (t (let ((text (match-string 1)))
458 (muse-insert-markup
459 (concat "<sup><a name=\"fnr." text "\" href=\"#fn." text "\">"
460 text "</a></sup>")))
461 (replace-match ""))))
463 (defun muse-html-markup-table ()
464 (muse-xml-markup-table muse-html-table-attributes))
466 ;; Handling of tags for HTML
468 (defun muse-html-insert-contents (depth)
469 (let ((max-depth (or depth 2))
470 (index 1)
471 base contents l)
472 (save-excursion
473 (goto-char (point-min))
474 (search-forward "Page published by Emacs Muse begins here" nil t)
475 (catch 'done
476 (while (re-search-forward "^<h\\([0-9]+\\)>\\(.+?\\)</h\\1>" nil t)
477 (unless (get-text-property (point) 'read-only)
478 (setq l (1- (string-to-number (match-string 1))))
479 (if (null base)
480 (setq base l)
481 (if (< l base)
482 (throw 'done t)))
483 (when (<= l max-depth)
484 (setq contents (cons (cons l (muse-match-string-no-properties 2))
485 contents))
486 (goto-char (match-beginning 2))
487 (muse-html-insert-anchor (concat "sec" (int-to-string index)))
488 (setq index (1+ index)))))))
489 (setq index 1 contents (reverse contents))
490 (let ((depth 1) (sub-open 0) (p (point)))
491 (muse-insert-markup "<dl class=\"contents\">\n")
492 (while contents
493 (muse-insert-markup "<dt class=\"contents\">\n"
494 "<a href=\"#sec" (int-to-string index) "\">"
495 (muse-publish-strip-tags (cdar contents))
496 "</a>\n"
497 "</dt>\n")
498 (setq index (1+ index)
499 depth (caar contents)
500 contents (cdr contents))
501 (when contents
502 (cond
503 ((< (caar contents) depth)
504 (let ((idx (caar contents)))
505 (while (< idx depth)
506 (muse-insert-markup "</dl>\n</dd>\n")
507 (setq sub-open (1- sub-open)
508 idx (1+ idx)))))
509 ((> (caar contents) depth) ; can't jump more than one ahead
510 (muse-insert-markup "<dd>\n<dl class=\"contents\">\n")
511 (setq sub-open (1+ sub-open))))))
512 (while (> sub-open 0)
513 (muse-insert-markup "</dl>\n</dd>\n")
514 (setq sub-open (1- sub-open)))
515 (muse-insert-markup "</dl>\n")
516 (muse-publish-mark-read-only p (point)))))
518 (defun muse-html-class-tag (beg end attrs)
519 (goto-char beg)
520 (muse-insert-markup "<span class=\"" (cdr (assoc "name" attrs)) "\">")
521 (goto-char end)
522 (muse-insert-markup "</span>"))
524 ;; Register the Muse HTML Publisher
526 (defun muse-html-browse-file (file)
527 (browse-url (concat "file:" file)))
529 (defun muse-html-encoding ()
530 (if (stringp muse-html-meta-content-encoding)
531 muse-html-meta-content-encoding
532 (muse-xml-transform-content-type
533 (or (and (boundp 'buffer-file-coding-system)
534 buffer-file-coding-system)
535 muse-html-encoding-default)
536 muse-html-charset-default)))
538 (defun muse-html-prepare-buffer ()
539 (make-local-variable 'muse-html-meta-http-equiv)
540 (set (make-local-variable 'muse-html-meta-content-type)
541 (if (save-match-data
542 (string-match "charset=" muse-html-meta-content-type))
543 muse-html-meta-content-type
544 (concat muse-html-meta-content-type "; charset="
545 (muse-html-encoding)))))
547 (defun muse-html-finalize-buffer ()
548 (when muse-publish-generate-contents
549 (goto-char (car muse-publish-generate-contents))
550 (muse-html-insert-contents (cdr muse-publish-generate-contents)))
551 (when (and (boundp 'buffer-file-coding-system)
552 (memq buffer-file-coding-system '(no-conversion undecided-unix)))
553 ;; make it agree with the default charset
554 (setq buffer-file-coding-system muse-html-encoding-default)))
556 (unless (assoc "html" muse-publishing-styles)
557 (muse-define-style "html"
558 :suffix 'muse-html-extension
559 :regexps 'muse-html-markup-regexps
560 :functions 'muse-html-markup-functions
561 :strings 'muse-html-markup-strings
562 :tags 'muse-html-markup-tags
563 :specials 'muse-xml-decide-specials
564 :before 'muse-html-prepare-buffer
565 :after 'muse-html-finalize-buffer
566 :header 'muse-html-header
567 :footer 'muse-html-footer
568 :browser 'muse-html-browse-file)
570 (muse-derive-style "xhtml" "html"
571 :suffix 'muse-xhtml-extension
572 :strings 'muse-xhtml-markup-strings
573 :header 'muse-xhtml-header
574 :footer 'muse-xhtml-footer))
576 (provide 'muse-html)
578 ;;; muse-html.el ends here