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