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