Fix tag-publishing in <insert> and <markup>.
[muse-el.git] / lisp / muse-publish.el
blob5ee89a80a19d768ce2a46c109bd958dde6c5887a
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 ;; define anchor points
106 (1500 "^\\(\\W*\\)#\\(\\S-+\\)\\s-*" 0 anchor)
108 ;; emphasized or literal text
109 (1600 ,(concat "\\(^\\|[-[" muse-regexp-blank
110 "<('`\"\n]\\)\\(=[^=" muse-regexp-blank
111 "\n]\\|_[^_" muse-regexp-blank
112 "\n]\\|\\*+[^*" muse-regexp-blank
113 "\n]\\)")
114 2 word)
116 ;; headings, outline-mode style
117 (1700 "^\\(\\*+\\)\\s-+" 0 heading)
119 ;; ellipses
120 (1800 "\\.\\.\\.\\." 0 enddots)
121 (1850 "\\.\\.\\." 0 dots)
123 ;; horizontal rule, or section separator
124 (1900 "^----+" 0 rule)
126 ;; non-breaking space
127 (1950 "~~" 0 no-break-space)
129 ;; beginning of footnotes section
130 (2000 "^Footnotes:?\\s-*" 0 fn-sep)
131 ;; footnote definition/reference (def if at beginning of line)
132 (2100 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote)
134 ;; unnumbered List items begin with a -. numbered list items
135 ;; begin with number and a period. definition lists have a
136 ;; leading term separated from the body with ::. centered
137 ;; paragraphs begin with at least six columns of whitespace; any
138 ;; other whitespace at the beginning indicates a blockquote. The
139 ;; reason all of these rules are handled here, is so that
140 ;; blockquote detection doesn't interfere with indented list
141 ;; members.
142 (2200 ,(format muse-list-item-regexp (concat "[" muse-regexp-blank "]*"))
143 0 list)
145 ;; simple table markup is supported, nothing fancy. use | to
146 ;; separate cells, || to separate header cells, and ||| for footer
147 ;; cells
148 (2300 ,(concat "\\(\\([" muse-regexp-blank "]*\n\\)?"
149 "\\(" muse-table-line-regexp "\\(?:\n\\|\\'\\)\\)\\)+")
150 0 table)
152 ;; blockquote and centered text
153 (2400 ,(concat "^\\([" muse-regexp-blank "]+\\).+") 0 quote)
155 ;; the emdash ("--")
156 (2500 ,(concat "\\(^\\|[" muse-regexp-blank "]+\\)--\\($\\|["
157 muse-regexp-blank "]+\\)")
158 0 emdash)
160 ;; "verse" text is indicated the same way as a quoted e-mail
161 ;; response: "> text", where text may contain initial whitespace
162 ;; (see below).
163 (2600 ,(concat "^[" muse-regexp-blank "]*> ") 0 verse)
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 nil muse-publish-lisp-tag)
250 ("class" t t muse-publish-class-tag)
251 ("command" t t muse-publish-command-tag)
252 ("comment" t nil muse-publish-comment-tag)
253 ("include" nil t muse-publish-include-tag)
254 ("markup" t t muse-publish-mark-up-tag))
255 "A list of tag specifications, for specially marking up text.
256 XML-style tags are the best way to add custom markup to Muse.
257 This is easily accomplished by customizing this list of markup tags.
259 For each entry, the name of the tag is given, whether it expects
260 a closing tag and/or an optional set of attributes, and a
261 function that performs whatever action is desired within the
262 delimited region.
264 The tags themselves are deleted during publishing, before the
265 function is called. The function is called with three arguments,
266 the beginning and end of the region surrounded by the tags. If
267 properties are allowed, they are passed as a third argument in
268 the form of an alist. The `end' argument to the function is
269 always a marker.
271 Point is always at the beginning of the region within the tags, when
272 the function is called. Wherever point is when the function finishes
273 is where tag markup will resume.
275 These tag rules are processed once at the beginning of markup, and
276 once at the end, to catch any tags which may have been inserted
277 in-between."
278 :type '(repeat (list (string :tag "Markup tag")
279 (boolean :tag "Expect closing tag" :value t)
280 (boolean :tag "Parse attributes" :value nil)
281 function))
282 :group 'muse-publish)
284 (defcustom muse-publish-markup-specials nil
285 "A table of characters which must be represented specially."
286 :type '(alist :key-type character :value-type string)
287 :group 'muse-publish)
289 (defvar muse-publishing-p nil
290 "Set to t while a page is being published.")
291 (defvar muse-batch-publishing-p nil
292 "Set to t while a page is being batch published.")
293 (defvar muse-publishing-styles nil
294 "The publishing styles that Muse recognizes.
295 This is automatically generated when loading publishing styles.")
296 (defvar muse-publishing-current-file nil
297 "The file that is currently being published.")
298 (defvar muse-publishing-current-output-path nil
299 "The path where the current file will be published to.")
300 (defvar muse-publishing-current-style nil
301 "The style of the file that is currently being published.")
302 (defvar muse-publishing-directives nil
303 "An alist of publishing directives from the top of a file.")
304 (defvar muse-publish-generate-contents nil
305 "Non-nil if a table of contents should be generated.
306 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
307 tell where the <contents> was seen, and to what depth the
308 contents were requested.")
309 (defvar muse-publishing-last-position nil
310 "Last position of the point when publishing.
311 This is used to make sure that publishing doesn't get stalled.")
313 ;; Functions for handling style information
315 (defsubst muse-style (&optional style)
316 "Resolve the given STYLE into a Muse style, if it is a string."
317 (if (null style)
318 muse-publishing-current-style
319 (if (stringp style)
320 (assoc style muse-publishing-styles)
321 (muse-assert (consp style))
322 style)))
324 (defun muse-define-style (name &rest elements)
325 (let ((entry (assoc name muse-publishing-styles)))
326 (if entry
327 (setcdr entry elements)
328 (setq muse-publishing-styles
329 (cons (append (list name) elements)
330 muse-publishing-styles)))))
332 (defun muse-derive-style (new-name base-name &rest elements)
333 (apply 'muse-define-style new-name
334 (append elements (list :base base-name))))
336 (defsubst muse-get-keyword (keyword list &optional direct)
337 (let ((value (cadr (memq keyword list))))
338 (if (and (not direct) (symbolp value))
339 (symbol-value value)
340 value)))
342 (defun muse-style-elements-list (elem &optional style)
343 "Return a list all references to ELEM in STYLE, including base styles.
344 If STYLE is not specified, use current style."
345 (let (base elements)
346 (while style
347 (setq style (muse-style style))
348 (setq elements (append elements
349 (muse-get-keyword elem style)))
350 (setq style (muse-get-keyword :base style)))
351 elements))
353 (defsubst muse-style-element (elem &optional style direct)
354 "Search for ELEM in STYLE, including base styles.
355 If STYLE is not specified, use current style."
356 (setq style (muse-style style))
357 (let ((value (muse-get-keyword elem style direct)))
358 (if value
359 value
360 (let ((base (muse-get-keyword :base style)))
361 (if base
362 (muse-style-element elem base direct))))))
364 (defun muse-find-markup-element (keyword ident style)
365 (let ((def (assq ident (muse-style-element keyword style))))
366 (if def
367 (cdr def)
368 (let ((base (muse-style-element :base style)))
369 (if base
370 (muse-find-markup-element keyword ident base))))))
372 (defun muse-markup-text (ident &rest args)
373 "Insert ARGS into the text markup associated with IDENT.
374 If the markup text has sections like %N%, this will be replaced
375 with the N-1th argument in ARGS. After that, `format' is applied
376 to the text with ARGS as parameters."
377 (let ((text (muse-find-markup-element :strings ident (muse-style))))
378 (if (and text args)
379 (progn
380 (let (start repl-text)
381 (while (setq start (string-match "%\\([1-9][0-9]*\\)%" text start))
382 ;; escape '%' in the argument text, since we will be
383 ;; using format on it
384 (setq repl-text (muse-replace-regexp-in-string
385 "%" "%%"
386 (nth (1- (string-to-number
387 (match-string 1 text))) args)
388 t t)
389 start (+ start (length repl-text))
390 text (replace-match repl-text t t text))))
391 (apply 'format text args))
392 (or text ""))))
394 (defun muse-insert-markup (&rest args)
395 (let ((beg (point)))
396 (apply 'insert args)
397 (muse-publish-mark-read-only beg (point))))
399 (defun muse-find-markup-tag (keyword tagname style)
400 (let ((def (assoc tagname (muse-style-element keyword style))))
401 (or def
402 (let ((base (muse-style-element :base style)))
403 (if base
404 (muse-find-markup-tag keyword tagname base))))))
406 (defsubst muse-markup-tag-info (tagname &rest args)
407 (let ((tag-info (muse-find-markup-tag :tags tagname (muse-style))))
408 (or tag-info
409 (assoc (match-string 1) muse-publish-markup-tags))))
411 (defsubst muse-markup-function (category)
412 (let ((func (muse-find-markup-element :functions category (muse-style))))
413 (or func
414 (cdr (assq category muse-publish-markup-functions)))))
416 ;; Publishing routines
418 (defun muse-publish-markup (name rules)
419 (let* ((case-fold-search nil)
420 (inhibit-read-only t)
421 (limit (* (length rules) (point-max)))
422 (verbose (and muse-publish-report-threshhold
423 (> (point-max) muse-publish-report-threshhold)))
424 (base 0))
425 (while rules
426 (goto-char (point-min))
427 (let ((regexp (nth 1 (car rules)))
428 (group (nth 2 (car rules)))
429 (repl (nth 3 (car rules)))
430 pos)
431 (setq muse-publishing-last-position nil)
432 (if (symbolp regexp)
433 (setq regexp (symbol-value regexp)))
434 (if (and verbose (not muse-batch-publishing-p))
435 (message "Publishing %s...%d%%" name
436 (* (/ (float (+ (point) base)) limit) 100)))
437 (while (and regexp (setq pos (re-search-forward regexp nil t)))
438 (if (and verbose (not muse-batch-publishing-p))
439 (message "Publishing %s...%d%%" name
440 (* (/ (float (+ (point) base)) limit) 100)))
441 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
442 (match-beginning group)
443 (get-text-property (match-beginning group) 'read-only))
444 (let* (func
445 (text (cond
446 ((and (symbolp repl)
447 (setq func (muse-markup-function repl)))
448 (funcall func))
449 ((functionp repl)
450 (funcall repl))
451 ((symbolp repl)
452 (symbol-value repl))
453 (t repl))))
454 (if (stringp text)
455 (replace-match text t))))
456 (if (and muse-publishing-last-position
457 (= pos muse-publishing-last-position))
458 (if (eobp)
459 (setq regexp nil)
460 (forward-char 1)))
461 (setq muse-publishing-last-position pos)))
462 (setq rules (cdr rules)
463 base (+ base (point-max))))
464 (if (and verbose (not muse-batch-publishing-p))
465 (message "Publishing %s...done" name))))
467 (defcustom muse-publish-markup-header-footer-tags
468 '(("lisp" t nil muse-publish-lisp-tag)
469 ("markup" t t muse-publish-mark-up-tag))
470 "Tags used when publishing headers and footers.
471 See `muse-publish-markup-tags' for details."
472 :type '(repeat (list (string :tag "Markup tag")
473 (boolean :tag "Expect closing tag" :value t)
474 (boolean :tag "Parse attributes" :value nil)
475 function))
476 :group 'muse-publish)
478 (defun muse-insert-file-or-string (file-or-string &optional title)
479 (let ((beg (point)) end)
480 (if (and (not (string-equal file-or-string ""))
481 (not (string-match "\n" file-or-string))
482 (file-readable-p file-or-string))
483 (setq end (+ beg (cadr (insert-file-contents file-or-string))))
484 (insert file-or-string)
485 (setq end (point)))
486 (save-restriction
487 (narrow-to-region beg end)
488 (remove-text-properties (point-min) (point-max)
489 '(read-only nil rear-nonsticky nil))
490 (goto-char (point-min))
491 (let ((muse-inhibit-style-tags t))
492 (muse-publish-markup (or title "")
493 '((100 muse-tag-regexp 0
494 muse-publish-markup-tag)))))))
496 (defun muse-style-run-hooks (keyword style &rest args)
497 (catch 'handled
498 (while (and style
499 (setq style (muse-style style)))
500 (let ((func (muse-get-keyword keyword style t)))
501 (when (and func
502 (apply func args))
503 (throw 'handled t)))
504 (setq style (muse-style-element :base style)))))
506 (defun muse-publish-markup-region (beg end &optional title style)
507 "Apply the given STYLE's markup rules to the given region.
508 TITLE is used when indicating the publishing progress; it may be nil."
509 (unless title (setq title ""))
510 (unless style
511 (or (setq style muse-publishing-current-style)
512 (error "Cannot find any publishing styles to use")))
513 (save-restriction
514 (narrow-to-region beg end)
515 (muse-style-run-hooks :before style)
516 (muse-publish-markup
517 title
518 (sort (copy-alist (append muse-publish-markup-regexps
519 (muse-style-elements-list :regexps style)))
520 (function
521 (lambda (l r)
522 (< (car l) (car r))))))
523 (muse-style-run-hooks :before-end style)
524 (muse-publish-escape-specials (point-min) (point-max) nil 'document)))
526 (defun muse-publish-markup-buffer (title style)
527 "Apply the given STYLE's markup rules to the current buffer."
528 (setq style (muse-style style))
529 (let ((style-header (muse-style-element :header style))
530 (style-footer (muse-style-element :footer style))
531 (muse-publishing-current-style style)
532 (muse-publishing-directives
533 (list (cons "title" title)
534 (cons "author" (user-full-name))
535 (cons "date" (format-time-string
536 "%B %e, %Y"
537 (nth 5 (file-attributes
538 muse-publishing-current-file))))))
539 (muse-publishing-p t)
540 (inhibit-read-only t))
541 (run-hooks 'muse-update-values-hook)
542 (run-hooks 'muse-before-publish-hook)
543 (muse-publish-markup-region (point-min) (point-max) title style)
544 (goto-char (point-min))
545 (when style-header
546 (muse-insert-file-or-string style-header title))
547 (goto-char (point-max))
548 (when style-footer
549 (muse-insert-file-or-string style-footer title))
550 (muse-style-run-hooks :after style)
551 (run-hooks 'muse-after-publish-hook)))
553 (defun muse-publish-markup-string (string &optional style)
554 "Markup STRING using the given STYLE's markup rules."
555 (setq style (muse-style style))
556 (muse-with-temp-buffer
557 (insert string)
558 (let ((muse-publishing-current-style style)
559 (muse-publishing-p t))
560 (muse-publish-markup "*string*" (muse-style-element :rules style)))
561 (buffer-string)))
563 ;; Commands for publishing files
565 (defsubst muse-publish-get-style (&optional styles)
566 (unless styles (setq styles muse-publishing-styles))
567 (if (= 1 (length styles))
568 (car styles)
569 (assoc (completing-read "Publish with style: " styles nil t)
570 styles)))
572 (defsubst muse-publish-get-output-dir (style)
573 (let ((default-directory (or (muse-style-element :path style)
574 default-directory)))
575 (muse-read-directory-name "Publish to directory: " nil default-directory)))
577 (defsubst muse-publish-get-info ()
578 (let ((style (muse-publish-get-style)))
579 (list style (muse-publish-get-output-dir style)
580 current-prefix-arg)))
582 (defsubst muse-publish-output-name (&optional file style)
583 (setq style (muse-style style))
584 (concat (muse-style-element :prefix style)
585 (muse-page-name file)
586 (muse-style-element :suffix style)))
588 (defsubst muse-publish-output-file (file &optional output-dir style)
589 (setq style (muse-style style))
590 (if output-dir
591 (expand-file-name (muse-publish-output-name file style) output-dir)
592 (concat (file-name-directory file)
593 (muse-publish-output-name file style))))
595 (defsubst muse-publish-link-name (&optional file style)
596 (setq style (muse-style style))
597 (concat (muse-style-element :prefix style)
598 (muse-page-name file)
599 (or (muse-style-element :link-suffix style)
600 (muse-style-element :suffix style))))
602 (defsubst muse-publish-link-file (file &optional output-dir style)
603 (setq style (muse-style style))
604 (if output-dir
605 (expand-file-name (muse-publish-link-name file style) output-dir)
606 (concat (file-name-directory file)
607 (muse-publish-link-name file style))))
609 ;;;###autoload
610 (defun muse-publish-file (file style &optional output-dir force)
611 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
612 If the argument FORCE is nil, each file is only published if it is
613 newer than the published version. If the argument FORCE is non-nil,
614 the file is published no matter what."
615 (interactive (cons (read-file-name "Publish file: ")
616 (muse-publish-get-info)))
617 (let ((style-name style))
618 (setq style (muse-style style))
619 (unless style
620 (error "There is no style '%s' defined." style-name)))
621 (let* ((output-path (muse-publish-output-file file output-dir style))
622 (output-suffix (muse-style-element :osuffix style))
623 (muse-publishing-current-file file)
624 (muse-publishing-current-output-path output-path)
625 (target (if output-suffix
626 (concat (file-name-sans-extension output-path)
627 output-suffix)
628 output-path))
629 (threshhold (nth 7 (file-attributes file))))
630 (if (not threshhold)
631 (message "Please save %s before publishing" file)
632 (when (or force (file-newer-than-file-p file target))
633 (if (and muse-publish-report-threshhold
634 (> threshhold
635 muse-publish-report-threshhold))
636 (message "Publishing %s ..." file))
637 (muse-with-temp-buffer
638 (insert-file-contents file)
639 (muse-publish-markup-buffer (muse-page-name file) style)
640 (let ((backup-inhibited t))
641 (write-file output-path))
642 (muse-style-run-hooks :final style file output-path target))
643 t))))
645 ;;;###autoload
646 (defun muse-publish-this-file (style output-dir &optional force)
647 "Publish the page in the current file."
648 (interactive (muse-publish-get-info))
649 (unless (muse-publish-file buffer-file-name style output-dir force)
650 (message (concat "The published version is up-to-date; use"
651 " C-u C-c C-t to force an update."))))
653 (defun muse-batch-publish-files ()
654 "Publish Muse files in batch mode."
655 (let ((muse-batch-publishing-p t)
656 style output-dir)
657 (setq style (car command-line-args-left)
658 command-line-args-left (cdr command-line-args-left)
659 output-dir (car command-line-args-left)
660 output-dir
661 (if (string-match "\\`--output-dir=" output-dir)
662 (prog1
663 (substring output-dir (match-end 0))
664 (setq command-line-args-left (cdr command-line-args-left)))))
665 (setq auto-mode-alist
666 (delete (cons (concat "\\." muse-file-extension "\\'")
667 'muse-mode-choose-mode)
668 auto-mode-alist))
669 (dolist (file command-line-args-left)
670 (muse-publish-file file style output-dir t))))
672 ;; Default publishing rules
674 (defun muse-publish-section-close (depth)
675 "Seach forward for the closing tag of given DEPTH."
676 (let (not-end)
677 (save-excursion
678 (while (and (setq not-end (re-search-forward
679 (concat "^\\*\\{1," (number-to-string depth)
680 "\\}\\s-+")
681 nil t))
682 (get-text-property (match-beginning 0) 'read-only)))
683 (if not-end
684 (forward-line 0)
685 (goto-char (point-max)))
686 (cond ((not (eq (char-before) ?\n))
687 (insert "\n\n"))
688 ((not (eq (char-before (1- (point))) ?\n))
689 (insert "\n")))
690 (muse-insert-markup (muse-markup-text 'section-close depth))
691 (insert "\n"))))
693 (defun muse-publish-markup-directive (&optional name value)
694 (unless name (setq name (match-string 1)))
695 (unless value (setq value (match-string 2)))
696 (let ((elem (assoc name muse-publishing-directives)))
697 (if elem
698 (setcdr elem value)
699 (setq muse-publishing-directives
700 (cons (cons name value)
701 muse-publishing-directives))))
702 ;; Make sure we don't ever try to move the point forward (past the
703 ;; beginning of buffer) while we're still searching for directives.
704 (setq muse-publishing-last-position nil)
705 (delete-region (match-beginning 0) (match-end 0)))
707 (defsubst muse-publishing-directive (name)
708 (cdr (assoc name muse-publishing-directives)))
710 (defun muse-publish-markup-anchor ()
711 (unless (get-text-property (match-end 1) 'muse-link)
712 (let ((text (muse-markup-text 'anchor (match-string 2))))
713 (unless (string= text "")
714 (save-match-data
715 (skip-chars-forward (concat muse-regexp-blank "\n"))
716 (muse-insert-markup text)))
717 (match-string 1))))
719 (defun muse-publish-markup-comment ()
720 (if (null muse-publish-comments-p)
722 (goto-char (match-end 0))
723 (muse-insert-markup (muse-markup-text 'comment-end))
724 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
725 (delete-region (match-beginning 0) (match-beginning 1))
726 (goto-char (match-beginning 0))
727 (muse-insert-markup (muse-markup-text 'comment-begin))))
729 (defvar muse-inhibit-style-tags nil
730 "If non-nil, do not search for style-specific tags.
731 This is used when publishing headers and footers.")
733 (defun muse-publish-markup-tag ()
734 (let ((tag-info (if muse-inhibit-style-tags
735 (assoc (match-string 1) muse-publish-markup-tags)
736 (muse-markup-tag-info (match-string 1)))))
737 (when (and tag-info
738 (not (get-text-property (match-beginning 0) 'read-only)))
739 (let ((closed-tag (match-string 3))
740 (start (match-beginning 0))
741 (beg (point))
742 end attrs)
743 (when (nth 2 tag-info)
744 (let ((attrstr (match-string 2)))
745 (while (and attrstr
746 (string-match (concat "\\([^"
747 muse-regexp-blank
748 "=\n]+\\)\\(=\"\\"
749 "([^\"]+\\)\"\\)?")
750 attrstr))
751 (let ((attr (cons (downcase
752 (muse-match-string-no-properties 1 attrstr))
753 (muse-match-string-no-properties 3 attrstr))))
754 (setq attrstr (replace-match "" t t attrstr))
755 (if attrs
756 (nconc attrs (list attr))
757 (setq attrs (list attr)))))))
758 (if (and (cadr tag-info) (not closed-tag))
759 (if (search-forward (concat "</" (car tag-info) ">") nil t)
760 (delete-region (match-beginning 0) (point))
761 (setq tag-info nil)))
762 (when tag-info
763 (setq end (point-marker))
764 (delete-region start beg)
765 (goto-char start)
766 (let ((args (list start end)))
767 (if (nth 2 tag-info)
768 (nconc args (list attrs)))
769 (let ((muse-inhibit-style-tags nil))
770 ;; remove the inhibition
771 (apply (nth 3 tag-info) args)))))))
772 nil)
774 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
775 "Escape specials from BEG to END using style-specific :specials.
776 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
777 CONTEXT is used to figure out what kind of specials to escape.
779 The following contexts exist in Muse.
780 'underline _underlined text_
781 'literal =monospaced text= or <code> region (monospaced, escaped)
782 'emphasis *emphasized text*
783 'email email@example.com
784 'url http://example.com
785 'url-desc [[...][description of an extended link]]
786 'example <example> region (monospaced, block context, escaped)
787 'verbatim <verbatim> region (escaped)
788 'document normal text"
789 (let ((specials (muse-style-element :specials nil t)))
790 (cond ((functionp specials)
791 (setq specials (funcall specials context)))
792 ((symbolp specials)
793 (setq specials (symbol-value specials))))
794 (if (functionp specials)
795 (funcall specials beg end ignore-read-only)
796 (save-excursion
797 (save-restriction
798 (narrow-to-region beg end)
799 (goto-char (point-min))
800 (while (< (point) (point-max))
801 (if (and (not ignore-read-only)
802 (get-text-property (point) 'read-only))
803 (goto-char (or (next-single-property-change (point) 'read-only)
804 (point-max)))
805 (let ((repl (or (assoc (char-after) specials)
806 (assoc (char-after)
807 muse-publish-markup-specials))))
808 (if (null repl)
809 (forward-char 1)
810 (delete-char 1)
811 (insert-before-markers (cdr repl)))))))))))
813 (defun muse-publish-markup-word ()
814 (let* ((beg (match-beginning 2))
815 (end (1- (match-end 2)))
816 (leader (buffer-substring-no-properties beg end))
817 open-tag close-tag mark-read-only loc context)
818 (cond
819 ((string= leader "_")
820 (setq context 'underline
821 open-tag (muse-markup-text 'begin-underline)
822 close-tag (muse-markup-text 'end-underline)))
823 ((string= leader "=")
824 (setq context 'literal
825 open-tag (muse-markup-text 'begin-literal)
826 close-tag (muse-markup-text 'end-literal))
827 (setq mark-read-only t))
829 (let ((l (length leader)))
830 (setq context 'emphasis)
831 (cond
832 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
833 close-tag (muse-markup-text 'end-emph)))
834 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
835 close-tag (muse-markup-text 'end-more-emph)))
836 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
837 close-tag (muse-markup-text 'end-most-emph)))
838 (t (setq context nil))))))
839 (if (and context
840 (not (get-text-property beg 'muse-link))
841 (setq loc (search-forward leader nil t))
842 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
843 (not (eq (char-syntax (char-before (point))) ?\ ))
844 (not (get-text-property (point) 'muse-link)))
845 (progn
846 (replace-match "")
847 (delete-region beg end)
848 (setq end (point-marker))
849 (muse-insert-markup close-tag)
850 (goto-char beg)
851 (muse-insert-markup open-tag)
852 (setq beg (point))
853 (when mark-read-only
854 (muse-publish-escape-specials beg end t context)
855 (muse-publish-mark-read-only beg end)))
856 (backward-char))
857 nil))
859 (defun muse-publish-markup-emdash ()
860 (unless (get-text-property (match-beginning 0) 'muse-link)
861 (let ((prespace (match-string 1))
862 (postspace (match-string 2)))
863 (delete-region (match-beginning 0) (match-end 0))
864 (muse-insert-markup (muse-markup-text 'emdash prespace postspace))
865 (when (eq (char-after) ?\<)
866 (insert ?\n)))))
868 (defun muse-publish-markup-enddots ()
869 (unless (get-text-property (match-beginning 0) 'muse-link)
870 (delete-region (match-beginning 0) (match-end 0))
871 (muse-insert-markup (muse-markup-text 'enddots))))
873 (defun muse-publish-markup-dots ()
874 (unless (get-text-property (match-beginning 0) 'muse-link)
875 (delete-region (match-beginning 0) (match-end 0))
876 (muse-insert-markup (muse-markup-text 'dots))))
878 (defun muse-publish-markup-rule ()
879 (unless (get-text-property (match-beginning 0) 'muse-link)
880 (delete-region (match-beginning 0) (match-end 0))
881 (muse-insert-markup (muse-markup-text 'rule))))
883 (defun muse-publish-markup-no-break-space ()
884 (unless (get-text-property (match-beginning 0) 'muse-link)
885 (delete-region (match-beginning 0) (match-end 0))
886 (muse-insert-markup (muse-markup-text 'no-break-space))))
888 (defun muse-publish-markup-heading ()
889 (let* ((len (length (match-string 1)))
890 (start (muse-markup-text
891 (cond ((= len 1) 'section)
892 ((= len 2) 'subsection)
893 ((= len 3) 'subsubsection)
894 (t 'section-other))
895 len))
896 (end (muse-markup-text
897 (cond ((= len 1) 'section-end)
898 ((= len 2) 'subsection-end)
899 ((= len 3) 'subsubsection-end)
900 (t 'section-other-end))
901 len)))
902 (delete-region (match-beginning 0) (match-end 0))
903 (muse-insert-markup start)
904 (end-of-line)
905 (when end
906 (muse-insert-markup end))
907 (muse-publish-section-close len)))
909 (defvar muse-publish-footnotes nil)
911 (defun muse-publish-markup-footnote ()
912 "Scan ahead and snarf up the footnote body"
913 (cond
914 ((get-text-property (match-beginning 0) 'muse-link)
915 nil)
916 ((= (muse-line-beginning-position) (match-beginning 0))
919 (let ((footnote (save-match-data
920 (string-to-number (match-string 1))))
921 footnotemark)
922 (delete-region (match-beginning 0) (match-end 0))
923 (save-excursion
924 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
925 (let* ((start (match-beginning 0))
926 (beg (goto-char (match-end 0)))
927 (end (save-excursion
928 (if (search-forward "\n\n" nil t)
929 (copy-marker (match-beginning 0))
930 (goto-char (point-max))
931 (skip-chars-backward "\n")
932 (point-marker)))))
933 (while (re-search-forward
934 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
935 end t)
936 (replace-match "\\1" t))
937 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
938 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
939 (if (string= "" footnotemark-cmd)
940 (setq footnotemark
941 (concat (muse-markup-text 'footnote)
942 (buffer-substring-no-properties beg end)
943 (muse-markup-text 'footnote-end)))
944 (setq footnotemark (format footnotemark-cmd footnote
945 footnotemark-end-cmd))
946 (unless muse-publish-footnotes
947 (set (make-local-variable 'muse-publish-footnotes)
948 (make-vector 256 nil)))
949 (unless (aref muse-publish-footnotes footnote)
950 (setq footnotemark
951 (concat
952 footnotemark
953 (concat (format (muse-markup-text 'footnotetext)
954 footnote)
955 (buffer-substring-no-properties beg end)
956 (muse-markup-text 'footnotetext-end))))
957 (aset muse-publish-footnotes footnote footnotemark))))
958 (goto-char end)
959 (skip-chars-forward "\n")
960 (delete-region start (point)))))
961 (muse-insert-markup (or footnotemark footnote))))))
963 (defun muse-publish-markup-fn-sep ()
964 (delete-region (match-beginning 0) (match-end 0))
965 (muse-insert-markup (muse-markup-text 'fn-sep)))
967 (defun muse-insert-markup-end-list (&rest args)
968 (let ((beg (point)))
969 (apply 'insert args)
970 (add-text-properties beg (point) '(end-list t))
971 (muse-publish-mark-read-only beg (point))))
973 (defun muse-publish-surround-dl (indent post-indent)
974 (let* ((beg-item (muse-markup-text 'begin-dl-item))
975 (end-item (muse-markup-text 'end-dl-item))
976 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
977 (end-ddt (muse-markup-text 'end-ddt))
978 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
979 (end-dde (muse-markup-text 'end-dde))
980 (continue t)
981 def-on-same-line beg)
982 (while continue
983 ;; envelope this as one term+definitions unit -- HTML does not
984 ;; need this, but DocBook and Muse's custom XML format do
985 (muse-insert-markup beg-item)
986 (when (looking-at muse-dl-term-regexp)
987 ;; find the term and wrap it with published markup
988 (setq beg (point))
989 (goto-char (match-end 1))
990 (delete-region (point) (match-end 0))
991 (muse-insert-markup-end-list end-ddt)
992 ;; if definition is immediately after term, move to next line
993 (unless (eq (char-after) ?\n)
994 (insert ?\n))
995 (save-excursion
996 (goto-char beg)
997 (delete-region (point) (match-beginning 1))
998 (muse-insert-markup beg-ddt)))
999 (setq beg (point)
1000 ;; move past current item
1001 continue (muse-forward-list-item 'dl-term indent))
1002 (save-restriction
1003 (narrow-to-region beg (point))
1004 (goto-char (point-min))
1005 ;; publish each definition that we find, defaulting to an
1006 ;; empty definition if none are found
1007 (muse-publish-surround-text beg-dde end-dde
1008 (lambda (indent)
1009 (muse-forward-list-item 'dl-entry indent))
1010 nil nil t)
1011 (goto-char (point-max))
1012 (skip-chars-backward (concat muse-regexp-blank "\n"))
1013 (muse-insert-markup-end-list end-item)
1014 (when continue
1015 (goto-char (point-max)))))))
1017 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent determine-indent)
1018 (let ((continue t)
1019 (list-item (format muse-list-item-regexp
1020 (concat "[" muse-regexp-blank "]*")))
1021 init-indent beg)
1022 (unless indent
1023 (setq indent (concat "[" muse-regexp-blank "]+")))
1024 (if post-indent
1025 (setq post-indent (concat " \\{0," (number-to-string post-indent)
1026 "\\}"))
1027 (setq post-indent ""))
1028 (while continue
1029 (muse-insert-markup beg-tag)
1030 (setq beg (point)
1031 ;; move past current item; continue is non-nil if there
1032 ;; are more like items to be processed
1033 continue (funcall move-func indent))
1034 (save-restriction
1035 (when determine-indent
1036 ;; if the caller doesn't know how much indentation
1037 ;; to use, figure it out ourselves
1038 (if (not continue)
1039 (setq indent "")
1040 (save-match-data
1041 ;; snarf all leading whitespace
1042 (let ((this-indent (and (match-beginning 2)
1043 (buffer-substring (match-beginning 1)
1044 (match-beginning 2)))))
1045 (when (and this-indent
1046 (not (string= this-indent "")))
1047 (setq indent this-indent
1048 determine-indent nil))))))
1049 (when continue
1050 ;; remove list markup if we encountered another item of the
1051 ;; same type
1052 (replace-match "" t t nil 1))
1053 (narrow-to-region beg (point))
1054 ;; narrow to current item
1055 (goto-char (point-min))
1056 (forward-line 1)
1057 (let ((list-nested nil)
1058 (indent-found nil)
1059 (post-indent post-indent))
1060 (while (< (point) (point-max))
1061 (when (and (looking-at list-item)
1062 (not (or (get-text-property
1063 (muse-list-item-critical-point) 'read-only)
1064 (get-text-property
1065 (muse-list-item-critical-point) 'muse-link))))
1066 ;; if we encounter a list item, allow no post-indent
1067 ;; space
1068 (setq list-nested t))
1069 (when (looking-at (concat indent "\\("
1070 (or (and list-nested "")
1071 post-indent)
1072 "\\)"))
1073 ;; if list is not nested, remove indentation
1074 (unless indent-found
1075 (setq post-indent (match-string 1)
1076 indent-found t))
1077 (replace-match ""))
1078 (forward-line 1)))
1079 (skip-chars-backward (concat muse-regexp-blank "\n"))
1080 (muse-insert-markup-end-list end-tag)
1081 (when continue
1082 (goto-char (point-max)))))))
1084 (defun muse-publish-markup-list ()
1085 "Markup a list entry.
1086 This function works by marking up items of the same list level
1087 and type, respecting the end-of-list property."
1088 (let* ((str (match-string 1))
1089 (type (muse-list-item-type str))
1090 (indent (buffer-substring (muse-line-beginning-position)
1091 (match-beginning 1)))
1092 (post-indent (length str))
1093 (last (match-beginning 0)))
1094 (cond
1095 ((or (get-text-property (muse-list-item-critical-point) 'read-only)
1096 (get-text-property (muse-list-item-critical-point) 'muse-link))
1097 nil)
1098 ((eq type 'ul)
1099 (unless (eq (char-after (match-end 1)) ?-)
1100 (delete-region (match-beginning 0) (match-end 0))
1101 (muse-insert-markup (muse-markup-text 'begin-uli))
1102 (save-excursion
1103 (muse-publish-surround-text
1104 (muse-markup-text 'begin-uli-item)
1105 (muse-markup-text 'end-uli-item)
1106 (lambda (indent)
1107 (muse-forward-list-item 'ul indent))
1108 indent post-indent)
1109 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1110 (forward-line 1)))
1111 ((eq type 'ol)
1112 (delete-region (match-beginning 0) (match-end 0))
1113 (muse-insert-markup (muse-markup-text 'begin-oli))
1114 (save-excursion
1115 (muse-publish-surround-text
1116 (muse-markup-text 'begin-oli-item)
1117 (muse-markup-text 'end-oli-item)
1118 (lambda (indent)
1119 (muse-forward-list-item 'ol indent))
1120 indent post-indent)
1121 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1122 (forward-line 1))
1123 ((not (string= (match-string 2) ""))
1124 ;; must have an initial term
1125 (goto-char (match-beginning 0))
1126 (muse-insert-markup (muse-markup-text 'begin-dl))
1127 (save-excursion
1128 (muse-publish-surround-dl indent post-indent)
1129 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1130 (forward-line 1))))
1131 nil)
1133 (defun muse-publish-markup-quote ()
1134 "Markup a quoted paragraph.
1135 The reason this function is so funky, is to prevent text properties
1136 like read-only from being inadvertently deleted."
1137 (let* ((ws (match-string 1))
1138 (centered (>= (string-width ws) 6))
1139 (begin-elem (if centered 'begin-center 'begin-quote-item))
1140 (end-elem (if centered 'end-center 'end-quote-item)))
1141 (replace-match "" t t nil 1)
1142 (unless centered
1143 (muse-insert-markup (muse-markup-text 'begin-quote)))
1144 (muse-publish-surround-text (muse-markup-text begin-elem)
1145 (muse-markup-text end-elem)
1146 (function (lambda (indent)
1147 (muse-forward-paragraph)
1148 nil)))
1149 (unless centered
1150 (muse-insert-markup (muse-markup-text 'end-quote)))))
1152 (defun muse-publish-markup-leading-space (markup-space multiple)
1153 (let (count)
1154 (when (and markup-space
1155 (>= (setq count (skip-chars-forward " ")) 0))
1156 (delete-region (muse-line-beginning-position) (point))
1157 (while (> count 0)
1158 (muse-insert-markup markup-space)
1159 (setq count (- count multiple))))))
1161 (defun muse-publish-markup-verse ()
1162 (let ((leader (match-string 0)))
1163 (goto-char (match-beginning 0))
1164 (muse-insert-markup (muse-markup-text 'begin-verse))
1165 (while (looking-at leader)
1166 (replace-match "")
1167 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1168 (let ((beg (point)))
1169 (end-of-line)
1170 (cond
1171 ((bolp)
1172 (let ((text (muse-markup-text 'empty-verse-line)))
1173 (when text (muse-insert-markup text))))
1174 ((save-excursion
1175 (save-match-data
1176 (forward-line 1)
1177 (or (looking-at (concat leader "["
1178 muse-regexp-blank
1179 "]*$"))
1180 (not (looking-at leader)))))
1181 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1182 (end-text (muse-markup-text 'end-last-stanza-line)))
1183 (when end-text (muse-insert-markup end-text))
1184 (goto-char beg)
1185 (when begin-text (muse-insert-markup begin-text))
1186 (end-of-line)))
1188 (let ((begin-text (muse-markup-text 'begin-verse-line))
1189 (end-text (muse-markup-text 'end-verse-line)))
1190 (when end-text (muse-insert-markup end-text))
1191 (goto-char beg)
1192 (when begin-text (muse-insert-markup begin-text))
1193 (end-of-line))))
1194 (forward-line 1))))
1195 (muse-insert-markup (muse-markup-text 'end-verse))
1196 (insert ?\n))
1198 (defun muse-publish-table-fields (beg end)
1199 "Parse given region as a table, returning a cons cell.
1200 The car is the length of the longest row.
1202 The cdr is a list of the fields of the table, with the first
1203 element indicating the type of the row:
1204 1: body, 2: header, 3: footer.
1206 The existing region will be removed, except for initial blank lines."
1207 (unless (muse-publishing-directive "disable-tables")
1208 (let ((longest 0)
1209 (left 0)
1210 fields field-list)
1211 (save-restriction
1212 (narrow-to-region beg end)
1213 (goto-char (point-min))
1214 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1215 (forward-line 1))
1216 (setq beg (point))
1217 (while (= left 0)
1218 (when (looking-at muse-table-line-regexp)
1219 (setq fields (cons (length (match-string 1))
1220 (split-string (match-string 0)
1221 muse-table-field-regexp))
1222 field-list (cons fields field-list)
1223 longest (max (length fields) longest)))
1224 (setq left (forward-line 1))))
1225 (delete-region beg end)
1226 (if (= longest 0)
1227 (cons 0 nil)
1228 (cons (1- longest) (nreverse field-list))))))
1230 (defun muse-publish-markup-table ()
1231 "Style does not support tables.")
1233 (defun muse-publish-escape-specials-in-string (string &optional context)
1234 "Escape specials in STRING using style-specific :specials.
1235 CONTEXT is used to figure out what kind of specials to escape.
1237 See the documentation of the `muse-publish-escape-specials'
1238 function for the list of available contexts."
1239 (unless string
1240 (setq string ""))
1241 (let ((specials (muse-style-element :specials nil t)))
1242 (cond ((functionp specials)
1243 (setq specials (funcall specials context)))
1244 ((symbolp specials)
1245 (setq specials (symbol-value specials))))
1246 (if (functionp specials)
1247 (funcall specials string)
1248 (apply (function concat)
1249 (mapcar
1250 (lambda (ch)
1251 (let (repl)
1252 (setq repl (or (assoc ch specials)
1253 (assoc ch muse-publish-markup-specials)))
1254 (if (null repl)
1255 (char-to-string ch)
1256 (cdr repl))))
1257 (append string nil))))))
1259 (defun muse-publish-markup-email ()
1260 (let* ((beg (match-end 1))
1261 (addr (buffer-substring-no-properties beg (match-end 0))))
1262 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1263 (goto-char beg)
1264 (delete-region beg (match-end 0))
1265 (if (or (eq (char-before (match-beginning 0)) ?\")
1266 (eq (char-after (match-end 0)) ?\"))
1267 (insert addr)
1268 (insert (format (muse-markup-text 'email-addr) addr addr)))
1269 (muse-publish-mark-read-only beg (point))))
1271 (defun muse-publish-classify-url (target)
1272 "Transform anchors and get published name, if TARGET is a page.
1273 The return value is a cons cell. The car is the type of link,
1274 the cadr is the page name, and the cddr is the anchor."
1275 (save-match-data
1276 (cond ((or (null target) (string= target ""))
1277 nil)
1278 ((string-match muse-url-regexp target)
1279 (cons 'url (cons target nil)))
1280 ((string-match muse-image-regexp target)
1281 (cons 'image (cons target nil)))
1282 ((string-match muse-file-regexp target)
1283 (cons 'file (cons target nil)))
1284 ((string-match "#" target)
1285 (if (eq (aref target 0) ?\#)
1286 (cons 'anchor-ref (cons nil (substring target 1)))
1287 (cons 'link-and-anchor
1288 (cons (muse-publish-link-name
1289 (substring target 0 (match-beginning 0)))
1290 (substring target (match-end 0))))))
1292 (cons 'link (cons (muse-publish-link-name target) nil))))))
1294 (defun muse-publish-url (url &optional desc explicit)
1295 "Resolve a URL into its final <a href> form."
1296 (let ((orig-url url)
1297 type anchor)
1298 (dolist (transform muse-publish-url-transforms)
1299 (setq url (save-match-data (when url (funcall transform url explicit)))))
1300 (dolist (transform muse-publish-desc-transforms)
1301 (setq desc (save-match-data
1302 (when desc (funcall transform desc explicit)))))
1303 (when desc
1304 (setq desc (muse-publish-escape-specials-in-string desc 'url-desc)))
1305 (setq orig-url
1306 (muse-publish-escape-specials-in-string orig-url 'url-desc))
1307 (let ((target (muse-publish-classify-url url)))
1308 (setq type (car target)
1309 url (muse-publish-escape-specials-in-string (cadr target) 'url)
1310 anchor (muse-publish-escape-specials-in-string
1311 (cddr target) 'url)))
1312 (cond ((eq type 'anchor-ref)
1313 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1314 ((string= url "")
1315 desc)
1316 ((eq type 'image)
1317 (if desc
1318 (muse-markup-text 'image-with-desc url desc)
1319 (muse-markup-text 'image-link url)))
1320 ((eq type 'link-and-anchor)
1321 (muse-markup-text 'link-and-anchor url anchor
1322 (or desc orig-url)))
1323 ((and desc (string-match muse-image-regexp desc))
1324 (muse-markup-text 'url-with-image url desc))
1325 ((eq type 'link)
1326 (muse-markup-text 'link url (or desc orig-url)))
1328 (or (and desc
1329 (let ((text (muse-markup-text 'url-and-desc url desc)))
1330 (and (not (string= text ""))
1331 text)))
1332 (muse-markup-text 'url url (or desc orig-url)))))))
1334 (defun muse-publish-insert-url (url &optional desc explicit)
1335 "Resolve a URL into its final <a href> form."
1336 (delete-region (match-beginning 0) (match-end 0))
1337 (let ((text (muse-publish-url url desc explicit)))
1338 (when text
1339 (muse-insert-markup text))))
1341 (defun muse-publish-markup-link ()
1342 (let (desc explicit link)
1343 (setq explicit (save-match-data
1344 (if (string-match muse-explicit-link-regexp
1345 (match-string 0))
1346 t nil)))
1347 (setq desc (if explicit
1348 (or (match-string 2) (match-string 1))
1349 (match-string 0)))
1350 (setq link (if explicit
1351 (muse-handle-explicit-link (match-string 1))
1352 (muse-handle-implicit-link (match-string 0))))
1353 (when (and link
1354 (or explicit
1355 (not (or (eq (char-before (match-beginning 0)) ?\")
1356 (eq (char-after (match-end 0)) ?\")))))
1357 ;; if explicit link has no user-provided description, treat it
1358 ;; as if it were an implicit link
1359 (when (and explicit (not (match-string 2)))
1360 (setq explicit nil))
1361 (muse-publish-insert-url link desc explicit))))
1363 (defun muse-publish-markup-url ()
1364 (unless (or (eq (char-before (match-beginning 0)) ?\")
1365 (eq (char-after (match-end 0)) ?\"))
1366 (muse-publish-insert-url (match-string 0))))
1368 ;; Default publishing tags
1370 (defcustom muse-publish-contents-depth 2
1371 "The number of heading levels to include with <contents> tags."
1372 :type 'integer
1373 :group 'muse-publish)
1375 (defun muse-publish-contents-tag (beg end attrs)
1376 (set (make-local-variable 'muse-publish-generate-contents)
1377 (cons (copy-marker (point) t)
1378 (let ((depth (cdr (assoc "depth" attrs))))
1379 (or (and depth (string-to-number depth))
1380 muse-publish-contents-depth)))))
1382 (defun muse-publish-verse-tag (beg end)
1383 (save-excursion
1384 (save-restriction
1385 (narrow-to-region beg end)
1386 (goto-char (point-min))
1387 (while (eq ?\ (char-syntax (char-after)))
1388 (delete-char 1))
1389 (while (< (point) (point-max))
1390 (insert "> ")
1391 (forward-line))
1392 (if (eq ?\ (char-syntax (char-before)))
1393 (delete-char -1)))))
1395 (defun muse-publish-mark-read-only (beg end)
1396 "Add read-only properties to the given region."
1397 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1398 nil)
1400 (defun muse-publish-mark-link (&optional beg end)
1401 "Indicate that the given region is a Muse link, so that other
1402 markup elements respect it. If a region is not specified, use
1403 the 0th match data to determine it.
1405 This is usually applied to extended links."
1406 (unless beg (setq beg (match-beginning 0)))
1407 (unless end (setq end (match-end 0)))
1408 (add-text-properties beg end '(muse-link t))
1409 nil)
1411 (defun muse-publish-quote-tag (beg end)
1412 (save-excursion
1413 (save-restriction
1414 (narrow-to-region beg end)
1415 (muse-insert-markup (muse-markup-text 'begin-quote))
1416 (muse-publish-surround-text (muse-markup-text 'begin-quote-item)
1417 (muse-markup-text 'end-quote-item)
1418 (function (lambda (indent)
1419 (muse-forward-paragraph)
1420 (goto-char (match-end 0))
1421 (< (point) (point-max)))))
1422 (muse-insert-markup (muse-markup-text 'end-quote)))))
1424 (defun muse-publish-code-tag (beg end)
1425 (muse-publish-escape-specials beg end nil 'literal)
1426 (goto-char beg)
1427 (insert (muse-markup-text 'begin-literal))
1428 (goto-char end)
1429 (insert (muse-markup-text 'end-literal))
1430 (muse-publish-mark-read-only beg (point)))
1432 (defun muse-publish-example-tag (beg end)
1433 (muse-publish-escape-specials beg end nil 'example)
1434 (goto-char beg)
1435 (insert (muse-markup-text 'begin-example))
1436 (goto-char end)
1437 (insert (muse-markup-text 'end-example))
1438 (muse-publish-mark-read-only beg (point)))
1440 (defun muse-publish-verbatim-tag (beg end)
1441 (muse-publish-escape-specials beg end nil 'verbatim)
1442 (muse-publish-mark-read-only beg end))
1444 (defalias 'muse-publish-class-tag 'ignore)
1446 (defun muse-publish-lisp-tag (beg end)
1447 (save-excursion
1448 (let ((str (muse-eval-lisp
1449 (prog1
1450 (concat "(progn "
1451 (buffer-substring-no-properties beg end)
1452 ")")
1453 (delete-region beg end)))))
1454 (set-text-properties 0 (length str) nil str)
1455 (insert str))))
1457 (defun muse-publish-command-tag (beg end attrs)
1458 (while (looking-at "\\s-*$")
1459 (forward-line))
1460 (let ((interp (cdr (assoc "interp" attrs))))
1461 (if (null interp)
1462 (shell-command
1463 (prog1
1464 (buffer-substring-no-properties (point) end)
1465 (delete-region beg end)) t)
1466 (shell-command-on-region beg end interp t t))
1467 (muse-publish-mark-read-only beg (point))))
1469 (defun muse-publish-comment-tag (beg end)
1470 (if (null muse-publish-comments-p)
1471 (delete-region beg end)
1472 (goto-char end)
1473 (muse-insert-markup (muse-markup-text 'comment-end))
1474 (muse-publish-mark-read-only beg end)
1475 (goto-char beg)
1476 (muse-insert-markup (muse-markup-text 'comment-begin))))
1478 (defun muse-publish-include-tag (beg end attrs)
1479 "Include the named file at the current location during publishing.
1481 <include file=\"...\">
1483 Files are marked up according to the Muse publishing rules. If
1484 you want no markup to be performed, either add
1485 <example>..</example> inside the source file or use the
1486 following invocation.
1488 <include file=\"...\" markup=\"nil\">
1490 The `markup' attribute controls how this section is marked up. If
1491 non-nil, it should be the name of a function to call after
1492 inserting the file with the buffer narrowed to the section
1493 inserted. Note that no further marking-up will be performed on
1494 this region."
1495 (let ((filename (cdr (assoc "file" attrs)))
1496 (markup (cdr (assoc "markup" attrs)))
1497 (muse-publishing-directives muse-publishing-directives))
1498 (if filename
1499 (setq filename (expand-file-name
1500 filename
1501 (file-name-directory muse-publishing-current-file)))
1502 (error "No file attribute specified in <include> tag"))
1503 (if markup
1504 (let ((markup-function (read markup)))
1505 (if (and markup-function (not (functionp markup-function)))
1506 (error "Invalid markup function `%s'" markup)
1507 (save-restriction
1508 (narrow-to-region beg end)
1509 (insert-file-contents filename)
1510 (when markup-function
1511 (funcall markup-function))
1512 (goto-char (point-max)))))
1513 (insert-file-contents filename)
1514 (muse-publish-markup-region beg (point)))
1515 (muse-publish-mark-read-only beg (point))))
1517 (defun muse-publish-mark-up-tag (beg end attrs)
1518 "Run an Emacs Lisp function on the region delimted by this tag.
1520 <markup function=\"...\">
1522 The optional `function' attribute controls how this section is
1523 marked up. If used, it should be the name of a function to call
1524 with the buffer narrowed to the delimited region. Note that no
1525 further marking-up will be performed on this region.
1527 If `function' is ommitted, use the standard Muse markup function.
1528 This is useful for marking up content in headers and footers."
1529 (let ((function (cdr (assoc "function" attrs)))
1530 (muse-publishing-directives muse-publishing-directives))
1531 (if function
1532 (let ((markup-function (intern function)))
1533 (if (and markup-function (functionp markup-function))
1534 (save-restriction
1535 (narrow-to-region beg end)
1536 (funcall markup-function)
1537 (goto-char (point-max)))
1538 (error "Invalid markup function `%s'" function)))
1539 (muse-publish-markup-region beg end))
1540 (muse-publish-mark-read-only beg (point))))
1542 ;; Miscellaneous helper functions
1544 (defun muse-publish-strip-tags (string)
1545 "Remove all tags from the string."
1546 (while (string-match "<.*?>" string)
1547 (setq string (replace-match "" nil t string)))
1548 string)
1550 (defun muse-publish-markup-type (category default-func)
1551 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1552 (funcall (or rule default-func))))
1554 (defun muse-published-buffer-contents (buffer)
1555 (with-current-buffer buffer
1556 (goto-char (point-min))
1557 (let ((beg (and (search-forward "Emacs Muse begins here")
1558 (muse-line-end-position)))
1559 (end (and (search-forward "Emacs Muse ends here")
1560 (muse-line-beginning-position))))
1561 (buffer-substring-no-properties beg end))))
1563 (defun muse-published-contents (file)
1564 (when (file-readable-p file)
1565 (muse-with-temp-buffer
1566 (insert-file-contents file)
1567 (muse-published-buffer-contents (current-buffer)))))
1569 (defun muse-publish-transform-output
1570 (file temp-file output-path name gen-func &rest cleanup-exts)
1571 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1572 (setq file (muse-page-name file))
1573 (message "Generating %s output for %s..." name file)
1574 (if (not (funcall gen-func temp-file output-path))
1575 (message "Generating %s from %s...failed" name file)
1576 (message "Generating %s output for %s...done" name file)
1577 (muse-delete-file-if-exists temp-file)
1578 (dolist (ext cleanup-exts)
1579 (muse-delete-file-if-exists
1580 (expand-file-name (concat file ext)
1581 (file-name-directory output-path))))
1582 (message "Wrote %s" output-path)))
1584 (defun muse-publish-read-only (string)
1585 (let ((end (1- (length string))))
1586 (add-text-properties 0 end
1587 '(rear-nonsticky (read-only) read-only t)
1588 string)
1589 string))
1591 (provide 'muse-publish)
1593 ;;; muse-publish.el ends here