Make second release candidate (3.02.92) available.
[muse-el.git] / lisp / muse-publish.el
blobfdf94770e0445dd2892444c9b5bc9363f8bf8748
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 muse-publish-contents-tag)
243 ("verse" t nil muse-publish-verse-tag)
244 ("example" t nil muse-publish-example-tag)
245 ("code" t nil muse-publish-code-tag)
246 ("quote" t nil muse-publish-quote-tag)
247 ("literal" t nil muse-publish-mark-read-only)
248 ("verbatim" t nil muse-publish-verbatim-tag)
249 ("lisp" t t muse-publish-lisp-tag)
250 ("class" t t muse-publish-class-tag)
251 ("command" t t muse-publish-command-tag)
252 ("perl" t t muse-publish-perl-tag)
253 ("python" t t muse-publish-python-tag)
254 ("ruby" t t muse-publish-ruby-tag)
255 ("comment" t nil muse-publish-comment-tag)
256 ("include" nil t muse-publish-include-tag)
257 ("markup" t t 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, and a
264 function that performs whatever action is desired within the
265 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 function))
285 :group 'muse-publish)
287 (defcustom muse-publish-markup-specials nil
288 "A table of characters which must be represented specially."
289 :type '(alist :key-type character :value-type string)
290 :group 'muse-publish)
292 (defvar muse-publishing-p nil
293 "Set to t while a page is being published.")
294 (defvar muse-batch-publishing-p nil
295 "Set to t while a page is being batch published.")
296 (defvar muse-publishing-styles nil
297 "The publishing styles that Muse recognizes.
298 This is automatically generated when loading publishing styles.")
299 (defvar muse-publishing-current-file nil
300 "The file that is currently being published.")
301 (defvar muse-publishing-current-output-path nil
302 "The path where the current file will be published to.")
303 (defvar muse-publishing-current-style nil
304 "The style of the file that is currently being published.")
305 (defvar muse-publishing-directives nil
306 "An alist of publishing directives from the top of a file.")
307 (defvar muse-publish-generate-contents nil
308 "Non-nil if a table of contents should be generated.
309 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
310 tell where the <contents> was seen, and to what depth the
311 contents were requested.")
312 (defvar muse-publishing-last-position nil
313 "Last position of the point when publishing.
314 This is used to make sure that publishing doesn't get stalled.")
316 ;; Functions for handling style information
318 (defsubst muse-style (&optional style)
319 "Resolve the given STYLE into a Muse style, if it is a string."
320 (if (null style)
321 muse-publishing-current-style
322 (if (stringp style)
323 (assoc style muse-publishing-styles)
324 (muse-assert (consp style))
325 style)))
327 (defun muse-define-style (name &rest elements)
328 (let ((entry (assoc name muse-publishing-styles)))
329 (if entry
330 (setcdr entry elements)
331 (setq muse-publishing-styles
332 (cons (append (list name) elements)
333 muse-publishing-styles)))))
335 (defun muse-derive-style (new-name base-name &rest elements)
336 (apply 'muse-define-style new-name
337 (append elements (list :base base-name))))
339 (defsubst muse-get-keyword (keyword list &optional direct)
340 (let ((value (cadr (memq keyword list))))
341 (if (and (not direct) (symbolp value))
342 (symbol-value value)
343 value)))
345 (defun muse-style-elements-list (elem &optional style)
346 "Return a list all references to ELEM in STYLE, including base styles.
347 If STYLE is not specified, use current style."
348 (let (base elements)
349 (while style
350 (setq style (muse-style style))
351 (setq elements (append elements
352 (muse-get-keyword elem style)))
353 (setq style (muse-get-keyword :base style)))
354 elements))
356 (defun muse-style-element (elem &optional style direct)
357 "Search for ELEM in STYLE, including base styles.
358 If STYLE is not specified, use current style."
359 (setq style (muse-style style))
360 (let ((value (muse-get-keyword elem style direct)))
361 (if value
362 value
363 (let ((base (muse-get-keyword :base style)))
364 (if base
365 (muse-style-element elem base direct))))))
367 (defun muse-find-markup-element (keyword ident style)
368 (let ((def (assq ident (muse-style-element keyword style))))
369 (if def
370 (cdr def)
371 (let ((base (muse-style-element :base style)))
372 (if base
373 (muse-find-markup-element keyword ident base))))))
375 (defun muse-markup-text (ident &rest args)
376 "Insert ARGS into the text markup associated with IDENT.
377 If the markup text has sections like %N%, this will be replaced
378 with the N-1th argument in ARGS. After that, `format' is applied
379 to the text with ARGS as parameters."
380 (let ((text (muse-find-markup-element :strings ident (muse-style))))
381 (if (and text args)
382 (progn
383 (let (start repl-text)
384 (while (setq start (string-match "%\\([1-9][0-9]*\\)%" text start))
385 ;; escape '%' in the argument text, since we will be
386 ;; using format on it
387 (setq repl-text (muse-replace-regexp-in-string
388 "%" "%%"
389 (nth (1- (string-to-number
390 (match-string 1 text))) args)
391 t t)
392 start (+ start (length repl-text))
393 text (replace-match repl-text t t text))))
394 (apply 'format text args))
395 (or text ""))))
397 (defun muse-insert-markup (&rest args)
398 (let ((beg (point)))
399 (apply 'insert args)
400 (muse-publish-mark-read-only beg (point))))
402 (defun muse-find-markup-tag (keyword tagname style)
403 (let ((def (assoc tagname (muse-style-element keyword style))))
404 (or def
405 (let ((base (muse-style-element :base style)))
406 (if base
407 (muse-find-markup-tag keyword tagname base))))))
409 (defsubst muse-markup-tag-info (tagname &rest args)
410 (let ((tag-info (muse-find-markup-tag :tags tagname (muse-style))))
411 (or tag-info
412 (assoc (match-string 1) muse-publish-markup-tags))))
414 (defsubst muse-markup-function (category)
415 (let ((func (muse-find-markup-element :functions category (muse-style))))
416 (or func
417 (cdr (assq category muse-publish-markup-functions)))))
419 ;; Publishing routines
421 (defun muse-publish-markup (name rules)
422 (let* ((case-fold-search nil)
423 (inhibit-read-only t)
424 (limit (* (length rules) (point-max)))
425 (verbose (and muse-publish-report-threshhold
426 (> (point-max) muse-publish-report-threshhold)))
427 (base 0))
428 (while rules
429 (goto-char (point-min))
430 (let ((regexp (nth 1 (car rules)))
431 (group (nth 2 (car rules)))
432 (repl (nth 3 (car rules)))
433 pos)
434 (setq muse-publishing-last-position nil)
435 (if (symbolp regexp)
436 (setq regexp (symbol-value regexp)))
437 (if (and verbose (not muse-batch-publishing-p))
438 (message "Publishing %s...%d%%" name
439 (* (/ (float (+ (point) base)) limit) 100)))
440 (while (and regexp (setq pos (re-search-forward regexp nil t)))
441 (if (and verbose (not muse-batch-publishing-p))
442 (message "Publishing %s...%d%%" name
443 (* (/ (float (+ (point) base)) limit) 100)))
444 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
445 (match-beginning group)
446 (get-text-property (match-beginning group) 'read-only))
447 (let* (func
448 (text (cond
449 ((and (symbolp repl)
450 (setq func (muse-markup-function repl)))
451 (funcall func))
452 ((functionp repl)
453 (funcall repl))
454 ((symbolp repl)
455 (symbol-value repl))
456 (t repl))))
457 (if (stringp text)
458 (replace-match text t))))
459 (if (and muse-publishing-last-position
460 (= pos muse-publishing-last-position))
461 (if (eobp)
462 (setq regexp nil)
463 (forward-char 1)))
464 (setq muse-publishing-last-position pos)))
465 (setq rules (cdr rules)
466 base (+ base (point-max))))
467 (if (and verbose (not muse-batch-publishing-p))
468 (message "Publishing %s...done" name))))
470 (defcustom muse-publish-markup-header-footer-tags
471 '(("lisp" t t muse-publish-lisp-tag)
472 ("markup" t t muse-publish-mark-up-tag))
473 "Tags used when publishing headers and footers.
474 See `muse-publish-markup-tags' for details."
475 :type '(repeat (list (string :tag "Markup tag")
476 (boolean :tag "Expect closing tag" :value t)
477 (boolean :tag "Parse attributes" :value nil)
478 function))
479 :group 'muse-publish)
481 (defun muse-insert-file-or-string (file-or-string &optional title)
482 (let ((beg (point)) end)
483 (if (and (not (string-equal file-or-string ""))
484 (not (string-match "\n" file-or-string))
485 (file-readable-p file-or-string))
486 (setq end (+ beg (cadr (insert-file-contents file-or-string))))
487 (insert file-or-string)
488 (setq end (point)))
489 (save-restriction
490 (narrow-to-region beg end)
491 (remove-text-properties (point-min) (point-max)
492 '(read-only nil rear-nonsticky nil))
493 (goto-char (point-min))
494 (let ((muse-inhibit-style-tags t))
495 (muse-publish-markup (or title "")
496 '((100 muse-tag-regexp 0
497 muse-publish-markup-tag)))))))
499 (defun muse-style-run-hooks (keyword style &rest args)
500 (catch 'handled
501 (let ((cache nil))
502 (while (and style
503 (setq style (muse-style style)))
504 (let ((func (muse-style-element keyword style t)))
505 (when (and func
506 (not (member func cache)))
507 (setq cache (cons func cache))
508 (when (apply func args)
509 (throw 'handled t))))
510 (setq style (muse-style-element :base style))))))
512 (defun muse-publish-markup-region (beg end &optional title style)
513 "Apply the given STYLE's markup rules to the given region.
514 TITLE is used when indicating the publishing progress; it may be nil."
515 (unless title (setq title ""))
516 (unless style
517 (or (setq style muse-publishing-current-style)
518 (error "Cannot find any publishing styles to use")))
519 (save-restriction
520 (narrow-to-region beg end)
521 (muse-style-run-hooks :before style)
522 (muse-publish-markup
523 title
524 (sort (copy-alist (append muse-publish-markup-regexps
525 (muse-style-elements-list :regexps style)))
526 (function
527 (lambda (l r)
528 (< (car l) (car r))))))
529 (muse-style-run-hooks :before-end style)
530 (muse-publish-escape-specials (point-min) (point-max) nil 'document)))
532 (defun muse-publish-markup-buffer (title style)
533 "Apply the given STYLE's markup rules to the current buffer."
534 (setq style (muse-style style))
535 (let ((style-header (muse-style-element :header style))
536 (style-footer (muse-style-element :footer style))
537 (muse-publishing-current-style style)
538 (muse-publishing-directives
539 (list (cons "title" title)
540 (cons "author" (user-full-name))
541 (cons "date" (format-time-string
542 "%B %e, %Y"
543 (if muse-publishing-current-file
544 (nth 5 (file-attributes
545 muse-publishing-current-file))
546 (current-time))))))
547 (muse-publishing-p t)
548 (inhibit-read-only t))
549 (run-hooks 'muse-update-values-hook)
550 (run-hooks 'muse-before-publish-hook)
551 (muse-publish-markup-region (point-min) (point-max) title style)
552 (goto-char (point-min))
553 (when style-header
554 (muse-insert-file-or-string style-header title))
555 (goto-char (point-max))
556 (when style-footer
557 (muse-insert-file-or-string style-footer title))
558 (muse-style-run-hooks :after style)
559 (run-hooks 'muse-after-publish-hook)))
561 (defun muse-publish-markup-string (string &optional style)
562 "Markup STRING using the given STYLE's markup rules."
563 (setq style (muse-style style))
564 (muse-with-temp-buffer
565 (insert string)
566 (let ((muse-publishing-current-style style)
567 (muse-publishing-p t))
568 (muse-publish-markup "*string*" (muse-style-element :rules style)))
569 (buffer-string)))
571 ;; Commands for publishing files
573 (defun muse-publish-get-style (&optional styles)
574 (unless styles (setq styles muse-publishing-styles))
575 (if (= 1 (length styles))
576 (car styles)
577 (when (catch 'different
578 (let ((first (car (car styles))))
579 (dolist (style (cdr styles))
580 (unless (equal first (car style))
581 (throw 'different t)))))
582 (setq styles (muse-collect-alist
583 styles
584 (completing-read "Publish with style: " styles nil t))))
585 (if (or (= 1 (length styles))
586 (not (muse-get-keyword :path (car styles))))
587 (car styles)
588 (setq styles (mapcar (lambda (style)
589 (cons (muse-get-keyword :path style)
590 style))
591 styles))
592 (cdr (assoc (completing-read "Publish to directory: " styles nil t)
593 styles)))))
595 (defsubst muse-publish-get-output-dir (style)
596 (let ((default-directory (or (muse-style-element :path style)
597 default-directory)))
598 (muse-read-directory-name "Publish to directory: " nil default-directory)))
600 (defsubst muse-publish-get-info ()
601 (let ((style (muse-publish-get-style)))
602 (list style (muse-publish-get-output-dir style)
603 current-prefix-arg)))
605 (defsubst muse-publish-output-name (&optional file style)
606 (setq style (muse-style style))
607 (concat (muse-style-element :prefix style)
608 (muse-page-name file)
609 (muse-style-element :suffix style)))
611 (defsubst muse-publish-output-file (file &optional output-dir style)
612 (setq style (muse-style style))
613 (if output-dir
614 (expand-file-name (muse-publish-output-name file style) output-dir)
615 (concat (file-name-directory file)
616 (muse-publish-output-name file style))))
618 (defsubst muse-publish-link-name (&optional file style)
619 (setq style (muse-style style))
620 (concat (muse-style-element :prefix style)
621 (muse-page-name file)
622 (or (muse-style-element :link-suffix style)
623 (muse-style-element :suffix style))))
625 (defsubst muse-publish-link-file (file &optional output-dir style)
626 (setq style (muse-style style))
627 (if output-dir
628 (expand-file-name (muse-publish-link-name file style) output-dir)
629 (concat (file-name-directory file)
630 (muse-publish-link-name file style))))
632 (defsubst muse-publish-link-page (page)
633 (if (fboundp 'muse-project-link-page)
634 (muse-project-link-page page)
635 (muse-publish-link-file page)))
637 ;;;###autoload
638 (defun muse-publish-file (file style &optional output-dir force)
639 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
640 If the argument FORCE is nil, each file is only published if it is
641 newer than the published version. If the argument FORCE is non-nil,
642 the file is published no matter what."
643 (interactive (cons (read-file-name "Publish file: ")
644 (muse-publish-get-info)))
645 (let ((style-name style))
646 (setq style (muse-style style))
647 (unless style
648 (error "There is no style '%s' defined." style-name)))
649 (let* ((output-path (muse-publish-output-file file output-dir style))
650 (output-suffix (muse-style-element :osuffix style))
651 (muse-publishing-current-file file)
652 (muse-publishing-current-output-path output-path)
653 (target (if output-suffix
654 (concat (file-name-sans-extension output-path)
655 output-suffix)
656 output-path))
657 (threshhold (nth 7 (file-attributes file))))
658 (if (not threshhold)
659 (message "Please save %s before publishing" file)
660 (when (or force (file-newer-than-file-p file target))
661 (if (and muse-publish-report-threshhold
662 (> threshhold
663 muse-publish-report-threshhold))
664 (message "Publishing %s ..." file))
665 (muse-with-temp-buffer
666 (insert-file-contents file)
667 (muse-publish-markup-buffer (muse-page-name file) style)
668 (let ((backup-inhibited t))
669 (write-file output-path))
670 (muse-style-run-hooks :final style file output-path target))
671 t))))
673 ;;;###autoload
674 (defun muse-publish-this-file (style output-dir &optional force)
675 "Publish the currently-visited file.
676 Prompt for both the STYLE and OUTPUT-DIR if they are not
677 supplied."
678 (interactive (muse-publish-get-info))
679 (if buffer-file-name
680 (unless (muse-publish-file buffer-file-name style output-dir force)
681 (message (concat "The published version is up-to-date; use"
682 " C-u C-c C-T to force an update.")))
683 (message "This buffer is not associated with any file")))
685 (defun muse-batch-publish-files ()
686 "Publish Muse files in batch mode."
687 (let ((muse-batch-publishing-p t)
688 style output-dir)
689 (setq style (car command-line-args-left)
690 command-line-args-left (cdr command-line-args-left)
691 output-dir (car command-line-args-left)
692 output-dir
693 (if (string-match "\\`--output-dir=" output-dir)
694 (prog1
695 (substring output-dir (match-end 0))
696 (setq command-line-args-left (cdr command-line-args-left)))))
697 (setq auto-mode-alist
698 (delete (cons (concat "\\." muse-file-extension "\\'")
699 'muse-mode-choose-mode)
700 auto-mode-alist))
701 (dolist (file command-line-args-left)
702 (muse-publish-file file style output-dir t))))
704 ;; Default publishing rules
706 (defun muse-publish-section-close (depth)
707 "Seach forward for the closing tag of given DEPTH."
708 (let (not-end)
709 (save-excursion
710 (while (and (setq not-end (re-search-forward
711 (concat "^\\*\\{1," (number-to-string depth)
712 "\\}\\s-+")
713 nil t))
714 (get-text-property (match-beginning 0) 'read-only)))
715 (if not-end
716 (forward-line 0)
717 (goto-char (point-max)))
718 (cond ((not (eq (char-before) ?\n))
719 (insert "\n\n"))
720 ((not (eq (char-before (1- (point))) ?\n))
721 (insert "\n")))
722 (muse-insert-markup (muse-markup-text 'section-close depth))
723 (insert "\n"))))
725 (defun muse-publish-markup-directive (&optional name value)
726 (unless name (setq name (match-string 1)))
727 (unless value (setq value (match-string 2)))
728 (let ((elem (assoc name muse-publishing-directives)))
729 (if elem
730 (setcdr elem value)
731 (setq muse-publishing-directives
732 (cons (cons name value)
733 muse-publishing-directives))))
734 ;; Make sure we don't ever try to move the point forward (past the
735 ;; beginning of buffer) while we're still searching for directives.
736 (setq muse-publishing-last-position nil)
737 (delete-region (match-beginning 0) (match-end 0)))
739 (defsubst muse-publishing-directive (name)
740 (cdr (assoc name muse-publishing-directives)))
742 (defun muse-publish-markup-anchor ()
743 (unless (get-text-property (match-end 1) 'muse-link)
744 (let ((text (muse-markup-text 'anchor (match-string 2))))
745 (unless (string= text "")
746 (save-match-data
747 (skip-chars-forward (concat muse-regexp-blank "\n"))
748 (muse-insert-markup text)))
749 (match-string 1))))
751 (defun muse-publish-markup-comment ()
752 (if (null muse-publish-comments-p)
754 (goto-char (match-end 0))
755 (muse-insert-markup (muse-markup-text 'comment-end))
756 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
757 (delete-region (match-beginning 0) (match-beginning 1))
758 (goto-char (match-beginning 0))
759 (muse-insert-markup (muse-markup-text 'comment-begin))))
761 (defvar muse-inhibit-style-tags nil
762 "If non-nil, do not search for style-specific tags.
763 This is used when publishing headers and footers.")
765 (defun muse-publish-markup-tag ()
766 (let ((tag-info (if muse-inhibit-style-tags
767 (assoc (match-string 1) muse-publish-markup-tags)
768 (muse-markup-tag-info (match-string 1)))))
769 (when (and tag-info
770 (not (get-text-property (match-beginning 0) 'read-only)))
771 (let ((closed-tag (match-string 3))
772 (start (match-beginning 0))
773 (beg (point))
774 end attrs)
775 (when (nth 2 tag-info)
776 (let ((attrstr (match-string 2)))
777 (while (and attrstr
778 (string-match (concat "\\([^"
779 muse-regexp-blank
780 "=\n]+\\)\\(=\"\\"
781 "([^\"]+\\)\"\\)?")
782 attrstr))
783 (let ((attr (cons (downcase
784 (muse-match-string-no-properties 1 attrstr))
785 (muse-match-string-no-properties 3 attrstr))))
786 (setq attrstr (replace-match "" t t attrstr))
787 (if attrs
788 (nconc attrs (list attr))
789 (setq attrs (list attr)))))))
790 (if (and (cadr tag-info) (not closed-tag))
791 (if (search-forward (concat "</" (car tag-info) ">") nil t)
792 (delete-region (match-beginning 0) (point))
793 (setq tag-info nil)))
794 (when tag-info
795 (setq end (point-marker))
796 (delete-region start beg)
797 (goto-char start)
798 (let ((args (list start end)))
799 (if (nth 2 tag-info)
800 (nconc args (list attrs)))
801 (let ((muse-inhibit-style-tags nil))
802 ;; remove the inhibition
803 (apply (nth 3 tag-info) args)))))))
804 nil)
806 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
807 "Escape specials from BEG to END using style-specific :specials.
808 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
809 CONTEXT is used to figure out what kind of specials to escape.
811 The following contexts exist in Muse.
812 'underline _underlined text_
813 'literal =monospaced text= or <code> region (monospaced, escaped)
814 'emphasis *emphasized text*
815 'email email@example.com
816 'url http://example.com
817 'url-desc [[...][description of an explicit link]]
818 'image [[image.png]]
819 'example <example> region (monospaced, block context, escaped)
820 'verbatim <verbatim> region (escaped)
821 'document normal text"
822 (let ((specials (muse-style-element :specials nil t)))
823 (cond ((functionp specials)
824 (setq specials (funcall specials context)))
825 ((symbolp specials)
826 (setq specials (symbol-value specials))))
827 (if (functionp specials)
828 (funcall specials beg end ignore-read-only)
829 (save-excursion
830 (save-restriction
831 (narrow-to-region beg end)
832 (goto-char (point-min))
833 (while (< (point) (point-max))
834 (if (and (not ignore-read-only)
835 (get-text-property (point) 'read-only))
836 (goto-char (or (next-single-property-change (point) 'read-only)
837 (point-max)))
838 (let ((repl (or (assoc (char-after) specials)
839 (assoc (char-after)
840 muse-publish-markup-specials))))
841 (if (null repl)
842 (forward-char 1)
843 (delete-char 1)
844 (insert-before-markers (cdr repl)))))))))))
846 (defun muse-publish-markup-word ()
847 (let* ((beg (match-beginning 2))
848 (end (1- (match-end 2)))
849 (leader (buffer-substring-no-properties beg end))
850 open-tag close-tag mark-read-only loc context)
851 (cond
852 ((string= leader "_")
853 (setq context 'underline
854 open-tag (muse-markup-text 'begin-underline)
855 close-tag (muse-markup-text 'end-underline)))
856 ((string= leader "=")
857 (setq context 'literal
858 open-tag (muse-markup-text 'begin-literal)
859 close-tag (muse-markup-text 'end-literal))
860 (setq mark-read-only t))
862 (let ((l (length leader)))
863 (setq context 'emphasis)
864 (cond
865 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
866 close-tag (muse-markup-text 'end-emph)))
867 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
868 close-tag (muse-markup-text 'end-more-emph)))
869 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
870 close-tag (muse-markup-text 'end-most-emph)))
871 (t (setq context nil))))))
872 (if (and context
873 (not (get-text-property beg 'muse-link))
874 (setq loc (search-forward leader nil t))
875 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
876 (not (eq (char-syntax (char-before (point))) ?\ ))
877 (not (get-text-property (point) 'muse-link)))
878 (progn
879 (replace-match "")
880 (delete-region beg end)
881 (setq end (point-marker))
882 (muse-insert-markup close-tag)
883 (goto-char beg)
884 (muse-insert-markup open-tag)
885 (setq beg (point))
886 (when mark-read-only
887 (muse-publish-escape-specials beg end t context)
888 (muse-publish-mark-read-only beg end)))
889 (backward-char))
890 nil))
892 (defun muse-publish-markup-emdash ()
893 (unless (get-text-property (match-beginning 0) 'muse-link)
894 (let ((prespace (match-string 1))
895 (postspace (match-string 2)))
896 (delete-region (match-beginning 0) (match-end 0))
897 (muse-insert-markup (muse-markup-text 'emdash prespace postspace))
898 (when (eq (char-after) ?\<)
899 (insert ?\n)))))
901 (defun muse-publish-markup-enddots ()
902 (unless (get-text-property (match-beginning 0) 'muse-link)
903 (delete-region (match-beginning 0) (match-end 0))
904 (muse-insert-markup (muse-markup-text 'enddots))))
906 (defun muse-publish-markup-dots ()
907 (unless (get-text-property (match-beginning 0) 'muse-link)
908 (delete-region (match-beginning 0) (match-end 0))
909 (muse-insert-markup (muse-markup-text 'dots))))
911 (defun muse-publish-markup-rule ()
912 (unless (get-text-property (match-beginning 0) 'muse-link)
913 (delete-region (match-beginning 0) (match-end 0))
914 (muse-insert-markup (muse-markup-text 'rule))))
916 (defun muse-publish-markup-no-break-space ()
917 (unless (get-text-property (match-beginning 0) 'muse-link)
918 (delete-region (match-beginning 0) (match-end 0))
919 (muse-insert-markup (muse-markup-text 'no-break-space))))
921 (defun muse-publish-markup-heading ()
922 (let* ((len (length (match-string 1)))
923 (start (muse-markup-text
924 (cond ((= len 1) 'section)
925 ((= len 2) 'subsection)
926 ((= len 3) 'subsubsection)
927 (t 'section-other))
928 len))
929 (end (muse-markup-text
930 (cond ((= len 1) 'section-end)
931 ((= len 2) 'subsection-end)
932 ((= len 3) 'subsubsection-end)
933 (t 'section-other-end))
934 len)))
935 (delete-region (match-beginning 0) (match-end 0))
936 (muse-insert-markup start)
937 (end-of-line)
938 (when end
939 (muse-insert-markup end))
940 (muse-publish-section-close len)))
942 (defvar muse-publish-footnotes nil)
944 (defun muse-publish-markup-footnote ()
945 "Scan ahead and snarf up the footnote body"
946 (cond
947 ((get-text-property (match-beginning 0) 'muse-link)
948 nil)
949 ((= (muse-line-beginning-position) (match-beginning 0))
952 (let ((footnote (save-match-data
953 (string-to-number (match-string 1))))
954 footnotemark)
955 (delete-region (match-beginning 0) (match-end 0))
956 (save-excursion
957 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
958 (let* ((start (match-beginning 0))
959 (beg (goto-char (match-end 0)))
960 (end (save-excursion
961 (if (search-forward "\n\n" nil t)
962 (copy-marker (match-beginning 0))
963 (goto-char (point-max))
964 (skip-chars-backward "\n")
965 (point-marker)))))
966 (while (re-search-forward
967 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
968 end t)
969 (replace-match "\\1" t))
970 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
971 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
972 (if (string= "" footnotemark-cmd)
973 (setq footnotemark
974 (concat (muse-markup-text 'footnote)
975 (buffer-substring-no-properties beg end)
976 (muse-markup-text 'footnote-end)))
977 (setq footnotemark (format footnotemark-cmd footnote
978 footnotemark-end-cmd))
979 (unless muse-publish-footnotes
980 (set (make-local-variable 'muse-publish-footnotes)
981 (make-vector 256 nil)))
982 (unless (aref muse-publish-footnotes footnote)
983 (setq footnotemark
984 (concat
985 footnotemark
986 (concat (format (muse-markup-text 'footnotetext)
987 footnote)
988 (buffer-substring-no-properties beg end)
989 (muse-markup-text 'footnotetext-end))))
990 (aset muse-publish-footnotes footnote footnotemark))))
991 (goto-char end)
992 (skip-chars-forward "\n")
993 (delete-region start (point)))))
994 (muse-insert-markup (or footnotemark footnote))))))
996 (defun muse-publish-markup-fn-sep ()
997 (delete-region (match-beginning 0) (match-end 0))
998 (muse-insert-markup (muse-markup-text 'fn-sep)))
1000 (defun muse-insert-markup-end-list (&rest args)
1001 (let ((beg (point)))
1002 (apply 'insert args)
1003 (add-text-properties beg (point) '(end-list t))
1004 (muse-publish-mark-read-only beg (point))))
1006 (defun muse-publish-surround-dl (indent post-indent)
1007 (let* ((beg-item (muse-markup-text 'begin-dl-item))
1008 (end-item (muse-markup-text 'end-dl-item))
1009 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
1010 (end-ddt (muse-markup-text 'end-ddt))
1011 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
1012 (end-dde (muse-markup-text 'end-dde))
1013 (continue t)
1014 def-on-same-line beg)
1015 (while continue
1016 ;; envelope this as one term+definitions unit -- HTML does not
1017 ;; need this, but DocBook and Muse's custom XML format do
1018 (muse-insert-markup beg-item)
1019 (when (looking-at muse-dl-term-regexp)
1020 ;; find the term and wrap it with published markup
1021 (setq beg (point))
1022 (goto-char (match-end 1))
1023 (delete-region (point) (match-end 0))
1024 (muse-insert-markup-end-list end-ddt)
1025 ;; if definition is immediately after term, move to next line
1026 (unless (eq (char-after) ?\n)
1027 (insert ?\n))
1028 (save-excursion
1029 (goto-char beg)
1030 (delete-region (point) (match-beginning 1))
1031 (muse-insert-markup beg-ddt)))
1032 (setq beg (point)
1033 ;; move past current item
1034 continue (muse-forward-list-item 'dl-term indent))
1035 (save-restriction
1036 (narrow-to-region beg (point))
1037 (goto-char (point-min))
1038 ;; publish each definition that we find, defaulting to an
1039 ;; empty definition if none are found
1040 (muse-publish-surround-text beg-dde end-dde
1041 (lambda (indent)
1042 (muse-forward-list-item 'dl-entry indent))
1043 nil nil t)
1044 (goto-char (point-max))
1045 (skip-chars-backward (concat muse-regexp-blank "\n"))
1046 (muse-insert-markup-end-list end-item)
1047 (when continue
1048 (goto-char (point-max)))))))
1050 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent determine-indent)
1051 (let ((continue t)
1052 (list-item (format muse-list-item-regexp
1053 (concat "[" muse-regexp-blank "]*")))
1054 init-indent beg)
1055 (unless indent
1056 (setq indent (concat "[" muse-regexp-blank "]+")))
1057 (if post-indent
1058 (setq post-indent (concat " \\{0," (number-to-string post-indent)
1059 "\\}"))
1060 (setq post-indent ""))
1061 (while continue
1062 (muse-insert-markup beg-tag)
1063 (setq beg (point)
1064 ;; move past current item; continue is non-nil if there
1065 ;; are more like items to be processed
1066 continue (funcall move-func indent))
1067 (save-restriction
1068 (when determine-indent
1069 ;; if the caller doesn't know how much indentation
1070 ;; to use, figure it out ourselves
1071 (if (not continue)
1072 (setq indent "")
1073 (save-match-data
1074 ;; snarf all leading whitespace
1075 (let ((this-indent (and (match-beginning 2)
1076 (buffer-substring (match-beginning 1)
1077 (match-beginning 2)))))
1078 (when (and this-indent
1079 (not (string= this-indent "")))
1080 (setq indent this-indent
1081 determine-indent nil))))))
1082 (when continue
1083 ;; remove list markup if we encountered another item of the
1084 ;; same type
1085 (replace-match "" t t nil 1))
1086 (narrow-to-region beg (point))
1087 ;; narrow to current item
1088 (goto-char (point-min))
1089 (forward-line 1)
1090 (let ((list-nested nil)
1091 (indent-found nil)
1092 (post-indent post-indent))
1093 (while (< (point) (point-max))
1094 (when (and (looking-at list-item)
1095 (not (or (get-text-property
1096 (muse-list-item-critical-point) 'read-only)
1097 (get-text-property
1098 (muse-list-item-critical-point) 'muse-link))))
1099 ;; if we encounter a list item, allow no post-indent
1100 ;; space
1101 (setq list-nested t))
1102 (when (looking-at (concat indent "\\("
1103 (or (and list-nested "")
1104 post-indent)
1105 "\\)"))
1106 ;; if list is not nested, remove indentation
1107 (unless indent-found
1108 (setq post-indent (match-string 1)
1109 indent-found t))
1110 (replace-match ""))
1111 (forward-line 1)))
1112 (skip-chars-backward (concat muse-regexp-blank "\n"))
1113 (muse-insert-markup-end-list end-tag)
1114 (when continue
1115 (goto-char (point-max)))))))
1117 (defun muse-publish-markup-list ()
1118 "Markup a list entry.
1119 This function works by marking up items of the same list level
1120 and type, respecting the end-of-list property."
1121 (let* ((str (match-string 1))
1122 (type (muse-list-item-type str))
1123 (indent (buffer-substring (muse-line-beginning-position)
1124 (match-beginning 1)))
1125 (post-indent (length str))
1126 (last (match-beginning 0)))
1127 (cond
1128 ((or (get-text-property (muse-list-item-critical-point) 'read-only)
1129 (get-text-property (muse-list-item-critical-point) 'muse-link))
1130 nil)
1131 ((eq type 'ul)
1132 (unless (eq (char-after (match-end 1)) ?-)
1133 (delete-region (match-beginning 0) (match-end 0))
1134 (muse-insert-markup (muse-markup-text 'begin-uli))
1135 (save-excursion
1136 (muse-publish-surround-text
1137 (muse-markup-text 'begin-uli-item)
1138 (muse-markup-text 'end-uli-item)
1139 (lambda (indent)
1140 (muse-forward-list-item 'ul indent))
1141 indent post-indent)
1142 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1143 (forward-line 1)))
1144 ((eq type 'ol)
1145 (delete-region (match-beginning 0) (match-end 0))
1146 (muse-insert-markup (muse-markup-text 'begin-oli))
1147 (save-excursion
1148 (muse-publish-surround-text
1149 (muse-markup-text 'begin-oli-item)
1150 (muse-markup-text 'end-oli-item)
1151 (lambda (indent)
1152 (muse-forward-list-item 'ol indent))
1153 indent post-indent)
1154 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1155 (forward-line 1))
1156 ((not (string= (match-string 2) ""))
1157 ;; must have an initial term
1158 (goto-char (match-beginning 0))
1159 (muse-insert-markup (muse-markup-text 'begin-dl))
1160 (save-excursion
1161 (muse-publish-surround-dl indent post-indent)
1162 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1163 (forward-line 1))))
1164 nil)
1166 (defun muse-publish-markup-quote ()
1167 "Markup a quoted paragraph.
1168 The reason this function is so funky, is to prevent text properties
1169 like read-only from being inadvertently deleted."
1170 (let* ((ws (match-string 1))
1171 (centered (>= (string-width ws) 6))
1172 (begin-elem (if centered 'begin-center 'begin-quote-item))
1173 (end-elem (if centered 'end-center 'end-quote-item)))
1174 (replace-match "" t t nil 1)
1175 (unless centered
1176 (muse-insert-markup (muse-markup-text 'begin-quote)))
1177 (muse-publish-surround-text (muse-markup-text begin-elem)
1178 (muse-markup-text end-elem)
1179 (function (lambda (indent)
1180 (muse-forward-paragraph)
1181 nil)))
1182 (unless centered
1183 (muse-insert-markup (muse-markup-text 'end-quote)))))
1185 (defun muse-publish-markup-leading-space (markup-space multiple)
1186 (let (count)
1187 (when (and markup-space
1188 (>= (setq count (skip-chars-forward " ")) 0))
1189 (delete-region (muse-line-beginning-position) (point))
1190 (while (> count 0)
1191 (muse-insert-markup markup-space)
1192 (setq count (- count multiple))))))
1194 (defun muse-publish-markup-verse ()
1195 (let ((leader (match-string 0)))
1196 (goto-char (match-beginning 0))
1197 (muse-insert-markup (muse-markup-text 'begin-verse))
1198 (while (looking-at leader)
1199 (replace-match "")
1200 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1201 (let ((beg (point)))
1202 (end-of-line)
1203 (cond
1204 ((bolp)
1205 (let ((text (muse-markup-text 'empty-verse-line)))
1206 (when text (muse-insert-markup text))))
1207 ((save-excursion
1208 (save-match-data
1209 (forward-line 1)
1210 (or (looking-at (concat leader "["
1211 muse-regexp-blank
1212 "]*$"))
1213 (not (looking-at leader)))))
1214 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1215 (end-text (muse-markup-text 'end-last-stanza-line)))
1216 (when end-text (muse-insert-markup end-text))
1217 (goto-char beg)
1218 (when begin-text (muse-insert-markup begin-text))
1219 (end-of-line)))
1221 (let ((begin-text (muse-markup-text 'begin-verse-line))
1222 (end-text (muse-markup-text 'end-verse-line)))
1223 (when end-text (muse-insert-markup end-text))
1224 (goto-char beg)
1225 (when begin-text (muse-insert-markup begin-text))
1226 (end-of-line))))
1227 (forward-line 1))))
1228 (muse-insert-markup (muse-markup-text 'end-verse))
1229 (insert ?\n))
1231 (defun muse-publish-table-fields (beg end)
1232 "Parse given region as a table, returning a cons cell.
1233 The car is the length of the longest row.
1235 The cdr is a list of the fields of the table, with the first
1236 element indicating the type of the row:
1237 1: body, 2: header, 3: footer.
1239 The existing region will be removed, except for initial blank lines."
1240 (unless (muse-publishing-directive "disable-tables")
1241 (let ((longest 0)
1242 (left 0)
1243 fields field-list)
1244 (save-restriction
1245 (narrow-to-region beg end)
1246 (goto-char (point-min))
1247 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1248 (forward-line 1))
1249 (setq beg (point))
1250 (while (= left 0)
1251 (when (looking-at muse-table-line-regexp)
1252 (setq fields (cons (length (match-string 1))
1253 (mapcar #'muse-trim-whitespace
1254 (split-string (match-string 0)
1255 muse-table-field-regexp)))
1256 field-list (cons fields field-list)
1257 longest (max (length fields) longest)))
1258 (setq left (forward-line 1))))
1259 (delete-region beg end)
1260 (if (= longest 0)
1261 (cons 0 nil)
1262 (cons (1- longest) (nreverse field-list))))))
1264 (defun muse-publish-markup-table ()
1265 "Style does not support tables.")
1267 (defun muse-publish-escape-specials-in-string (string &optional context)
1268 "Escape specials in STRING using style-specific :specials.
1269 CONTEXT is used to figure out what kind of specials to escape.
1271 See the documentation of the `muse-publish-escape-specials'
1272 function for the list of available contexts."
1273 (unless string
1274 (setq string ""))
1275 (let ((specials (muse-style-element :specials nil t)))
1276 (cond ((functionp specials)
1277 (setq specials (funcall specials context)))
1278 ((symbolp specials)
1279 (setq specials (symbol-value specials))))
1280 (if (functionp specials)
1281 (funcall specials string)
1282 (apply (function concat)
1283 (mapcar
1284 (lambda (ch)
1285 (let ((repl (or (assoc ch specials)
1286 (assoc ch muse-publish-markup-specials))))
1287 (if (null repl)
1288 (char-to-string ch)
1289 (cdr repl))))
1290 (append string nil))))))
1292 (defun muse-publish-markup-email ()
1293 (let* ((beg (match-end 1))
1294 (addr (buffer-substring-no-properties beg (match-end 0))))
1295 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1296 (goto-char beg)
1297 (delete-region beg (match-end 0))
1298 (if (or (eq (char-before (match-beginning 0)) ?\")
1299 (eq (char-after (match-end 0)) ?\"))
1300 (insert addr)
1301 (insert (format (muse-markup-text 'email-addr) addr addr)))
1302 (muse-publish-mark-read-only beg (point))))
1304 (defun muse-publish-classify-url (target)
1305 "Transform anchors and get published name, if TARGET is a page.
1306 The return value is a cons cell. The car is the type of link,
1307 the cadr is the page name, and the cddr is the anchor."
1308 (save-match-data
1309 (cond ((or (null target) (string= target ""))
1310 nil)
1311 ((string-match muse-url-regexp target)
1312 (cons 'url (cons target nil)))
1313 ((string-match muse-image-regexp target)
1314 (cons 'image (cons target nil)))
1315 ((string-match muse-file-regexp target)
1316 (cons 'file (cons target nil)))
1317 ((string-match "#" target)
1318 (if (eq (aref target 0) ?\#)
1319 (cons 'anchor-ref (cons nil (substring target 1)))
1320 (cons 'link-and-anchor
1321 (cons (muse-publish-link-page
1322 (substring target 0 (match-beginning 0)))
1323 (substring target (match-end 0))))))
1325 (cons 'link (cons (muse-publish-link-page target) nil))))))
1327 (defun muse-publish-url-desc (desc explicit)
1328 (when desc
1329 (dolist (transform muse-publish-desc-transforms)
1330 (setq desc (save-match-data
1331 (when desc (funcall transform desc explicit)))))
1332 (setq desc (muse-link-unescape desc))
1333 (muse-publish-escape-specials-in-string desc 'url-desc)))
1335 (defun muse-publish-url (url &optional desc orig-url explicit)
1336 "Resolve a URL into its final <a href> form."
1337 (let (type anchor)
1338 (dolist (transform muse-publish-url-transforms)
1339 (setq url (save-match-data (when url (funcall transform url explicit)))))
1340 (if desc
1341 (setq desc (muse-publish-url-desc desc explicit))
1342 (if orig-url
1343 (setq orig-url (muse-publish-url-desc orig-url explicit))))
1344 (let ((target (muse-publish-classify-url url)))
1345 (setq type (car target)
1346 url (if (eq type 'image)
1347 (muse-publish-escape-specials-in-string (cadr target)
1348 'image)
1349 (muse-publish-escape-specials-in-string (cadr target) 'url))
1350 anchor (muse-publish-escape-specials-in-string
1351 (cddr target) 'url)))
1352 (cond ((eq type 'anchor-ref)
1353 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1354 ((string= url "")
1355 desc)
1356 ((eq type 'image)
1357 (let ((ext (or (file-name-extension url) "")))
1358 (setq url (file-name-sans-extension url))
1359 (if desc
1360 (muse-markup-text 'image-with-desc url ext desc)
1361 (muse-markup-text 'image url ext))))
1362 ((eq type 'link-and-anchor)
1363 (muse-markup-text 'link-and-anchor url anchor
1364 (or desc orig-url)))
1365 ((and desc (string-match muse-image-regexp desc))
1366 (let ((ext (or (file-name-extension desc) "")))
1367 (setq desc (file-name-sans-extension desc))
1368 (muse-markup-text 'image-link url desc ext)))
1369 ((eq type 'link)
1370 (muse-markup-text 'link url (or desc orig-url)))
1372 (or (and desc
1373 (let ((text (muse-markup-text 'url-and-desc url desc)))
1374 (and (not (string= text ""))
1375 text)))
1376 (muse-markup-text 'url url (or desc orig-url)))))))
1378 (defun muse-publish-insert-url (url &optional desc orig-url explicit)
1379 "Resolve a URL into its final <a href> form."
1380 (delete-region (match-beginning 0) (match-end 0))
1381 (let ((text (muse-publish-url url desc orig-url explicit)))
1382 (when text
1383 (muse-insert-markup text))))
1385 (defun muse-publish-markup-link ()
1386 (let (desc explicit orig-link link)
1387 (setq explicit (save-match-data
1388 (if (string-match muse-explicit-link-regexp
1389 (match-string 0))
1390 t nil)))
1391 (setq orig-link (if explicit (match-string 1) (match-string 0)))
1392 (setq desc (when explicit (match-string 2)))
1393 (setq link (if explicit
1394 (muse-handle-explicit-link orig-link)
1395 (muse-handle-implicit-link orig-link)))
1396 (when (and link
1397 (or explicit
1398 (not (or (eq (char-before (match-beginning 0)) ?\")
1399 (eq (char-after (match-end 0)) ?\")))))
1400 ;; if explicit link has no user-provided description, treat it
1401 ;; as if it were an implicit link
1402 (when (and explicit (not desc))
1403 (setq explicit nil))
1404 (muse-publish-insert-url link desc orig-link explicit))))
1406 (defun muse-publish-markup-url ()
1407 (unless (or (eq (char-before (match-beginning 0)) ?\")
1408 (eq (char-after (match-end 0)) ?\"))
1409 (let ((url (match-string 0)))
1410 (muse-publish-insert-url url nil url))))
1412 ;; Default publishing tags
1414 (defcustom muse-publish-contents-depth 2
1415 "The number of heading levels to include with <contents> tags."
1416 :type 'integer
1417 :group 'muse-publish)
1419 (defun muse-publish-contents-tag (beg end attrs)
1420 (set (make-local-variable 'muse-publish-generate-contents)
1421 (cons (copy-marker (point) t)
1422 (let ((depth (cdr (assoc "depth" attrs))))
1423 (or (and depth (string-to-number depth))
1424 muse-publish-contents-depth)))))
1426 (defun muse-publish-verse-tag (beg end)
1427 (save-excursion
1428 (save-restriction
1429 (narrow-to-region beg end)
1430 (goto-char (point-min))
1431 (while (eq ?\ (char-syntax (char-after)))
1432 (delete-char 1))
1433 (while (< (point) (point-max))
1434 (insert "> ")
1435 (forward-line))
1436 (if (eq ?\ (char-syntax (char-before)))
1437 (delete-char -1)))))
1439 (defun muse-publish-mark-read-only (beg end)
1440 "Add read-only properties to the given region."
1441 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1442 nil)
1444 (defun muse-publish-mark-link (&optional beg end)
1445 "Indicate that the given region is a Muse link, so that other
1446 markup elements respect it. If a region is not specified, use
1447 the 0th match data to determine it.
1449 This is usually applied to explicit links."
1450 (unless beg (setq beg (match-beginning 0)))
1451 (unless end (setq end (match-end 0)))
1452 (add-text-properties beg end '(muse-link t))
1453 nil)
1455 (defun muse-publish-quote-tag (beg end)
1456 (save-excursion
1457 (save-restriction
1458 (narrow-to-region beg end)
1459 (muse-insert-markup (muse-markup-text 'begin-quote))
1460 (muse-publish-surround-text (muse-markup-text 'begin-quote-item)
1461 (muse-markup-text 'end-quote-item)
1462 (function (lambda (indent)
1463 (muse-forward-paragraph)
1464 (goto-char (match-end 0))
1465 (< (point) (point-max)))))
1466 (muse-insert-markup (muse-markup-text 'end-quote)))))
1468 (defun muse-publish-code-tag (beg end)
1469 (muse-publish-escape-specials beg end nil 'literal)
1470 (goto-char beg)
1471 (insert (muse-markup-text 'begin-literal))
1472 (goto-char end)
1473 (insert (muse-markup-text 'end-literal))
1474 (muse-publish-mark-read-only beg (point)))
1476 (defun muse-publish-example-tag (beg end)
1477 (muse-publish-escape-specials beg end nil 'example)
1478 (goto-char beg)
1479 (insert (muse-markup-text 'begin-example))
1480 (goto-char end)
1481 (insert (muse-markup-text 'end-example))
1482 (muse-publish-mark-read-only beg (point)))
1484 (defun muse-publish-verbatim-tag (beg end)
1485 (muse-publish-escape-specials beg end nil 'verbatim)
1486 (muse-publish-mark-read-only beg end))
1488 (defalias 'muse-publish-class-tag 'ignore)
1490 (defun muse-publish-examplify-buffer ()
1491 "Transform the current buffer as if it were an <example> region."
1492 (let ((end (progn (goto-char (point-max)) (point-marker))))
1493 (muse-publish-example-tag (point-min) end)))
1495 (defun muse-publish-versify-buffer ()
1496 "Transform the current buffer as if it were a <verse> region."
1497 (muse-publish-verse-tag (point-min) (point-max))
1498 (muse-publish-markup ""
1499 `((100 ,(concat "^[" muse-regexp-blank "]*> ") 0
1500 muse-publish-markup-verse)))
1501 (goto-char (point-min)))
1503 (defmacro muse-publish-markup-attribute (beg end attrs reinterp &rest body)
1504 "Evaluate BODY within the bounds of BEG and END.
1505 ATTRS is an alist. Only the \"markup\" element of ATTRS is acted
1508 If it is omitted, publish the region with the normal Muse rules.
1509 If RE-INTERP is specified, this is done immediately in a new
1510 publishing process. Currently, RE-INTERP is specified only by
1511 the <include> tag.
1513 If \"nil\", do not mark up the region at all, but prevent it from
1514 being further interpreted by Muse.
1516 If \"example\", treat the region as if it was surrounded by the
1517 <example> tag.
1519 If \"verse\", treat the region as if it was surrounded by the
1520 <verse> tag, to preserve newlines.
1522 Otherwise, it should be the name of a function to call in the
1523 narrowed region after evaluating BODY."
1524 (let ((markup (make-symbol "markup"))
1525 (markup-function (make-symbol "markup-function")))
1526 `(let ((,markup (cdr (assoc "markup" ,attrs))))
1527 (save-restriction
1528 (narrow-to-region ,beg ,end)
1529 (goto-char (point-min))
1530 ,@body
1531 (if (not ,markup)
1532 (when ,reinterp
1533 (muse-publish-markup-region (point-min) (point-max))
1534 (muse-publish-mark-read-only (point-min) (point-max))
1535 (goto-char (point-max)))
1536 (let ((,markup-function (read ,markup)))
1537 (cond ((eq ,markup-function 'example)
1538 (setq ,markup-function #'muse-publish-examplify-buffer))
1539 ((eq ,markup-function 'verse)
1540 (setq ,markup-function #'muse-publish-versify-buffer))
1541 ((and ,markup-function (not (functionp ,markup-function)))
1542 (error "Invalid markup function `%s'" ,markup))
1543 (t nil))
1544 (if ,markup-function
1545 (funcall ,markup-function)
1546 (muse-publish-mark-read-only (point-min) (point-max))
1547 (goto-char (point-max)))))))))
1549 (put 'muse-publish-markup-attribute 'lisp-indent-function 4)
1550 (put 'muse-publish-markup-attribute 'edebug-form-spec
1551 '(form form form form body))
1553 (defun muse-publish-lisp-tag (beg end attrs)
1554 (muse-publish-markup-attribute beg end attrs nil
1555 (save-excursion
1556 (let ((str (muse-eval-lisp
1557 (prog1
1558 (concat "(progn "
1559 (buffer-substring-no-properties (point-min)
1560 (point-max))
1561 ")")
1562 (delete-region beg end)))))
1563 (set-text-properties 0 (length str) nil str)
1564 (insert str)))))
1566 (defun muse-publish-command-tag (beg end attrs)
1567 (muse-publish-markup-attribute beg end attrs nil
1568 (while (looking-at "\\s-*$")
1569 (forward-line))
1570 (let ((interp (cdr (assoc "interp" attrs))))
1571 (if interp
1572 (shell-command-on-region (point) (point-max) interp t t)
1573 (shell-command
1574 (prog1
1575 (buffer-substring-no-properties (point) (point-max))
1576 (delete-region (point-min) (point-max)))
1577 t)))
1578 ;; make sure there is a newline at end
1579 (goto-char (point-max))
1580 (forward-line 0)
1581 (unless (looking-at "\\s-*$")
1582 (goto-char (point-max))
1583 (insert ?\n))
1584 (goto-char (point-min))))
1586 (defun muse-publish-perl-tag (beg end attrs)
1587 (muse-publish-command-tag beg end
1588 (cons (cons "interp" (executable-find "perl"))
1589 attrs)))
1591 (defun muse-publish-python-tag (beg end attrs)
1592 (muse-publish-command-tag beg end
1593 (cons (cons "interp" (executable-find "python"))
1594 attrs)))
1596 (defun muse-publish-ruby-tag (beg end attrs)
1597 (muse-publish-command-tag beg end
1598 (cons (cons "interp" (executable-find "ruby"))
1599 attrs)))
1601 (defun muse-publish-comment-tag (beg end)
1602 (if (null muse-publish-comments-p)
1603 (delete-region beg end)
1604 (goto-char end)
1605 (muse-insert-markup (muse-markup-text 'comment-end))
1606 (muse-publish-mark-read-only beg end)
1607 (goto-char beg)
1608 (muse-insert-markup (muse-markup-text 'comment-begin))))
1610 (defun muse-publish-include-tag (beg end attrs)
1611 "Include the named file at the current location during publishing.
1613 <include file=\"...\" markup=\"...\">
1615 The `markup' attribute controls how this file is marked up after
1616 being inserted. See `muse-publish-markup-attribute' for an
1617 explanation of how it works."
1618 (let ((filename (cdr (assoc "file" attrs)))
1619 (muse-publishing-directives muse-publishing-directives))
1620 (if filename
1621 (setq filename (expand-file-name
1622 filename
1623 (file-name-directory muse-publishing-current-file)))
1624 (error "No file attribute specified in <include> tag"))
1625 (muse-publish-markup-attribute beg end attrs t
1626 (insert-file-contents filename))))
1628 (defun muse-publish-mark-up-tag (beg end attrs)
1629 "Run an Emacs Lisp function on the region delimted by this tag.
1631 <markup function=\"...\">
1633 The optional `function' attribute controls how this section is
1634 marked up. If used, it should be the name of a function to call
1635 with the buffer narrowed to the delimited region. Note that no
1636 further marking-up will be performed on this region.
1638 If `function' is ommitted, use the standard Muse markup function.
1639 This is useful for marking up content in headers and footers."
1640 (let ((function (cdr (assoc "function" attrs)))
1641 (muse-publishing-directives muse-publishing-directives))
1642 (if function
1643 (let ((markup-function (intern function)))
1644 (if (and markup-function (functionp markup-function))
1645 (save-restriction
1646 (narrow-to-region beg end)
1647 (funcall markup-function)
1648 (goto-char (point-max)))
1649 (error "Invalid markup function `%s'" function)))
1650 (muse-publish-markup-region beg end))
1651 (muse-publish-mark-read-only beg (point))))
1653 ;; Miscellaneous helper functions
1655 (defun muse-publish-strip-tags (string)
1656 "Remove all tags from the string."
1657 (while (string-match "<.*?>" string)
1658 (setq string (replace-match "" nil t string)))
1659 string)
1661 (defun muse-publish-markup-type (category default-func)
1662 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1663 (funcall (or rule default-func))))
1665 (defun muse-published-buffer-contents (buffer)
1666 (with-current-buffer buffer
1667 (goto-char (point-min))
1668 (let ((beg (and (search-forward "Emacs Muse begins here")
1669 (muse-line-end-position)))
1670 (end (and (search-forward "Emacs Muse ends here")
1671 (muse-line-beginning-position))))
1672 (buffer-substring-no-properties beg end))))
1674 (defun muse-published-contents (file)
1675 (when (file-readable-p file)
1676 (muse-with-temp-buffer
1677 (insert-file-contents file)
1678 (muse-published-buffer-contents (current-buffer)))))
1680 (defun muse-publish-transform-output
1681 (file temp-file output-path name gen-func &rest cleanup-exts)
1682 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1683 (setq file (muse-page-name file))
1684 (message "Generating %s output for %s..." name file)
1685 (if (not (funcall gen-func temp-file output-path))
1686 (message "Generating %s from %s...failed" name file)
1687 (message "Generating %s output for %s...done" name file)
1688 (muse-delete-file-if-exists temp-file)
1689 (dolist (ext cleanup-exts)
1690 (muse-delete-file-if-exists
1691 (expand-file-name (concat file ext)
1692 (file-name-directory output-path))))
1693 (message "Wrote %s" output-path)))
1695 (defun muse-publish-read-only (string)
1696 (let ((end (1- (length string))))
1697 (add-text-properties 0 end
1698 '(rear-nonsticky (read-only) read-only t)
1699 string)
1700 string))
1702 (provide 'muse-publish)
1704 ;;; muse-publish.el ends here