Fix stray footnote references causing control chars to be inserted bug
[muse-el.git] / lisp / muse-publish.el
blob7b2592b18765b1036b91a8f5be434d026bd51153
1 ;;; muse-publish.el --- base publishing implementation
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 ;; 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 ;; Jim Ottaway (j DOT ottaway AT lse DOT ac DOT uk) provided a
33 ;; reference implementation for nested lists, as well as some code for
34 ;; the "style" element of the <literal> tag.
36 ;;; Code:
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
40 ;; Muse Publishing
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 (provide 'muse-publish)
46 (require 'muse)
47 (require 'muse-regexps)
49 (defgroup muse-publish nil
50 "Options controlling the general behavior of Muse publishing."
51 :group 'muse)
53 (defcustom muse-before-publish-hook nil
54 "A hook run in the buffer to be published, before it is done."
55 :type 'hook
56 :group 'muse-publish)
58 (defcustom muse-after-publish-hook nil
59 "A hook run in the buffer to be published, after it is done."
60 :type 'hook
61 :group 'muse-publish)
63 (defcustom muse-publish-url-transforms
64 '(muse-resolve-url)
65 "A list of functions used to prepare URLs for publication.
66 Each is passed the URL. The transformed URL should be returned."
67 :type 'hook
68 :options '(muse-resolve-url)
69 :group 'muse-publish)
71 (defcustom muse-publish-desc-transforms nil
72 "A list of functions used to prepare URL desciptions for publication.
73 Each is passed the description. The modified description should
74 be returned."
75 :type 'hook
76 :group 'muse-publish)
78 (defcustom muse-publish-date-format "%B %e, %Y"
79 "Format string for the date, used by `muse-publish-markup-buffer'.
80 See `format-time-string' for details on the format options."
81 :type 'string
82 :group 'muse-publish)
84 (defcustom muse-publish-comments-p nil
85 "If nil, remove comments before publishing.
86 If non-nil, publish comments using the markup of the current style."
87 :type 'boolean
88 :group 'muse-publish)
90 (defcustom muse-publish-report-threshhold 100000
91 "If a file is this size or larger, report publishing progress."
92 :type 'integer
93 :group 'muse-publish)
95 (defcustom muse-publish-markup-regexps
96 `(;; Remove leading and trailing whitespace from the file
97 (1000 "\\(\\`\n+\\|\n+\\'\\)" 0 "")
99 ;; Remove trailing whitespace from all lines
100 (1100 ,(concat "[" muse-regexp-blank "]+$") 0 "")
102 ;; Handle any leading #directives
103 (1200 "\\`#\\([a-zA-Z-]+\\)\\s-+\\(.+\\)\n+" 0 directive)
105 ;; commented lines
106 (1250 ,(concat "^;\\(?:[" muse-regexp-blank "]+\\(.+\\)\\|$\\|'\\)")
107 0 comment)
109 ;; markup tags
110 (1300 muse-tag-regexp 0 tag)
112 ;; prevent emphasis characters in explicit links from being marked
113 (1400 muse-explicit-link-regexp 0 muse-publish-mark-link)
115 ;; emphasized or literal text
116 (1600 ,(concat "\\(^\\|[-[" muse-regexp-blank
117 "<('`\"\n]\\)\\(=[^=" muse-regexp-blank
118 "\n]\\|_[^_" muse-regexp-blank
119 "\n]\\|\\*+[^*" muse-regexp-blank
120 "\n]\\)")
121 2 word)
123 ;; headings, outline-mode style
124 (1700 "^\\(\\*+\\)\\s-+" 0 heading)
126 ;; ellipses
127 (1800 "\\.\\.\\.\\." 0 enddots)
128 (1850 "\\.\\.\\." 0 dots)
130 ;; horizontal rule, or section separator
131 (1900 "^----+" 0 rule)
133 ;; non-breaking space
134 (1950 "~~" 0 no-break-space)
136 ;; beginning of footnotes section
137 (2000 "^Footnotes:?\\s-*" 0 fn-sep)
138 ;; footnote definition/reference (def if at beginning of line)
139 (2100 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote)
141 ;; unnumbered List items begin with a -. numbered list items
142 ;; begin with number and a period. definition lists have a
143 ;; leading term separated from the body with ::. centered
144 ;; paragraphs begin with at least six columns of whitespace; any
145 ;; other whitespace at the beginning indicates a blockquote. The
146 ;; reason all of these rules are handled here, is so that
147 ;; blockquote detection doesn't interfere with indented list
148 ;; members.
149 (2200 ,(format muse-list-item-regexp (concat "[" muse-regexp-blank "]*"))
150 0 list)
152 ;; simple table markup is supported, nothing fancy. use | to
153 ;; separate cells, || to separate header cells, and ||| for footer
154 ;; cells
155 (2300 ,(concat "\\(\\([" muse-regexp-blank "]*\n\\)?"
156 "\\(" muse-table-line-regexp "\\(?:\n\\|\\'\\)\\)\\)+")
157 0 table)
159 ;; blockquote and centered text
160 (2400 ,(concat "^\\([" muse-regexp-blank "]+\\).+") 0 quote)
162 ;; the emdash ("--")
163 (2500 ,(concat "\\(^\\|[" muse-regexp-blank "]*\\)--\\($\\|["
164 muse-regexp-blank "]*\\)")
165 0 emdash)
167 ;; "verse" text is indicated the same way as a quoted e-mail
168 ;; response: "> text", where text may contain initial whitespace
169 ;; (see below).
170 (2600 ,(concat "^[" muse-regexp-blank "]*> ") 0 verse)
172 ;; define anchor points
173 (2700 "^\\(\\W*\\)#\\(\\S-+\\)\\s-*" 0 anchor)
175 ;; replace links in the buffer (links to other pages)
176 (2900 muse-explicit-link-regexp 0 link)
178 ;; bare URLs
179 (3000 muse-url-regexp 0 url)
181 ;; bare email addresses
182 (3500
183 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email)
185 "List of markup rules for publishing a page with Muse.
186 The rules given in this variable are invoked first, followed by
187 whatever rules are specified by the current style.
189 Each member of the list is either a function, or a list of the form:
191 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
193 REGEXP is a regular expression, or symbol whose value is a regular
194 expression, which is searched for using `re-search-forward'.
195 TEXT-BEGIN-GROUP is the matching group within that regexp which
196 denotes the beginning of the actual text to be marked up.
197 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
198 If it is not a string, but a function, it will be called to determine
199 what the replacement text should be (it must return a string). If it
200 is a symbol, the value of that symbol should be a string.
202 The replacements are done in order, one rule at a time. Writing
203 the regular expressions can be a tricky business. Note that case
204 is never ignored. `case-fold-search' is always bound to nil
205 while processing the markup rules."
206 :type '(repeat (choice
207 (list :tag "Markup rule"
208 integer
209 (choice regexp symbol)
210 integer
211 (choice string function symbol))
212 function))
213 :group 'muse-publish)
215 (defcustom muse-publish-markup-functions
216 '((directive . muse-publish-markup-directive)
217 (comment . muse-publish-markup-comment)
218 (anchor . muse-publish-markup-anchor)
219 (tag . muse-publish-markup-tag)
220 (word . muse-publish-markup-word)
221 (emdash . muse-publish-markup-emdash)
222 (enddots . muse-publish-markup-enddots)
223 (dots . muse-publish-markup-dots)
224 (rule . muse-publish-markup-rule)
225 (no-break-space . muse-publish-markup-no-break-space)
226 (heading . muse-publish-markup-heading)
227 (footnote . muse-publish-markup-footnote)
228 (fn-sep . muse-publish-markup-fn-sep)
229 (list . muse-publish-markup-list)
230 (quote . muse-publish-markup-quote)
231 (verse . muse-publish-markup-verse)
232 (table . muse-publish-markup-table)
233 (email . muse-publish-markup-email)
234 (link . muse-publish-markup-link)
235 (url . muse-publish-markup-url))
236 "An alist of style types to custom functions for that kind of text.
238 Each member of the list is of the form:
240 (SYMBOL FUNCTION)
242 SYMBOL describes the type of text to associate with this rule.
243 `muse-publish-markup-regexps' maps regexps to these symbols.
245 FUNCTION is the function to use to mark up this kind of rule if
246 no suitable function is found through the :functions tag of the
247 current style."
248 :type '(alist :key-type symbol :value-type function)
249 :group 'muse-publish)
251 (defcustom muse-publish-markup-tags
252 '(("contents" nil t nil muse-publish-contents-tag)
253 ("verse" t nil nil muse-publish-verse-tag)
254 ("example" t nil nil muse-publish-example-tag)
255 ("src" t t nil muse-publish-src-tag)
256 ("code" t nil nil muse-publish-code-tag)
257 ("quote" t nil t muse-publish-quote-tag)
258 ("literal" t t nil muse-publish-literal-tag)
259 ("verbatim" t nil nil muse-publish-verbatim-tag)
260 ("lisp" t t nil muse-publish-lisp-tag)
261 ("class" t t nil muse-publish-class-tag)
262 ("command" t t nil muse-publish-command-tag)
263 ("perl" t t nil muse-publish-perl-tag)
264 ("python" t t nil muse-publish-python-tag)
265 ("ruby" t t nil muse-publish-ruby-tag)
266 ("comment" t nil nil muse-publish-comment-tag)
267 ("include" nil t nil muse-publish-include-tag)
268 ("markup" t t nil muse-publish-mark-up-tag))
269 "A list of tag specifications, for specially marking up text.
270 XML-style tags are the best way to add custom markup to Muse.
271 This is easily accomplished by customizing this list of markup tags.
273 For each entry, the name of the tag is given, whether it expects
274 a closing tag and/or an optional set of attributes, whether it is
275 nestable, and a function that performs whatever action is desired
276 within the delimited region.
278 The tags themselves are deleted during publishing, before the
279 function is called. The function is called with three arguments,
280 the beginning and end of the region surrounded by the tags. If
281 properties are allowed, they are passed as a third argument in
282 the form of an alist. The `end' argument to the function is
283 always a marker.
285 Point is always at the beginning of the region within the tags, when
286 the function is called. Wherever point is when the function finishes
287 is where tag markup will resume.
289 These tag rules are processed once at the beginning of markup, and
290 once at the end, to catch any tags which may have been inserted
291 in-between."
292 :type '(repeat (list (string :tag "Markup tag")
293 (boolean :tag "Expect closing tag" :value t)
294 (boolean :tag "Parse attributes" :value nil)
295 (boolean :tag "Nestable" :value nil)
296 function))
297 :group 'muse-publish)
299 (defcustom muse-publish-markup-specials nil
300 "A table of characters which must be represented specially."
301 :type '(alist :key-type character :value-type string)
302 :group 'muse-publish)
304 (defvar muse-publishing-p nil
305 "Set to t while a page is being published.")
306 (defvar muse-batch-publishing-p nil
307 "Set to t while a page is being batch published.")
308 (defvar muse-publishing-styles nil
309 "The publishing styles that Muse recognizes.
310 This is automatically generated when loading publishing styles.")
311 (defvar muse-publishing-current-file nil
312 "The file that is currently being published.")
313 (defvar muse-publishing-current-output-path nil
314 "The path where the current file will be published to.")
315 (defvar muse-publishing-current-style nil
316 "The style of the file that is currently being published.")
317 (defvar muse-publishing-directives nil
318 "An alist of publishing directives from the top of a file.")
319 (defvar muse-publish-generate-contents nil
320 "Non-nil if a table of contents should be generated.
321 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
322 tell where the <contents> was seen, and to what depth the
323 contents were requested.")
324 (defvar muse-publishing-last-position nil
325 "Last position of the point when publishing.
326 This is used to make sure that publishing doesn't get stalled.")
328 ;; Functions for handling style information
330 (defsubst muse-style (&optional style)
331 "Resolve the given STYLE into a Muse style, if it is a string."
332 (if (null style)
333 muse-publishing-current-style
334 (if (stringp style)
335 (assoc style muse-publishing-styles)
336 (muse-assert (consp style))
337 style)))
339 (defun muse-define-style (name &rest elements)
340 (let ((entry (assoc name muse-publishing-styles)))
341 (if entry
342 (setcdr entry elements)
343 (setq muse-publishing-styles
344 (cons (append (list name) elements)
345 muse-publishing-styles)))))
347 (defun muse-derive-style (new-name base-name &rest elements)
348 (apply 'muse-define-style new-name
349 (append elements (list :base base-name))))
351 (defsubst muse-get-keyword (keyword list &optional direct)
352 (let ((value (cadr (memq keyword list))))
353 (if (and (not direct) (symbolp value))
354 (symbol-value value)
355 value)))
357 (defun muse-style-elements-list (elem &optional style)
358 "Return a list all references to ELEM in STYLE, including base styles.
359 If STYLE is not specified, use current style."
360 (let (base elements)
361 (while style
362 (setq style (muse-style style))
363 (setq elements (append elements
364 (muse-get-keyword elem style)))
365 (setq style (muse-get-keyword :base style)))
366 elements))
368 (defun muse-style-element (elem &optional style direct)
369 "Search for ELEM in STYLE, including base styles.
370 If STYLE is not specified, use current style."
371 (setq style (muse-style style))
372 (let ((value (muse-get-keyword elem style direct)))
373 (if value
374 value
375 (let ((base (muse-get-keyword :base style)))
376 (if base
377 (muse-style-element elem base direct))))))
379 (defun muse-style-derived-p-1 (base style)
380 "Internal function used by `muse-style-derived-p'."
381 (if (and (stringp style)
382 (string= style base))
384 (setq style (muse-style style))
385 (let ((value (muse-get-keyword :base style)))
386 (when value
387 (muse-style-derived-p base value)))))
389 (defun muse-style-derived-p (base &optional style)
390 "Return non-nil if STYLE is equal to or derived from BASE,
391 non-nil otherwise.
393 BASE should be a string."
394 (unless style
395 (setq style (muse-style)))
396 (when (and (consp style)
397 (stringp (car style)))
398 (setq style (car style)))
399 (muse-style-derived-p-1 base style))
401 (defun muse-find-markup-element (keyword ident style)
402 (let ((def (assq ident (muse-style-element keyword style))))
403 (if def
404 (cdr def)
405 (let ((base (muse-style-element :base style)))
406 (if base
407 (muse-find-markup-element keyword ident base))))))
409 (defun muse-markup-text (ident &rest args)
410 "Insert ARGS into the text markup associated with IDENT.
411 If the markup text has sections like %N%, this will be replaced
412 with the N-1th argument in ARGS. After that, `format' is applied
413 to the text with ARGS as parameters."
414 (let ((text (muse-find-markup-element :strings ident (muse-style))))
415 (if (and text args)
416 (progn
417 (let (start repl-text)
418 (while (setq start (string-match "%\\([1-9][0-9]*\\)%" text start))
419 ;; escape '%' in the argument text, since we will be
420 ;; using format on it
421 (setq repl-text (muse-replace-regexp-in-string
422 "%" "%%"
423 (nth (1- (string-to-number
424 (match-string 1 text))) args)
425 t t)
426 start (+ start (length repl-text))
427 text (replace-match repl-text t t text))))
428 (apply 'format text args))
429 (or text ""))))
431 (defun muse-insert-markup (&rest args)
432 (let ((beg (point)))
433 (apply 'insert args)
434 (muse-publish-mark-read-only beg (point))))
436 (defun muse-find-markup-tag (keyword tagname style)
437 (let ((def (assoc tagname (muse-style-element keyword style))))
438 (or def
439 (let ((base (muse-style-element :base style)))
440 (if base
441 (muse-find-markup-tag keyword tagname base))))))
443 (defsubst muse-markup-tag-info (tagname &rest args)
444 (let ((tag-info (muse-find-markup-tag :tags tagname (muse-style))))
445 (or tag-info
446 (assoc tagname muse-publish-markup-tags))))
448 (defsubst muse-markup-function (category)
449 (let ((func (muse-find-markup-element :functions category (muse-style))))
450 (or func
451 (cdr (assq category muse-publish-markup-functions)))))
453 ;; Publishing routines
455 (defun muse-publish-markup (name rules)
456 (let* ((case-fold-search nil)
457 (inhibit-read-only t)
458 (limit (* (length rules) (point-max)))
459 (verbose (and muse-publish-report-threshhold
460 (> (point-max) muse-publish-report-threshhold)))
461 (base 0))
462 (while rules
463 (goto-char (point-min))
464 (let ((regexp (nth 1 (car rules)))
465 (group (nth 2 (car rules)))
466 (repl (nth 3 (car rules)))
467 pos)
468 (setq muse-publishing-last-position nil)
469 (if (symbolp regexp)
470 (setq regexp (symbol-value regexp)))
471 (if (and verbose (not muse-batch-publishing-p))
472 (message "Publishing %s...%d%%" name
473 (* (/ (float (+ (point) base)) limit) 100)))
474 (while (and regexp (setq pos (re-search-forward regexp nil t)))
475 (if (and verbose (not muse-batch-publishing-p))
476 (message "Publishing %s...%d%%" name
477 (* (/ (float (+ (point) base)) limit) 100)))
478 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
479 (match-beginning group)
480 (get-text-property (match-beginning group) 'read-only))
481 (let* (func
482 (text (cond
483 ((and (symbolp repl)
484 (setq func (muse-markup-function repl)))
485 (funcall func))
486 ((functionp repl)
487 (funcall repl))
488 ((symbolp repl)
489 (symbol-value repl))
490 (t repl))))
491 (if (stringp text)
492 (replace-match text t))))
493 (if (and muse-publishing-last-position
494 (= pos muse-publishing-last-position))
495 (if (eobp)
496 (setq regexp nil)
497 (forward-char 1)))
498 (setq muse-publishing-last-position pos)))
499 (setq rules (cdr rules)
500 base (+ base (point-max))))
501 (if (and verbose (not muse-batch-publishing-p))
502 (message "Publishing %s...done" name))))
504 (defcustom muse-publish-markup-header-footer-tags
505 '(("lisp" t t nil muse-publish-lisp-tag)
506 ("markup" t t nil muse-publish-mark-up-tag))
507 "Tags used when publishing headers and footers.
508 See `muse-publish-markup-tags' for details."
509 :type '(repeat (list (string :tag "Markup tag")
510 (boolean :tag "Expect closing tag" :value t)
511 (boolean :tag "Parse attributes" :value nil)
512 (boolean :tag "Nestable" :value nil)
513 function))
514 :group 'muse-publish)
516 (defun muse-insert-file-or-string (file-or-string &optional title)
517 (let ((beg (point)) end)
518 (if (and (not (string-equal file-or-string ""))
519 (not (string-match "\n" file-or-string))
520 (file-readable-p file-or-string))
521 (setq end (+ beg (cadr (insert-file-contents file-or-string))))
522 (insert file-or-string)
523 (setq end (point)))
524 (save-restriction
525 (narrow-to-region beg end)
526 (remove-text-properties (point-min) (point-max)
527 '(read-only nil rear-nonsticky nil))
528 (goto-char (point-min))
529 (let ((muse-inhibit-style-tags t))
530 (muse-publish-markup (or title "")
531 '((100 muse-tag-regexp 0
532 muse-publish-markup-tag)))))))
534 (defun muse-style-run-hooks (keyword style &rest args)
535 (catch 'handled
536 (let ((cache nil))
537 (while (and style
538 (setq style (muse-style style)))
539 (let ((func (muse-style-element keyword style t)))
540 (when (and func
541 (not (member func cache)))
542 (setq cache (cons func cache))
543 (when (apply func args)
544 (throw 'handled t))))
545 (setq style (muse-style-element :base style))))))
547 (defvar muse-publish-inhibit-style-hooks nil
548 "If non-nil, do not call the :before or :before-end hooks when publishing.")
550 (defun muse-publish-markup-region (beg end &optional title style)
551 "Apply the given STYLE's markup rules to the given region.
552 TITLE is used when indicating the publishing progress; it may be nil.
554 The point is guaranteed to be at END if the routine terminates
555 normally."
556 (unless title (setq title ""))
557 (unless style
558 (or (setq style muse-publishing-current-style)
559 (error "Cannot find any publishing styles to use")))
560 (save-restriction
561 (narrow-to-region beg end)
562 (unless muse-publish-inhibit-style-hooks
563 (muse-style-run-hooks :before style))
564 (muse-publish-markup
565 title
566 (sort (copy-alist (append muse-publish-markup-regexps
567 (muse-style-elements-list :regexps style)))
568 (function
569 (lambda (l r)
570 (< (car l) (car r))))))
571 (unless muse-publish-inhibit-style-hooks
572 (muse-style-run-hooks :before-end style))
573 (muse-publish-escape-specials (point-min) (point-max) nil 'document)
574 (goto-char (point-max))))
576 (defun muse-publish-markup-buffer (title style)
577 "Apply the given STYLE's markup rules to the current buffer."
578 (setq style (muse-style style))
579 (let ((style-header (muse-style-element :header style))
580 (style-footer (muse-style-element :footer style))
581 (muse-publishing-current-style style)
582 (muse-publishing-directives
583 (list (cons "title" title)
584 (cons "author" (user-full-name))
585 (cons "date" (format-time-string
586 muse-publish-date-format
587 (if muse-publishing-current-file
588 (nth 5 (file-attributes
589 muse-publishing-current-file))
590 (current-time))))))
591 (muse-publishing-p t)
592 (inhibit-read-only t))
593 (run-hooks 'muse-update-values-hook)
594 (run-hooks 'muse-before-publish-hook)
595 (muse-publish-markup-region (point-min) (point-max) title style)
596 (goto-char (point-min))
597 (when style-header
598 (muse-insert-file-or-string style-header title))
599 (goto-char (point-max))
600 (when style-footer
601 (muse-insert-file-or-string style-footer title))
602 (muse-style-run-hooks :after style)
603 (run-hooks 'muse-after-publish-hook)))
605 (defun muse-publish-markup-string (string &optional style)
606 "Markup STRING using the given STYLE's markup rules."
607 (setq style (muse-style style))
608 (muse-with-temp-buffer
609 (insert string)
610 (let ((muse-publishing-current-style style)
611 (muse-publishing-p t))
612 (muse-publish-markup "*string*" (muse-style-element :rules style)))
613 (buffer-string)))
615 ;; Commands for publishing files
617 (defun muse-publish-get-style (&optional styles)
618 (unless styles (setq styles muse-publishing-styles))
619 (if (= 1 (length styles))
620 (car styles)
621 (when (catch 'different
622 (let ((first (car (car styles))))
623 (dolist (style (cdr styles))
624 (unless (equal first (car style))
625 (throw 'different t)))))
626 (setq styles (muse-collect-alist
627 styles
628 (completing-read "Publish with style: " styles nil t))))
629 (if (or (= 1 (length styles))
630 (not (muse-get-keyword :path (car styles))))
631 (car styles)
632 (setq styles (mapcar (lambda (style)
633 (cons (muse-get-keyword :path style)
634 style))
635 styles))
636 (cdr (assoc (completing-read "Publish to directory: " styles nil t)
637 styles)))))
639 (defsubst muse-publish-get-output-dir (style)
640 (let ((default-directory (or (muse-style-element :path style)
641 default-directory)))
642 (muse-read-directory-name "Publish to directory: " nil default-directory)))
644 (defsubst muse-publish-get-info ()
645 (let ((style (muse-publish-get-style)))
646 (list style (muse-publish-get-output-dir style)
647 current-prefix-arg)))
649 (defsubst muse-publish-output-name (&optional file style)
650 (setq style (muse-style style))
651 (concat (muse-style-element :prefix style)
652 (muse-page-name file)
653 (muse-style-element :suffix style)))
655 (defsubst muse-publish-output-file (file &optional output-dir style)
656 (setq style (muse-style style))
657 (if output-dir
658 (expand-file-name (muse-publish-output-name file style) output-dir)
659 (concat (file-name-directory file)
660 (muse-publish-output-name file style))))
662 (defsubst muse-publish-link-name (&optional file style)
663 (setq style (muse-style style))
664 (concat (muse-style-element :prefix style)
665 (muse-page-name file)
666 (or (muse-style-element :link-suffix style)
667 (muse-style-element :suffix style))))
669 (defsubst muse-publish-link-file (file &optional style)
670 (setq style (muse-style style))
671 (if (file-exists-p file)
672 file
673 (concat (file-name-directory file)
674 (muse-publish-link-name file style))))
676 (defsubst muse-publish-link-page (page)
677 (if (fboundp 'muse-project-link-page)
678 (muse-project-link-page page)
679 (muse-publish-link-file page)))
681 ;;;###autoload
682 (defun muse-publish-region (beg end &optional title style)
683 "Apply the given STYLE's markup rules to the given region.
684 The result is placed in a new buffer that includes TITLE in its name."
685 (interactive "r")
686 (when (interactive-p)
687 (unless title (setq title (read-string "Title: ")))
688 (unless style (setq style (muse-publish-get-style))))
689 (let ((muse-publishing-current-style style)
690 (muse-publishing-p t)
691 (text (buffer-substring beg end))
692 (buf (generate-new-buffer (concat "*Muse: " title "*"))))
693 (with-current-buffer buf
694 (insert text)
695 (muse-publish-markup-buffer title style)
696 (goto-char (point-min)))
697 (pop-to-buffer buf)))
699 ;;;###autoload
700 (defun muse-publish-file (file style &optional output-dir force)
701 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
702 If the argument FORCE is nil, each file is only published if it is
703 newer than the published version. If the argument FORCE is non-nil,
704 the file is published no matter what."
705 (interactive (cons (read-file-name "Publish file: ")
706 (muse-publish-get-info)))
707 (let ((style-name style))
708 (setq style (muse-style style))
709 (unless style
710 (error "There is no style '%s' defined" style-name)))
711 (let* ((output-path (muse-publish-output-file file output-dir style))
712 (output-suffix (muse-style-element :osuffix style))
713 (muse-publishing-current-file file)
714 (muse-publishing-current-output-path output-path)
715 (target (if output-suffix
716 (concat (muse-path-sans-extension output-path)
717 output-suffix)
718 output-path))
719 (threshhold (nth 7 (file-attributes file))))
720 (if (not threshhold)
721 (message "Please save %s before publishing" file)
722 (when (or force (file-newer-than-file-p file target))
723 (if (and muse-publish-report-threshhold
724 (> threshhold
725 muse-publish-report-threshhold))
726 (message "Publishing %s ..." file))
727 (muse-with-temp-buffer
728 (insert-file-contents file)
729 (muse-publish-markup-buffer (muse-page-name file) style)
730 (let ((backup-inhibited t))
731 (write-file output-path))
732 (muse-style-run-hooks :final style file output-path target))
733 t))))
735 ;;;###autoload
736 (defun muse-publish-this-file (style output-dir &optional force)
737 "Publish the currently-visited file.
738 Prompt for both the STYLE and OUTPUT-DIR if they are not
739 supplied."
740 (interactive (muse-publish-get-info))
741 (if buffer-file-name
742 (let ((muse-current-output-style (list :base (car style)
743 :path output-dir)))
744 (unless (muse-publish-file buffer-file-name style output-dir force)
745 (message (concat "The published version is up-to-date; use"
746 " C-u C-c C-T to force an update."))))
747 (message "This buffer is not associated with any file")))
749 (defun muse-batch-publish-files ()
750 "Publish Muse files in batch mode."
751 (let ((muse-batch-publishing-p t)
752 style output-dir)
753 (setq style (car command-line-args-left)
754 command-line-args-left (cdr command-line-args-left)
755 output-dir (car command-line-args-left)
756 output-dir
757 (if (string-match "\\`--output-dir=" output-dir)
758 (prog1
759 (substring output-dir (match-end 0))
760 (setq command-line-args-left (cdr command-line-args-left)))))
761 (setq auto-mode-alist
762 (delete (cons (concat "\\." muse-file-extension "\\'")
763 'muse-mode-choose-mode)
764 auto-mode-alist))
765 (dolist (file command-line-args-left)
766 (muse-publish-file file style output-dir t))))
768 ;; Default publishing rules
770 (defun muse-publish-section-close (depth)
771 "Seach forward for the closing tag of given DEPTH."
772 (let (not-end)
773 (save-excursion
774 (while (and (setq not-end (re-search-forward
775 (concat "^\\*\\{1," (number-to-string depth)
776 "\\}\\s-+")
777 nil t))
778 (get-text-property (match-beginning 0) 'read-only)))
779 (if not-end
780 (forward-line 0)
781 (goto-char (point-max)))
782 (cond ((not (eq (char-before) ?\n))
783 (insert "\n\n"))
784 ((not (eq (char-before (1- (point))) ?\n))
785 (insert "\n")))
786 (muse-insert-markup (muse-markup-text 'section-close depth))
787 (insert "\n"))))
789 (defun muse-publish-markup-directive (&optional name value)
790 (unless name (setq name (match-string 1)))
791 (unless value (setq value (match-string 2)))
792 (let ((elem (assoc name muse-publishing-directives)))
793 (if elem
794 (setcdr elem value)
795 (setq muse-publishing-directives
796 (cons (cons name value)
797 muse-publishing-directives))))
798 ;; Make sure we don't ever try to move the point forward (past the
799 ;; beginning of buffer) while we're still searching for directives.
800 (setq muse-publishing-last-position nil)
801 (delete-region (match-beginning 0) (match-end 0)))
803 (defsubst muse-publishing-directive (name)
804 (cdr (assoc name muse-publishing-directives)))
806 (defun muse-publish-markup-anchor ()
807 (unless (get-text-property (match-end 1) 'muse-link)
808 (let ((text (muse-markup-text 'anchor (match-string 2))))
809 (unless (string= text "")
810 (save-match-data
811 (skip-chars-forward (concat muse-regexp-blank "\n"))
812 (muse-insert-markup text)))
813 (match-string 1))))
815 (defun muse-publish-markup-comment ()
816 (if (null muse-publish-comments-p)
818 (goto-char (match-end 0))
819 (muse-insert-markup (muse-markup-text 'comment-end))
820 (if (match-beginning 1)
821 (progn
822 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
823 (delete-region (match-beginning 0) (match-beginning 1)))
824 (delete-region (match-beginning 0) (match-end 0)))
825 (goto-char (match-beginning 0))
826 (muse-insert-markup (muse-markup-text 'comment-begin))))
828 (defvar muse-inhibit-style-tags nil
829 "If non-nil, do not search for style-specific tags.
830 This is used when publishing headers and footers.")
832 (defun muse-publish-markup-tag ()
833 (let ((tag-info (if muse-inhibit-style-tags
834 (assoc (match-string 1) muse-publish-markup-tags)
835 (muse-markup-tag-info (match-string 1)))))
836 (when (and tag-info
837 (not (get-text-property (match-beginning 0) 'read-only)))
838 (let ((closed-tag (match-string 3))
839 (start (match-beginning 0))
840 (beg (point))
841 end attrs)
842 (when (nth 2 tag-info)
843 (let ((attrstr (match-string 2)))
844 (while (and attrstr
845 (string-match (concat "\\([^"
846 muse-regexp-blank
847 "=\n]+\\)\\(=\"\\"
848 "([^\"]+\\)\"\\)?")
849 attrstr))
850 (let ((attr (cons (downcase
851 (muse-match-string-no-properties 1 attrstr))
852 (muse-match-string-no-properties 3 attrstr))))
853 (setq attrstr (replace-match "" t t attrstr))
854 (if attrs
855 (nconc attrs (list attr))
856 (setq attrs (list attr)))))))
857 (if (and (cadr tag-info) (not closed-tag))
858 (if (muse-goto-tag-end (car tag-info) (nth 3 tag-info))
859 (delete-region (match-beginning 0) (point))
860 (setq tag-info nil)))
861 (when tag-info
862 (setq end (point-marker))
863 (delete-region start beg)
864 (goto-char start)
865 (let ((args (list start end)))
866 (if (nth 2 tag-info)
867 (nconc args (list attrs)))
868 (let ((muse-inhibit-style-tags nil))
869 ;; remove the inhibition
870 (apply (nth 4 tag-info) args)))))))
871 nil)
873 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
874 "Escape specials from BEG to END using style-specific :specials.
875 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
876 CONTEXT is used to figure out what kind of specials to escape.
878 The following contexts exist in Muse.
879 'underline _underlined text_
880 'literal =monospaced text= or <code> region (monospaced, escaped)
881 'emphasis *emphasized text*
882 'email email@example.com
883 'url http://example.com
884 'url-desc [[...][description of an explicit link]]
885 'image [[image.png]]
886 'example <example> region (monospaced, block context, escaped)
887 'verbatim <verbatim> region (escaped)
888 'document normal text"
889 (let ((specials (muse-style-element :specials nil t)))
890 (cond ((functionp specials)
891 (setq specials (funcall specials context)))
892 ((symbolp specials)
893 (setq specials (symbol-value specials))))
894 (if (functionp specials)
895 (funcall specials beg end ignore-read-only)
896 (save-excursion
897 (save-restriction
898 (narrow-to-region beg end)
899 (goto-char (point-min))
900 (while (< (point) (point-max))
901 (if (and (not ignore-read-only)
902 (get-text-property (point) 'read-only))
903 (goto-char (or (next-single-property-change (point) 'read-only)
904 (point-max)))
905 (let ((repl (or (assoc (char-after) specials)
906 (assoc (char-after)
907 muse-publish-markup-specials))))
908 (if (null repl)
909 (forward-char 1)
910 (delete-char 1)
911 (insert-before-markers (cdr repl)))))))))))
913 (defun muse-publish-markup-word ()
914 (let* ((beg (match-beginning 2))
915 (end (1- (match-end 2)))
916 (leader (buffer-substring-no-properties beg end))
917 open-tag close-tag mark-read-only loc context)
918 (cond
919 ((string= leader "_")
920 (setq context 'underline
921 open-tag (muse-markup-text 'begin-underline)
922 close-tag (muse-markup-text 'end-underline)))
923 ((string= leader "=")
924 (setq context 'literal
925 open-tag (muse-markup-text 'begin-literal)
926 close-tag (muse-markup-text 'end-literal))
927 (setq mark-read-only t))
929 (let ((l (length leader)))
930 (setq context 'emphasis)
931 (cond
932 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
933 close-tag (muse-markup-text 'end-emph)))
934 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
935 close-tag (muse-markup-text 'end-more-emph)))
936 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
937 close-tag (muse-markup-text 'end-most-emph)))
938 (t (setq context nil))))))
939 (if (and context
940 (not (get-text-property beg 'muse-link))
941 (setq loc (search-forward leader nil t))
942 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
943 (not (eq (char-syntax (char-before (point))) ?\ ))
944 (not (get-text-property (point) 'muse-link)))
945 (progn
946 (replace-match "")
947 (delete-region beg end)
948 (setq end (point-marker))
949 (muse-insert-markup close-tag)
950 (goto-char beg)
951 (muse-insert-markup open-tag)
952 (setq beg (point))
953 (when mark-read-only
954 (muse-publish-escape-specials beg end t context)
955 (muse-publish-mark-read-only beg end)))
956 (backward-char))
957 nil))
959 (defun muse-publish-markup-emdash ()
960 (unless (get-text-property (match-beginning 0) 'muse-link)
961 (let ((prespace (match-string 1))
962 (postspace (match-string 2)))
963 (delete-region (match-beginning 0) (match-end 0))
964 (muse-insert-markup (muse-markup-text 'emdash prespace postspace))
965 (when (eq (char-after) ?\<)
966 (insert ?\n)))))
968 (defun muse-publish-markup-enddots ()
969 (unless (get-text-property (match-beginning 0) 'muse-link)
970 (delete-region (match-beginning 0) (match-end 0))
971 (muse-insert-markup (muse-markup-text 'enddots))))
973 (defun muse-publish-markup-dots ()
974 (unless (get-text-property (match-beginning 0) 'muse-link)
975 (delete-region (match-beginning 0) (match-end 0))
976 (muse-insert-markup (muse-markup-text 'dots))))
978 (defun muse-publish-markup-rule ()
979 (unless (get-text-property (match-beginning 0) 'muse-link)
980 (delete-region (match-beginning 0) (match-end 0))
981 (muse-insert-markup (muse-markup-text 'rule))))
983 (defun muse-publish-markup-no-break-space ()
984 (unless (get-text-property (match-beginning 0) 'muse-link)
985 (delete-region (match-beginning 0) (match-end 0))
986 (muse-insert-markup (muse-markup-text 'no-break-space))))
988 (defun muse-publish-markup-heading ()
989 (let* ((len (length (match-string 1)))
990 (start (muse-markup-text
991 (cond ((= len 1) 'section)
992 ((= len 2) 'subsection)
993 ((= len 3) 'subsubsection)
994 (t 'section-other))
995 len))
996 (end (muse-markup-text
997 (cond ((= len 1) 'section-end)
998 ((= len 2) 'subsection-end)
999 ((= len 3) 'subsubsection-end)
1000 (t 'section-other-end))
1001 len)))
1002 (delete-region (match-beginning 0) (match-end 0))
1003 (muse-insert-markup start)
1004 (end-of-line)
1005 (when end
1006 (muse-insert-markup end))
1007 (forward-line 1)
1008 (unless (eq (char-after) ?\n)
1009 (insert "\n"))
1010 (muse-publish-section-close len)))
1012 (defvar muse-publish-footnotes nil)
1014 (defun muse-publish-markup-footnote ()
1015 "Scan ahead and snarf up the footnote body"
1016 (cond
1017 ((get-text-property (match-beginning 0) 'muse-link)
1018 nil)
1019 ((= (muse-line-beginning-position) (match-beginning 0))
1022 (let ((footnote (save-match-data
1023 (string-to-number (match-string 1))))
1024 (oldtext (match-string 0))
1025 footnotemark)
1026 (delete-region (match-beginning 0) (match-end 0))
1027 (save-excursion
1028 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
1029 (let* ((start (match-beginning 0))
1030 (beg (goto-char (match-end 0)))
1031 (end (save-excursion
1032 (if (search-forward "\n\n" nil t)
1033 (copy-marker (match-beginning 0))
1034 (goto-char (point-max))
1035 (skip-chars-backward "\n")
1036 (point-marker)))))
1037 (while (re-search-forward
1038 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
1039 end t)
1040 (replace-match "\\1" t))
1041 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
1042 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
1043 (if (string= "" footnotemark-cmd)
1044 (setq footnotemark
1045 (concat (muse-markup-text 'footnote)
1046 (buffer-substring-no-properties beg end)
1047 (muse-markup-text 'footnote-end)))
1048 (setq footnotemark (format footnotemark-cmd footnote
1049 footnotemark-end-cmd))
1050 (unless muse-publish-footnotes
1051 (set (make-local-variable 'muse-publish-footnotes)
1052 (make-vector 256 nil)))
1053 (unless (aref muse-publish-footnotes footnote)
1054 (setq footnotemark
1055 (concat
1056 footnotemark
1057 (concat (format (muse-markup-text 'footnotetext)
1058 footnote)
1059 (buffer-substring-no-properties beg end)
1060 (muse-markup-text 'footnotetext-end))))
1061 (aset muse-publish-footnotes footnote footnotemark))))
1062 (goto-char end)
1063 (skip-chars-forward "\n")
1064 (delete-region start (point)))))
1065 (if footnotemark
1066 (muse-insert-markup footnotemark)
1067 (insert oldtext))))))
1069 (defun muse-publish-markup-fn-sep ()
1070 (delete-region (match-beginning 0) (match-end 0))
1071 (muse-insert-markup (muse-markup-text 'fn-sep)))
1073 (defun muse-insert-markup-end-list (&rest args)
1074 (let ((beg (point)))
1075 (apply 'insert args)
1076 (add-text-properties beg (point) '(end-list t))
1077 (muse-publish-mark-read-only beg (point))))
1079 (defun muse-publish-determine-dl-indent (continue indent-sym determine-sym)
1080 ;; If the caller doesn't know how much indentation to use, figure it
1081 ;; out ourselves. It is assumed that `muse-forward-list-item' has
1082 ;; been called just before this to set the match data.
1083 (when (and continue
1084 (symbol-value determine-sym))
1085 (save-match-data
1086 ;; snarf all leading whitespace
1087 (let ((indent (and (match-beginning 2)
1088 (buffer-substring (match-beginning 1)
1089 (match-beginning 2)))))
1090 (when (and indent
1091 (not (string= indent "")))
1092 (set indent-sym indent)
1093 (set determine-sym nil))))))
1095 (defun muse-publish-surround-dl (indent post-indent)
1096 (let* ((beg-item (muse-markup-text 'begin-dl-item))
1097 (end-item (muse-markup-text 'end-dl-item))
1098 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
1099 (end-ddt (muse-markup-text 'end-ddt))
1100 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
1101 (end-dde (muse-markup-text 'end-dde))
1102 (continue t)
1103 def-on-same-line beg)
1104 (while continue
1105 ;; envelope this as one term+definitions unit -- HTML does not
1106 ;; need this, but DocBook and Muse's custom XML format do
1107 (muse-insert-markup beg-item)
1108 (when (looking-at muse-dl-term-regexp)
1109 ;; find the term and wrap it with published markup
1110 (setq beg (point))
1111 (goto-char (match-end 1))
1112 (delete-region (point) (match-end 0))
1113 (muse-insert-markup-end-list end-ddt)
1114 ;; if definition is immediately after term, move to next line
1115 (unless (eq (char-after) ?\n)
1116 (insert ?\n))
1117 (save-excursion
1118 (goto-char beg)
1119 (delete-region (point) (match-beginning 1))
1120 (muse-insert-markup beg-ddt)))
1121 (setq beg (point)
1122 ;; move past current item
1123 continue (muse-forward-list-item 'dl-term indent))
1124 (save-restriction
1125 (narrow-to-region beg (point))
1126 (goto-char (point-min))
1127 ;; publish each definition that we find, defaulting to an
1128 ;; empty definition if none are found
1129 (muse-publish-surround-text beg-dde end-dde
1130 (lambda (indent)
1131 (muse-forward-list-item 'dl-entry indent))
1132 indent post-indent
1133 #'muse-publish-determine-dl-indent)
1134 (goto-char (point-max))
1135 (skip-chars-backward (concat muse-regexp-blank "\n"))
1136 (muse-insert-markup-end-list end-item)
1137 (when continue
1138 (goto-char (point-max)))))))
1140 (defun muse-publish-strip-list-indentation (list-item empty-line indent post-indent)
1141 (let ((list-nested nil)
1142 (indent-found nil))
1143 (while (< (point) (point-max))
1144 (when (and (looking-at list-item)
1145 (not (or (get-text-property
1146 (muse-list-item-critical-point) 'read-only)
1147 (get-text-property
1148 (muse-list-item-critical-point) 'muse-link))))
1149 ;; if we encounter a list item, allow no post-indent space
1150 (setq list-nested t))
1151 (when (and (not (looking-at empty-line))
1152 (looking-at (concat indent "\\("
1153 (or (and list-nested "")
1154 post-indent)
1155 "\\)")))
1156 ;; if list is not nested, remove indentation
1157 (unless indent-found
1158 (setq post-indent (match-string 1)
1159 indent-found t))
1160 (replace-match ""))
1161 (forward-line 1))))
1163 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent determine-indent-func list-item)
1164 (unless list-item
1165 (setq list-item (format muse-list-item-regexp
1166 (concat "[" muse-regexp-blank "]*"))))
1167 (let ((continue t)
1168 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
1169 (determine-indent (if determine-indent-func t nil))
1170 (new-indent indent)
1171 (first t)
1172 beg)
1173 (unless indent
1174 (setq indent (concat "[" muse-regexp-blank "]+")))
1175 (if post-indent
1176 (setq post-indent (concat " \\{0," (number-to-string post-indent)
1177 "\\}"))
1178 (setq post-indent ""))
1179 (while continue
1180 (if (or (not end-tag) (string= end-tag ""))
1181 ;; if no end of list item markup exists, treat the beginning
1182 ;; of list item markup as it if it were the end -- this
1183 ;; prevents multiple-level lists from being confused
1184 (muse-insert-markup-end-list beg-tag)
1185 (muse-insert-markup beg-tag))
1186 (setq beg (point)
1187 ;; move past current item; continue is non-nil if there
1188 ;; are more like items to be processed
1189 continue (if (and determine-indent-func first)
1190 (funcall move-func (concat indent post-indent))
1191 (funcall move-func indent)))
1192 (when determine-indent-func
1193 (funcall determine-indent-func continue 'new-indent 'determine-indent))
1194 (when continue
1195 ;; remove list markup if we encountered another item of the
1196 ;; same type
1197 (replace-match "" t t nil 1))
1198 (save-restriction
1199 (narrow-to-region beg (point))
1200 ;; narrow to current item
1201 (goto-char (point-min))
1202 (forward-line 1)
1203 (muse-publish-strip-list-indentation list-item empty-line
1204 indent post-indent)
1205 (skip-chars-backward (concat muse-regexp-blank "\n"))
1206 (muse-insert-markup-end-list end-tag)
1207 (when determine-indent-func
1208 (setq indent new-indent))
1209 (when first
1210 (setq first nil))
1211 (when continue
1212 (goto-char (point-max)))))))
1214 (defun muse-publish-markup-list ()
1215 "Markup a list entry.
1216 This function works by marking up items of the same list level
1217 and type, respecting the end-of-list property."
1218 (let* ((str (match-string 1))
1219 (type (muse-list-item-type str))
1220 (indent (buffer-substring (muse-line-beginning-position)
1221 (match-beginning 1)))
1222 (post-indent (length str))
1223 (last (match-beginning 0)))
1224 (cond
1225 ((or (get-text-property (muse-list-item-critical-point) 'read-only)
1226 (get-text-property (muse-list-item-critical-point) 'muse-link))
1227 nil)
1228 ((eq type 'ul)
1229 (unless (eq (char-after (match-end 1)) ?-)
1230 (delete-region (match-beginning 0) (match-end 0))
1231 (muse-insert-markup (muse-markup-text 'begin-uli))
1232 (save-excursion
1233 (muse-publish-surround-text
1234 (muse-markup-text 'begin-uli-item)
1235 (muse-markup-text 'end-uli-item)
1236 (lambda (indent)
1237 (muse-forward-list-item 'ul indent))
1238 indent post-indent)
1239 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1240 (forward-line 1)))
1241 ((eq type 'ol)
1242 (delete-region (match-beginning 0) (match-end 0))
1243 (muse-insert-markup (muse-markup-text 'begin-oli))
1244 (save-excursion
1245 (muse-publish-surround-text
1246 (muse-markup-text 'begin-oli-item)
1247 (muse-markup-text 'end-oli-item)
1248 (lambda (indent)
1249 (muse-forward-list-item 'ol indent))
1250 indent post-indent)
1251 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1252 (forward-line 1))
1253 ((not (string= (match-string 2) ""))
1254 ;; must have an initial term
1255 (goto-char (match-beginning 0))
1256 (muse-insert-markup (muse-markup-text 'begin-dl))
1257 (save-excursion
1258 (muse-publish-surround-dl indent post-indent)
1259 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1260 (forward-line 1))))
1261 nil)
1263 (defun muse-publish-markup-quote ()
1264 "Markup a quoted paragraph.
1265 The reason this function is so funky, is to prevent text properties
1266 like read-only from being inadvertently deleted."
1267 (let* ((ws (match-string 1))
1268 (centered (>= (string-width ws) 6))
1269 (begin-elem (if centered 'begin-center 'begin-quote-item))
1270 (end-elem (if centered 'end-center 'end-quote-item)))
1271 (replace-match "" t t nil 1)
1272 (unless centered
1273 (muse-insert-markup (muse-markup-text 'begin-quote)))
1274 (muse-publish-surround-text (muse-markup-text begin-elem)
1275 (muse-markup-text end-elem)
1276 (function (lambda (indent)
1277 (muse-forward-paragraph)
1278 nil)))
1279 (unless centered
1280 (muse-insert-markup (muse-markup-text 'end-quote)))))
1282 (defun muse-publish-markup-leading-space (markup-space multiple)
1283 (let (count)
1284 (when (and markup-space
1285 (>= (setq count (skip-chars-forward " ")) 0))
1286 (delete-region (muse-line-beginning-position) (point))
1287 (while (> count 0)
1288 (muse-insert-markup markup-space)
1289 (setq count (- count multiple))))))
1291 (defun muse-publish-markup-verse ()
1292 (let ((leader (match-string 0)))
1293 (goto-char (match-beginning 0))
1294 (muse-insert-markup (muse-markup-text 'begin-verse))
1295 (while (looking-at leader)
1296 (replace-match "")
1297 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1298 (let ((beg (point)))
1299 (end-of-line)
1300 (cond
1301 ((bolp)
1302 (let ((text (muse-markup-text 'empty-verse-line)))
1303 (when text (muse-insert-markup text))))
1304 ((save-excursion
1305 (save-match-data
1306 (forward-line 1)
1307 (or (looking-at (concat leader "["
1308 muse-regexp-blank
1309 "]*$"))
1310 (not (looking-at leader)))))
1311 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1312 (end-text (muse-markup-text 'end-last-stanza-line)))
1313 (when end-text (muse-insert-markup end-text))
1314 (goto-char beg)
1315 (when begin-text (muse-insert-markup begin-text))
1316 (end-of-line)))
1318 (let ((begin-text (muse-markup-text 'begin-verse-line))
1319 (end-text (muse-markup-text 'end-verse-line)))
1320 (when end-text (muse-insert-markup end-text))
1321 (goto-char beg)
1322 (when begin-text (muse-insert-markup begin-text))
1323 (end-of-line))))
1324 (forward-line 1))))
1325 (muse-insert-markup (muse-markup-text 'end-verse))
1326 (insert ?\n))
1328 (defun muse-publish-table-fields (beg end)
1329 "Parse given region as a table, returning a cons cell.
1330 The car is the length of the longest row.
1332 The cdr is a list of the fields of the table, with the first
1333 element indicating the type of the row:
1334 1: body, 2: header, 3: footer.
1336 The existing region will be removed, except for initial blank lines."
1337 (unless (muse-publishing-directive "disable-tables")
1338 (let ((longest 0)
1339 (left 0)
1340 fields field-list)
1341 (save-restriction
1342 (narrow-to-region beg end)
1343 (goto-char (point-min))
1344 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1345 (forward-line 1))
1346 (setq beg (point))
1347 (while (= left 0)
1348 (when (looking-at muse-table-line-regexp)
1349 (setq fields (cons (length (match-string 1))
1350 (mapcar #'muse-trim-whitespace
1351 (split-string (match-string 0)
1352 muse-table-field-regexp)))
1353 field-list (cons fields field-list)
1354 longest (max (length fields) longest)))
1355 (setq left (forward-line 1))))
1356 (delete-region beg end)
1357 (if (= longest 0)
1358 (cons 0 nil)
1359 (cons (1- longest) (nreverse field-list))))))
1361 (defun muse-publish-markup-table ()
1362 "Style does not support tables.")
1364 (defun muse-publish-escape-specials-in-string (string &optional context)
1365 "Escape specials in STRING using style-specific :specials.
1366 CONTEXT is used to figure out what kind of specials to escape.
1368 See the documentation of the `muse-publish-escape-specials'
1369 function for the list of available contexts."
1370 (unless string
1371 (setq string ""))
1372 (let ((specials (muse-style-element :specials nil t)))
1373 (cond ((functionp specials)
1374 (setq specials (funcall specials context)))
1375 ((symbolp specials)
1376 (setq specials (symbol-value specials))))
1377 (if (functionp specials)
1378 (funcall specials string)
1379 (apply (function concat)
1380 (mapcar
1381 (lambda (ch)
1382 (let ((repl (or (assoc ch specials)
1383 (assoc ch muse-publish-markup-specials))))
1384 (if (null repl)
1385 (char-to-string ch)
1386 (cdr repl))))
1387 (append string nil))))))
1389 (defun muse-publish-markup-email ()
1390 (let* ((beg (match-end 1))
1391 (addr (buffer-substring-no-properties beg (match-end 0))))
1392 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1393 (goto-char beg)
1394 (delete-region beg (match-end 0))
1395 (if (or (eq (char-before (match-beginning 0)) ?\")
1396 (eq (char-after (match-end 0)) ?\"))
1397 (insert addr)
1398 (insert (format (muse-markup-text 'email-addr) addr addr)))
1399 (muse-publish-mark-read-only beg (point))))
1401 (defun muse-publish-classify-url (target)
1402 "Transform anchors and get published name, if TARGET is a page.
1403 The return value is a cons cell. The car is the type of link,
1404 the cadr is the page name, and the cddr is the anchor."
1405 (save-match-data
1406 (cond ((or (null target) (string= target ""))
1407 nil)
1408 ((string-match muse-image-regexp target)
1409 (cons 'image (cons target nil)))
1410 ((string-match muse-url-regexp target)
1411 (cons 'url (cons target nil)))
1412 ((string-match muse-file-regexp target)
1413 (cons 'file (cons target nil)))
1414 ((string-match "#" target)
1415 (if (eq (aref target 0) ?\#)
1416 (cons 'anchor-ref (cons nil (substring target 1)))
1417 (cons 'link-and-anchor
1418 (cons (muse-publish-link-page
1419 (substring target 0 (match-beginning 0)))
1420 (substring target (match-end 0))))))
1422 (cons 'link (cons (muse-publish-link-page target) nil))))))
1424 (defun muse-publish-url-desc (desc explicit)
1425 (when desc
1426 (dolist (transform muse-publish-desc-transforms)
1427 (setq desc (save-match-data
1428 (when desc (funcall transform desc explicit)))))
1429 (setq desc (muse-link-unescape desc))
1430 (muse-publish-escape-specials-in-string desc 'url-desc)))
1432 (defun muse-publish-url (url &optional desc orig-url explicit)
1433 "Resolve a URL into its final <a href> form."
1434 (let (type anchor)
1435 (dolist (transform muse-publish-url-transforms)
1436 (setq url (save-match-data (when url (funcall transform url explicit)))))
1437 (if desc
1438 (setq desc (muse-publish-url-desc desc explicit))
1439 (if orig-url
1440 (setq orig-url (muse-publish-url-desc orig-url explicit))))
1441 (let ((target (muse-publish-classify-url url)))
1442 (setq type (car target)
1443 url (if (eq type 'image)
1444 (muse-publish-escape-specials-in-string (cadr target)
1445 'image)
1446 (muse-publish-escape-specials-in-string (cadr target) 'url))
1447 anchor (muse-publish-escape-specials-in-string
1448 (cddr target) 'url)))
1449 (cond ((eq type 'anchor-ref)
1450 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1451 ((string= url "")
1452 desc)
1453 ((eq type 'image)
1454 (let ((ext (or (file-name-extension url) "")))
1455 (setq url (muse-path-sans-extension url))
1456 (if desc
1457 (muse-markup-text 'image-with-desc url ext desc)
1458 (muse-markup-text 'image url ext))))
1459 ((eq type 'link-and-anchor)
1460 (muse-markup-text 'link-and-anchor url anchor
1461 (or desc orig-url)))
1462 ((and desc (string-match muse-image-regexp desc))
1463 (let ((ext (or (file-name-extension desc) "")))
1464 (setq desc (muse-path-sans-extension desc))
1465 (muse-markup-text 'image-link url desc ext)))
1466 ((eq type 'link)
1467 (muse-markup-text 'link url (or desc orig-url)))
1469 (or (and (or desc
1470 (not (string= url orig-url)))
1471 (let ((text (muse-markup-text 'url-and-desc url
1472 (or desc orig-url))))
1473 (and (not (string= text ""))
1474 text)))
1475 (muse-markup-text 'url url (or desc orig-url)))))))
1477 (defun muse-publish-insert-url (url &optional desc orig-url explicit)
1478 "Resolve a URL into its final <a href> form."
1479 (delete-region (match-beginning 0) (match-end 0))
1480 (let ((text (muse-publish-url url desc orig-url explicit)))
1481 (when text
1482 (muse-insert-markup text))))
1484 (defun muse-publish-markup-link ()
1485 (let (desc explicit orig-link link)
1486 (setq explicit (save-match-data
1487 (if (string-match muse-explicit-link-regexp
1488 (match-string 0))
1489 t nil)))
1490 (setq orig-link (if explicit (match-string 1) (match-string 0)))
1491 (setq desc (when explicit (match-string 2)))
1492 (setq link (if explicit
1493 (muse-handle-explicit-link orig-link)
1494 (muse-handle-implicit-link orig-link)))
1495 (when (and link
1496 (or explicit
1497 (not (or (eq (char-before (match-beginning 0)) ?\")
1498 (eq (char-after (match-end 0)) ?\")))))
1499 ;; if explicit link has no user-provided description, treat it
1500 ;; as if it were an implicit link
1501 (when (and explicit (not desc))
1502 (setq explicit nil))
1503 (muse-publish-insert-url link desc orig-link explicit))))
1505 (defun muse-publish-markup-url ()
1506 (unless (or (eq (char-before (match-beginning 0)) ?\")
1507 (eq (char-after (match-end 0)) ?\"))
1508 (let ((url (match-string 0)))
1509 (muse-publish-insert-url url nil url))))
1511 ;; Default publishing tags
1513 (defcustom muse-publish-contents-depth 2
1514 "The number of heading levels to include with <contents> tags."
1515 :type 'integer
1516 :group 'muse-publish)
1518 (defmacro muse-publish-ensure-block-tag (beg)
1519 "Ensure that block-level tag at BEG is published with at least one
1520 blank line. BEG is modified to be the new position."
1521 `(progn
1522 (goto-char ,beg)
1523 (cond ((not (bolp)) (insert "\n\n"))
1524 ((eq (point) (point-min)) nil)
1525 ((prog2 (backward-char) (bolp) (forward-char)) nil)
1526 (t (insert "\n")))
1527 (setq ,beg (point))))
1529 (defun muse-publish-contents-tag (beg end attrs)
1530 (set (make-local-variable 'muse-publish-generate-contents)
1531 (cons (copy-marker (point) t)
1532 (let ((depth (cdr (assoc "depth" attrs))))
1533 (or (and depth (string-to-number depth))
1534 muse-publish-contents-depth)))))
1536 (defun muse-publish-verse-tag (beg end)
1537 (save-excursion
1538 (save-restriction
1539 (narrow-to-region beg end)
1540 (goto-char (point-min))
1541 (while (eq ?\ (char-syntax (char-after)))
1542 (delete-char 1))
1543 (while (< (point) (point-max))
1544 (insert "> ")
1545 (forward-line))
1546 (if (eq ?\ (char-syntax (char-before)))
1547 (delete-char -1)))))
1549 (defun muse-publish-mark-read-only (beg end)
1550 "Add read-only properties to the given region."
1551 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1552 nil)
1554 (defun muse-publish-mark-link (&optional beg end)
1555 "Indicate that the given region is a Muse link, so that other
1556 markup elements respect it. If a region is not specified, use
1557 the 0th match data to determine it.
1559 This is usually applied to explicit links."
1560 (unless beg (setq beg (match-beginning 0)))
1561 (unless end (setq end (match-end 0)))
1562 (add-text-properties beg end '(muse-link t))
1563 nil)
1565 (defun muse-publish-quote-tag (beg end)
1566 (save-excursion
1567 (save-restriction
1568 (narrow-to-region beg end)
1569 (let ((quote-regexp "^\\(<\\(/?\\)quote>\\)"))
1570 (muse-insert-markup (muse-markup-text 'begin-quote))
1571 (while (progn
1572 (unless (looking-at (concat "[" muse-regexp-blank "\n]*"
1573 "<quote>"))
1574 (muse-publish-surround-text
1575 (muse-markup-text 'begin-quote-item)
1576 (muse-markup-text 'end-quote-item)
1577 (function
1578 (lambda (indent)
1579 (muse-forward-paragraph)
1580 (goto-char (match-end 0))
1581 (and (< (point) (point-max))
1582 (not (looking-at quote-regexp)))))
1583 nil nil nil
1584 quote-regexp))
1585 (if (>= (point) (point-max))
1587 (and (search-forward "<quote>" nil t)
1588 (muse-goto-tag-end "quote" t)
1589 (progn (forward-line 1) t)
1590 (< (point) (point-max))))))
1591 (goto-char (point-max))
1592 (muse-insert-markup (muse-markup-text 'end-quote))))))
1594 (defun muse-publish-code-tag (beg end)
1595 (muse-publish-escape-specials beg end nil 'literal)
1596 (goto-char beg)
1597 (insert (muse-markup-text 'begin-literal))
1598 (goto-char end)
1599 (insert (muse-markup-text 'end-literal))
1600 (muse-publish-mark-read-only beg (point)))
1602 (defun muse-publish-src-tag (beg end attrs)
1603 (muse-publish-example-tag beg end))
1605 (defun muse-publish-example-tag (beg end)
1606 (muse-publish-ensure-block-tag beg)
1607 (muse-publish-escape-specials beg end nil 'example)
1608 (goto-char beg)
1609 (insert (muse-markup-text 'begin-example))
1610 (goto-char end)
1611 (insert (muse-markup-text 'end-example))
1612 (muse-publish-mark-read-only beg (point)))
1614 (defun muse-publish-literal-tag (beg end attrs)
1615 "Ensure that the text between BEG and END is not interpreted later on.
1617 ATTRS is an alist of attributes.
1619 If it contains a \"style\" element, delete the region if the
1620 current style is neither derived from nor equal to this style.
1622 If it contains both a \"style\" element and an \"exact\" element
1623 with the value \"t\", delete the region only if the current style
1624 is exactly this style."
1625 (let* ((style (cdr (assoc "style" attrs)))
1626 (exact (cdr (assoc "exact" attrs)))
1627 (exactp (and (stringp exact) (string= exact "t"))))
1628 (if (or (not style)
1629 (and exactp (equal (muse-style style)
1630 muse-publishing-current-style))
1631 (and (not exactp) (muse-style-derived-p style)))
1632 (muse-publish-mark-read-only beg end)
1633 (delete-region beg end))))
1635 (defun muse-publish-verbatim-tag (beg end)
1636 (muse-publish-escape-specials beg end nil 'verbatim)
1637 (muse-publish-mark-read-only beg end))
1639 (defalias 'muse-publish-class-tag 'ignore)
1641 (defun muse-publish-call-tag-on-buffer (tag &optional attrs)
1642 "Transform the current buffer as if it were surrounded by the tag TAG.
1643 If attributes ATTRS are given, pass them to the tag function."
1644 (let ((tag-info (if muse-inhibit-style-tags
1645 (assoc tag muse-publish-markup-tags)
1646 (muse-markup-tag-info tag))))
1647 (when tag-info
1648 (let* ((end (progn (goto-char (point-max)) (point-marker)))
1649 (args (list (point-min) end))
1650 (muse-inhibit-style-tags nil))
1651 (when (nth 2 tag-info)
1652 (nconc args (list attrs)))
1653 (apply (nth 4 tag-info) args)))))
1655 (defun muse-publish-examplify-buffer (&optional attrs)
1656 "Transform the current buffer as if it were an <example> region."
1657 (muse-publish-call-tag-on-buffer "example" attrs))
1659 (defun muse-publish-srcify-buffer (&optional attrs)
1660 "Transform the current buffer as if it were a <src> region."
1661 (muse-publish-call-tag-on-buffer "src" attrs))
1663 (defun muse-publish-versify-buffer (&optional attrs)
1664 "Transform the current buffer as if it were a <verse> region."
1665 (muse-publish-call-tag-on-buffer "verse" attrs)
1666 (muse-publish-markup ""
1667 `((100 ,(concat "^[" muse-regexp-blank "]*> ") 0
1668 muse-publish-markup-verse)))
1669 (goto-char (point-min)))
1671 (defmacro muse-publish-get-and-delete-attr (attr attrs)
1672 "Delete attribute ATTR from ATTRS only once, destructively.
1674 This function returns the matching attribute value, if found."
1675 (let ((last (make-symbol "last"))
1676 (found (make-symbol "found"))
1677 (vals (make-symbol "vals")))
1678 `(let ((,vals ,attrs))
1679 (if (string= (caar ,vals) ,attr)
1680 (prog1 (cdar ,vals)
1681 (setq ,attrs (cdr ,vals)))
1682 (let ((,last ,vals)
1683 (,found nil))
1684 (while ,vals
1685 (setq ,vals (cdr ,vals))
1686 (when (string= (caar ,vals) ,attr)
1687 (setq ,found (cdar ,vals))
1688 (setcdr ,last (cdr ,vals))
1689 (setq ,vals nil))
1690 (setq ,last ,vals))
1691 ,found)))))
1693 (defmacro muse-publish-markup-attribute (beg end attrs reinterp &rest body)
1694 "Evaluate BODY within the bounds of BEG and END.
1695 ATTRS is an alist. Only the \"markup\" element of ATTRS is acted
1698 If it is omitted, publish the region with the normal Muse rules.
1699 If RE-INTERP is specified, this is done immediately in a new
1700 publishing process. Currently, RE-INTERP is specified only by
1701 the <include> tag.
1703 If \"nil\", do not mark up the region at all, but prevent it from
1704 being further interpreted by Muse.
1706 If \"example\", treat the region as if it was surrounded by the
1707 <example> tag.
1709 If \"src\", treat the region as if it was surrounded by the
1710 <src> tag.
1712 If \"verse\", treat the region as if it was surrounded by the
1713 <verse> tag, to preserve newlines.
1715 Otherwise, it should be the name of a function to call in the
1716 narrowed region after evaluating BODY. The function should
1717 take the ATTRS parameter."
1718 (let ((markup (make-symbol "markup"))
1719 (markup-function (make-symbol "markup-function")))
1720 `(let ((,markup (muse-publish-get-and-delete-attr "markup" ,attrs)))
1721 (save-restriction
1722 (narrow-to-region ,beg ,end)
1723 (goto-char (point-min))
1724 ,@body
1725 (if (not ,markup)
1726 (when ,reinterp
1727 (muse-publish-markup-region (point-min) (point-max))
1728 (muse-publish-mark-read-only (point-min) (point-max))
1729 (goto-char (point-max)))
1730 (let ((,markup-function (read ,markup)))
1731 (cond ((eq ,markup-function 'example)
1732 (setq ,markup-function #'muse-publish-examplify-buffer))
1733 ((eq ,markup-function 'src)
1734 (setq ,markup-function #'muse-publish-srcify-buffer))
1735 ((eq ,markup-function 'verse)
1736 (setq ,markup-function #'muse-publish-versify-buffer))
1737 ((and ,markup-function (not (functionp ,markup-function)))
1738 (error "Invalid markup function `%s'" ,markup))
1739 (t nil))
1740 (if ,markup-function
1741 (funcall ,markup-function ,attrs)
1742 (muse-publish-mark-read-only (point-min) (point-max))
1743 (goto-char (point-max)))))))))
1745 (put 'muse-publish-markup-attribute 'lisp-indent-function 4)
1746 (put 'muse-publish-markup-attribute 'edebug-form-spec
1747 '(form form form form body))
1749 (defun muse-publish-lisp-tag (beg end attrs)
1750 (muse-publish-markup-attribute beg end attrs nil
1751 (save-excursion
1752 (let ((str (muse-eval-lisp
1753 (prog1
1754 (concat "(progn "
1755 (buffer-substring-no-properties (point-min)
1756 (point-max))
1757 ")")
1758 (delete-region beg end)))))
1759 (set-text-properties 0 (length str) nil str)
1760 (insert str)))))
1762 (defun muse-publish-command-tag (beg end attrs)
1763 (muse-publish-markup-attribute beg end attrs nil
1764 (while (looking-at "\\s-*$")
1765 (forward-line))
1766 (let ((interp (muse-publish-get-and-delete-attr "interp" attrs)))
1767 (if interp
1768 (shell-command-on-region (point) (point-max) interp t t)
1769 (shell-command
1770 (prog1
1771 (buffer-substring-no-properties (point) (point-max))
1772 (delete-region (point-min) (point-max)))
1773 t)))
1774 ;; make sure there is a newline at end
1775 (goto-char (point-max))
1776 (forward-line 0)
1777 (unless (looking-at "\\s-*$")
1778 (goto-char (point-max))
1779 (insert ?\n))
1780 (goto-char (point-min))))
1782 (defun muse-publish-perl-tag (beg end attrs)
1783 (muse-publish-command-tag beg end
1784 (cons (cons "interp" (executable-find "perl"))
1785 attrs)))
1787 (defun muse-publish-python-tag (beg end attrs)
1788 (muse-publish-command-tag beg end
1789 (cons (cons "interp" (executable-find "python"))
1790 attrs)))
1792 (defun muse-publish-ruby-tag (beg end attrs)
1793 (muse-publish-command-tag beg end
1794 (cons (cons "interp" (executable-find "ruby"))
1795 attrs)))
1797 (defun muse-publish-comment-tag (beg end)
1798 (if (null muse-publish-comments-p)
1799 (delete-region beg end)
1800 (goto-char end)
1801 (muse-insert-markup (muse-markup-text 'comment-end))
1802 (muse-publish-mark-read-only beg end)
1803 (goto-char beg)
1804 (muse-insert-markup (muse-markup-text 'comment-begin))))
1806 (defun muse-publish-include-tag (beg end attrs)
1807 "Include the named file at the current location during publishing.
1809 <include file=\"...\" markup=\"...\">
1811 The `markup' attribute controls how this file is marked up after
1812 being inserted. See `muse-publish-markup-attribute' for an
1813 explanation of how it works."
1814 (let ((filename (muse-publish-get-and-delete-attr "file" attrs))
1815 (muse-publishing-directives muse-publishing-directives))
1816 (if filename
1817 (setq filename (expand-file-name
1818 filename
1819 (file-name-directory muse-publishing-current-file)))
1820 (error "No file attribute specified in <include> tag"))
1821 (muse-publish-markup-attribute beg end attrs t
1822 (insert-file-contents filename))))
1824 (defun muse-publish-mark-up-tag (beg end attrs)
1825 "Run an Emacs Lisp function on the region delimted by this tag.
1827 <markup function=\"...\" style=\"...\" exact=\"...\">
1829 The optional \"function\" attribute controls how this section is
1830 marked up. If used, it should be the name of a function to call
1831 with the buffer narrowed to the delimited region. Note that no
1832 further marking-up will be performed on this region.
1834 If \"function\" is omitted, use the standard Muse markup function.
1835 This is useful for marking up content in headers and footers.
1837 The optional \"style\" attribute causes the region to be deleted
1838 if the current style is neither derived from nor equal to this
1839 style.
1841 If both a \"style\" attribute and an \"exact\" attribute are
1842 provided, and \"exact\" is \"t\", delete the region only if the
1843 current style is exactly this style."
1844 (let* ((style (cdr (assoc "style" attrs)))
1845 (exact (cdr (assoc "exact" attrs)))
1846 (exactp (and (stringp exact) (string= exact "t"))))
1847 (if (or (not style)
1848 (and exactp (equal (muse-style style)
1849 muse-publishing-current-style))
1850 (and (not exactp) (muse-style-derived-p style)))
1851 (let* ((function (cdr (assoc "function" attrs)))
1852 (muse-publishing-directives muse-publishing-directives)
1853 (markup-function (and function (intern function))))
1854 (if (and markup-function (functionp markup-function))
1855 (save-restriction
1856 (narrow-to-region beg end)
1857 (funcall markup-function)
1858 (goto-char (point-max)))
1859 (let ((muse-publish-inhibit-style-hooks t))
1860 (muse-publish-markup-region beg end)))
1861 (muse-publish-mark-read-only beg (point)))
1862 (delete-region beg end))))
1864 ;; Miscellaneous helper functions
1866 (defun muse-publish-strip-tags (string)
1867 "Remove all tags from the string."
1868 (while (string-match "<.*?>" string)
1869 (setq string (replace-match "" nil t string)))
1870 string)
1872 (defun muse-publish-markup-type (category default-func)
1873 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1874 (funcall (or rule default-func))))
1876 (defun muse-published-buffer-contents (buffer)
1877 (with-current-buffer buffer
1878 (goto-char (point-min))
1879 (let ((beg (and (search-forward "Emacs Muse begins here")
1880 (muse-line-end-position)))
1881 (end (and (search-forward "Emacs Muse ends here")
1882 (muse-line-beginning-position))))
1883 (buffer-substring-no-properties beg end))))
1885 (defun muse-published-contents (file)
1886 (when (file-readable-p file)
1887 (muse-with-temp-buffer
1888 (insert-file-contents file)
1889 (muse-published-buffer-contents (current-buffer)))))
1891 (defun muse-publish-transform-output
1892 (file temp-file output-path name gen-func &rest cleanup-exts)
1893 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1894 (setq file (muse-page-name file))
1895 (message "Generating %s output for %s..." name file)
1896 (if (not (funcall gen-func temp-file output-path))
1897 (message "Generating %s from %s...failed" name file)
1898 (message "Generating %s output for %s...done" name file)
1899 (muse-delete-file-if-exists temp-file)
1900 (dolist (ext cleanup-exts)
1901 (muse-delete-file-if-exists
1902 (expand-file-name (concat file ext)
1903 (file-name-directory output-path))))
1904 (message "Wrote %s" output-path)))
1906 (defun muse-publish-read-only (string)
1907 (let ((end (1- (length string))))
1908 (add-text-properties 0 end
1909 '(rear-nonsticky (read-only) read-only t)
1910 string)
1911 string))
1913 ;;; muse-publish.el ends here