muse-publish-region: Remove read-only properties after publishing
[muse-el.git] / lisp / muse-publish.el
blob76893f17d6694cc6d4fece38b0cf8c0d6fe4abec
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 (let ((inhibit-read-only t))
698 (remove-text-properties (point-min) (point-max)
699 '(rear-nonsticky nil read-only nil))))
700 (pop-to-buffer buf)))
702 ;;;###autoload
703 (defun muse-publish-file (file style &optional output-dir force)
704 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
705 If the argument FORCE is nil, each file is only published if it is
706 newer than the published version. If the argument FORCE is non-nil,
707 the file is published no matter what."
708 (interactive (cons (read-file-name "Publish file: ")
709 (muse-publish-get-info)))
710 (let ((style-name style))
711 (setq style (muse-style style))
712 (unless style
713 (error "There is no style '%s' defined" style-name)))
714 (let* ((output-path (muse-publish-output-file file output-dir style))
715 (output-suffix (muse-style-element :osuffix style))
716 (muse-publishing-current-file file)
717 (muse-publishing-current-output-path output-path)
718 (target (if output-suffix
719 (concat (muse-path-sans-extension output-path)
720 output-suffix)
721 output-path))
722 (threshhold (nth 7 (file-attributes file))))
723 (if (not threshhold)
724 (message "Please save %s before publishing" file)
725 (when (or force (file-newer-than-file-p file target))
726 (if (and muse-publish-report-threshhold
727 (> threshhold
728 muse-publish-report-threshhold))
729 (message "Publishing %s ..." file))
730 (muse-with-temp-buffer
731 (insert-file-contents file)
732 (muse-publish-markup-buffer (muse-page-name file) style)
733 (let ((backup-inhibited t))
734 (write-file output-path))
735 (muse-style-run-hooks :final style file output-path target))
736 t))))
738 ;;;###autoload
739 (defun muse-publish-this-file (style output-dir &optional force)
740 "Publish the currently-visited file.
741 Prompt for both the STYLE and OUTPUT-DIR if they are not
742 supplied."
743 (interactive (muse-publish-get-info))
744 (if buffer-file-name
745 (let ((muse-current-output-style (list :base (car style)
746 :path output-dir)))
747 (unless (muse-publish-file buffer-file-name style output-dir force)
748 (message (concat "The published version is up-to-date; use"
749 " C-u C-c C-T to force an update."))))
750 (message "This buffer is not associated with any file")))
752 (defun muse-batch-publish-files ()
753 "Publish Muse files in batch mode."
754 (let ((muse-batch-publishing-p t)
755 style output-dir)
756 (setq style (car command-line-args-left)
757 command-line-args-left (cdr command-line-args-left)
758 output-dir (car command-line-args-left)
759 output-dir
760 (if (string-match "\\`--output-dir=" output-dir)
761 (prog1
762 (substring output-dir (match-end 0))
763 (setq command-line-args-left (cdr command-line-args-left)))))
764 (setq auto-mode-alist
765 (delete (cons (concat "\\." muse-file-extension "\\'")
766 'muse-mode-choose-mode)
767 auto-mode-alist))
768 (dolist (file command-line-args-left)
769 (muse-publish-file file style output-dir t))))
771 ;; Default publishing rules
773 (defun muse-publish-section-close (depth)
774 "Seach forward for the closing tag of given DEPTH."
775 (let (not-end)
776 (save-excursion
777 (while (and (setq not-end (re-search-forward
778 (concat "^\\*\\{1," (number-to-string depth)
779 "\\}\\s-+")
780 nil t))
781 (get-text-property (match-beginning 0) 'read-only)))
782 (if not-end
783 (forward-line 0)
784 (goto-char (point-max)))
785 (cond ((not (eq (char-before) ?\n))
786 (insert "\n\n"))
787 ((not (eq (char-before (1- (point))) ?\n))
788 (insert "\n")))
789 (muse-insert-markup (muse-markup-text 'section-close depth))
790 (insert "\n"))))
792 (defun muse-publish-markup-directive (&optional name value)
793 (unless name (setq name (match-string 1)))
794 (unless value (setq value (match-string 2)))
795 (let ((elem (assoc name muse-publishing-directives)))
796 (if elem
797 (setcdr elem value)
798 (setq muse-publishing-directives
799 (cons (cons name value)
800 muse-publishing-directives))))
801 ;; Make sure we don't ever try to move the point forward (past the
802 ;; beginning of buffer) while we're still searching for directives.
803 (setq muse-publishing-last-position nil)
804 (delete-region (match-beginning 0) (match-end 0)))
806 (defsubst muse-publishing-directive (name)
807 (cdr (assoc name muse-publishing-directives)))
809 (defun muse-publish-markup-anchor ()
810 (unless (get-text-property (match-end 1) 'muse-link)
811 (let ((text (muse-markup-text 'anchor (match-string 2))))
812 (unless (string= text "")
813 (save-match-data
814 (skip-chars-forward (concat muse-regexp-blank "\n"))
815 (muse-insert-markup text)))
816 (match-string 1))))
818 (defun muse-publish-markup-comment ()
819 (if (null muse-publish-comments-p)
821 (goto-char (match-end 0))
822 (muse-insert-markup (muse-markup-text 'comment-end))
823 (if (match-beginning 1)
824 (progn
825 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
826 (delete-region (match-beginning 0) (match-beginning 1)))
827 (delete-region (match-beginning 0) (match-end 0)))
828 (goto-char (match-beginning 0))
829 (muse-insert-markup (muse-markup-text 'comment-begin))))
831 (defvar muse-inhibit-style-tags nil
832 "If non-nil, do not search for style-specific tags.
833 This is used when publishing headers and footers.")
835 (defun muse-publish-markup-tag ()
836 (let ((tag-info (if muse-inhibit-style-tags
837 (assoc (match-string 1) muse-publish-markup-tags)
838 (muse-markup-tag-info (match-string 1)))))
839 (when (and tag-info
840 (not (get-text-property (match-beginning 0) 'read-only)))
841 (let ((closed-tag (match-string 3))
842 (start (match-beginning 0))
843 (beg (point))
844 end attrs)
845 (when (nth 2 tag-info)
846 (let ((attrstr (match-string 2)))
847 (while (and attrstr
848 (string-match (concat "\\([^"
849 muse-regexp-blank
850 "=\n]+\\)\\(=\"\\"
851 "([^\"]+\\)\"\\)?")
852 attrstr))
853 (let ((attr (cons (downcase
854 (muse-match-string-no-properties 1 attrstr))
855 (muse-match-string-no-properties 3 attrstr))))
856 (setq attrstr (replace-match "" t t attrstr))
857 (if attrs
858 (nconc attrs (list attr))
859 (setq attrs (list attr)))))))
860 (if (and (cadr tag-info) (not closed-tag))
861 (if (muse-goto-tag-end (car tag-info) (nth 3 tag-info))
862 (delete-region (match-beginning 0) (point))
863 (setq tag-info nil)))
864 (when tag-info
865 (setq end (point-marker))
866 (delete-region start beg)
867 (goto-char start)
868 (let ((args (list start end)))
869 (if (nth 2 tag-info)
870 (nconc args (list attrs)))
871 (let ((muse-inhibit-style-tags nil))
872 ;; remove the inhibition
873 (apply (nth 4 tag-info) args)))))))
874 nil)
876 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
877 "Escape specials from BEG to END using style-specific :specials.
878 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
879 CONTEXT is used to figure out what kind of specials to escape.
881 The following contexts exist in Muse.
882 'underline _underlined text_
883 'literal =monospaced text= or <code> region (monospaced, escaped)
884 'emphasis *emphasized text*
885 'email email@example.com
886 'url http://example.com
887 'url-desc [[...][description of an explicit link]]
888 'image [[image.png]]
889 'example <example> region (monospaced, block context, escaped)
890 'verbatim <verbatim> region (escaped)
891 'document normal text"
892 (let ((specials (muse-style-element :specials nil t)))
893 (cond ((functionp specials)
894 (setq specials (funcall specials context)))
895 ((symbolp specials)
896 (setq specials (symbol-value specials))))
897 (if (functionp specials)
898 (funcall specials beg end ignore-read-only)
899 (save-excursion
900 (save-restriction
901 (narrow-to-region beg end)
902 (goto-char (point-min))
903 (while (< (point) (point-max))
904 (if (and (not ignore-read-only)
905 (get-text-property (point) 'read-only))
906 (goto-char (or (next-single-property-change (point) 'read-only)
907 (point-max)))
908 (let ((repl (or (assoc (char-after) specials)
909 (assoc (char-after)
910 muse-publish-markup-specials))))
911 (if (null repl)
912 (forward-char 1)
913 (delete-char 1)
914 (insert-before-markers (cdr repl)))))))))))
916 (defun muse-publish-markup-word ()
917 (let* ((beg (match-beginning 2))
918 (end (1- (match-end 2)))
919 (leader (buffer-substring-no-properties beg end))
920 open-tag close-tag mark-read-only loc context)
921 (cond
922 ((string= leader "_")
923 (setq context 'underline
924 open-tag (muse-markup-text 'begin-underline)
925 close-tag (muse-markup-text 'end-underline)))
926 ((string= leader "=")
927 (setq context 'literal
928 open-tag (muse-markup-text 'begin-literal)
929 close-tag (muse-markup-text 'end-literal))
930 (setq mark-read-only t))
932 (let ((l (length leader)))
933 (setq context 'emphasis)
934 (cond
935 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
936 close-tag (muse-markup-text 'end-emph)))
937 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
938 close-tag (muse-markup-text 'end-more-emph)))
939 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
940 close-tag (muse-markup-text 'end-most-emph)))
941 (t (setq context nil))))))
942 (if (and context
943 (not (get-text-property beg 'muse-link))
944 (setq loc (search-forward leader nil t))
945 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
946 (not (eq (char-syntax (char-before (point))) ?\ ))
947 (not (get-text-property (point) 'muse-link)))
948 (progn
949 (replace-match "")
950 (delete-region beg end)
951 (setq end (point-marker))
952 (muse-insert-markup close-tag)
953 (goto-char beg)
954 (muse-insert-markup open-tag)
955 (setq beg (point))
956 (when mark-read-only
957 (muse-publish-escape-specials beg end t context)
958 (muse-publish-mark-read-only beg end)))
959 (backward-char))
960 nil))
962 (defun muse-publish-markup-emdash ()
963 (unless (get-text-property (match-beginning 0) 'muse-link)
964 (let ((prespace (match-string 1))
965 (postspace (match-string 2)))
966 (delete-region (match-beginning 0) (match-end 0))
967 (muse-insert-markup (muse-markup-text 'emdash prespace postspace))
968 (when (eq (char-after) ?\<)
969 (insert ?\n)))))
971 (defun muse-publish-markup-enddots ()
972 (unless (get-text-property (match-beginning 0) 'muse-link)
973 (delete-region (match-beginning 0) (match-end 0))
974 (muse-insert-markup (muse-markup-text 'enddots))))
976 (defun muse-publish-markup-dots ()
977 (unless (get-text-property (match-beginning 0) 'muse-link)
978 (delete-region (match-beginning 0) (match-end 0))
979 (muse-insert-markup (muse-markup-text 'dots))))
981 (defun muse-publish-markup-rule ()
982 (unless (get-text-property (match-beginning 0) 'muse-link)
983 (delete-region (match-beginning 0) (match-end 0))
984 (muse-insert-markup (muse-markup-text 'rule))))
986 (defun muse-publish-markup-no-break-space ()
987 (unless (get-text-property (match-beginning 0) 'muse-link)
988 (delete-region (match-beginning 0) (match-end 0))
989 (muse-insert-markup (muse-markup-text 'no-break-space))))
991 (defun muse-publish-markup-heading ()
992 (let* ((len (length (match-string 1)))
993 (start (muse-markup-text
994 (cond ((= len 1) 'section)
995 ((= len 2) 'subsection)
996 ((= len 3) 'subsubsection)
997 (t 'section-other))
998 len))
999 (end (muse-markup-text
1000 (cond ((= len 1) 'section-end)
1001 ((= len 2) 'subsection-end)
1002 ((= len 3) 'subsubsection-end)
1003 (t 'section-other-end))
1004 len)))
1005 (delete-region (match-beginning 0) (match-end 0))
1006 (muse-insert-markup start)
1007 (end-of-line)
1008 (when end
1009 (muse-insert-markup end))
1010 (forward-line 1)
1011 (unless (eq (char-after) ?\n)
1012 (insert "\n"))
1013 (muse-publish-section-close len)))
1015 (defvar muse-publish-footnotes nil)
1017 (defun muse-publish-markup-footnote ()
1018 "Scan ahead and snarf up the footnote body"
1019 (cond
1020 ((get-text-property (match-beginning 0) 'muse-link)
1021 nil)
1022 ((= (muse-line-beginning-position) (match-beginning 0))
1025 (let ((footnote (save-match-data
1026 (string-to-number (match-string 1))))
1027 (oldtext (match-string 0))
1028 footnotemark)
1029 (delete-region (match-beginning 0) (match-end 0))
1030 (save-excursion
1031 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
1032 (let* ((start (match-beginning 0))
1033 (beg (goto-char (match-end 0)))
1034 (end (save-excursion
1035 (if (search-forward "\n\n" nil t)
1036 (copy-marker (match-beginning 0))
1037 (goto-char (point-max))
1038 (skip-chars-backward "\n")
1039 (point-marker)))))
1040 (while (re-search-forward
1041 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
1042 end t)
1043 (replace-match "\\1" t))
1044 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
1045 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
1046 (if (string= "" footnotemark-cmd)
1047 (setq footnotemark
1048 (concat (muse-markup-text 'footnote)
1049 (buffer-substring-no-properties beg end)
1050 (muse-markup-text 'footnote-end)))
1051 (setq footnotemark (format footnotemark-cmd footnote
1052 footnotemark-end-cmd))
1053 (unless muse-publish-footnotes
1054 (set (make-local-variable 'muse-publish-footnotes)
1055 (make-vector 256 nil)))
1056 (unless (aref muse-publish-footnotes footnote)
1057 (setq footnotemark
1058 (concat
1059 footnotemark
1060 (concat (format (muse-markup-text 'footnotetext)
1061 footnote)
1062 (buffer-substring-no-properties beg end)
1063 (muse-markup-text 'footnotetext-end))))
1064 (aset muse-publish-footnotes footnote footnotemark))))
1065 (goto-char end)
1066 (skip-chars-forward "\n")
1067 (delete-region start (point)))))
1068 (if footnotemark
1069 (muse-insert-markup footnotemark)
1070 (insert oldtext))))))
1072 (defun muse-publish-markup-fn-sep ()
1073 (delete-region (match-beginning 0) (match-end 0))
1074 (muse-insert-markup (muse-markup-text 'fn-sep)))
1076 (defun muse-insert-markup-end-list (&rest args)
1077 (let ((beg (point)))
1078 (apply 'insert args)
1079 (add-text-properties beg (point) '(end-list t))
1080 (muse-publish-mark-read-only beg (point))))
1082 (defun muse-publish-determine-dl-indent (continue indent-sym determine-sym)
1083 ;; If the caller doesn't know how much indentation to use, figure it
1084 ;; out ourselves. It is assumed that `muse-forward-list-item' has
1085 ;; been called just before this to set the match data.
1086 (when (and continue
1087 (symbol-value determine-sym))
1088 (save-match-data
1089 ;; snarf all leading whitespace
1090 (let ((indent (and (match-beginning 2)
1091 (buffer-substring (match-beginning 1)
1092 (match-beginning 2)))))
1093 (when (and indent
1094 (not (string= indent "")))
1095 (set indent-sym indent)
1096 (set determine-sym nil))))))
1098 (defun muse-publish-surround-dl (indent post-indent)
1099 (let* ((beg-item (muse-markup-text 'begin-dl-item))
1100 (end-item (muse-markup-text 'end-dl-item))
1101 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
1102 (end-ddt (muse-markup-text 'end-ddt))
1103 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
1104 (end-dde (muse-markup-text 'end-dde))
1105 (continue t)
1106 def-on-same-line beg)
1107 (while continue
1108 ;; envelope this as one term+definitions unit -- HTML does not
1109 ;; need this, but DocBook and Muse's custom XML format do
1110 (muse-insert-markup beg-item)
1111 (when (looking-at muse-dl-term-regexp)
1112 ;; find the term and wrap it with published markup
1113 (setq beg (point))
1114 (goto-char (match-end 1))
1115 (delete-region (point) (match-end 0))
1116 (muse-insert-markup-end-list end-ddt)
1117 ;; if definition is immediately after term, move to next line
1118 (unless (eq (char-after) ?\n)
1119 (insert ?\n))
1120 (save-excursion
1121 (goto-char beg)
1122 (delete-region (point) (match-beginning 1))
1123 (muse-insert-markup beg-ddt)))
1124 (setq beg (point)
1125 ;; move past current item
1126 continue (muse-forward-list-item 'dl-term indent))
1127 (save-restriction
1128 (narrow-to-region beg (point))
1129 (goto-char (point-min))
1130 ;; publish each definition that we find, defaulting to an
1131 ;; empty definition if none are found
1132 (muse-publish-surround-text beg-dde end-dde
1133 (lambda (indent)
1134 (muse-forward-list-item 'dl-entry indent))
1135 indent post-indent
1136 #'muse-publish-determine-dl-indent)
1137 (goto-char (point-max))
1138 (skip-chars-backward (concat muse-regexp-blank "\n"))
1139 (muse-insert-markup-end-list end-item)
1140 (when continue
1141 (goto-char (point-max)))))))
1143 (defun muse-publish-strip-list-indentation (list-item empty-line indent post-indent)
1144 (let ((list-nested nil)
1145 (indent-found nil))
1146 (while (< (point) (point-max))
1147 (when (and (looking-at list-item)
1148 (not (or (get-text-property
1149 (muse-list-item-critical-point) 'read-only)
1150 (get-text-property
1151 (muse-list-item-critical-point) 'muse-link))))
1152 ;; if we encounter a list item, allow no post-indent space
1153 (setq list-nested t))
1154 (when (and (not (looking-at empty-line))
1155 (looking-at (concat indent "\\("
1156 (or (and list-nested "")
1157 post-indent)
1158 "\\)")))
1159 ;; if list is not nested, remove indentation
1160 (unless indent-found
1161 (setq post-indent (match-string 1)
1162 indent-found t))
1163 (replace-match ""))
1164 (forward-line 1))))
1166 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent determine-indent-func list-item)
1167 (unless list-item
1168 (setq list-item (format muse-list-item-regexp
1169 (concat "[" muse-regexp-blank "]*"))))
1170 (let ((continue t)
1171 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
1172 (determine-indent (if determine-indent-func t nil))
1173 (new-indent indent)
1174 (first t)
1175 beg)
1176 (unless indent
1177 (setq indent (concat "[" muse-regexp-blank "]+")))
1178 (if post-indent
1179 (setq post-indent (concat " \\{0," (number-to-string post-indent)
1180 "\\}"))
1181 (setq post-indent ""))
1182 (while continue
1183 (if (or (not end-tag) (string= end-tag ""))
1184 ;; if no end of list item markup exists, treat the beginning
1185 ;; of list item markup as it if it were the end -- this
1186 ;; prevents multiple-level lists from being confused
1187 (muse-insert-markup-end-list beg-tag)
1188 (muse-insert-markup beg-tag))
1189 (setq beg (point)
1190 ;; move past current item; continue is non-nil if there
1191 ;; are more like items to be processed
1192 continue (if (and determine-indent-func first)
1193 (funcall move-func (concat indent post-indent))
1194 (funcall move-func indent)))
1195 (when determine-indent-func
1196 (funcall determine-indent-func continue 'new-indent 'determine-indent))
1197 (when continue
1198 ;; remove list markup if we encountered another item of the
1199 ;; same type
1200 (replace-match "" t t nil 1))
1201 (save-restriction
1202 (narrow-to-region beg (point))
1203 ;; narrow to current item
1204 (goto-char (point-min))
1205 (forward-line 1)
1206 (muse-publish-strip-list-indentation list-item empty-line
1207 indent post-indent)
1208 (skip-chars-backward (concat muse-regexp-blank "\n"))
1209 (muse-insert-markup-end-list end-tag)
1210 (when determine-indent-func
1211 (setq indent new-indent))
1212 (when first
1213 (setq first nil))
1214 (when continue
1215 (goto-char (point-max)))))))
1217 (defun muse-publish-markup-list ()
1218 "Markup a list entry.
1219 This function works by marking up items of the same list level
1220 and type, respecting the end-of-list property."
1221 (let* ((str (match-string 1))
1222 (type (muse-list-item-type str))
1223 (indent (buffer-substring (muse-line-beginning-position)
1224 (match-beginning 1)))
1225 (post-indent (length str))
1226 (last (match-beginning 0)))
1227 (cond
1228 ((or (get-text-property (muse-list-item-critical-point) 'read-only)
1229 (get-text-property (muse-list-item-critical-point) 'muse-link))
1230 nil)
1231 ((eq type 'ul)
1232 (unless (eq (char-after (match-end 1)) ?-)
1233 (delete-region (match-beginning 0) (match-end 0))
1234 (muse-insert-markup (muse-markup-text 'begin-uli))
1235 (save-excursion
1236 (muse-publish-surround-text
1237 (muse-markup-text 'begin-uli-item)
1238 (muse-markup-text 'end-uli-item)
1239 (lambda (indent)
1240 (muse-forward-list-item 'ul indent))
1241 indent post-indent)
1242 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1243 (forward-line 1)))
1244 ((eq type 'ol)
1245 (delete-region (match-beginning 0) (match-end 0))
1246 (muse-insert-markup (muse-markup-text 'begin-oli))
1247 (save-excursion
1248 (muse-publish-surround-text
1249 (muse-markup-text 'begin-oli-item)
1250 (muse-markup-text 'end-oli-item)
1251 (lambda (indent)
1252 (muse-forward-list-item 'ol indent))
1253 indent post-indent)
1254 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1255 (forward-line 1))
1256 ((not (string= (match-string 2) ""))
1257 ;; must have an initial term
1258 (goto-char (match-beginning 0))
1259 (muse-insert-markup (muse-markup-text 'begin-dl))
1260 (save-excursion
1261 (muse-publish-surround-dl indent post-indent)
1262 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1263 (forward-line 1))))
1264 nil)
1266 (defun muse-publish-markup-quote ()
1267 "Markup a quoted paragraph.
1268 The reason this function is so funky, is to prevent text properties
1269 like read-only from being inadvertently deleted."
1270 (let* ((ws (match-string 1))
1271 (centered (>= (string-width ws) 6))
1272 (begin-elem (if centered 'begin-center 'begin-quote-item))
1273 (end-elem (if centered 'end-center 'end-quote-item)))
1274 (replace-match "" t t nil 1)
1275 (unless centered
1276 (muse-insert-markup (muse-markup-text 'begin-quote)))
1277 (muse-publish-surround-text (muse-markup-text begin-elem)
1278 (muse-markup-text end-elem)
1279 (function (lambda (indent)
1280 (muse-forward-paragraph)
1281 nil)))
1282 (unless centered
1283 (muse-insert-markup (muse-markup-text 'end-quote)))))
1285 (defun muse-publish-markup-leading-space (markup-space multiple)
1286 (let (count)
1287 (when (and markup-space
1288 (>= (setq count (skip-chars-forward " ")) 0))
1289 (delete-region (muse-line-beginning-position) (point))
1290 (while (> count 0)
1291 (muse-insert-markup markup-space)
1292 (setq count (- count multiple))))))
1294 (defun muse-publish-markup-verse ()
1295 (let ((leader (match-string 0)))
1296 (goto-char (match-beginning 0))
1297 (muse-insert-markup (muse-markup-text 'begin-verse))
1298 (while (looking-at leader)
1299 (replace-match "")
1300 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1301 (let ((beg (point)))
1302 (end-of-line)
1303 (cond
1304 ((bolp)
1305 (let ((text (muse-markup-text 'empty-verse-line)))
1306 (when text (muse-insert-markup text))))
1307 ((save-excursion
1308 (save-match-data
1309 (forward-line 1)
1310 (or (looking-at (concat leader "["
1311 muse-regexp-blank
1312 "]*$"))
1313 (not (looking-at leader)))))
1314 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1315 (end-text (muse-markup-text 'end-last-stanza-line)))
1316 (when end-text (muse-insert-markup end-text))
1317 (goto-char beg)
1318 (when begin-text (muse-insert-markup begin-text))
1319 (end-of-line)))
1321 (let ((begin-text (muse-markup-text 'begin-verse-line))
1322 (end-text (muse-markup-text 'end-verse-line)))
1323 (when end-text (muse-insert-markup end-text))
1324 (goto-char beg)
1325 (when begin-text (muse-insert-markup begin-text))
1326 (end-of-line))))
1327 (forward-line 1))))
1328 (muse-insert-markup (muse-markup-text 'end-verse))
1329 (insert ?\n))
1331 (defun muse-publish-table-fields (beg end)
1332 "Parse given region as a table, returning a cons cell.
1333 The car is the length of the longest row.
1335 The cdr is a list of the fields of the table, with the first
1336 element indicating the type of the row:
1337 1: body, 2: header, 3: footer.
1339 The existing region will be removed, except for initial blank lines."
1340 (unless (muse-publishing-directive "disable-tables")
1341 (let ((longest 0)
1342 (left 0)
1343 fields field-list)
1344 (save-restriction
1345 (narrow-to-region beg end)
1346 (goto-char (point-min))
1347 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1348 (forward-line 1))
1349 (setq beg (point))
1350 (while (= left 0)
1351 (when (looking-at muse-table-line-regexp)
1352 (setq fields (cons (length (match-string 1))
1353 (mapcar #'muse-trim-whitespace
1354 (split-string (match-string 0)
1355 muse-table-field-regexp)))
1356 field-list (cons fields field-list)
1357 longest (max (length fields) longest)))
1358 (setq left (forward-line 1))))
1359 (delete-region beg end)
1360 (if (= longest 0)
1361 (cons 0 nil)
1362 (cons (1- longest) (nreverse field-list))))))
1364 (defun muse-publish-markup-table ()
1365 "Style does not support tables.")
1367 (defun muse-publish-escape-specials-in-string (string &optional context)
1368 "Escape specials in STRING using style-specific :specials.
1369 CONTEXT is used to figure out what kind of specials to escape.
1371 See the documentation of the `muse-publish-escape-specials'
1372 function for the list of available contexts."
1373 (unless string
1374 (setq string ""))
1375 (let ((specials (muse-style-element :specials nil t)))
1376 (cond ((functionp specials)
1377 (setq specials (funcall specials context)))
1378 ((symbolp specials)
1379 (setq specials (symbol-value specials))))
1380 (if (functionp specials)
1381 (funcall specials string)
1382 (apply (function concat)
1383 (mapcar
1384 (lambda (ch)
1385 (let ((repl (or (assoc ch specials)
1386 (assoc ch muse-publish-markup-specials))))
1387 (if (null repl)
1388 (char-to-string ch)
1389 (cdr repl))))
1390 (append string nil))))))
1392 (defun muse-publish-markup-email ()
1393 (let* ((beg (match-end 1))
1394 (addr (buffer-substring-no-properties beg (match-end 0))))
1395 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1396 (goto-char beg)
1397 (delete-region beg (match-end 0))
1398 (if (or (eq (char-before (match-beginning 0)) ?\")
1399 (eq (char-after (match-end 0)) ?\"))
1400 (insert addr)
1401 (insert (format (muse-markup-text 'email-addr) addr addr)))
1402 (muse-publish-mark-read-only beg (point))))
1404 (defun muse-publish-classify-url (target)
1405 "Transform anchors and get published name, if TARGET is a page.
1406 The return value is a cons cell. The car is the type of link,
1407 the cadr is the page name, and the cddr is the anchor."
1408 (save-match-data
1409 (cond ((or (null target) (string= target ""))
1410 nil)
1411 ((string-match muse-image-regexp target)
1412 (cons 'image (cons target nil)))
1413 ((string-match muse-url-regexp target)
1414 (cons 'url (cons target nil)))
1415 ((string-match muse-file-regexp target)
1416 (cons 'file (cons target nil)))
1417 ((string-match "#" target)
1418 (if (eq (aref target 0) ?\#)
1419 (cons 'anchor-ref (cons nil (substring target 1)))
1420 (cons 'link-and-anchor
1421 (cons (muse-publish-link-page
1422 (substring target 0 (match-beginning 0)))
1423 (substring target (match-end 0))))))
1425 (cons 'link (cons (muse-publish-link-page target) nil))))))
1427 (defun muse-publish-url-desc (desc explicit)
1428 (when desc
1429 (dolist (transform muse-publish-desc-transforms)
1430 (setq desc (save-match-data
1431 (when desc (funcall transform desc explicit)))))
1432 (setq desc (muse-link-unescape desc))
1433 (muse-publish-escape-specials-in-string desc 'url-desc)))
1435 (defun muse-publish-url (url &optional desc orig-url explicit)
1436 "Resolve a URL into its final <a href> form."
1437 (let (type anchor)
1438 (dolist (transform muse-publish-url-transforms)
1439 (setq url (save-match-data (when url (funcall transform url explicit)))))
1440 (if desc
1441 (setq desc (muse-publish-url-desc desc explicit))
1442 (if orig-url
1443 (setq orig-url (muse-publish-url-desc orig-url explicit))))
1444 (let ((target (muse-publish-classify-url url)))
1445 (setq type (car target)
1446 url (if (eq type 'image)
1447 (muse-publish-escape-specials-in-string (cadr target)
1448 'image)
1449 (muse-publish-escape-specials-in-string (cadr target) 'url))
1450 anchor (muse-publish-escape-specials-in-string
1451 (cddr target) 'url)))
1452 (cond ((eq type 'anchor-ref)
1453 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1454 ((string= url "")
1455 desc)
1456 ((eq type 'image)
1457 (let ((ext (or (file-name-extension url) "")))
1458 (setq url (muse-path-sans-extension url))
1459 (if desc
1460 (muse-markup-text 'image-with-desc url ext desc)
1461 (muse-markup-text 'image url ext))))
1462 ((eq type 'link-and-anchor)
1463 (muse-markup-text 'link-and-anchor url anchor
1464 (or desc orig-url)))
1465 ((and desc (string-match muse-image-regexp desc))
1466 (let ((ext (or (file-name-extension desc) "")))
1467 (setq desc (muse-path-sans-extension desc))
1468 (muse-markup-text 'image-link url desc ext)))
1469 ((eq type 'link)
1470 (muse-markup-text 'link url (or desc orig-url)))
1472 (or (and (or desc
1473 (not (string= url orig-url)))
1474 (let ((text (muse-markup-text 'url-and-desc url
1475 (or desc orig-url))))
1476 (and (not (string= text ""))
1477 text)))
1478 (muse-markup-text 'url url (or desc orig-url)))))))
1480 (defun muse-publish-insert-url (url &optional desc orig-url explicit)
1481 "Resolve a URL into its final <a href> form."
1482 (delete-region (match-beginning 0) (match-end 0))
1483 (let ((text (muse-publish-url url desc orig-url explicit)))
1484 (when text
1485 (muse-insert-markup text))))
1487 (defun muse-publish-markup-link ()
1488 (let (desc explicit orig-link link)
1489 (setq explicit (save-match-data
1490 (if (string-match muse-explicit-link-regexp
1491 (match-string 0))
1492 t nil)))
1493 (setq orig-link (if explicit (match-string 1) (match-string 0)))
1494 (setq desc (when explicit (match-string 2)))
1495 (setq link (if explicit
1496 (muse-handle-explicit-link orig-link)
1497 (muse-handle-implicit-link orig-link)))
1498 (when (and link
1499 (or explicit
1500 (not (or (eq (char-before (match-beginning 0)) ?\")
1501 (eq (char-after (match-end 0)) ?\")))))
1502 ;; if explicit link has no user-provided description, treat it
1503 ;; as if it were an implicit link
1504 (when (and explicit (not desc))
1505 (setq explicit nil))
1506 (muse-publish-insert-url link desc orig-link explicit))))
1508 (defun muse-publish-markup-url ()
1509 (unless (or (eq (char-before (match-beginning 0)) ?\")
1510 (eq (char-after (match-end 0)) ?\"))
1511 (let ((url (match-string 0)))
1512 (muse-publish-insert-url url nil url))))
1514 ;; Default publishing tags
1516 (defcustom muse-publish-contents-depth 2
1517 "The number of heading levels to include with <contents> tags."
1518 :type 'integer
1519 :group 'muse-publish)
1521 (defmacro muse-publish-ensure-block-tag (beg)
1522 "Ensure that block-level tag at BEG is published with at least one
1523 blank line. BEG is modified to be the new position."
1524 `(progn
1525 (goto-char ,beg)
1526 (cond ((not (bolp)) (insert "\n\n"))
1527 ((eq (point) (point-min)) nil)
1528 ((prog2 (backward-char) (bolp) (forward-char)) nil)
1529 (t (insert "\n")))
1530 (setq ,beg (point))))
1532 (defun muse-publish-contents-tag (beg end attrs)
1533 (set (make-local-variable 'muse-publish-generate-contents)
1534 (cons (copy-marker (point) t)
1535 (let ((depth (cdr (assoc "depth" attrs))))
1536 (or (and depth (string-to-number depth))
1537 muse-publish-contents-depth)))))
1539 (defun muse-publish-verse-tag (beg end)
1540 (save-excursion
1541 (save-restriction
1542 (narrow-to-region beg end)
1543 (goto-char (point-min))
1544 (while (eq ?\ (char-syntax (char-after)))
1545 (delete-char 1))
1546 (while (< (point) (point-max))
1547 (insert "> ")
1548 (forward-line))
1549 (if (eq ?\ (char-syntax (char-before)))
1550 (delete-char -1)))))
1552 (defun muse-publish-mark-read-only (beg end)
1553 "Add read-only properties to the given region."
1554 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1555 nil)
1557 (defun muse-publish-mark-link (&optional beg end)
1558 "Indicate that the given region is a Muse link, so that other
1559 markup elements respect it. If a region is not specified, use
1560 the 0th match data to determine it.
1562 This is usually applied to explicit links."
1563 (unless beg (setq beg (match-beginning 0)))
1564 (unless end (setq end (match-end 0)))
1565 (add-text-properties beg end '(muse-link t))
1566 nil)
1568 (defun muse-publish-quote-tag (beg end)
1569 (save-excursion
1570 (save-restriction
1571 (narrow-to-region beg end)
1572 (let ((quote-regexp "^\\(<\\(/?\\)quote>\\)"))
1573 (muse-insert-markup (muse-markup-text 'begin-quote))
1574 (while (progn
1575 (unless (looking-at (concat "[" muse-regexp-blank "\n]*"
1576 "<quote>"))
1577 (muse-publish-surround-text
1578 (muse-markup-text 'begin-quote-item)
1579 (muse-markup-text 'end-quote-item)
1580 (function
1581 (lambda (indent)
1582 (muse-forward-paragraph)
1583 (goto-char (match-end 0))
1584 (and (< (point) (point-max))
1585 (not (looking-at quote-regexp)))))
1586 nil nil nil
1587 quote-regexp))
1588 (if (>= (point) (point-max))
1590 (and (search-forward "<quote>" nil t)
1591 (muse-goto-tag-end "quote" t)
1592 (progn (forward-line 1) t)
1593 (< (point) (point-max))))))
1594 (goto-char (point-max))
1595 (muse-insert-markup (muse-markup-text 'end-quote))))))
1597 (defun muse-publish-code-tag (beg end)
1598 (muse-publish-escape-specials beg end nil 'literal)
1599 (goto-char beg)
1600 (insert (muse-markup-text 'begin-literal))
1601 (goto-char end)
1602 (insert (muse-markup-text 'end-literal))
1603 (muse-publish-mark-read-only beg (point)))
1605 (defun muse-publish-src-tag (beg end attrs)
1606 (muse-publish-example-tag beg end))
1608 (defun muse-publish-example-tag (beg end)
1609 (muse-publish-ensure-block-tag beg)
1610 (muse-publish-escape-specials beg end nil 'example)
1611 (goto-char beg)
1612 (insert (muse-markup-text 'begin-example))
1613 (goto-char end)
1614 (insert (muse-markup-text 'end-example))
1615 (muse-publish-mark-read-only beg (point)))
1617 (defun muse-publish-literal-tag (beg end attrs)
1618 "Ensure that the text between BEG and END is not interpreted later on.
1620 ATTRS is an alist of attributes.
1622 If it contains a \"style\" element, delete the region if the
1623 current style is neither derived from nor equal to this style.
1625 If it contains both a \"style\" element and an \"exact\" element
1626 with the value \"t\", delete the region only if the current style
1627 is exactly this style."
1628 (let* ((style (cdr (assoc "style" attrs)))
1629 (exact (cdr (assoc "exact" attrs)))
1630 (exactp (and (stringp exact) (string= exact "t"))))
1631 (if (or (not style)
1632 (and exactp (equal (muse-style style)
1633 muse-publishing-current-style))
1634 (and (not exactp) (muse-style-derived-p style)))
1635 (muse-publish-mark-read-only beg end)
1636 (delete-region beg end))))
1638 (defun muse-publish-verbatim-tag (beg end)
1639 (muse-publish-escape-specials beg end nil 'verbatim)
1640 (muse-publish-mark-read-only beg end))
1642 (defalias 'muse-publish-class-tag 'ignore)
1644 (defun muse-publish-call-tag-on-buffer (tag &optional attrs)
1645 "Transform the current buffer as if it were surrounded by the tag TAG.
1646 If attributes ATTRS are given, pass them to the tag function."
1647 (let ((tag-info (if muse-inhibit-style-tags
1648 (assoc tag muse-publish-markup-tags)
1649 (muse-markup-tag-info tag))))
1650 (when tag-info
1651 (let* ((end (progn (goto-char (point-max)) (point-marker)))
1652 (args (list (point-min) end))
1653 (muse-inhibit-style-tags nil))
1654 (when (nth 2 tag-info)
1655 (nconc args (list attrs)))
1656 (apply (nth 4 tag-info) args)))))
1658 (defun muse-publish-examplify-buffer (&optional attrs)
1659 "Transform the current buffer as if it were an <example> region."
1660 (muse-publish-call-tag-on-buffer "example" attrs))
1662 (defun muse-publish-srcify-buffer (&optional attrs)
1663 "Transform the current buffer as if it were a <src> region."
1664 (muse-publish-call-tag-on-buffer "src" attrs))
1666 (defun muse-publish-versify-buffer (&optional attrs)
1667 "Transform the current buffer as if it were a <verse> region."
1668 (muse-publish-call-tag-on-buffer "verse" attrs)
1669 (muse-publish-markup ""
1670 `((100 ,(concat "^[" muse-regexp-blank "]*> ") 0
1671 muse-publish-markup-verse)))
1672 (goto-char (point-min)))
1674 (defmacro muse-publish-get-and-delete-attr (attr attrs)
1675 "Delete attribute ATTR from ATTRS only once, destructively.
1677 This function returns the matching attribute value, if found."
1678 (let ((last (make-symbol "last"))
1679 (found (make-symbol "found"))
1680 (vals (make-symbol "vals")))
1681 `(let ((,vals ,attrs))
1682 (if (string= (caar ,vals) ,attr)
1683 (prog1 (cdar ,vals)
1684 (setq ,attrs (cdr ,vals)))
1685 (let ((,last ,vals)
1686 (,found nil))
1687 (while ,vals
1688 (setq ,vals (cdr ,vals))
1689 (when (string= (caar ,vals) ,attr)
1690 (setq ,found (cdar ,vals))
1691 (setcdr ,last (cdr ,vals))
1692 (setq ,vals nil))
1693 (setq ,last ,vals))
1694 ,found)))))
1696 (defmacro muse-publish-markup-attribute (beg end attrs reinterp &rest body)
1697 "Evaluate BODY within the bounds of BEG and END.
1698 ATTRS is an alist. Only the \"markup\" element of ATTRS is acted
1701 If it is omitted, publish the region with the normal Muse rules.
1702 If RE-INTERP is specified, this is done immediately in a new
1703 publishing process. Currently, RE-INTERP is specified only by
1704 the <include> tag.
1706 If \"nil\", do not mark up the region at all, but prevent it from
1707 being further interpreted by Muse.
1709 If \"example\", treat the region as if it was surrounded by the
1710 <example> tag.
1712 If \"src\", treat the region as if it was surrounded by the
1713 <src> tag.
1715 If \"verse\", treat the region as if it was surrounded by the
1716 <verse> tag, to preserve newlines.
1718 Otherwise, it should be the name of a function to call in the
1719 narrowed region after evaluating BODY. The function should
1720 take the ATTRS parameter."
1721 (let ((markup (make-symbol "markup"))
1722 (markup-function (make-symbol "markup-function")))
1723 `(let ((,markup (muse-publish-get-and-delete-attr "markup" ,attrs)))
1724 (save-restriction
1725 (narrow-to-region ,beg ,end)
1726 (goto-char (point-min))
1727 ,@body
1728 (if (not ,markup)
1729 (when ,reinterp
1730 (muse-publish-markup-region (point-min) (point-max))
1731 (muse-publish-mark-read-only (point-min) (point-max))
1732 (goto-char (point-max)))
1733 (let ((,markup-function (read ,markup)))
1734 (cond ((eq ,markup-function 'example)
1735 (setq ,markup-function #'muse-publish-examplify-buffer))
1736 ((eq ,markup-function 'src)
1737 (setq ,markup-function #'muse-publish-srcify-buffer))
1738 ((eq ,markup-function 'verse)
1739 (setq ,markup-function #'muse-publish-versify-buffer))
1740 ((and ,markup-function (not (functionp ,markup-function)))
1741 (error "Invalid markup function `%s'" ,markup))
1742 (t nil))
1743 (if ,markup-function
1744 (funcall ,markup-function ,attrs)
1745 (muse-publish-mark-read-only (point-min) (point-max))
1746 (goto-char (point-max)))))))))
1748 (put 'muse-publish-markup-attribute 'lisp-indent-function 4)
1749 (put 'muse-publish-markup-attribute 'edebug-form-spec
1750 '(form form form form body))
1752 (defun muse-publish-lisp-tag (beg end attrs)
1753 (muse-publish-markup-attribute beg end attrs nil
1754 (save-excursion
1755 (let ((str (muse-eval-lisp
1756 (prog1
1757 (concat "(progn "
1758 (buffer-substring-no-properties (point-min)
1759 (point-max))
1760 ")")
1761 (delete-region beg end)))))
1762 (set-text-properties 0 (length str) nil str)
1763 (insert str)))))
1765 (defun muse-publish-command-tag (beg end attrs)
1766 (muse-publish-markup-attribute beg end attrs nil
1767 (while (looking-at "\\s-*$")
1768 (forward-line))
1769 (let ((interp (muse-publish-get-and-delete-attr "interp" attrs)))
1770 (if interp
1771 (shell-command-on-region (point) (point-max) interp t t)
1772 (shell-command
1773 (prog1
1774 (buffer-substring-no-properties (point) (point-max))
1775 (delete-region (point-min) (point-max)))
1776 t)))
1777 ;; make sure there is a newline at end
1778 (goto-char (point-max))
1779 (forward-line 0)
1780 (unless (looking-at "\\s-*$")
1781 (goto-char (point-max))
1782 (insert ?\n))
1783 (goto-char (point-min))))
1785 (defun muse-publish-perl-tag (beg end attrs)
1786 (muse-publish-command-tag beg end
1787 (cons (cons "interp" (executable-find "perl"))
1788 attrs)))
1790 (defun muse-publish-python-tag (beg end attrs)
1791 (muse-publish-command-tag beg end
1792 (cons (cons "interp" (executable-find "python"))
1793 attrs)))
1795 (defun muse-publish-ruby-tag (beg end attrs)
1796 (muse-publish-command-tag beg end
1797 (cons (cons "interp" (executable-find "ruby"))
1798 attrs)))
1800 (defun muse-publish-comment-tag (beg end)
1801 (if (null muse-publish-comments-p)
1802 (delete-region beg end)
1803 (goto-char end)
1804 (muse-insert-markup (muse-markup-text 'comment-end))
1805 (muse-publish-mark-read-only beg end)
1806 (goto-char beg)
1807 (muse-insert-markup (muse-markup-text 'comment-begin))))
1809 (defun muse-publish-include-tag (beg end attrs)
1810 "Include the named file at the current location during publishing.
1812 <include file=\"...\" markup=\"...\">
1814 The `markup' attribute controls how this file is marked up after
1815 being inserted. See `muse-publish-markup-attribute' for an
1816 explanation of how it works."
1817 (let ((filename (muse-publish-get-and-delete-attr "file" attrs))
1818 (muse-publishing-directives muse-publishing-directives))
1819 (if filename
1820 (setq filename (expand-file-name
1821 filename
1822 (file-name-directory muse-publishing-current-file)))
1823 (error "No file attribute specified in <include> tag"))
1824 (muse-publish-markup-attribute beg end attrs t
1825 (insert-file-contents filename))))
1827 (defun muse-publish-mark-up-tag (beg end attrs)
1828 "Run an Emacs Lisp function on the region delimted by this tag.
1830 <markup function=\"...\" style=\"...\" exact=\"...\">
1832 The optional \"function\" attribute controls how this section is
1833 marked up. If used, it should be the name of a function to call
1834 with the buffer narrowed to the delimited region. Note that no
1835 further marking-up will be performed on this region.
1837 If \"function\" is omitted, use the standard Muse markup function.
1838 This is useful for marking up content in headers and footers.
1840 The optional \"style\" attribute causes the region to be deleted
1841 if the current style is neither derived from nor equal to this
1842 style.
1844 If both a \"style\" attribute and an \"exact\" attribute are
1845 provided, and \"exact\" is \"t\", delete the region only if the
1846 current style is exactly this style."
1847 (let* ((style (cdr (assoc "style" attrs)))
1848 (exact (cdr (assoc "exact" attrs)))
1849 (exactp (and (stringp exact) (string= exact "t"))))
1850 (if (or (not style)
1851 (and exactp (equal (muse-style style)
1852 muse-publishing-current-style))
1853 (and (not exactp) (muse-style-derived-p style)))
1854 (let* ((function (cdr (assoc "function" attrs)))
1855 (muse-publishing-directives muse-publishing-directives)
1856 (markup-function (and function (intern function))))
1857 (if (and markup-function (functionp markup-function))
1858 (save-restriction
1859 (narrow-to-region beg end)
1860 (funcall markup-function)
1861 (goto-char (point-max)))
1862 (let ((muse-publish-inhibit-style-hooks t))
1863 (muse-publish-markup-region beg end)))
1864 (muse-publish-mark-read-only beg (point)))
1865 (delete-region beg end))))
1867 ;; Miscellaneous helper functions
1869 (defun muse-publish-strip-tags (string)
1870 "Remove all tags from the string."
1871 (while (string-match "<.*?>" string)
1872 (setq string (replace-match "" nil t string)))
1873 string)
1875 (defun muse-publish-markup-type (category default-func)
1876 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1877 (funcall (or rule default-func))))
1879 (defun muse-published-buffer-contents (buffer)
1880 (with-current-buffer buffer
1881 (goto-char (point-min))
1882 (let ((beg (and (search-forward "Emacs Muse begins here")
1883 (muse-line-end-position)))
1884 (end (and (search-forward "Emacs Muse ends here")
1885 (muse-line-beginning-position))))
1886 (buffer-substring-no-properties beg end))))
1888 (defun muse-published-contents (file)
1889 (when (file-readable-p file)
1890 (muse-with-temp-buffer
1891 (insert-file-contents file)
1892 (muse-published-buffer-contents (current-buffer)))))
1894 (defun muse-publish-transform-output
1895 (file temp-file output-path name gen-func &rest cleanup-exts)
1896 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1897 (setq file (muse-page-name file))
1898 (message "Generating %s output for %s..." name file)
1899 (if (not (funcall gen-func temp-file output-path))
1900 (message "Generating %s from %s...failed" name file)
1901 (message "Generating %s output for %s...done" name file)
1902 (muse-delete-file-if-exists temp-file)
1903 (dolist (ext cleanup-exts)
1904 (muse-delete-file-if-exists
1905 (expand-file-name (concat file ext)
1906 (file-name-directory output-path))))
1907 (message "Wrote %s" output-path)))
1909 (defun muse-publish-read-only (string)
1910 (let ((end (1- (length string))))
1911 (add-text-properties 0 end
1912 '(rear-nonsticky (read-only) read-only t)
1913 string)
1914 string))
1916 ;;; muse-publish.el ends here