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