Include regexps from base styles. Fix muse-project-ignore-regexp and use it more.
[muse-el.git] / lisp / muse-publish.el
blobd0c1e2608b13d90c594a5db4081fedc0ec067424
1 ;;; muse-publish.el --- Base publishing implementation.
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
10 ;; version.
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
15 ;; for more details.
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.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;; Yann Hodique (yann DOT hodique AT gmail DOT com) fixed an
27 ;; unnecessary URL description transform in `muse-publish-url'.
29 ;; Peter K. Lee (saint AT corenova DOT com) provided the
30 ;; `muse-style-elements-list' function.
32 ;;; Code:
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 ;; Muse Publishing
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
40 (require 'muse)
41 (require 'muse-regexps)
43 (defgroup muse-publish nil
44 "Options controlling the general behavior of Muse publishing.
45 See `muse-publish' for more information."
46 :group 'muse)
48 (defcustom muse-before-publish-hook nil
49 "A hook run in the buffer to be published, before it is done."
50 :type 'hook
51 :group 'muse-publish)
53 (defcustom muse-after-publish-hook nil
54 "A hook run in the buffer to be published, after it is done."
55 :type 'hook
56 :group 'muse-publish)
58 (defcustom muse-publish-url-transforms '(muse-publish-escape-specials-in-string
59 muse-publish-prepare-url)
60 "A list of functions used to prepare URLs for publication.
61 Each is passed the URL and expects a URL to be returned."
62 :type 'hook
63 :options '(muse-publish-escape-specials-in-string
64 muse-publish-prepare-url)
65 :group 'muse-publish)
67 (defcustom muse-publish-report-threshhold 100000
68 "If a file is this size or larger, report publishing progress."
69 :type 'integer
70 :group 'muse-publish)
72 (defcustom muse-publish-markup-regexps
73 `(;; Remove leading and trailing whitespace from the file
74 (1000 "\\(\\`\n+\\|\n+\\'\\)" 0 "")
76 ;; Remove trailing whitespace from all lines
77 (1100 ,(concat "[" muse-regexp-blank "]+$") 0 "")
79 ;; Handle any leading #directives
80 (1200 "\\`#\\([a-zA-Z]+\\)\\s-+\\(.+\\)\n+" 0 directive)
82 ;; markup tags
83 (1300 muse-tag-regexp 0 tag)
85 ;; commented lines
86 (1350 "^;\\s-+\\(.+\\)" 0 comment)
88 ;; define anchor points
89 (1400 "^#\\(\\S-+\\)\\s-*" 0 anchor)
91 ;; prevent emphasis characters in explicit links from being marked
92 (1500 muse-explicit-link-regexp 0 muse-publish-mark-noemphasis)
94 ;; emphasized or literal text
95 (1600 ,(concat
96 "\\(^\\|[-["
97 muse-regexp-space
98 "<('`\"]\\)\\(=[^="
99 muse-regexp-space
100 "]\\|_[^_"
101 muse-regexp-space
102 "]\\|\\*+[^*"
103 muse-regexp-space
104 "]\\)")
105 2 word)
107 ;; headings, outline-mode style
108 (1700 "^\\(\\*+\\)\\s-+" 0 heading)
110 ;; ellipses
111 (1800 "\\.\\.\\.\\." 0 enddots)
112 (1850 "\\.\\.\\." 0 dots)
114 ;; horizontal rule, or section separator
115 (1900 "^----+" 0 rule)
117 ;; beginning of footnotes section
118 (2000 "^Footnotes:?\\s-*" 0 fn-sep)
119 ;; footnote definition/reference (def if at beginning of line)
120 (2100 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote)
122 ;; unnumbered List items begin with a -. numbered list items
123 ;; begin with number and a period. definition lists have a
124 ;; leading term separated from the body with ::. centered
125 ;; paragraphs begin with at least six columns of whitespace; any
126 ;; other whitespace at the beginning indicates a blockquote. The
127 ;; reason all of these rules are handled here, is so that
128 ;; blockquote detection doesn't interfere with indented list
129 ;; members.
130 (2200 ,(concat "^["
131 muse-regexp-blank
132 "]+\\(-["
133 muse-regexp-blank
134 "]*\\|[0-9]+\\.["
135 muse-regexp-blank
136 "]*\\|\\(?:.+?\\)["
137 muse-regexp-blank
138 "]+::\n?\\)")
139 1 list)
141 (2300 ,(concat "^\\(\\(?:.+?\\)["
142 muse-regexp-blank
143 "]+::\n?\\)")
144 0 list)
146 (2400 ,(concat "^\\(["
147 muse-regexp-blank
148 "]+\\)")
149 0 quote)
151 (2500 ,(concat "\\(^\\|["
152 muse-regexp-blank
153 "]+\\)--\\($\\|["
154 muse-regexp-blank
155 "]+\\)")
156 0 emdash)
158 ;; "verse" text is indicated the same way as a quoted e-mail
159 ;; response: "> text", where text may contain initial whitespace
160 ;; (see below).
161 (2600 ,(concat "^["
162 muse-regexp-blank
163 "]*> ")
164 0 verse)
166 ;; simple table markup is supported, nothing fancy. use | to
167 ;; separate cells, || to separate header cells, and ||| for footer
168 ;; cells
169 (2700 ,(concat "^["
170 muse-regexp-blank
171 "]*\\(.+?\\(["
172 muse-regexp-blank
173 "]+|+["
174 muse-regexp-blank
175 "]+.+?\\)\\)$")
176 0 table)
178 ;; replace links in the buffer (links to other pages)
179 (2900 muse-explicit-link-regexp 0 link)
181 ;; bare URLs
182 (3000 muse-url-regexp 0 url)
184 ;; bare email addresses
185 (3500
186 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email)
188 "List of markup rules for publishing a page with Muse.
189 The rules given in this variable are invoked first, followed by
190 whatever rules are specified by the current style.
192 Each member of the list is either a function, or a list of the form:
194 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
196 REGEXP is a regular expression, or symbol whose value is a regular
197 expression, which is searched for using `re-search-forward'.
198 TEXT-BEGIN-GROUP is the matching group within that regexp which
199 denotes the beginning of the actual text to be marked up.
200 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
201 If it is not a string, but a function, it will be called to determine
202 what the replacement text should be (it must return a string). If it
203 is a symbol, the value of that symbol should be a string.
205 The replacements are done in order, one rule at a time. Writing
206 the regular expressions can be a tricky business. Note that case
207 is never ignored. `case-fold-search' is always bound to nil
208 while processing the markup rules."
209 :type '(repeat (choice
210 (list :tag "Markup rule"
211 integer
212 (choice regexp symbol)
213 integer
214 (choice string function symbol))
215 function))
216 :group 'muse-publish)
218 (defcustom muse-publish-markup-functions
219 '((directive . muse-publish-markup-directive)
220 (comment . muse-publish-markup-comment)
221 (anchor . muse-publish-markup-anchor)
222 (tag . muse-publish-markup-tag)
223 (word . muse-publish-markup-word)
224 (emdash . muse-publish-markup-emdash)
225 (enddots . muse-publish-markup-enddots)
226 (dots . muse-publish-markup-dots)
227 (rule . muse-publish-markup-rule)
228 (heading . muse-publish-markup-heading)
229 (footnote . muse-publish-markup-footnote)
230 (fn-sep . muse-publish-markup-fn-sep)
231 (list . muse-publish-markup-list)
232 (quote . muse-publish-markup-quote)
233 (verse . muse-publish-markup-verse)
234 (table . muse-publish-markup-table)
235 (email . muse-publish-markup-email)
236 (link . muse-publish-markup-link)
237 (url . muse-publish-markup-url))
238 "An alist of style types to custom functions for that kind of text.
240 Each member of the list is of the form:
242 (SYMBOL FUNCTION)
244 SYMBOL describes the type of text to associate with this rule.
245 `muse-publish-markup-regexps' maps regexps to these symbols.
247 FUNCTION is the function to use to mark up this kind of rule if
248 no suitable function is found through the :functions tag of the
249 current style."
250 :type '(alist :key-type symbol :value-type function)
251 :group 'muse-publish)
253 (defcustom muse-publish-markup-tags
254 '(("contents" nil t muse-publish-contents-tag)
255 ("verse" t nil muse-publish-verse-tag)
256 ("example" t nil muse-publish-example-tag)
257 ("literal" t nil muse-publish-mark-read-only)
258 ("verbatim" t nil muse-publish-verbatim-tag)
259 ("lisp" t nil muse-publish-lisp-tag)
260 ("class" t t muse-publish-class-tag)
261 ("command" t t muse-publish-command-tag)
262 ("comment" t nil muse-publish-comment-tag))
263 "A list of tag specifications, for specially marking up text.
264 XML-style tags are the best way to add custom markup to Muse.
265 This is easily accomplished by customizing this list of markup tags.
267 For each entry, the name of the tag is given, whether it expects
268 a closing tag and/or an optional set of attributes, and a
269 function that performs whatever action is desired within the
270 delimited region.
272 The tags themselves are deleted during publishing, before the
273 function is called. The function is called with three arguments,
274 the beginning and end of the region surrounded by the tags. If
275 properties are allowed, they are passed as a third argument in
276 the form of an alist. The `end' argument to the function is
277 always a marker.
279 Point is always at the beginning of the region within the tags, when
280 the function is called. Wherever point is when the function finishes
281 is where tag markup will resume.
283 These tag rules are processed once at the beginning of markup, and
284 once at the end, to catch any tags which may have been inserted
285 in-between."
286 :type '(repeat (list (string :tag "Markup tag")
287 (boolean :tag "Expect closing tag" :value t)
288 (boolean :tag "Parse attributes" :value nil)
289 function))
290 :group 'muse-publish)
292 (defcustom muse-publish-markup-specials nil
293 "A table of characters which must be represented specially."
294 :type '(alist :key-type character :value-type string)
295 :group 'muse-publish)
297 (defvar muse-publishing-p nil
298 "Set to t while a page is being published.")
299 (defvar muse-batch-publishing-p nil
300 "Set to t while a page is being batch published.")
301 (defvar muse-publishing-styles nil)
302 (defvar muse-publishing-current-file nil)
303 (defvar muse-publishing-current-style nil)
304 (defvar muse-publishing-directives nil
305 "An alist of publishing directives from the top of a file.")
306 (defvar muse-publish-generate-contents nil
307 "Non-nil if a table of contents should be generated.
308 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
309 tell where the <contents> was seen, and to what depth the
310 contents were requested.")
312 ;; Functions for handling style information
314 (defsubst muse-style (&optional style)
315 "Resolve the given STYLE into a Muse style, if it is a string."
316 (if (null style)
317 muse-publishing-current-style
318 (if (stringp style)
319 (assoc style muse-publishing-styles)
320 (muse-assert (consp style))
321 style)))
323 (defun muse-define-style (name &rest elements)
324 (let ((entry (assoc name muse-publishing-styles)))
325 (if entry
326 (error "There is already a style named %s." name)
327 (setq muse-publishing-styles
328 (cons (append (list name) elements)
329 muse-publishing-styles)))))
331 (defun muse-derive-style (new-name base-name &rest elements)
332 (let ((entry (assoc new-name muse-publishing-styles)))
333 (if entry
334 (error "There is already a style named %s." new-name)
335 (apply 'muse-define-style new-name
336 (append elements (list :base base-name))))))
338 (defsubst muse-get-keyword (keyword list &optional direct)
339 (let ((value (cadr (memq keyword list))))
340 (if (and (not direct) (symbolp value))
341 (symbol-value value)
342 value)))
344 (defun muse-style-elements-list (elem &optional style)
345 "Return a list all references to ELEM in STYLE, including base styles.
346 If STYLE is not specified, use current style."
347 (let (base elements)
348 (while style
349 (setq style (muse-style style))
350 (setq elements (append elements
351 (muse-get-keyword elem style)))
352 (setq style (muse-get-keyword :base style)))
353 elements))
355 (defsubst muse-style-element (elem &optional style direct)
356 "Search for ELEM in STYLE, including base styles.
357 If STYLE is not specified, use current style."
358 (setq style (muse-style style))
359 (let ((value (muse-get-keyword elem style direct)))
360 (if value
361 value
362 (let ((base (muse-get-keyword :base style)))
363 (if base
364 (muse-style-element elem base direct))))))
366 (defun muse-find-markup-element (keyword ident style)
367 (let ((def (assq ident (muse-style-element keyword style))))
368 (if def
369 (cdr def)
370 (let ((base (muse-style-element :base style)))
371 (if base
372 (muse-find-markup-element keyword ident base))))))
374 (defsubst muse-markup-text (ident &rest args)
375 (let ((text (muse-find-markup-element :strings ident (muse-style))))
376 (if (and text args)
377 (apply 'format text args)
378 (or text ""))))
380 (defun muse-find-markup-tag (keyword tagname style)
381 (let ((def (assoc tagname (muse-style-element keyword style))))
382 (or def
383 (let ((base (muse-style-element :base style)))
384 (if base
385 (muse-find-markup-tag keyword tagname base))))))
387 (defsubst muse-markup-tag-info (tagname &rest args)
388 (let ((tag-info (muse-find-markup-tag :tags tagname (muse-style))))
389 (or tag-info
390 (assoc (match-string 1) muse-publish-markup-tags))))
392 (defsubst muse-markup-function (category)
393 (let ((func (muse-find-markup-element :functions category (muse-style))))
394 (or func
395 (cdr (assq category muse-publish-markup-functions)))))
397 ;; Publishing routines
399 (defun muse-publish-markup (name rules)
400 (let* ((case-fold-search nil)
401 (inhibit-read-only t)
402 (limit (* (length rules) (point-max)))
403 (verbose (and muse-publish-report-threshhold
404 (> (point-max) muse-publish-report-threshhold)))
405 (base 0))
406 (while rules
407 (goto-char (point-min))
408 (let ((regexp (nth 1 (car rules)))
409 (group (nth 2 (car rules)))
410 (repl (nth 3 (car rules)))
411 last-pos pos)
412 (if (symbolp regexp)
413 (setq regexp (symbol-value regexp)))
414 (if (and verbose (not muse-batch-publishing-p))
415 (message "Publishing %s...%d%%" name
416 (* (/ (float (+ (point) base)) limit) 100)))
417 (while (and regexp (setq pos (re-search-forward regexp nil t)))
418 (if (and verbose (not muse-batch-publishing-p))
419 (message "Publishing %s...%d%%" name
420 (* (/ (float (+ (point) base)) limit) 100)))
421 (unless (get-text-property (match-beginning group) 'read-only)
422 (let* (func
423 (text (cond
424 ((and (symbolp repl)
425 (setq func (muse-markup-function repl)))
426 (funcall func))
427 ((functionp repl)
428 (funcall repl))
429 ((symbolp repl)
430 (symbol-value repl))
431 (t repl))))
432 (if text
433 (replace-match text t))))
434 (if (and last-pos (= pos last-pos))
435 (if (eobp)
436 (setq regexp nil)
437 (forward-char 1)))
438 (setq last-pos pos)))
439 (setq rules (cdr rules)
440 base (+ base (point-max))))
441 (if (and verbose (not muse-batch-publishing-p))
442 (message "Publishing %s...done" name))))
444 (defun muse-insert-file-or-string (file-or-string &optional title)
445 (let ((beg (point)) end)
446 (if (and (not (string-equal file-or-string ""))
447 (file-readable-p file-or-string))
448 (setq end (+ beg (cadr (insert-file-contents file-or-string))))
449 (insert file-or-string)
450 (setq end (point)))
451 (save-restriction
452 (narrow-to-region beg end)
453 (muse-publish-markup (or title "")
454 '((100 "<\\(lisp\\)>" 0
455 muse-publish-markup-tag))))))
457 (defun muse-style-run-hooks (keyword style &rest args)
458 (let (handled)
459 (while (and style (not handled))
460 (setq style (muse-style style))
461 (let ((func (muse-get-keyword keyword style t)))
462 (if func
463 (if (apply func args)
464 (setq handled t))))
465 (unless handled
466 (setq style (muse-style-element :base style))))
467 handled))
469 (defun muse-publish-markup-region (beg end title style)
470 "Apply the given STYLE's markup rules to the given region."
471 (save-restriction
472 (narrow-to-region beg end)
473 (muse-style-run-hooks :before style)
474 (muse-publish-markup
475 title
476 (sort (copy-alist (append muse-publish-markup-regexps
477 (muse-style-elements-list :regexps style)))
478 (function
479 (lambda (l r)
480 (< (car l) (car r))))))
481 (muse-style-run-hooks :before-end style)))
483 (defun muse-publish-markup-buffer (title style)
484 "Apply the given STYLE's markup rules to the current buffer."
485 (setq style (muse-style style))
486 (let ((style-header (muse-style-element :header style))
487 (style-footer (muse-style-element :footer style))
488 (muse-publishing-current-style style)
489 (muse-publishing-directives
490 (list (cons "title" title)
491 (cons "author" (user-full-name))
492 (cons "date" (format-time-string "%B %e, %Y"))))
493 (muse-publishing-p t))
494 (run-hooks 'muse-before-publish-hook)
495 (muse-publish-markup-region (point-min) (point-max) title style)
496 (goto-char (point-min))
497 (if style-header (muse-insert-file-or-string style-header title))
498 (goto-char (point-max))
499 (if style-footer (muse-insert-file-or-string style-footer title))
500 (muse-style-run-hooks :after style)
501 (run-hooks 'muse-after-publish-hook)))
503 (defun muse-publish-markup-string (string &optional style)
504 "Markup STRING using the given STYLE's markup rules."
505 (setq style (muse-style style))
506 (muse-with-temp-buffer
507 (insert string)
508 (let ((muse-publishing-current-style style)
509 (muse-publishing-p t))
510 (muse-publish-markup "*string*" (muse-style-element :rules style)))
511 (buffer-string)))
513 ;; Commands for publishing files
515 (defsubst muse-publish-get-style ()
516 (if (= 1 (length muse-publishing-styles))
517 (car muse-publishing-styles)
518 (assoc (completing-read "Publish with style: "
519 muse-publishing-styles nil t)
520 muse-publishing-styles)))
522 (defsubst muse-publish-get-output-dir (style)
523 (let ((default-directory (or (muse-style-element :path style)
524 default-directory)))
525 (read-file-name "Publish to directory: " nil default-directory)))
527 (defsubst muse-publish-get-info ()
528 (let ((style (muse-publish-get-style)))
529 (list style (muse-publish-get-output-dir style)
530 current-prefix-arg)))
532 (defsubst muse-publish-output-name (&optional file style)
533 (setq style (muse-style style))
534 (concat (muse-style-element :prefix style)
535 (muse-page-name file)
536 (muse-style-element :suffix style)))
538 (defsubst muse-publish-output-file (file &optional output-dir style)
539 (setq style (muse-style style))
540 (if output-dir
541 (expand-file-name (muse-publish-output-name file style) output-dir)
542 (concat (file-name-directory file)
543 (muse-publish-output-name file style))))
545 (defun muse-publish-file (file style &optional output-dir force)
546 "Publish the given file in list FILES.
547 If the argument FORCE is nil, each file is only published if it is
548 newer than the published version. If the argument FORCE is non-nil,
549 the file is published no matter what."
550 (interactive (cons (read-file-name "Publish file: ")
551 (muse-publish-get-info)))
552 (setq style (muse-style style))
553 (let* ((output-path (muse-publish-output-file file output-dir style))
554 (output-suffix (muse-style-element :osuffix style))
555 (muse-publishing-current-file file)
556 (target (if output-suffix
557 (concat (file-name-sans-extension output-path)
558 output-suffix)
559 output-path)))
560 (when (or force (file-newer-than-file-p file target))
561 (if (and muse-publish-report-threshhold
562 (> (nth 7 (file-attributes file))
563 muse-publish-report-threshhold))
564 (message "Publishing %s ..." file))
565 (muse-with-temp-buffer
566 (insert-file-contents file)
567 (muse-publish-markup-buffer (muse-page-name file) style)
568 (let ((backup-inhibited t))
569 (write-file output-path))
570 (muse-style-run-hooks :final style file output-path target))
571 t)))
573 (defun muse-publish-this-file (style output-dir &optional force)
574 "Publish the page in the current file."
575 (interactive (muse-publish-get-info))
576 (unless (muse-publish-file buffer-file-name style output-dir force)
577 (message (concat "The published version is up-to-date; use"
578 " C-u C-c C-t to force an update."))))
580 (defun muse-batch-publish-files ()
581 "Publish Muse files in batch mode."
582 (let ((muse-batch-publishing-p t)
583 style-name style output-dir)
584 (setq style-name (car command-line-args-left)
585 style (muse-style style-name)
586 command-line-args-left (cdr command-line-args-left)
587 output-dir (car command-line-args-left)
588 output-dir
589 (if (string-match "\\`--output-dir=" output-dir)
590 (prog1
591 (substring output-dir (match-end 0))
592 (setq command-line-args-left (cdr command-line-args-left)))))
593 (unless style
594 (error "There is no style '%s' defined." style-name))
595 (dolist (file command-line-args-left)
596 (muse-publish-file file style output-dir t))))
598 ;; Default publishing rules
600 (defun muse-publish-section-close (depth)
601 "Seach forward for the closing tag of given DEPTH."
602 (let (not-end)
603 (save-excursion
604 (while (and (setq not-end (re-search-forward
605 (concat "^\\*\\{1," (number-to-string depth)
606 "\\}\\s-+")
607 nil t))
608 (get-text-property (match-beginning 0) 'read-only)))
609 (if not-end
610 (forward-line 0)
611 (goto-char (point-max)))
612 (cond ((not (eq (char-before) ?\n))
613 (insert "\n\n"))
614 ((not (eq (char-before (1- (point))) ?\n))
615 (insert "\n")))
616 (insert (muse-markup-text 'section-close) "\n"))))
618 (defun muse-publish-markup-directive (&optional name value)
619 (unless name (setq name (match-string 1)))
620 (unless value (setq value (match-string 2)))
621 (let ((elem (assoc name muse-publishing-directives)))
622 (if elem
623 (setcdr elem value)
624 (setq muse-publishing-directives
625 (cons (cons name value)
626 muse-publishing-directives))))
627 (delete-region (match-beginning 0) (match-end 0)))
629 (defun muse-publish-markup-anchor () "")
630 (defun muse-publish-markup-comment () "")
632 (defun muse-publish-markup-tag ()
633 (let ((tag-info (muse-markup-tag-info (match-string 1))))
634 (when (and tag-info
635 (not (get-text-property (match-beginning 0) 'read-only)))
636 (let ((closed-tag (match-string 3))
637 (start (match-beginning 0))
638 (beg (point)) end attrs)
639 (when (nth 2 tag-info)
640 (let ((attrstr (match-string 2)))
641 (while (and attrstr
642 (string-match (concat "\\([^"
643 muse-regexp-space
644 "=]+\\)\\(=\"\\"
645 "([^\"]+\\)\"\\)?")
646 attrstr))
647 (let ((attr (cons (downcase
648 (muse-match-string-no-properties 1 attrstr))
649 (muse-match-string-no-properties 3 attrstr))))
650 (setq attrstr (replace-match "" t t attrstr))
651 (if attrs
652 (nconc attrs (list attr))
653 (setq attrs (list attr)))))))
654 (if (and (cadr tag-info) (not closed-tag))
655 (if (search-forward (concat "</" (car tag-info) ">") nil t)
656 (delete-region (match-beginning 0) (point))
657 (setq tag-info nil)))
658 (when tag-info
659 (setq end (point-marker))
660 (delete-region start beg)
661 (goto-char start)
662 (let ((args (list start end)))
663 (if (nth 2 tag-info)
664 (nconc args (list attrs)))
665 (apply (nth 3 tag-info) args))))))
666 nil)
668 (defun muse-publish-escape-specials (beg end &optional ignore-read-only)
669 "Escape specials from BEG to END using style-specific :specials.
670 If IGNORE-READ-ONLY is non-nil, ignore the read-only property."
671 (save-excursion
672 (goto-char beg)
673 (while (< (point) end)
674 (if (and (not ignore-read-only) (get-text-property (point) 'read-only))
675 (forward-char 1)
676 (let ((repl
677 (or (assoc (char-after) (muse-style-element :specials))
678 (assoc (char-after) muse-publish-markup-specials))))
679 (if (null repl)
680 (forward-char 1)
681 (delete-char 1)
682 (insert (cdr repl))))))))
684 (defun muse-publish-markup-word ()
685 (let* ((beg (match-beginning 2))
686 (end (1- (match-end 2)))
687 (leader (buffer-substring-no-properties beg end))
688 open-tag close-tag mark-read-only loc)
689 (cond
690 ((string= leader "_")
691 (setq open-tag (muse-markup-text 'begin-underline)
692 close-tag (muse-markup-text 'end-underline)))
693 ((string= leader "=")
694 (setq open-tag (muse-markup-text 'begin-literal)
695 close-tag (muse-markup-text 'end-literal))
696 (setq mark-read-only t))
698 (let ((l (length leader)))
699 (cond
700 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
701 close-tag (muse-markup-text 'end-emph)))
702 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
703 close-tag (muse-markup-text 'end-more-emph)))
704 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
705 close-tag (muse-markup-text 'end-most-emph)))))))
706 (if (and (not (get-text-property beg 'noemphasis))
707 (setq loc (search-forward leader nil t))
708 (not (eq (char-syntax (char-after loc)) ?w))
709 (not (eq (char-syntax (char-before (point))) ?\ ))
710 (not (get-text-property (point) 'noemphasis)))
711 (progn
712 (replace-match "")
713 (delete-region beg end)
714 (setq end (point-marker))
715 (insert close-tag)
716 (save-excursion
717 (goto-char beg)
718 (insert open-tag)
719 (setq beg (point)))
720 (when mark-read-only
721 (muse-publish-escape-specials beg end t)
722 (muse-publish-mark-read-only (1- beg) (1+ end))))
723 (backward-char))
724 nil))
726 (defun muse-publish-markup-emdash ()
727 (delete-region (match-beginning 0) (match-end 0))
728 (insert (muse-markup-text 'emdash))
729 (if (eq (char-after) ?\<)
730 (insert ?\n)))
732 (defun muse-publish-markup-enddots ()
733 (delete-region (match-beginning 0) (match-end 0))
734 (insert (muse-markup-text 'enddots)))
736 (defun muse-publish-markup-dots ()
737 (delete-region (match-beginning 0) (match-end 0))
738 (insert (muse-markup-text 'dots)))
740 (defun muse-publish-markup-rule ()
741 (delete-region (match-beginning 0) (match-end 0))
742 (insert (muse-markup-text 'rule)))
744 (defun muse-publish-markup-heading ()
745 (let* ((len (length (match-string 1)))
746 (start (muse-markup-text
747 (cond ((= len 1) 'section)
748 ((= len 2) 'subsection)
749 ((= len 3) 'subsubsection)
750 (t 'section-other))))
751 (end (muse-markup-text
752 (cond ((= len 1) 'section-end)
753 ((= len 2) 'subsection-end)
754 ((= len 3) 'subsubsection-end)
755 (t 'section-other-end)))))
756 (delete-region (match-beginning 0) (match-end 0))
757 (insert start)
758 (end-of-line)
759 (if end (insert end))
760 (muse-publish-section-close len)))
762 (defvar muse-publish-footnotes nil)
764 (defun muse-publish-markup-footnote ()
765 "Scan ahead and snarf up the footnote body"
766 (if (= (muse-line-beginning-position) (match-beginning 0))
768 (let ((footnote (save-match-data
769 (string-to-number (match-string 1))))
770 footnotemark)
771 (delete-region (match-beginning 0) (match-end 0))
772 (save-excursion
773 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
774 (let* ((start (match-beginning 0))
775 (beg (goto-char (match-end 0)))
776 (end (save-excursion
777 (if (search-forward "\n\n" nil t)
778 (copy-marker (match-beginning 0))
779 (goto-char (point-max))
780 (skip-chars-backward "\n")
781 (point-marker)))))
782 (while (re-search-forward (concat "^["
783 muse-regexp-blank
784 "]+\\([^\n]\\)")
785 end t)
786 (replace-match "\\1" t))
787 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
788 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
789 (if (string= "" footnotemark-cmd)
790 (setq footnotemark
791 (concat (muse-markup-text 'footnote)
792 (buffer-substring-no-properties beg end)
793 (muse-markup-text 'footnote-end)))
794 (setq footnotemark (format footnotemark-cmd footnote
795 footnotemark-end-cmd))
796 (unless muse-publish-footnotes
797 (set (make-local-variable 'muse-publish-footnotes)
798 (make-vector 256 nil)))
799 (unless (aref muse-publish-footnotes footnote)
800 (setq footnotemark
801 (concat
802 footnotemark
803 (concat (format (muse-markup-text 'footnotetext)
804 footnote)
805 (buffer-substring-no-properties beg end)
806 (muse-markup-text 'footnotetext-end))))
807 (aset muse-publish-footnotes footnote footnotemark))))
808 (goto-char end)
809 (skip-chars-forward "\n")
810 (delete-region start (point)))))
811 (insert (or footnotemark footnote)))))
813 (defun muse-publish-markup-fn-sep ()
814 (delete-region (match-beginning 0) (match-end 0))
815 (insert (muse-markup-text 'fn-sep)))
817 (defun muse-publish-surround-text (beg-tag end-tag move-func)
818 (let ((beg (point)) end)
819 (skip-chars-backward muse-regexp-space)
820 (delete-region (point) beg)
821 (insert "\n\n" beg-tag)
822 (funcall move-func)
823 (setq end (point-marker))
824 (goto-char beg)
825 (while (< (point) end)
826 (if (looking-at "^\\s-+")
827 (replace-match ""))
828 (forward-line 1))
829 (goto-char end)
830 (setq beg (point))
831 (skip-chars-backward muse-regexp-space)
832 (delete-region (point) beg))
833 (insert end-tag "\n"))
835 (defsubst muse-forward-paragraph (&optional pattern)
836 (if (re-search-forward (if pattern
837 (concat "^\\(" pattern "["
838 muse-regexp-blank
839 "]+\\|$\\|\\'\\)")
840 "^\\s-*\\($\\|\\'\\)") nil t)
841 (goto-char (match-beginning 0))
842 (goto-char (point-max))))
844 (defun muse-publish-markup-list ()
845 "Markup a list entry or quoted paragraph.
846 The reason this function is so funky, is to prevent text properties
847 like read-only from being inadvertently deleted."
848 (let ((str (match-string 1)))
849 (cond
850 ((and (eq (aref str 0) ?-))
851 (delete-region (match-beginning 0) (match-end 0))
852 (muse-publish-surround-text
853 (muse-markup-text 'begin-uli)
854 (muse-markup-text 'end-uli)
855 (function
856 (lambda ()
857 (muse-forward-paragraph (concat "["
858 muse-regexp-blank
859 "]+-"))))))
860 ((and (>= (aref str 0) ?0)
861 (<= (aref str 0) ?9))
862 (delete-region (match-beginning 0) (match-end 0))
863 (muse-publish-surround-text
864 (muse-markup-text 'begin-oli)
865 (muse-markup-text 'end-oli)
866 (function
867 (lambda ()
868 (muse-forward-paragraph (concat "["
869 muse-regexp-blank
870 "]+[0-9]+\\."))))))
872 (goto-char (match-beginning 1))
873 (insert (muse-markup-text 'begin-ddt))
874 (save-match-data
875 (save-excursion
876 (forward-line 1)
877 (while (looking-at (concat "^\\(["
878 muse-regexp-blank
879 "]*\\)[^"
880 muse-regexp-space
881 "]"))
882 (delete-region (match-beginning 1) (match-end 1))
883 (forward-line 1))))
884 (save-match-data
885 (when (re-search-forward (concat "["
886 muse-regexp-space
887 "]+::["
888 muse-regexp-space
889 "]+")
890 nil t)
891 (replace-match (muse-markup-text 'start-dde))))
892 (muse-forward-paragraph)
893 (insert (muse-markup-text 'end-ddt) ?\n)))))
895 (defun muse-publish-markup-quote ()
896 "Markup a list entry or quoted paragraph.
897 The reason this function is so funky, is to prevent text properties
898 like read-only from being inadvertently deleted."
899 (let* ((ws (match-string 0))
900 (centered (>= (string-width ws) 6))
901 (begin-elem (if centered 'begin-center 'begin-quote))
902 (end-elem (if centered 'end-center 'end-quote)))
903 (muse-publish-surround-text (muse-markup-text begin-elem)
904 (muse-markup-text end-elem)
905 'muse-forward-paragraph)))
907 (defun muse-publish-markup-leading-space ()
908 (let ((markup-space (muse-markup-text 'verse-space))
909 count)
910 (when (and markup-space
911 (>= (setq count (skip-chars-forward " ")) 0))
912 (delete-region (muse-line-beginning-position) (point))
913 (while (> count 0)
914 (insert markup-space)
915 (setq count (- count 2))))))
917 (defun muse-publish-markup-verse ()
918 (let ((leader (match-string 0)))
919 (goto-char (match-beginning 0))
920 (insert (muse-markup-text 'begin-verse))
921 (while (looking-at leader)
922 (replace-match "")
923 (muse-publish-markup-leading-space)
924 (end-of-line)
925 (cond
926 ((bolp)
927 (let ((text (muse-markup-text 'empty-verse-line)))
928 (if text (insert text))))
929 ((save-excursion
930 (save-match-data
931 (forward-line 1)
932 (or (looking-at (concat leader "["
933 muse-regexp-blank
934 "]*$"))
935 (not (looking-at leader)))))
936 (let ((text (muse-markup-text 'last-stanza-end)))
937 (if text (insert text))))
939 (let ((text (muse-markup-text 'end-verse-line)))
940 (if text (insert text)))))
941 (forward-line 1)))
942 (insert (muse-markup-text 'end-verse) ?\n))
944 (defun muse-publish-markup-table ()
945 "Style does not support tables.")
947 (defun muse-publish-markup-email ()
948 (let* ((beg (match-end 1))
949 (addr (buffer-substring-no-properties beg (match-end 0))))
950 (muse-with-temp-buffer
951 (insert addr)
952 (muse-publish-escape-specials (point-min) (point-max))
953 (setq addr (buffer-string)))
954 (goto-char beg)
955 (delete-region beg (match-end 0))
956 (insert (format (muse-markup-text 'email-addr) addr addr))
957 (muse-publish-mark-read-only beg (point))))
959 (defun muse-publish-escape-specials-in-string (string &rest ignored)
960 "Escape specials in STRING using style-specific :specials."
961 (save-excursion
962 (apply (function concat)
963 (mapcar
964 (lambda (ch)
965 (let ((repl
966 (or (assoc ch (muse-style-element :specials))
967 (assoc ch muse-publish-markup-specials))))
968 (if (null repl)
969 (char-to-string ch)
970 (cdr repl))))
971 (append string nil)))))
973 (defun muse-publish-url (url &optional desc explicit)
974 "Resolve a URL into its final <a href> form."
975 (let ((orig-url url))
976 (dolist (transform muse-publish-url-transforms)
977 (setq url (save-match-data (when url (funcall transform url explicit)))))
978 (setq desc (when desc (muse-publish-escape-specials-in-string desc)))
979 (if url
980 (if (string-match muse-image-regexp url)
981 (if desc
982 (muse-markup-text 'image-with-desc url desc)
983 (muse-markup-text 'image-link url))
984 (if (and desc (string-match muse-image-regexp desc))
985 (muse-markup-text 'url-with-image url desc)
986 (muse-markup-text 'url-link url (or desc orig-url))))
987 orig-url)))
989 (defun muse-publish-insert-url (url &optional desc explicit)
990 "Resolve a URL into its final <a href> form."
991 (delete-region (match-beginning 0) (match-end 0))
992 (let ((beg (point)))
993 (insert (muse-publish-url url desc explicit))
994 (muse-publish-mark-read-only beg (point))))
996 (defun muse-publish-markup-link ()
997 (let (desc explicit link)
998 (setq explicit (save-match-data
999 (if (string-match muse-explicit-link-regexp
1000 (match-string 0))
1001 t nil)))
1002 (setq desc (if explicit (match-string 2) (match-string 0)))
1003 (setq link (if explicit
1004 (muse-handle-explicit-link (match-string 1))
1005 (muse-handle-implicit-link (match-string 0))))
1006 (when (and link
1007 (or explicit
1008 (not (or (eq (char-before (match-beginning 0)) ?\")
1009 (eq (char-after (match-end 0)) ?\")))))
1011 (muse-publish-insert-url link desc explicit))))
1013 (defun muse-publish-markup-url ()
1014 (muse-publish-insert-url (match-string 0)))
1016 ;; Default publishing tags
1018 (defun muse-publish-contents-tag (beg end attrs)
1019 (set (make-local-variable 'muse-publish-generate-contents)
1020 (cons (copy-marker (point) t)
1021 (let ((depth (cdr (assoc "depth" attrs))))
1022 (or (and depth (string-to-number depth)) 2)))))
1024 (defun muse-publish-verse-tag (beg end)
1025 (save-excursion
1026 (goto-char beg)
1027 (while (eq ?\ (char-syntax (char-after)))
1028 (delete-char 1))
1029 (while (< (point) end)
1030 (insert "> ")
1031 (forward-line))
1032 (if (eq ?\ (char-syntax (char-before)))
1033 (delete-char -1))))
1035 (defun muse-publish-example-tag (beg end)
1036 (muse-publish-escape-specials beg end)
1037 (goto-char beg)
1038 (insert (muse-markup-text 'begin-example))
1039 (goto-char end)
1040 (insert (muse-markup-text 'end-example))
1041 (muse-publish-mark-read-only beg (point)))
1043 (defun muse-publish-mark-read-only (beg end)
1044 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1045 nil)
1047 (defun muse-publish-mark-noemphasis (&optional beg end)
1048 (unless beg (setq beg (match-beginning 0)))
1049 (unless end (setq end (match-end 0)))
1050 (add-text-properties beg end '(noemphasis t))
1051 nil)
1053 (defun muse-publish-verbatim-tag (beg end)
1054 (muse-publish-escape-specials beg end)
1055 (muse-publish-mark-read-only beg end))
1057 (defalias 'muse-publish-class-tag 'ignore)
1059 (defun muse-publish-lisp-tag (beg end)
1060 (save-excursion
1061 (let ((str (muse-eval-lisp
1062 (prog1
1063 (buffer-substring-no-properties beg end)
1064 (delete-region beg end)))))
1065 (set-text-properties 0 (length str) nil str)
1066 (insert str))))
1068 (defun muse-publish-command-tag (beg end attrs)
1069 (while (looking-at "\\s-*$")
1070 (forward-line))
1071 (let ((interp (cdr (assoc "interp" attrs))))
1072 (if (null interp)
1073 (shell-command
1074 (prog1
1075 (buffer-substring-no-properties (point) end)
1076 (delete-region beg end)) t)
1077 (shell-command-on-region beg end interp t t))
1078 (muse-publish-mark-read-only beg (point))))
1080 (defun muse-publish-comment-tag (beg end)
1081 (delete-region beg end))
1083 ;; Miscellaneous helper functions
1085 (defsubst muse-publishing-directive (name)
1086 (cdr (assoc name muse-publishing-directives)))
1088 (defun muse-publish-strip-tags (string)
1089 "Remove all tags from the string."
1090 (while (string-match "<.*?>" string)
1091 (setq string (replace-match "" nil t string)))
1092 string)
1094 (defun muse-publish-markup-type (category default-func)
1095 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1096 (funcall (or rule default-func))))
1098 (defun muse-published-buffer-contents (buffer)
1099 (with-current-buffer buffer
1100 (goto-char (point-min))
1101 (let ((beg (and (search-forward "Emacs Muse begins here")
1102 (muse-line-end-position)))
1103 (end (and (search-forward "Emacs Muse ends here")
1104 (muse-line-beginning-position))))
1105 (buffer-substring-no-properties beg end))))
1107 (defun muse-published-contents (file)
1108 (when (file-readable-p file)
1109 (muse-with-temp-buffer
1110 (insert-file-contents file)
1111 (muse-published-buffer-contents (current-buffer)))))
1113 (defun muse-publish-transform-output
1114 (file temp-file output-path name gen-func &rest cleanup-exts)
1115 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1116 (setq file (muse-page-name file))
1117 (message "Generating %s output for %s..." name file)
1118 (if (not (funcall gen-func temp-file output-path))
1119 (message "Generating %s from %s...failed" name file)
1120 (message "Generating %s output for %s...done" name file)
1121 (muse-delete-file-if-exists temp-file)
1122 (dolist (ext cleanup-exts)
1123 (muse-delete-file-if-exists
1124 (expand-file-name (concat file ext)
1125 (file-name-directory output-path))))
1126 (message "Wrote %s" output-path)))
1128 (defun muse-publish-read-only (string)
1129 (let ((end (1- (length string))))
1130 (add-text-properties 0 end
1131 '(rear-nonsticky (read-only) read-only t)
1132 string)
1133 string))
1135 (defun muse-publish-prepare-url (target &rest ignored)
1136 "Transform anchors and get published name, if TARGET is a page."
1137 (save-match-data
1138 (unless (or (string-match muse-url-regexp target)
1139 (string-match muse-image-regexp target)
1140 (string-match muse-file-regexp target))
1141 (setq target (if (string-match "#" target)
1142 (concat (muse-publish-output-name
1143 (substring target 0 (match-beginning 0)))
1144 "#" (substring target (match-end 0)))
1145 (muse-publish-output-name target)))))
1146 target)
1148 (provide 'muse-publish)
1150 ;;; muse-publish.el ends here