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