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