Use insert-file-contents-literally instead of insert-file-contents
[muse-el.git] / lisp / muse-publish.el
blob61f663be6be942b17710359216b3deca79e00e36
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 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
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 (let ((backup-inhibited t))
790 (write-file output-path))
791 (muse-style-run-hooks :final style file output-path target))
792 t))))
794 ;;;###autoload
795 (defun muse-publish-this-file (style output-dir &optional force)
796 "Publish the currently-visited file.
797 Prompt for both the STYLE and OUTPUT-DIR if they are not
798 supplied."
799 (interactive (muse-publish-get-info))
800 (if buffer-file-name
801 (let ((muse-current-output-style (list :base (car style)
802 :path output-dir)))
803 (unless (muse-publish-file buffer-file-name style output-dir force)
804 (message (concat "The published version is up-to-date; use"
805 " C-u C-c C-T to force an update."))))
806 (message "This buffer is not associated with any file")))
808 (defun muse-batch-publish-files ()
809 "Publish Muse files in batch mode."
810 (let ((muse-batch-publishing-p t)
811 style output-dir)
812 (setq style (car command-line-args-left)
813 command-line-args-left (cdr command-line-args-left)
814 output-dir (car command-line-args-left)
815 output-dir
816 (if (string-match "\\`--output-dir=" output-dir)
817 (prog1
818 (substring output-dir (match-end 0))
819 (setq command-line-args-left (cdr command-line-args-left)))))
820 (setq auto-mode-alist
821 (delete (cons (concat "\\." muse-file-extension "\\'")
822 'muse-mode-choose-mode)
823 auto-mode-alist))
824 (dolist (file command-line-args-left)
825 (muse-publish-file file style output-dir t))))
827 ;; Default publishing rules
829 (defun muse-publish-section-close (depth)
830 "Seach forward for the closing tag of given DEPTH."
831 (let (not-end)
832 (save-excursion
833 (while (and (setq not-end (re-search-forward
834 (concat "^\\*\\{1," (number-to-string depth)
835 "\\}\\s-+")
836 nil t))
837 (get-text-property (match-beginning 0) 'read-only)))
838 (if not-end
839 (forward-line 0)
840 (goto-char (point-max)))
841 (cond ((not (eq (char-before) ?\n))
842 (insert "\n\n"))
843 ((not (eq (char-before (1- (point))) ?\n))
844 (insert "\n")))
845 (muse-insert-markup (muse-markup-text 'section-close depth))
846 (insert "\n"))))
848 (defun muse-publish-markup-directive (&optional name value)
849 (unless name (setq name (match-string 1)))
850 (unless value (setq value (match-string 2)))
851 (let ((elem (assoc name muse-publishing-directives)))
852 (if elem
853 (setcdr elem value)
854 (setq muse-publishing-directives
855 (cons (cons name value)
856 muse-publishing-directives))))
857 ;; Make sure we don't ever try to move the point forward (past the
858 ;; beginning of buffer) while we're still searching for directives.
859 (setq muse-publishing-last-position nil)
860 (delete-region (match-beginning 0) (match-end 0)))
862 (defsubst muse-publishing-directive (name)
863 (cdr (assoc name muse-publishing-directives)))
865 (defmacro muse-publish-get-and-delete-attr (attr attrs)
866 "Delete attribute ATTR from ATTRS only once, destructively.
868 This function returns the matching attribute value, if found."
869 (let ((last (make-symbol "last"))
870 (found (make-symbol "found"))
871 (vals (make-symbol "vals")))
872 `(let ((,vals ,attrs))
873 (if (string= (caar ,vals) ,attr)
874 (prog1 (cdar ,vals)
875 (setq ,attrs (cdr ,vals)))
876 (let ((,last ,vals)
877 (,found nil))
878 (while ,vals
879 (setq ,vals (cdr ,vals))
880 (when (string= (caar ,vals) ,attr)
881 (setq ,found (cdar ,vals))
882 (setcdr ,last (cdr ,vals))
883 (setq ,vals nil))
884 (setq ,last ,vals))
885 ,found)))))
887 (defun muse-publish-markup-anchor ()
888 (unless (get-text-property (match-end 1) 'muse-link)
889 (let ((text (muse-markup-text 'anchor (match-string 2))))
890 (unless (string= text "")
891 (save-match-data
892 (skip-chars-forward (concat muse-regexp-blank "\n"))
893 (muse-insert-markup text)))
894 (match-string 1))))
896 (defun muse-publish-markup-comment ()
897 (if (null muse-publish-comments-p)
899 (goto-char (match-end 0))
900 (muse-insert-markup (muse-markup-text 'comment-end))
901 (if (match-beginning 1)
902 (progn
903 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
904 (delete-region (match-beginning 0) (match-beginning 1)))
905 (delete-region (match-beginning 0) (match-end 0)))
906 (goto-char (match-beginning 0))
907 (muse-insert-markup (muse-markup-text 'comment-begin))))
909 (defun muse-publish-markup-tag ()
910 (let ((tag-info (muse-markup-tag-info (match-string 1))))
911 (when (and tag-info
912 (not (get-text-property (match-beginning 0) 'read-only)))
913 (let ((closed-tag (match-string 3))
914 (start (match-beginning 0))
915 (beg (point))
916 end attrs)
917 (when (nth 2 tag-info)
918 (let ((attrstr (match-string 2)))
919 (while (and attrstr
920 (string-match (concat "\\([^"
921 muse-regexp-blank
922 "=\n]+\\)\\(=\"\\"
923 "([^\"]+\\)\"\\)?")
924 attrstr))
925 (let ((attr (cons (downcase
926 (muse-match-string-no-properties 1 attrstr))
927 (muse-match-string-no-properties 3 attrstr))))
928 (setq attrstr (replace-match "" t t attrstr))
929 (if attrs
930 (nconc attrs (list attr))
931 (setq attrs (list attr)))))))
932 (if (and (cadr tag-info) (not closed-tag))
933 (if (muse-goto-tag-end (car tag-info) (nth 3 tag-info))
934 (delete-region (match-beginning 0) (point))
935 (setq tag-info nil)))
936 (when tag-info
937 (setq end (point-marker))
938 (delete-region start beg)
939 (goto-char start)
940 (let ((args (list start end)))
941 (if (nth 2 tag-info)
942 (nconc args (list attrs)))
943 (let ((muse-inhibit-style-tags nil))
944 ;; remove the inhibition
945 (apply (nth 4 tag-info) args)))
946 (set-marker end nil)))))
947 nil)
949 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
950 "Escape specials from BEG to END using style-specific :specials.
951 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
952 CONTEXT is used to figure out what kind of specials to escape.
954 The following contexts exist in Muse.
955 'underline _underlined text_
956 'literal =monospaced text= or <code> region (monospaced, escaped)
957 'emphasis *emphasized text*
958 'email email@example.com
959 'url http://example.com
960 'url-desc [[...][description of an explicit link]]
961 'image [[image.png]]
962 'example <example> region (monospaced, block context, escaped)
963 'verbatim <verbatim> region (escaped)
964 'document normal text"
965 (let ((specials (muse-style-element :specials nil t)))
966 (cond ((functionp specials)
967 (setq specials (funcall specials context)))
968 ((symbolp specials)
969 (setq specials (symbol-value specials))))
970 (if (functionp specials)
971 (funcall specials beg end ignore-read-only)
972 (save-excursion
973 (save-restriction
974 (narrow-to-region beg end)
975 (goto-char (point-min))
976 (while (< (point) (point-max))
977 (if (and (not ignore-read-only)
978 (get-text-property (point) 'read-only))
979 (goto-char (or (next-single-property-change (point) 'read-only)
980 (point-max)))
981 (let ((repl (or (assoc (char-after) specials)
982 (assoc (char-after)
983 muse-publish-markup-specials))))
984 (if (null repl)
985 (forward-char 1)
986 (delete-char 1)
987 (insert-before-markers (cdr repl)))))))))))
989 (defun muse-publish-markup-word ()
990 (let* ((beg (match-beginning 2))
991 (end (1- (match-end 2)))
992 (leader (buffer-substring-no-properties beg end))
993 open-tag close-tag mark-read-only loc context)
994 (cond
995 ((string= leader "_")
996 (setq context 'underline
997 open-tag (muse-markup-text 'begin-underline)
998 close-tag (muse-markup-text 'end-underline)))
999 ((string= leader "=")
1000 (setq context 'literal
1001 open-tag (muse-markup-text 'begin-literal)
1002 close-tag (muse-markup-text 'end-literal))
1003 (setq mark-read-only t))
1005 (let ((l (length leader)))
1006 (setq context 'emphasis)
1007 (cond
1008 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
1009 close-tag (muse-markup-text 'end-emph)))
1010 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
1011 close-tag (muse-markup-text 'end-more-emph)))
1012 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
1013 close-tag (muse-markup-text 'end-most-emph)))
1014 (t (setq context nil))))))
1015 (if (and context
1016 (not (get-text-property beg 'muse-link))
1017 (setq loc (search-forward leader nil t))
1018 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
1019 (not (eq (char-syntax (char-before (point))) ?\ ))
1020 (not (get-text-property (point) 'muse-link)))
1021 (progn
1022 (replace-match "")
1023 (delete-region beg end)
1024 (setq end (point-marker))
1025 (muse-insert-markup close-tag)
1026 (goto-char beg)
1027 (muse-insert-markup open-tag)
1028 (setq beg (point))
1029 (when mark-read-only
1030 (muse-publish-escape-specials beg end t context)
1031 (muse-publish-mark-read-only beg end))
1032 (set-marker end nil))
1033 (backward-char))
1034 nil))
1036 (defun muse-publish-markup-emdash ()
1037 (unless (get-text-property (match-beginning 0) 'muse-link)
1038 (let ((prespace (match-string 1))
1039 (postspace (match-string 2)))
1040 (delete-region (match-beginning 0) (match-end 0))
1041 (muse-insert-markup (muse-markup-text 'emdash prespace postspace))
1042 (when (eq (char-after) ?\<)
1043 (insert ?\n)))))
1045 (defun muse-publish-markup-enddots ()
1046 (unless (get-text-property (match-beginning 0) 'muse-link)
1047 (delete-region (match-beginning 0) (match-end 0))
1048 (muse-insert-markup (muse-markup-text 'enddots))))
1050 (defun muse-publish-markup-dots ()
1051 (unless (get-text-property (match-beginning 0) 'muse-link)
1052 (delete-region (match-beginning 0) (match-end 0))
1053 (muse-insert-markup (muse-markup-text 'dots))))
1055 (defun muse-publish-markup-rule ()
1056 (unless (get-text-property (match-beginning 0) 'muse-link)
1057 (delete-region (match-beginning 0) (match-end 0))
1058 (muse-insert-markup (muse-markup-text 'rule))))
1060 (defun muse-publish-markup-no-break-space ()
1061 (unless (get-text-property (match-beginning 0) 'muse-link)
1062 (delete-region (match-beginning 0) (match-end 0))
1063 (muse-insert-markup (muse-markup-text 'no-break-space))))
1065 (defun muse-publish-markup-heading ()
1066 (let* ((len (length (match-string 1)))
1067 (start (muse-markup-text
1068 (cond ((= len 1) 'section)
1069 ((= len 2) 'subsection)
1070 ((= len 3) 'subsubsection)
1071 (t 'section-other))
1072 len))
1073 (end (muse-markup-text
1074 (cond ((= len 1) 'section-end)
1075 ((= len 2) 'subsection-end)
1076 ((= len 3) 'subsubsection-end)
1077 (t 'section-other-end))
1078 len)))
1079 (delete-region (match-beginning 0) (match-end 0))
1080 (muse-insert-markup start)
1081 (end-of-line)
1082 (when end
1083 (muse-insert-markup end))
1084 (forward-line 1)
1085 (unless (eq (char-after) ?\n)
1086 (insert "\n"))
1087 (muse-publish-section-close len)))
1089 (defvar muse-publish-footnotes nil)
1091 (defun muse-publish-markup-footnote ()
1092 "Scan ahead and snarf up the footnote body"
1093 (cond
1094 ((get-text-property (match-beginning 0) 'muse-link)
1095 nil)
1096 ((= (muse-line-beginning-position) (match-beginning 0))
1099 (let ((footnote (save-match-data
1100 (string-to-number (match-string 1))))
1101 (oldtext (match-string 0))
1102 footnotemark)
1103 (delete-region (match-beginning 0) (match-end 0))
1104 (save-excursion
1105 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
1106 (let* ((start (match-beginning 0))
1107 (beg (goto-char (match-end 0)))
1108 (end (save-excursion
1109 (if (search-forward "\n\n" nil t)
1110 (copy-marker (match-beginning 0))
1111 (goto-char (point-max))
1112 (skip-chars-backward "\n")
1113 (point-marker)))))
1114 (while (re-search-forward
1115 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
1116 end t)
1117 (replace-match "\\1" t))
1118 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
1119 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
1120 (if (string= "" footnotemark-cmd)
1121 (setq footnotemark
1122 (concat (muse-markup-text 'footnote)
1123 (buffer-substring-no-properties beg end)
1124 (muse-markup-text 'footnote-end)))
1125 (setq footnotemark (format footnotemark-cmd footnote
1126 footnotemark-end-cmd))
1127 (unless muse-publish-footnotes
1128 (set (make-local-variable 'muse-publish-footnotes)
1129 (make-vector 256 nil)))
1130 (unless (aref muse-publish-footnotes footnote)
1131 (setq footnotemark
1132 (concat
1133 footnotemark
1134 (concat (format (muse-markup-text 'footnotetext)
1135 footnote)
1136 (buffer-substring-no-properties beg end)
1137 (muse-markup-text 'footnotetext-end))))
1138 (aset muse-publish-footnotes footnote footnotemark))))
1139 (goto-char end)
1140 (skip-chars-forward "\n")
1141 (delete-region start (point))
1142 (set-marker end nil))))
1143 (if footnotemark
1144 (muse-insert-markup footnotemark)
1145 (insert oldtext))))))
1147 (defun muse-publish-markup-fn-sep ()
1148 (delete-region (match-beginning 0) (match-end 0))
1149 (muse-insert-markup (muse-markup-text 'fn-sep)))
1151 (defun muse-insert-markup-end-list (&rest args)
1152 (let ((beg (point)))
1153 (apply 'insert args)
1154 (add-text-properties beg (point) '(end-list t))
1155 (muse-publish-mark-read-only beg (point))))
1157 (defun muse-publish-determine-dl-indent (continue indent-sym determine-sym)
1158 ;; If the caller doesn't know how much indentation to use, figure it
1159 ;; out ourselves. It is assumed that `muse-forward-list-item' has
1160 ;; been called just before this to set the match data.
1161 (when (and continue
1162 (symbol-value determine-sym))
1163 (save-match-data
1164 ;; snarf all leading whitespace
1165 (let ((indent (and (match-beginning 2)
1166 (buffer-substring (match-beginning 1)
1167 (match-beginning 2)))))
1168 (when (and indent
1169 (not (string= indent "")))
1170 (set indent-sym indent)
1171 (set determine-sym nil))))))
1173 (defun muse-publish-surround-dl (indent post-indent)
1174 (let* ((beg-item (muse-markup-text 'begin-dl-item))
1175 (end-item (muse-markup-text 'end-dl-item))
1176 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
1177 (end-ddt (muse-markup-text 'end-ddt))
1178 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
1179 (end-dde (muse-markup-text 'end-dde))
1180 (continue t)
1181 def-on-same-line beg)
1182 (while continue
1183 ;; envelope this as one term+definitions unit -- HTML does not
1184 ;; need this, but DocBook and Muse's custom XML format do
1185 (muse-insert-markup beg-item)
1186 (when (looking-at muse-dl-term-regexp)
1187 ;; find the term and wrap it with published markup
1188 (setq beg (point))
1189 (goto-char (match-end 1))
1190 (delete-region (point) (match-end 0))
1191 (muse-insert-markup-end-list end-ddt)
1192 ;; if definition is immediately after term, move to next line
1193 (unless (eq (char-after) ?\n)
1194 (insert ?\n))
1195 (save-excursion
1196 (goto-char beg)
1197 (delete-region (point) (match-beginning 1))
1198 (muse-insert-markup beg-ddt)))
1199 (setq beg (point)
1200 ;; move past current item
1201 continue (muse-forward-list-item 'dl-term indent))
1202 (save-restriction
1203 (narrow-to-region beg (point))
1204 (goto-char (point-min))
1205 ;; publish each definition that we find, defaulting to an
1206 ;; empty definition if none are found
1207 (muse-publish-surround-text beg-dde end-dde
1208 (lambda (indent)
1209 (muse-forward-list-item 'dl-entry indent))
1210 indent post-indent
1211 #'muse-publish-determine-dl-indent)
1212 (goto-char (point-max))
1213 (skip-chars-backward (concat muse-regexp-blank "\n"))
1214 (muse-insert-markup-end-list end-item)
1215 (when continue
1216 (goto-char (point-max)))))))
1218 (defun muse-publish-strip-list-indentation (list-item empty-line indent post-indent)
1219 (let ((list-nested nil)
1220 (indent-found nil))
1221 (while (< (point) (point-max))
1222 (when (and (looking-at list-item)
1223 (not (or (get-text-property
1224 (muse-list-item-critical-point) 'read-only)
1225 (get-text-property
1226 (muse-list-item-critical-point) 'muse-link))))
1227 ;; if we encounter a list item, allow no post-indent space
1228 (setq list-nested t))
1229 (when (and (not (looking-at empty-line))
1230 (looking-at (concat indent "\\("
1231 (or (and list-nested "")
1232 post-indent)
1233 "\\)")))
1234 ;; if list is not nested, remove indentation
1235 (unless indent-found
1236 (setq post-indent (match-string 1)
1237 indent-found t))
1238 (replace-match ""))
1239 (forward-line 1))))
1241 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent determine-indent-func list-item)
1242 (unless list-item
1243 (setq list-item (format muse-list-item-regexp
1244 (concat "[" muse-regexp-blank "]*"))))
1245 (let ((continue t)
1246 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
1247 (determine-indent (if determine-indent-func t nil))
1248 (new-indent indent)
1249 (first t)
1250 beg)
1251 (unless indent
1252 (setq indent (concat "[" muse-regexp-blank "]+")))
1253 (if post-indent
1254 (setq post-indent (concat " \\{0," (number-to-string post-indent)
1255 "\\}"))
1256 (setq post-indent ""))
1257 (while continue
1258 (if (or (not end-tag) (string= end-tag ""))
1259 ;; if no end of list item markup exists, treat the beginning
1260 ;; of list item markup as it if it were the end -- this
1261 ;; prevents multiple-level lists from being confused
1262 (muse-insert-markup-end-list beg-tag)
1263 (muse-insert-markup beg-tag))
1264 (setq beg (point)
1265 ;; move past current item; continue is non-nil if there
1266 ;; are more like items to be processed
1267 continue (if (and determine-indent-func first)
1268 (funcall move-func (concat indent post-indent))
1269 (funcall move-func indent)))
1270 (when determine-indent-func
1271 (funcall determine-indent-func continue 'new-indent 'determine-indent))
1272 (when continue
1273 ;; remove list markup if we encountered another item of the
1274 ;; same type
1275 (replace-match "" t t nil 1))
1276 (save-restriction
1277 ;; narrow to current item
1278 (narrow-to-region beg (point))
1279 ;; move to second line of text
1280 (goto-char (point-min))
1281 (while (and (< (point) (point-max))
1282 (looking-at empty-line))
1283 (forward-line 1))
1284 (forward-line 1)
1285 ;; strip list indentation
1286 (muse-publish-strip-list-indentation list-item empty-line
1287 indent post-indent)
1288 (skip-chars-backward (concat muse-regexp-blank "\n"))
1289 (muse-insert-markup-end-list end-tag)
1290 (when determine-indent-func
1291 (setq indent new-indent))
1292 (when first
1293 (setq first nil))
1294 (when continue
1295 (goto-char (point-max)))))))
1297 (defun muse-publish-ensure-blank-line ()
1298 "Make sure that a blank line exists on the line before point."
1299 (save-excursion
1300 (beginning-of-line)
1301 (cond ((eq (point) (point-min)) nil)
1302 ((prog2 (backward-char) (bolp) (forward-char)) nil)
1303 (t (insert "\n")))))
1305 (defun muse-publish-markup-list ()
1306 "Markup a list entry.
1307 This function works by marking up items of the same list level
1308 and type, respecting the end-of-list property."
1309 (let* ((str (match-string 1))
1310 (type (muse-list-item-type str))
1311 (indent (buffer-substring (muse-line-beginning-position)
1312 (match-beginning 1)))
1313 (post-indent (length str)))
1314 (cond
1315 ((or (get-text-property (muse-list-item-critical-point) 'read-only)
1316 (get-text-property (muse-list-item-critical-point) 'muse-link))
1317 nil)
1318 ((eq type 'ul)
1319 (unless (eq (char-after (match-end 1)) ?-)
1320 (delete-region (match-beginning 0) (match-end 0))
1321 (muse-publish-ensure-blank-line)
1322 (muse-insert-markup (muse-markup-text 'begin-uli))
1323 (save-excursion
1324 (muse-publish-surround-text
1325 (muse-markup-text 'begin-uli-item)
1326 (muse-markup-text 'end-uli-item)
1327 (lambda (indent)
1328 (muse-forward-list-item 'ul indent))
1329 indent post-indent)
1330 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1331 (forward-line 1)))
1332 ((eq type 'ol)
1333 (delete-region (match-beginning 0) (match-end 0))
1334 (muse-publish-ensure-blank-line)
1335 (muse-insert-markup (muse-markup-text 'begin-oli))
1336 (save-excursion
1337 (muse-publish-surround-text
1338 (muse-markup-text 'begin-oli-item)
1339 (muse-markup-text 'end-oli-item)
1340 (lambda (indent)
1341 (muse-forward-list-item 'ol indent))
1342 indent post-indent)
1343 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1344 (forward-line 1))
1345 ((not (string= (match-string 2) ""))
1346 ;; must have an initial term
1347 (goto-char (match-beginning 0))
1348 (muse-publish-ensure-blank-line)
1349 (muse-insert-markup (muse-markup-text 'begin-dl))
1350 (save-excursion
1351 (muse-publish-surround-dl indent post-indent)
1352 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1353 (forward-line 1))))
1354 nil)
1356 (defun muse-publish-markup-quote ()
1357 "Markup a quoted paragraph.
1358 The reason this function is so funky, is to prevent text properties
1359 like read-only from being inadvertently deleted."
1360 (let* ((ws (match-string 1))
1361 (centered (>= (string-width ws) 6))
1362 (begin-elem (if centered 'begin-center 'begin-quote-item))
1363 (end-elem (if centered 'end-center 'end-quote-item)))
1364 (replace-match "" t t nil 1)
1365 (unless centered
1366 (muse-insert-markup (muse-markup-text 'begin-quote)))
1367 (muse-publish-surround-text (muse-markup-text begin-elem)
1368 (muse-markup-text end-elem)
1369 (function (lambda (indent)
1370 (muse-forward-paragraph)
1371 nil)))
1372 (unless centered
1373 (muse-insert-markup (muse-markup-text 'end-quote)))))
1375 (defun muse-publish-markup-leading-space (markup-space multiple)
1376 (let (count)
1377 (when (and markup-space
1378 (>= (setq count (skip-chars-forward " ")) 0))
1379 (delete-region (muse-line-beginning-position) (point))
1380 (while (> count 0)
1381 (muse-insert-markup markup-space)
1382 (setq count (- count multiple))))))
1384 (defun muse-publish-markup-verse ()
1385 (let ((leader (match-string 0)))
1386 (goto-char (match-beginning 0))
1387 (muse-insert-markup (muse-markup-text 'begin-verse))
1388 (while (looking-at leader)
1389 (replace-match "")
1390 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1391 (let ((beg (point)))
1392 (end-of-line)
1393 (cond
1394 ((bolp)
1395 (let ((text (muse-markup-text 'empty-verse-line)))
1396 (when text (muse-insert-markup text))))
1397 ((save-excursion
1398 (save-match-data
1399 (forward-line 1)
1400 (or (looking-at (concat leader "["
1401 muse-regexp-blank
1402 "]*$"))
1403 (not (looking-at leader)))))
1404 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1405 (end-text (muse-markup-text 'end-last-stanza-line)))
1406 (when end-text (muse-insert-markup end-text))
1407 (goto-char beg)
1408 (when begin-text (muse-insert-markup begin-text))
1409 (end-of-line)))
1411 (let ((begin-text (muse-markup-text 'begin-verse-line))
1412 (end-text (muse-markup-text 'end-verse-line)))
1413 (when end-text (muse-insert-markup end-text))
1414 (goto-char beg)
1415 (when begin-text (muse-insert-markup begin-text))
1416 (end-of-line))))
1417 (forward-line 1))))
1418 (muse-insert-markup (muse-markup-text 'end-verse))
1419 (insert ?\n))
1421 (defun muse-publish-trim-table (table)
1422 "Remove completely blank columns from table, if at start or end of row."
1423 ;; remove first
1424 (catch 'found
1425 (dolist (row (cdr table))
1426 (let ((el (cadr row)))
1427 (when (and (stringp el) (not (string= el "")))
1428 (throw 'found t))))
1429 (dolist (row (cdr table))
1430 (setcdr row (cddr row)))
1431 (setcar table (1- (car table))))
1432 ;; remove last
1433 (catch 'found
1434 (dolist (row (cdr table))
1435 (let ((el (car (last row))))
1436 (when (and (stringp el) (not (string= el "")))
1437 (throw 'found t))))
1438 (dolist (row (cdr table))
1439 (setcdr (last row 2) nil))
1440 (setcar table (1- (car table))))
1441 table)
1443 (defun muse-publish-table-fields (beg end)
1444 "Parse given region as a table, returning a cons cell.
1445 The car is the length of the longest row.
1447 The cdr is a list of the fields of the table, with the first
1448 element indicating the type of the row:
1449 1: body, 2: header, 3: footer, hline: separator.
1451 The existing region will be removed, except for initial blank lines."
1452 (unless (muse-publishing-directive "disable-tables")
1453 (let ((longest 0)
1454 (left 0)
1455 (seen-hline nil)
1456 fields field-list)
1457 (save-restriction
1458 (narrow-to-region beg end)
1459 (goto-char (point-min))
1460 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1461 (forward-line 1))
1462 (setq beg (point))
1463 (while (= left 0)
1464 (cond
1465 ((looking-at muse-table-hline-regexp)
1466 (when field-list ; skip if at the beginning of table
1467 (if seen-hline
1468 (setq field-list (cons (cons 'hline nil) field-list))
1469 (dolist (field field-list)
1470 ;; the preceding fields are header lines
1471 (setcar field 2))
1472 (setq seen-hline t))))
1473 ((looking-at muse-table-line-regexp)
1474 (setq fields (cons (length (match-string 1))
1475 (mapcar #'muse-trim-whitespace
1476 (split-string (match-string 0)
1477 muse-table-field-regexp)))
1478 field-list (cons fields field-list)
1479 longest (max (length fields) longest))
1480 ;; strip initial bars, if they exist
1481 (let ((first (cadr fields)))
1482 (when (and first (string-match "\\`|+\\s-*" first))
1483 (setcar (cdr fields) (replace-match "" t t first))))))
1484 (setq left (forward-line 1))))
1485 (delete-region beg end)
1486 (if (= longest 0)
1487 (cons 0 nil)
1488 ;; if the last line was an hline, remove it
1489 (when (eq (caar field-list) 'hline)
1490 (setq field-list (cdr field-list)))
1491 (muse-publish-trim-table (cons (1- longest) (nreverse field-list)))))))
1493 (defun muse-publish-markup-table ()
1494 "Style does not support tables.\n")
1496 (defun muse-publish-table-el-table (variant)
1497 "Publish table.el-style tables in the format given by VARIANT."
1498 (when (condition-case nil
1499 (progn (require 'table)
1501 (error nil))
1502 (let ((muse-buf (current-buffer)))
1503 (save-restriction
1504 (narrow-to-region (match-beginning 0) (match-end 0))
1505 (goto-char (point-min))
1506 (forward-line 1)
1507 (search-forward "|" nil t)
1508 (with-temp-buffer
1509 (let ((temp-buf (current-buffer)))
1510 (with-current-buffer muse-buf
1511 (table-generate-source variant temp-buf))
1512 (with-current-buffer muse-buf
1513 (delete-region (point-min) (point-max))
1514 (insert-buffer-substring temp-buf)
1515 (muse-publish-mark-read-only (point-min) (point-max)))))))))
1517 (defun muse-publish-markup-table-el ()
1518 "Mark up table.el-style tables."
1519 (cond ((muse-style-derived-p 'html)
1520 (muse-publish-table-el-table 'html))
1521 ((muse-style-derived-p 'latex)
1522 (muse-publish-table-el-table 'latex))
1523 ((muse-style-derived-p 'docbook)
1524 (muse-publish-table-el-table 'cals))
1525 (t "Style does not support table.el tables.\n")))
1527 (defun muse-publish-escape-specials-in-string (string &optional context)
1528 "Escape specials in STRING using style-specific :specials.
1529 CONTEXT is used to figure out what kind of specials to escape.
1531 See the documentation of the `muse-publish-escape-specials'
1532 function for the list of available contexts."
1533 (unless string
1534 (setq string ""))
1535 (let ((specials (muse-style-element :specials nil t)))
1536 (cond ((functionp specials)
1537 (setq specials (funcall specials context)))
1538 ((symbolp specials)
1539 (setq specials (symbol-value specials))))
1540 (if (functionp specials)
1541 (funcall specials string)
1542 (apply (function concat)
1543 (mapcar
1544 (lambda (ch)
1545 (let ((repl (or (assoc ch specials)
1546 (assoc ch muse-publish-markup-specials))))
1547 (if (null repl)
1548 (char-to-string ch)
1549 (cdr repl))))
1550 (append string nil))))))
1552 (defun muse-publish-markup-email ()
1553 (let* ((beg (match-end 1))
1554 (addr (buffer-substring-no-properties beg (match-end 0))))
1555 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1556 (goto-char beg)
1557 (delete-region beg (match-end 0))
1558 (if (or (eq (char-before (match-beginning 0)) ?\")
1559 (eq (char-after (match-end 0)) ?\"))
1560 (insert addr)
1561 (insert (format (muse-markup-text 'email-addr) addr addr)))
1562 (muse-publish-mark-read-only beg (point))))
1564 (defun muse-publish-classify-url (target)
1565 "Transform anchors and get published name, if TARGET is a page.
1566 The return value is two linked cons cells. The car is the type
1567 of link, the cadr is the page name, and the cddr is the anchor."
1568 (save-match-data
1569 (cond ((or (null target) (string= target ""))
1570 nil)
1571 ((string-match "\\`[uU][rR][lL]:\\(.+\\)\\'" target)
1572 (cons 'url (cons (match-string 1 target) nil)))
1573 ((string-match muse-image-regexp target)
1574 (cons 'image (cons target nil)))
1575 ((string-match muse-url-regexp target)
1576 (cons 'url (cons target nil)))
1577 ((string-match muse-file-regexp target)
1578 (cons 'file (cons target nil)))
1579 ((string-match "#" target)
1580 (if (eq (aref target 0) ?\#)
1581 (cons 'anchor-ref (cons nil (substring target 1)))
1582 (cons 'link-and-anchor
1583 (cons (muse-publish-link-page
1584 (substring target 0 (match-beginning 0)))
1585 (substring target (match-end 0))))))
1587 (cons 'link (cons (muse-publish-link-page target) nil))))))
1589 (defun muse-publish-url-desc (desc explicit)
1590 (when desc
1591 (dolist (transform muse-publish-desc-transforms)
1592 (setq desc (save-match-data
1593 (when desc (funcall transform desc explicit)))))
1594 (setq desc (muse-link-unescape desc))
1595 (muse-publish-escape-specials-in-string desc 'url-desc)))
1597 (defun muse-publish-url (url &optional desc orig-url explicit)
1598 "Resolve a URL into its final <a href> form."
1599 (let (type anchor)
1600 (dolist (transform muse-publish-url-transforms)
1601 (setq url (save-match-data (when url (funcall transform url explicit)))))
1602 (if desc
1603 (setq desc (muse-publish-url-desc desc explicit))
1604 (if orig-url
1605 (setq orig-url (muse-publish-url-desc orig-url explicit))))
1606 (let ((target (muse-publish-classify-url url)))
1607 (setq type (car target)
1608 url (if (eq type 'image)
1609 (muse-publish-escape-specials-in-string (cadr target)
1610 'image)
1611 (muse-publish-escape-specials-in-string (cadr target) 'url))
1612 anchor (muse-publish-escape-specials-in-string
1613 (cddr target) 'url)))
1614 (cond ((eq type 'anchor-ref)
1615 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1616 ((and desc (string-match muse-image-regexp desc))
1617 (let ((ext (or (file-name-extension desc) "")))
1618 (setq desc (muse-path-sans-extension desc))
1619 (muse-markup-text 'image-link url desc ext)))
1620 ((string= url "")
1621 desc)
1622 ((eq type 'image)
1623 (let ((ext (or (file-name-extension url) "")))
1624 (setq url (muse-path-sans-extension url))
1625 (if desc
1626 (muse-markup-text 'image-with-desc url ext desc)
1627 (muse-markup-text 'image url ext))))
1628 ((eq type 'link-and-anchor)
1629 (muse-markup-text 'link-and-anchor url anchor
1630 (or desc orig-url)
1631 (muse-path-sans-extension url)))
1632 ((eq type 'link)
1633 (muse-markup-text 'link url (or desc orig-url)))
1635 (or (and (or desc
1636 (not (string= url orig-url)))
1637 (let ((text (muse-markup-text 'url-and-desc url
1638 (or desc orig-url))))
1639 (and (not (string= text ""))
1640 text)))
1641 (muse-markup-text 'url url (or desc orig-url)))))))
1643 (defun muse-publish-insert-url (url &optional desc orig-url explicit)
1644 "Resolve a URL into its final <a href> form."
1645 (delete-region (match-beginning 0) (match-end 0))
1646 (let ((text (muse-publish-url url desc orig-url explicit)))
1647 (when text
1648 (muse-insert-markup text))))
1650 (defun muse-publish-markup-link ()
1651 (let (desc explicit orig-link link)
1652 (setq explicit (save-match-data
1653 (if (string-match muse-explicit-link-regexp
1654 (match-string 0))
1655 t nil)))
1656 (setq orig-link (if explicit (match-string 1) (match-string 0)))
1657 (setq desc (when explicit (match-string 2)))
1658 (setq link (if explicit
1659 (muse-handle-explicit-link orig-link)
1660 (muse-handle-implicit-link orig-link)))
1661 (when (and link
1662 (or explicit
1663 (not (or (eq (char-before (match-beginning 0)) ?\")
1664 (eq (char-after (match-end 0)) ?\")))))
1665 ;; if explicit link has no user-provided description, treat it
1666 ;; as if it were an implicit link
1667 (when (and explicit (not desc))
1668 (setq explicit nil))
1669 (muse-publish-insert-url link desc orig-link explicit))))
1671 (defun muse-publish-markup-url ()
1672 (unless (or (eq (char-before (match-beginning 0)) ?\")
1673 (eq (char-after (match-end 0)) ?\"))
1674 (let ((url (match-string 0)))
1675 (muse-publish-insert-url url nil url))))
1677 ;; Default publishing tags
1679 (defcustom muse-publish-contents-depth 2
1680 "The number of heading levels to include with <contents> tags."
1681 :type 'integer
1682 :group 'muse-publish)
1684 (defun muse-publish-contents-tag (beg end attrs)
1685 (set (make-local-variable 'muse-publish-generate-contents)
1686 (cons (copy-marker (point) t)
1687 (let ((depth (cdr (assoc "depth" attrs))))
1688 (or (and depth (string-to-number depth))
1689 muse-publish-contents-depth)))))
1691 (defun muse-publish-verse-tag (beg end)
1692 (muse-publish-ensure-block beg end)
1693 (save-excursion
1694 (save-restriction
1695 (narrow-to-region beg end)
1696 (goto-char (point-min))
1697 (while (eq ?\ (char-syntax (char-after)))
1698 (delete-char 1))
1699 (while (< (point) (point-max))
1700 (insert "> ")
1701 (forward-line))
1702 (if (eq ?\ (char-syntax (char-before)))
1703 (delete-char -1)))))
1705 (defun muse-publish-mark-read-only (beg end)
1706 "Add read-only properties to the given region."
1707 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1708 nil)
1710 (defun muse-publish-mark-link (&optional beg end)
1711 "Indicate that the given region is a Muse link, so that other
1712 markup elements respect it. If a region is not specified, use
1713 the 0th match data to determine it.
1715 This is usually applied to explicit links."
1716 (unless beg (setq beg (match-beginning 0)))
1717 (unless end (setq end (match-end 0)))
1718 (add-text-properties beg end '(muse-link t))
1719 nil)
1721 (defun muse-publish-quote-tag (beg end)
1722 (muse-publish-ensure-block beg)
1723 (save-excursion
1724 (save-restriction
1725 (narrow-to-region beg end)
1726 (let ((quote-regexp "^\\(<\\(/?\\)quote>\\)"))
1727 (muse-insert-markup (muse-markup-text 'begin-quote))
1728 (while (progn
1729 (unless (looking-at (concat "[" muse-regexp-blank "\n]*"
1730 "<quote>"))
1731 (muse-publish-surround-text
1732 (muse-markup-text 'begin-quote-item)
1733 (muse-markup-text 'end-quote-item)
1734 (function
1735 (lambda (indent)
1736 (muse-forward-paragraph)
1737 (goto-char (match-end 0))
1738 (and (< (point) (point-max))
1739 (not (looking-at quote-regexp)))))
1740 nil nil nil
1741 quote-regexp))
1742 (if (>= (point) (point-max))
1744 (and (search-forward "<quote>" nil t)
1745 (muse-goto-tag-end "quote" t)
1746 (progn (forward-line 1) t)
1747 (< (point) (point-max))))))
1748 (goto-char (point-max))
1749 (muse-insert-markup (muse-markup-text 'end-quote))))))
1751 (defun muse-publish-code-tag (beg end)
1752 (muse-publish-escape-specials beg end nil 'literal)
1753 (goto-char beg)
1754 (insert (muse-markup-text 'begin-literal))
1755 (goto-char end)
1756 (insert (muse-markup-text 'end-literal))
1757 (muse-publish-mark-read-only beg (point)))
1759 (defun muse-publish-cite-tag (beg end attrs)
1760 (let* ((type (muse-publish-get-and-delete-attr "type" attrs))
1761 (muse-publishing-directives muse-publishing-directives)
1762 (citetag
1763 (cond ((string-equal type "author")
1764 'begin-cite-author)
1765 ((string-equal type "year")
1766 'begin-cite-year)
1768 'begin-cite))))
1769 (goto-char beg)
1770 (insert (muse-markup-text citetag (muse-publishing-directive "bibsource")))
1771 (goto-char end)
1772 (insert (muse-markup-text 'end-cite))
1773 (muse-publish-mark-read-only beg (point))))
1775 (defun muse-publish-src-tag (beg end attrs)
1776 (muse-publish-example-tag beg end))
1778 (defun muse-publish-example-tag (beg end)
1779 (muse-publish-ensure-block beg end)
1780 (muse-publish-escape-specials beg end nil 'example)
1781 (goto-char beg)
1782 (insert (muse-markup-text 'begin-example))
1783 (goto-char end)
1784 (insert (muse-markup-text 'end-example))
1785 (muse-publish-mark-read-only beg (point)))
1787 (defun muse-publish-literal-tag (beg end attrs)
1788 "Ensure that the text between BEG and END is not interpreted later on.
1790 ATTRS is an alist of attributes.
1792 If it contains a \"style\" element, delete the region if the
1793 current style is neither derived from nor equal to this style.
1795 If it contains both a \"style\" element and an \"exact\" element
1796 with the value \"t\", delete the region only if the current style
1797 is exactly this style."
1798 (let* ((style (cdr (assoc "style" attrs)))
1799 (exact (cdr (assoc "exact" attrs)))
1800 (exactp (and (stringp exact) (string= exact "t"))))
1801 (if (or (not style)
1802 (and exactp (equal (muse-style style)
1803 muse-publishing-current-style))
1804 (and (not exactp) (muse-style-derived-p style)))
1805 (muse-publish-mark-read-only beg end)
1806 (delete-region beg end)
1807 (when (and (bolp) (eolp) (not (eobp)))
1808 (delete-char 1)))))
1810 (defun muse-publish-verbatim-tag (beg end)
1811 (muse-publish-escape-specials beg end nil 'verbatim)
1812 (muse-publish-mark-read-only beg end))
1814 (defalias 'muse-publish-class-tag 'ignore)
1816 (defun muse-publish-call-tag-on-buffer (tag &optional attrs)
1817 "Transform the current buffer as if it were surrounded by the tag TAG.
1818 If attributes ATTRS are given, pass them to the tag function."
1819 (let ((tag-info (muse-markup-tag-info tag)))
1820 (when tag-info
1821 (let* ((end (progn (goto-char (point-max)) (point-marker)))
1822 (args (list (point-min) end))
1823 (muse-inhibit-style-tags nil))
1824 (when (nth 2 tag-info)
1825 (nconc args (list attrs)))
1826 (apply (nth 4 tag-info) args)
1827 (set-marker end nil)))))
1829 (defun muse-publish-examplify-buffer (&optional attrs)
1830 "Transform the current buffer as if it were an <example> region."
1831 (muse-publish-call-tag-on-buffer "example" attrs))
1833 (defun muse-publish-srcify-buffer (&optional attrs)
1834 "Transform the current buffer as if it were a <src> region."
1835 (muse-publish-call-tag-on-buffer "src" attrs))
1837 (defun muse-publish-versify-buffer (&optional attrs)
1838 "Transform the current buffer as if it were a <verse> region."
1839 (muse-publish-call-tag-on-buffer "verse" attrs)
1840 (muse-publish-markup ""
1841 `((100 ,(concat "^[" muse-regexp-blank "]*> ") 0
1842 muse-publish-markup-verse)))
1843 (goto-char (point-min)))
1845 (defmacro muse-publish-markup-attribute (beg end attrs reinterp &rest body)
1846 "Evaluate BODY within the bounds of BEG and END.
1847 ATTRS is an alist. Only the \"markup\" element of ATTRS is acted
1850 If it is omitted, publish the region with the normal Muse rules.
1851 If RE-INTERP is specified, this is done immediately in a new
1852 publishing process. Currently, RE-INTERP is specified only by
1853 the <include> tag.
1855 If \"nil\", do not mark up the region at all, but prevent it from
1856 being further interpreted by Muse.
1858 If \"example\", treat the region as if it was surrounded by the
1859 <example> tag.
1861 If \"src\", treat the region as if it was surrounded by the
1862 <src> tag.
1864 If \"verse\", treat the region as if it was surrounded by the
1865 <verse> tag, to preserve newlines.
1867 Otherwise, it should be the name of a function to call in the
1868 narrowed region after evaluating BODY. The function should
1869 take the ATTRS parameter.
1871 BEG is modified to be the start of the published markup."
1872 (let ((markup (make-symbol "markup"))
1873 (markup-function (make-symbol "markup-function")))
1874 `(let ((,markup (muse-publish-get-and-delete-attr "markup" ,attrs)))
1875 (save-restriction
1876 (narrow-to-region ,beg ,end)
1877 (goto-char (point-min))
1878 ,@body
1879 (if (not ,markup)
1880 (when ,reinterp
1881 (muse-publish-markup-region (point-min) (point-max))
1882 (muse-publish-mark-read-only (point-min) (point-max))
1883 (goto-char (point-max)))
1884 (let ((,markup-function (read ,markup)))
1885 (cond ((eq ,markup-function 'example)
1886 (setq ,markup-function #'muse-publish-examplify-buffer))
1887 ((eq ,markup-function 'src)
1888 (setq ,markup-function #'muse-publish-srcify-buffer))
1889 ((eq ,markup-function 'verse)
1890 (setq ,markup-function #'muse-publish-versify-buffer))
1891 ((and ,markup-function (not (functionp ,markup-function)))
1892 (error "Invalid markup function `%s'" ,markup))
1893 (t nil))
1894 (if ,markup-function
1895 (funcall ,markup-function ,attrs)
1896 (muse-publish-mark-read-only (point-min) (point-max))
1897 (goto-char (point-max)))))))))
1899 (put 'muse-publish-markup-attribute 'lisp-indent-function 4)
1900 (put 'muse-publish-markup-attribute 'edebug-form-spec
1901 '(form form form form body))
1903 (defun muse-publish-lisp-tag (beg end attrs)
1904 (muse-publish-markup-attribute beg end attrs nil
1905 (save-excursion
1906 (save-restriction
1907 (let ((str (muse-eval-lisp
1908 (prog1
1909 (concat "(progn "
1910 (buffer-substring-no-properties (point-min)
1911 (point-max))
1912 ")")
1913 (delete-region (point-min) (point-max))
1914 (widen)))))
1915 (set-text-properties 0 (length str) nil str)
1916 (insert str))))))
1918 (defun muse-publish-command-tag (beg end attrs)
1919 (muse-publish-markup-attribute beg end attrs nil
1920 (while (looking-at "\\s-*$")
1921 (forward-line))
1922 (let ((interp (muse-publish-get-and-delete-attr "interp" attrs)))
1923 (if interp
1924 (shell-command-on-region (point) (point-max) interp t t)
1925 (shell-command
1926 (prog1
1927 (buffer-substring-no-properties (point) (point-max))
1928 (delete-region (point-min) (point-max)))
1929 t)))
1930 ;; make sure there is a newline at end
1931 (goto-char (point-max))
1932 (forward-line 0)
1933 (unless (looking-at "\\s-*$")
1934 (goto-char (point-max))
1935 (insert ?\n))
1936 (goto-char (point-min))))
1938 (defun muse-publish-perl-tag (beg end attrs)
1939 (muse-publish-command-tag beg end
1940 (cons (cons "interp" (executable-find "perl"))
1941 attrs)))
1943 (defun muse-publish-python-tag (beg end attrs)
1944 (muse-publish-command-tag beg end
1945 (cons (cons "interp" (executable-find "python"))
1946 attrs)))
1948 (defun muse-publish-ruby-tag (beg end attrs)
1949 (muse-publish-command-tag beg end
1950 (cons (cons "interp" (executable-find "ruby"))
1951 attrs)))
1953 (defun muse-publish-comment-tag (beg end)
1954 (if (null muse-publish-comments-p)
1955 (delete-region beg end)
1956 (goto-char end)
1957 (muse-insert-markup (muse-markup-text 'comment-end))
1958 (muse-publish-mark-read-only beg end)
1959 (goto-char beg)
1960 (muse-insert-markup (muse-markup-text 'comment-begin))))
1962 (defun muse-publish-include-tag (beg end attrs)
1963 "Include the named file at the current location during publishing.
1965 <include file=\"...\" markup=\"...\">
1967 The `markup' attribute controls how this file is marked up after
1968 being inserted. See `muse-publish-markup-attribute' for an
1969 explanation of how it works."
1970 (let ((filename (muse-publish-get-and-delete-attr "file" attrs))
1971 (muse-publishing-directives muse-publishing-directives))
1972 (if filename
1973 (setq filename (expand-file-name
1974 filename
1975 (file-name-directory muse-publishing-current-file)))
1976 (error "No file attribute specified in <include> tag"))
1977 (muse-publish-markup-attribute beg end attrs t
1978 (insert-file-contents-literally filename))))
1980 (defun muse-publish-mark-up-tag (beg end attrs)
1981 "Run an Emacs Lisp function on the region delimted by this tag.
1983 <markup function=\"...\" style=\"...\" exact=\"...\">
1985 The optional \"function\" attribute controls how this section is
1986 marked up. If used, it should be the name of a function to call
1987 with the buffer narrowed to the delimited region. Note that no
1988 further marking-up will be performed on this region.
1990 If \"function\" is omitted, use the standard Muse markup function.
1991 This is useful for marking up content in headers and footers.
1993 The optional \"style\" attribute causes the region to be deleted
1994 if the current style is neither derived from nor equal to this
1995 style.
1997 If both a \"style\" attribute and an \"exact\" attribute are
1998 provided, and \"exact\" is \"t\", delete the region only if the
1999 current style is exactly this style."
2000 (let* ((style (cdr (assoc "style" attrs)))
2001 (exact (cdr (assoc "exact" attrs)))
2002 (exactp (and (stringp exact) (string= exact "t"))))
2003 (if (or (not style)
2004 (and exactp (equal (muse-style style)
2005 muse-publishing-current-style))
2006 (and (not exactp) (muse-style-derived-p style)))
2007 (let* ((function (cdr (assoc "function" attrs)))
2008 (muse-publish-use-header-footer-tags nil)
2009 (muse-publishing-directives muse-publishing-directives)
2010 (markup-function (and function (intern function))))
2011 (if (and markup-function (functionp markup-function))
2012 (save-restriction
2013 (narrow-to-region beg end)
2014 (funcall markup-function)
2015 (goto-char (point-max)))
2016 (let ((muse-publish-inhibit-style-hooks t))
2017 (muse-publish-markup-region beg end)))
2018 (muse-publish-mark-read-only beg (point)))
2019 (delete-region beg end))))
2021 ;; Miscellaneous helper functions
2023 (defun muse-publish-strip-URL (string &rest ignored)
2024 "If the text \"URL:\" exists at the beginning of STRING, remove it.
2025 The text is removed regardless of whether and part of it is uppercase."
2026 (save-match-data
2027 (if (string-match "\\`[uU][rR][lL]:\\(.+\\)\\'" string)
2028 (match-string 1 string)
2029 string)))
2031 (defun muse-publish-strip-tags (string)
2032 "Remove all tags from the string."
2033 (while (string-match "<.*?>" string)
2034 (setq string (replace-match "" nil t string)))
2035 string)
2037 (defun muse-publish-markup-type (category default-func)
2038 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
2039 (funcall (or rule default-func))))
2041 (defun muse-published-buffer-contents (buffer)
2042 (with-current-buffer buffer
2043 (goto-char (point-min))
2044 (let ((beg (and (search-forward "Emacs Muse begins here")
2045 (muse-line-end-position)))
2046 (end (and (search-forward "Emacs Muse ends here")
2047 (muse-line-beginning-position))))
2048 (buffer-substring-no-properties beg end))))
2050 (defun muse-published-contents (file)
2051 (when (file-readable-p file)
2052 (muse-with-temp-buffer
2053 (insert-file-contents-literally file)
2054 (muse-published-buffer-contents (current-buffer)))))
2056 (defun muse-publish-transform-output
2057 (file temp-file output-path name gen-func &rest cleanup-exts)
2058 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
2059 (setq file (muse-page-name file))
2060 (message "Generating %s output for %s..." name file)
2061 (if (not (funcall gen-func temp-file output-path))
2062 (message "Generating %s from %s...failed" name file)
2063 (message "Generating %s output for %s...done" name file)
2064 (muse-delete-file-if-exists temp-file)
2065 (dolist (ext cleanup-exts)
2066 (muse-delete-file-if-exists
2067 (expand-file-name (concat file ext)
2068 (file-name-directory output-path))))
2069 (message "Wrote %s" output-path)))
2071 (defun muse-publish-read-only (string)
2072 (let ((end (1- (length string))))
2073 (add-text-properties 0 end
2074 '(rear-nonsticky (read-only) read-only t)
2075 string)
2076 string))
2078 ;;; muse-publish.el ends here