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