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.
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, as well as some code for
34 ;; the "style" element of the <literal> tag.
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 (provide 'muse-publish
)
47 (require 'muse-regexps
)
49 (defgroup muse-publish nil
50 "Options controlling the general behavior of Muse publishing."
53 (defcustom muse-before-publish-hook nil
54 "A hook run in the buffer to be published, before it is done."
58 (defcustom muse-after-publish-hook nil
59 "A hook run in the buffer to be published, after it is done."
63 (defcustom muse-publish-url-transforms
65 "A list of functions used to prepare URLs for publication.
66 Each is passed the URL. The transformed URL should be returned."
68 :options
'(muse-resolve-url)
71 (defcustom muse-publish-desc-transforms nil
72 "A list of functions used to prepare URL desciptions for publication.
73 Each is passed the description. The modified description should
78 (defcustom muse-publish-date-format
"%B %e, %Y"
79 "Format string for the date, used by `muse-publish-markup-buffer'.
80 See `format-time-string' for details on the format options."
84 (defcustom muse-publish-comments-p nil
85 "If nil, remove comments before publishing.
86 If non-nil, publish comments using the markup of the current style."
90 (defcustom muse-publish-report-threshhold
100000
91 "If a file is this size or larger, report publishing progress."
95 (defcustom muse-publish-markup-regexps
96 `(;; Remove leading and trailing whitespace from the file
97 (1000 "\\(\\`\n+\\|\n+\\'\\)" 0 "")
99 ;; Remove trailing whitespace from all lines
100 (1100 ,(concat "[" muse-regexp-blank
"]+$") 0 "")
102 ;; Handle any leading #directives
103 (1200 "\\`#\\([a-zA-Z-]+\\)\\s-+\\(.+\\)\n+" 0 directive
)
106 (1250 ,(concat "^;\\(?:[" muse-regexp-blank
"]+\\(.+\\)\\|$\\|'\\)")
110 (1300 muse-tag-regexp
0 tag
)
112 ;; prevent emphasis characters in explicit links from being marked
113 (1400 muse-explicit-link-regexp
0 muse-publish-mark-link
)
115 ;; emphasized or literal text
116 (1600 ,(concat "\\(^\\|[-[" muse-regexp-blank
117 "<('`\"\n]\\)\\(=[^=" muse-regexp-blank
118 "\n]\\|_[^_" muse-regexp-blank
119 "\n]\\|\\*+[^*" muse-regexp-blank
123 ;; headings, outline-mode style
124 (1700 "^\\(\\*+\\)\\s-+" 0 heading
)
127 (1800 "\\.\\.\\.\\." 0 enddots
)
128 (1850 "\\.\\.\\." 0 dots
)
130 ;; horizontal rule, or section separator
131 (1900 "^----+" 0 rule
)
133 ;; non-breaking space
134 (1950 "~~" 0 no-break-space
)
136 ;; beginning of footnotes section
137 (2000 "^Footnotes:?\\s-*" 0 fn-sep
)
138 ;; footnote definition/reference (def if at beginning of line)
139 (2100 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote
)
141 ;; unnumbered List items begin with a -. numbered list items
142 ;; begin with number and a period. definition lists have a
143 ;; leading term separated from the body with ::. centered
144 ;; paragraphs begin with at least six columns of whitespace; any
145 ;; other whitespace at the beginning indicates a blockquote. The
146 ;; reason all of these rules are handled here, is so that
147 ;; blockquote detection doesn't interfere with indented list
149 (2200 ,(format muse-list-item-regexp
(concat "[" muse-regexp-blank
"]*"))
152 ;; simple table markup is supported, nothing fancy. use | to
153 ;; separate cells, || to separate header cells, and ||| for footer
155 (2300 ,(concat "\\(\\([" muse-regexp-blank
"]*\n\\)?"
156 "\\(" muse-table-line-regexp
"\\(?:\n\\|\\'\\)\\)\\)+")
159 ;; blockquote and centered text
160 (2400 ,(concat "^\\([" muse-regexp-blank
"]+\\).+") 0 quote
)
163 (2500 ,(concat "\\(^\\|[" muse-regexp-blank
"]*\\)--\\($\\|["
164 muse-regexp-blank
"]*\\)")
167 ;; "verse" text is indicated the same way as a quoted e-mail
168 ;; response: "> text", where text may contain initial whitespace
170 (2600 ,(concat "^[" muse-regexp-blank
"]*> ") 0 verse
)
172 ;; define anchor points
173 (2700 "^\\(\\W*\\)#\\(\\S-+\\)\\s-*" 0 anchor
)
175 ;; replace links in the buffer (links to other pages)
176 (2900 muse-explicit-link-regexp
0 link
)
179 (3000 muse-url-regexp
0 url
)
181 ;; bare email addresses
183 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email
)
185 "List of markup rules for publishing a page with Muse.
186 The rules given in this variable are invoked first, followed by
187 whatever rules are specified by the current style.
189 Each member of the list is either a function, or a list of the form:
191 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
193 REGEXP is a regular expression, or symbol whose value is a regular
194 expression, which is searched for using `re-search-forward'.
195 TEXT-BEGIN-GROUP is the matching group within that regexp which
196 denotes the beginning of the actual text to be marked up.
197 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
198 If it is not a string, but a function, it will be called to determine
199 what the replacement text should be (it must return a string). If it
200 is a symbol, the value of that symbol should be a string.
202 The replacements are done in order, one rule at a time. Writing
203 the regular expressions can be a tricky business. Note that case
204 is never ignored. `case-fold-search' is always bound to nil
205 while processing the markup rules."
206 :type
'(repeat (choice
207 (list :tag
"Markup rule"
209 (choice regexp symbol
)
211 (choice string function symbol
))
213 :group
'muse-publish
)
215 (defcustom muse-publish-markup-functions
216 '((directive . muse-publish-markup-directive
)
217 (comment . muse-publish-markup-comment
)
218 (anchor . muse-publish-markup-anchor
)
219 (tag . muse-publish-markup-tag
)
220 (word . muse-publish-markup-word
)
221 (emdash . muse-publish-markup-emdash
)
222 (enddots . muse-publish-markup-enddots
)
223 (dots . muse-publish-markup-dots
)
224 (rule . muse-publish-markup-rule
)
225 (no-break-space . muse-publish-markup-no-break-space
)
226 (heading . muse-publish-markup-heading
)
227 (footnote . muse-publish-markup-footnote
)
228 (fn-sep . muse-publish-markup-fn-sep
)
229 (list . muse-publish-markup-list
)
230 (quote . muse-publish-markup-quote
)
231 (verse . muse-publish-markup-verse
)
232 (table . muse-publish-markup-table
)
233 (email . muse-publish-markup-email
)
234 (link . muse-publish-markup-link
)
235 (url . muse-publish-markup-url
))
236 "An alist of style types to custom functions for that kind of text.
238 Each member of the list is of the form:
242 SYMBOL describes the type of text to associate with this rule.
243 `muse-publish-markup-regexps' maps regexps to these symbols.
245 FUNCTION is the function to use to mark up this kind of rule if
246 no suitable function is found through the :functions tag of the
248 :type
'(alist :key-type symbol
:value-type function
)
249 :group
'muse-publish
)
251 (defcustom muse-publish-markup-tags
252 '(("contents" nil t nil muse-publish-contents-tag
)
253 ("verse" t nil nil muse-publish-verse-tag
)
254 ("example" t nil nil muse-publish-example-tag
)
255 ("src" t t nil muse-publish-src-tag
)
256 ("code" t nil nil muse-publish-code-tag
)
257 ("quote" t nil t muse-publish-quote-tag
)
258 ("literal" t t nil muse-publish-literal-tag
)
259 ("verbatim" t nil nil muse-publish-verbatim-tag
)
260 ("lisp" t t nil muse-publish-lisp-tag
)
261 ("class" t t nil muse-publish-class-tag
)
262 ("command" t t nil muse-publish-command-tag
)
263 ("perl" t t nil muse-publish-perl-tag
)
264 ("python" t t nil muse-publish-python-tag
)
265 ("ruby" t t nil muse-publish-ruby-tag
)
266 ("comment" t nil nil muse-publish-comment-tag
)
267 ("include" nil t nil muse-publish-include-tag
)
268 ("markup" t t nil muse-publish-mark-up-tag
))
269 "A list of tag specifications, for specially marking up text.
270 XML-style tags are the best way to add custom markup to Muse.
271 This is easily accomplished by customizing this list of markup tags.
273 For each entry, the name of the tag is given, whether it expects
274 a closing tag and/or an optional set of attributes, whether it is
275 nestable, and a function that performs whatever action is desired
276 within the delimited region.
278 The tags themselves are deleted during publishing, before the
279 function is called. The function is called with three arguments,
280 the beginning and end of the region surrounded by the tags. If
281 properties are allowed, they are passed as a third argument in
282 the form of an alist. The `end' argument to the function is
285 Point is always at the beginning of the region within the tags, when
286 the function is called. Wherever point is when the function finishes
287 is where tag markup will resume.
289 These tag rules are processed once at the beginning of markup, and
290 once at the end, to catch any tags which may have been inserted
292 :type
'(repeat (list (string :tag
"Markup tag")
293 (boolean :tag
"Expect closing tag" :value t
)
294 (boolean :tag
"Parse attributes" :value nil
)
295 (boolean :tag
"Nestable" :value nil
)
297 :group
'muse-publish
)
299 (defcustom muse-publish-markup-specials nil
300 "A table of characters which must be represented specially."
301 :type
'(alist :key-type character
:value-type string
)
302 :group
'muse-publish
)
304 (defvar muse-publishing-p nil
305 "Set to t while a page is being published.")
306 (defvar muse-batch-publishing-p nil
307 "Set to t while a page is being batch published.")
308 (defvar muse-publishing-styles nil
309 "The publishing styles that Muse recognizes.
310 This is automatically generated when loading publishing styles.")
311 (defvar muse-publishing-current-file nil
312 "The file that is currently being published.")
313 (defvar muse-publishing-current-output-path nil
314 "The path where the current file will be published to.")
315 (defvar muse-publishing-current-style nil
316 "The style of the file that is currently being published.")
317 (defvar muse-publishing-directives nil
318 "An alist of publishing directives from the top of a file.")
319 (defvar muse-publish-generate-contents nil
320 "Non-nil if a table of contents should be generated.
321 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
322 tell where the <contents> was seen, and to what depth the
323 contents were requested.")
324 (defvar muse-publishing-last-position nil
325 "Last position of the point when publishing.
326 This is used to make sure that publishing doesn't get stalled.")
328 ;; Functions for handling style information
330 (defsubst muse-style
(&optional style
)
331 "Resolve the given STYLE into a Muse style, if it is a string."
333 muse-publishing-current-style
335 (assoc style muse-publishing-styles
)
336 (muse-assert (consp style
))
339 (defun muse-define-style (name &rest elements
)
340 (let ((entry (assoc name muse-publishing-styles
)))
342 (setcdr entry elements
)
343 (setq muse-publishing-styles
344 (cons (append (list name
) elements
)
345 muse-publishing-styles
)))))
347 (defun muse-derive-style (new-name base-name
&rest elements
)
348 (apply 'muse-define-style new-name
349 (append elements
(list :base base-name
))))
351 (defsubst muse-get-keyword
(keyword list
&optional direct
)
352 (let ((value (cadr (memq keyword list
))))
353 (if (and (not direct
) (symbolp value
))
357 (defun muse-style-elements-list (elem &optional style
)
358 "Return a list all references to ELEM in STYLE, including base styles.
359 If STYLE is not specified, use current style."
362 (setq style
(muse-style style
))
363 (setq elements
(append elements
364 (muse-get-keyword elem style
)))
365 (setq style
(muse-get-keyword :base style
)))
368 (defun muse-style-element (elem &optional style direct
)
369 "Search for ELEM in STYLE, including base styles.
370 If STYLE is not specified, use current style."
371 (setq style
(muse-style style
))
372 (let ((value (muse-get-keyword elem style direct
)))
375 (let ((base (muse-get-keyword :base style
)))
377 (muse-style-element elem base direct
))))))
379 (defun muse-style-derived-p-1 (base style
)
380 "Internal function used by `muse-style-derived-p'."
381 (if (and (stringp style
)
382 (string= style base
))
384 (setq style
(muse-style style
))
385 (let ((value (muse-get-keyword :base style
)))
387 (muse-style-derived-p base value
)))))
389 (defun muse-style-derived-p (base &optional style
)
390 "Return non-nil if STYLE is equal to or derived from BASE,
393 BASE should be a string."
395 (setq style
(muse-style)))
396 (when (and (consp style
)
397 (stringp (car style
)))
398 (setq style
(car style
)))
399 (muse-style-derived-p-1 base style
))
401 (defun muse-find-markup-element (keyword ident style
)
402 (let ((def (assq ident
(muse-style-element keyword style
))))
405 (let ((base (muse-style-element :base style
)))
407 (muse-find-markup-element keyword ident base
))))))
409 (defun muse-markup-text (ident &rest args
)
410 "Insert ARGS into the text markup associated with IDENT.
411 If the markup text has sections like %N%, this will be replaced
412 with the N-1th argument in ARGS. After that, `format' is applied
413 to the text with ARGS as parameters."
414 (let ((text (muse-find-markup-element :strings ident
(muse-style))))
417 (let (start repl-text
)
418 (while (setq start
(string-match "%\\([1-9][0-9]*\\)%" text start
))
419 ;; escape '%' in the argument text, since we will be
420 ;; using format on it
421 (setq repl-text
(muse-replace-regexp-in-string
423 (nth (1- (string-to-number
424 (match-string 1 text
))) args
)
426 start
(+ start
(length repl-text
))
427 text
(replace-match repl-text t t text
))))
428 (apply 'format text args
))
431 (defun muse-insert-markup (&rest args
)
434 (muse-publish-mark-read-only beg
(point))))
436 (defun muse-find-markup-tag (keyword tagname style
)
437 (let ((def (assoc tagname
(muse-style-element keyword style
))))
439 (let ((base (muse-style-element :base style
)))
441 (muse-find-markup-tag keyword tagname base
))))))
443 (defsubst muse-markup-tag-info
(tagname &rest args
)
444 (let ((tag-info (muse-find-markup-tag :tags tagname
(muse-style))))
446 (assoc tagname muse-publish-markup-tags
))))
448 (defsubst muse-markup-function
(category)
449 (let ((func (muse-find-markup-element :functions category
(muse-style))))
451 (cdr (assq category muse-publish-markup-functions
)))))
453 ;; Publishing routines
455 (defun muse-publish-markup (name rules
)
456 (let* ((case-fold-search nil
)
457 (inhibit-read-only t
)
458 (limit (* (length rules
) (point-max)))
459 (verbose (and muse-publish-report-threshhold
460 (> (point-max) muse-publish-report-threshhold
)))
463 (goto-char (point-min))
464 (let ((regexp (nth 1 (car rules
)))
465 (group (nth 2 (car rules
)))
466 (repl (nth 3 (car rules
)))
468 (setq muse-publishing-last-position nil
)
470 (setq regexp
(symbol-value regexp
)))
471 (if (and verbose
(not muse-batch-publishing-p
))
472 (message "Publishing %s...%d%%" name
473 (* (/ (float (+ (point) base
)) limit
) 100)))
474 (while (and regexp
(setq pos
(re-search-forward regexp nil t
)))
475 (if (and verbose
(not muse-batch-publishing-p
))
476 (message "Publishing %s...%d%%" name
477 (* (/ (float (+ (point) base
)) limit
) 100)))
478 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
479 (match-beginning group
)
480 (get-text-property (match-beginning group
) 'read-only
))
484 (setq func
(muse-markup-function repl
)))
492 (replace-match text t
))))
493 (if (and muse-publishing-last-position
494 (= pos muse-publishing-last-position
))
498 (setq muse-publishing-last-position pos
)))
499 (setq rules
(cdr rules
)
500 base
(+ base
(point-max))))
501 (if (and verbose
(not muse-batch-publishing-p
))
502 (message "Publishing %s...done" name
))))
504 (defcustom muse-publish-markup-header-footer-tags
505 '(("lisp" t t nil muse-publish-lisp-tag
)
506 ("markup" t t nil muse-publish-mark-up-tag
))
507 "Tags used when publishing headers and footers.
508 See `muse-publish-markup-tags' for details."
509 :type
'(repeat (list (string :tag
"Markup tag")
510 (boolean :tag
"Expect closing tag" :value t
)
511 (boolean :tag
"Parse attributes" :value nil
)
512 (boolean :tag
"Nestable" :value nil
)
514 :group
'muse-publish
)
516 (defun muse-insert-file-or-string (file-or-string &optional title
)
517 (let ((beg (point)) end
)
518 (if (and (not (string-equal file-or-string
""))
519 (not (string-match "\n" file-or-string
))
520 (file-readable-p file-or-string
))
521 (setq end
(+ beg
(cadr (insert-file-contents file-or-string
))))
522 (insert file-or-string
)
525 (narrow-to-region beg end
)
526 (remove-text-properties (point-min) (point-max)
527 '(read-only nil rear-nonsticky nil
))
528 (goto-char (point-min))
529 (let ((muse-inhibit-style-tags t
))
530 (muse-publish-markup (or title
"")
531 '((100 muse-tag-regexp
0
532 muse-publish-markup-tag
)))))))
534 (defun muse-style-run-hooks (keyword style
&rest args
)
538 (setq style
(muse-style style
)))
539 (let ((func (muse-style-element keyword style t
)))
541 (not (member func cache
)))
542 (setq cache
(cons func cache
))
543 (when (apply func args
)
544 (throw 'handled t
))))
545 (setq style
(muse-style-element :base style
))))))
547 (defvar muse-publish-inhibit-style-hooks nil
548 "If non-nil, do not call the :before or :before-end hooks when publishing.")
550 (defun muse-publish-markup-region (beg end
&optional title style
)
551 "Apply the given STYLE's markup rules to the given region.
552 TITLE is used when indicating the publishing progress; it may be nil.
554 The point is guaranteed to be at END if the routine terminates
556 (unless title
(setq title
""))
558 (or (setq style muse-publishing-current-style
)
559 (error "Cannot find any publishing styles to use")))
561 (narrow-to-region beg end
)
562 (unless muse-publish-inhibit-style-hooks
563 (muse-style-run-hooks :before style
))
566 (sort (copy-alist (append muse-publish-markup-regexps
567 (muse-style-elements-list :regexps style
)))
570 (< (car l
) (car r
))))))
571 (unless muse-publish-inhibit-style-hooks
572 (muse-style-run-hooks :before-end style
))
573 (muse-publish-escape-specials (point-min) (point-max) nil
'document
)
574 (goto-char (point-max))))
576 (defun muse-publish-markup-buffer (title style
)
577 "Apply the given STYLE's markup rules to the current buffer."
578 (setq style
(muse-style style
))
579 (let ((style-header (muse-style-element :header style
))
580 (style-footer (muse-style-element :footer style
))
581 (muse-publishing-current-style style
)
582 (muse-publishing-directives
583 (list (cons "title" title
)
584 (cons "author" (user-full-name))
585 (cons "date" (format-time-string
586 muse-publish-date-format
587 (if muse-publishing-current-file
588 (nth 5 (file-attributes
589 muse-publishing-current-file
))
591 (muse-publishing-p t
)
592 (inhibit-read-only t
))
593 (run-hooks 'muse-update-values-hook
)
594 (run-hooks 'muse-before-publish-hook
)
595 (muse-publish-markup-region (point-min) (point-max) title style
)
596 (goto-char (point-min))
598 (muse-insert-file-or-string style-header title
))
599 (goto-char (point-max))
601 (muse-insert-file-or-string style-footer title
))
602 (muse-style-run-hooks :after style
)
603 (run-hooks 'muse-after-publish-hook
)))
605 (defun muse-publish-markup-string (string &optional style
)
606 "Markup STRING using the given STYLE's markup rules."
607 (setq style
(muse-style style
))
608 (muse-with-temp-buffer
610 (let ((muse-publishing-current-style style
)
611 (muse-publishing-p t
))
612 (muse-publish-markup "*string*" (muse-style-element :rules style
)))
615 ;; Commands for publishing files
617 (defun muse-publish-get-style (&optional styles
)
618 (unless styles
(setq styles muse-publishing-styles
))
619 (if (= 1 (length styles
))
621 (when (catch 'different
622 (let ((first (car (car styles
))))
623 (dolist (style (cdr styles
))
624 (unless (equal first
(car style
))
625 (throw 'different t
)))))
626 (setq styles
(muse-collect-alist
628 (completing-read "Publish with style: " styles nil t
))))
629 (if (or (= 1 (length styles
))
630 (not (muse-get-keyword :path
(car styles
))))
632 (setq styles
(mapcar (lambda (style)
633 (cons (muse-get-keyword :path style
)
636 (cdr (assoc (completing-read "Publish to directory: " styles nil t
)
639 (defsubst muse-publish-get-output-dir
(style)
640 (let ((default-directory (or (muse-style-element :path style
)
642 (muse-read-directory-name "Publish to directory: " nil default-directory
)))
644 (defsubst muse-publish-get-info
()
645 (let ((style (muse-publish-get-style)))
646 (list style
(muse-publish-get-output-dir style
)
647 current-prefix-arg
)))
649 (defsubst muse-publish-output-name
(&optional file style
)
650 (setq style
(muse-style style
))
651 (concat (muse-style-element :prefix style
)
652 (muse-page-name file
)
653 (muse-style-element :suffix style
)))
655 (defsubst muse-publish-output-file
(file &optional output-dir style
)
656 (setq style
(muse-style style
))
658 (expand-file-name (muse-publish-output-name file style
) output-dir
)
659 (concat (file-name-directory file
)
660 (muse-publish-output-name file style
))))
662 (defsubst muse-publish-link-name
(&optional file style
)
663 (setq style
(muse-style style
))
664 (concat (muse-style-element :prefix style
)
665 (muse-page-name file
)
666 (or (muse-style-element :link-suffix style
)
667 (muse-style-element :suffix style
))))
669 (defsubst muse-publish-link-file
(file &optional style
)
670 (setq style
(muse-style style
))
671 (if (file-exists-p file
)
673 (concat (file-name-directory file
)
674 (muse-publish-link-name file style
))))
676 (defsubst muse-publish-link-page
(page)
677 (if (fboundp 'muse-project-link-page
)
678 (muse-project-link-page page
)
679 (muse-publish-link-file page
)))
682 (defun muse-publish-region (beg end
&optional title style
)
683 "Apply the given STYLE's markup rules to the given region.
684 The result is placed in a new buffer that includes TITLE in its name."
686 (when (interactive-p)
687 (unless title
(setq title
(read-string "Title: ")))
688 (unless style
(setq style
(muse-publish-get-style))))
689 (let ((muse-publishing-current-style style
)
690 (muse-publishing-p t
)
691 (text (buffer-substring beg end
))
692 (buf (generate-new-buffer (concat "*Muse: " title
"*"))))
693 (with-current-buffer buf
695 (muse-publish-markup-buffer title style
)
696 (goto-char (point-min))
697 (let ((inhibit-read-only t
))
698 (remove-text-properties (point-min) (point-max)
699 '(rear-nonsticky nil read-only nil
))))
700 (pop-to-buffer buf
)))
703 (defun muse-publish-file (file style
&optional output-dir force
)
704 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
705 If the argument FORCE is nil, each file is only published if it is
706 newer than the published version. If the argument FORCE is non-nil,
707 the file is published no matter what."
708 (interactive (cons (read-file-name "Publish file: ")
709 (muse-publish-get-info)))
710 (let ((style-name style
))
711 (setq style
(muse-style style
))
713 (error "There is no style '%s' defined" style-name
)))
714 (let* ((output-path (muse-publish-output-file file output-dir style
))
715 (output-suffix (muse-style-element :osuffix style
))
716 (muse-publishing-current-file file
)
717 (muse-publishing-current-output-path output-path
)
718 (target (if output-suffix
719 (concat (muse-path-sans-extension output-path
)
722 (threshhold (nth 7 (file-attributes file
))))
724 (message "Please save %s before publishing" file
)
725 (when (or force
(file-newer-than-file-p file target
))
726 (if (and muse-publish-report-threshhold
728 muse-publish-report-threshhold
))
729 (message "Publishing %s ..." file
))
730 (muse-with-temp-buffer
731 (insert-file-contents file
)
732 (muse-publish-markup-buffer (muse-page-name file
) style
)
733 (let ((backup-inhibited t
))
734 (write-file output-path
))
735 (muse-style-run-hooks :final style file output-path target
))
739 (defun muse-publish-this-file (style output-dir
&optional force
)
740 "Publish the currently-visited file.
741 Prompt for both the STYLE and OUTPUT-DIR if they are not
743 (interactive (muse-publish-get-info))
745 (let ((muse-current-output-style (list :base
(car style
)
747 (unless (muse-publish-file buffer-file-name style output-dir force
)
748 (message (concat "The published version is up-to-date; use"
749 " C-u C-c C-T to force an update."))))
750 (message "This buffer is not associated with any file")))
752 (defun muse-batch-publish-files ()
753 "Publish Muse files in batch mode."
754 (let ((muse-batch-publishing-p t
)
756 (setq style
(car command-line-args-left
)
757 command-line-args-left
(cdr command-line-args-left
)
758 output-dir
(car command-line-args-left
)
760 (if (string-match "\\`--output-dir=" output-dir
)
762 (substring output-dir
(match-end 0))
763 (setq command-line-args-left
(cdr command-line-args-left
)))))
764 (setq auto-mode-alist
765 (delete (cons (concat "\\." muse-file-extension
"\\'")
766 'muse-mode-choose-mode
)
768 (dolist (file command-line-args-left
)
769 (muse-publish-file file style output-dir t
))))
771 ;; Default publishing rules
773 (defun muse-publish-section-close (depth)
774 "Seach forward for the closing tag of given DEPTH."
777 (while (and (setq not-end
(re-search-forward
778 (concat "^\\*\\{1," (number-to-string depth
)
781 (get-text-property (match-beginning 0) 'read-only
)))
784 (goto-char (point-max)))
785 (cond ((not (eq (char-before) ?
\n))
787 ((not (eq (char-before (1- (point))) ?
\n))
789 (muse-insert-markup (muse-markup-text 'section-close depth
))
792 (defun muse-publish-markup-directive (&optional name value
)
793 (unless name
(setq name
(match-string 1)))
794 (unless value
(setq value
(match-string 2)))
795 (let ((elem (assoc name muse-publishing-directives
)))
798 (setq muse-publishing-directives
799 (cons (cons name value
)
800 muse-publishing-directives
))))
801 ;; Make sure we don't ever try to move the point forward (past the
802 ;; beginning of buffer) while we're still searching for directives.
803 (setq muse-publishing-last-position nil
)
804 (delete-region (match-beginning 0) (match-end 0)))
806 (defsubst muse-publishing-directive
(name)
807 (cdr (assoc name muse-publishing-directives
)))
809 (defun muse-publish-markup-anchor ()
810 (unless (get-text-property (match-end 1) 'muse-link
)
811 (let ((text (muse-markup-text 'anchor
(match-string 2))))
812 (unless (string= text
"")
814 (skip-chars-forward (concat muse-regexp-blank
"\n"))
815 (muse-insert-markup text
)))
818 (defun muse-publish-markup-comment ()
819 (if (null muse-publish-comments-p
)
821 (goto-char (match-end 0))
822 (muse-insert-markup (muse-markup-text 'comment-end
))
823 (if (match-beginning 1)
825 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
826 (delete-region (match-beginning 0) (match-beginning 1)))
827 (delete-region (match-beginning 0) (match-end 0)))
828 (goto-char (match-beginning 0))
829 (muse-insert-markup (muse-markup-text 'comment-begin
))))
831 (defvar muse-inhibit-style-tags nil
832 "If non-nil, do not search for style-specific tags.
833 This is used when publishing headers and footers.")
835 (defun muse-publish-markup-tag ()
836 (let ((tag-info (if muse-inhibit-style-tags
837 (assoc (match-string 1) muse-publish-markup-tags
)
838 (muse-markup-tag-info (match-string 1)))))
840 (not (get-text-property (match-beginning 0) 'read-only
)))
841 (let ((closed-tag (match-string 3))
842 (start (match-beginning 0))
845 (when (nth 2 tag-info
)
846 (let ((attrstr (match-string 2)))
848 (string-match (concat "\\([^"
853 (let ((attr (cons (downcase
854 (muse-match-string-no-properties 1 attrstr
))
855 (muse-match-string-no-properties 3 attrstr
))))
856 (setq attrstr
(replace-match "" t t attrstr
))
858 (nconc attrs
(list attr
))
859 (setq attrs
(list attr
)))))))
860 (if (and (cadr tag-info
) (not closed-tag
))
861 (if (muse-goto-tag-end (car tag-info
) (nth 3 tag-info
))
862 (delete-region (match-beginning 0) (point))
863 (setq tag-info nil
)))
865 (setq end
(point-marker))
866 (delete-region start beg
)
868 (let ((args (list start end
)))
870 (nconc args
(list attrs
)))
871 (let ((muse-inhibit-style-tags nil
))
872 ;; remove the inhibition
873 (apply (nth 4 tag-info
) args
)))))))
876 (defun muse-publish-escape-specials (beg end
&optional ignore-read-only context
)
877 "Escape specials from BEG to END using style-specific :specials.
878 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
879 CONTEXT is used to figure out what kind of specials to escape.
881 The following contexts exist in Muse.
882 'underline _underlined text_
883 'literal =monospaced text= or <code> region (monospaced, escaped)
884 'emphasis *emphasized text*
885 'email email@example.com
886 'url http://example.com
887 'url-desc [[...][description of an explicit link]]
889 'example <example> region (monospaced, block context, escaped)
890 'verbatim <verbatim> region (escaped)
891 'document normal text"
892 (let ((specials (muse-style-element :specials nil t
)))
893 (cond ((functionp specials
)
894 (setq specials
(funcall specials context
)))
896 (setq specials
(symbol-value specials
))))
897 (if (functionp specials
)
898 (funcall specials beg end ignore-read-only
)
901 (narrow-to-region beg end
)
902 (goto-char (point-min))
903 (while (< (point) (point-max))
904 (if (and (not ignore-read-only
)
905 (get-text-property (point) 'read-only
))
906 (goto-char (or (next-single-property-change (point) 'read-only
)
908 (let ((repl (or (assoc (char-after) specials
)
910 muse-publish-markup-specials
))))
914 (insert-before-markers (cdr repl
)))))))))))
916 (defun muse-publish-markup-word ()
917 (let* ((beg (match-beginning 2))
918 (end (1- (match-end 2)))
919 (leader (buffer-substring-no-properties beg end
))
920 open-tag close-tag mark-read-only loc context
)
922 ((string= leader
"_")
923 (setq context
'underline
924 open-tag
(muse-markup-text 'begin-underline
)
925 close-tag
(muse-markup-text 'end-underline
)))
926 ((string= leader
"=")
927 (setq context
'literal
928 open-tag
(muse-markup-text 'begin-literal
)
929 close-tag
(muse-markup-text 'end-literal
))
930 (setq mark-read-only t
))
932 (let ((l (length leader
)))
933 (setq context
'emphasis
)
935 ((= l
1) (setq open-tag
(muse-markup-text 'begin-emph
)
936 close-tag
(muse-markup-text 'end-emph
)))
937 ((= l
2) (setq open-tag
(muse-markup-text 'begin-more-emph
)
938 close-tag
(muse-markup-text 'end-more-emph
)))
939 ((= l
3) (setq open-tag
(muse-markup-text 'begin-most-emph
)
940 close-tag
(muse-markup-text 'end-most-emph
)))
941 (t (setq context nil
))))))
943 (not (get-text-property beg
'muse-link
))
944 (setq loc
(search-forward leader nil t
))
945 (or (eobp) (not (eq (char-syntax (char-after loc
)) ?w
)))
946 (not (eq (char-syntax (char-before (point))) ?\
))
947 (not (get-text-property (point) 'muse-link
)))
950 (delete-region beg end
)
951 (setq end
(point-marker))
952 (muse-insert-markup close-tag
)
954 (muse-insert-markup open-tag
)
957 (muse-publish-escape-specials beg end t context
)
958 (muse-publish-mark-read-only beg end
)))
962 (defun muse-publish-markup-emdash ()
963 (unless (get-text-property (match-beginning 0) 'muse-link
)
964 (let ((prespace (match-string 1))
965 (postspace (match-string 2)))
966 (delete-region (match-beginning 0) (match-end 0))
967 (muse-insert-markup (muse-markup-text 'emdash prespace postspace
))
968 (when (eq (char-after) ?\
<)
971 (defun muse-publish-markup-enddots ()
972 (unless (get-text-property (match-beginning 0) 'muse-link
)
973 (delete-region (match-beginning 0) (match-end 0))
974 (muse-insert-markup (muse-markup-text 'enddots
))))
976 (defun muse-publish-markup-dots ()
977 (unless (get-text-property (match-beginning 0) 'muse-link
)
978 (delete-region (match-beginning 0) (match-end 0))
979 (muse-insert-markup (muse-markup-text 'dots
))))
981 (defun muse-publish-markup-rule ()
982 (unless (get-text-property (match-beginning 0) 'muse-link
)
983 (delete-region (match-beginning 0) (match-end 0))
984 (muse-insert-markup (muse-markup-text 'rule
))))
986 (defun muse-publish-markup-no-break-space ()
987 (unless (get-text-property (match-beginning 0) 'muse-link
)
988 (delete-region (match-beginning 0) (match-end 0))
989 (muse-insert-markup (muse-markup-text 'no-break-space
))))
991 (defun muse-publish-markup-heading ()
992 (let* ((len (length (match-string 1)))
993 (start (muse-markup-text
994 (cond ((= len
1) 'section
)
995 ((= len
2) 'subsection
)
996 ((= len
3) 'subsubsection
)
999 (end (muse-markup-text
1000 (cond ((= len
1) 'section-end
)
1001 ((= len
2) 'subsection-end
)
1002 ((= len
3) 'subsubsection-end
)
1003 (t 'section-other-end
))
1005 (delete-region (match-beginning 0) (match-end 0))
1006 (muse-insert-markup start
)
1009 (muse-insert-markup end
))
1011 (unless (eq (char-after) ?
\n)
1013 (muse-publish-section-close len
)))
1015 (defvar muse-publish-footnotes nil
)
1017 (defun muse-publish-markup-footnote ()
1018 "Scan ahead and snarf up the footnote body"
1020 ((get-text-property (match-beginning 0) 'muse-link
)
1022 ((= (muse-line-beginning-position) (match-beginning 0))
1025 (let ((footnote (save-match-data
1026 (string-to-number (match-string 1))))
1027 (oldtext (match-string 0))
1029 (delete-region (match-beginning 0) (match-end 0))
1031 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote
) nil t
)
1032 (let* ((start (match-beginning 0))
1033 (beg (goto-char (match-end 0)))
1034 (end (save-excursion
1035 (if (search-forward "\n\n" nil t
)
1036 (copy-marker (match-beginning 0))
1037 (goto-char (point-max))
1038 (skip-chars-backward "\n")
1040 (while (re-search-forward
1041 (concat "^[" muse-regexp-blank
"]+\\([^\n]\\)")
1043 (replace-match "\\1" t
))
1044 (let ((footnotemark-cmd (muse-markup-text 'footnotemark
))
1045 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end
)))
1046 (if (string= "" footnotemark-cmd
)
1048 (concat (muse-markup-text 'footnote
)
1049 (buffer-substring-no-properties beg end
)
1050 (muse-markup-text 'footnote-end
)))
1051 (setq footnotemark
(format footnotemark-cmd footnote
1052 footnotemark-end-cmd
))
1053 (unless muse-publish-footnotes
1054 (set (make-local-variable 'muse-publish-footnotes
)
1055 (make-vector 256 nil
)))
1056 (unless (aref muse-publish-footnotes footnote
)
1060 (concat (format (muse-markup-text 'footnotetext
)
1062 (buffer-substring-no-properties beg end
)
1063 (muse-markup-text 'footnotetext-end
))))
1064 (aset muse-publish-footnotes footnote footnotemark
))))
1066 (skip-chars-forward "\n")
1067 (delete-region start
(point)))))
1069 (muse-insert-markup footnotemark
)
1070 (insert oldtext
))))))
1072 (defun muse-publish-markup-fn-sep ()
1073 (delete-region (match-beginning 0) (match-end 0))
1074 (muse-insert-markup (muse-markup-text 'fn-sep
)))
1076 (defun muse-insert-markup-end-list (&rest args
)
1077 (let ((beg (point)))
1078 (apply 'insert args
)
1079 (add-text-properties beg
(point) '(end-list t
))
1080 (muse-publish-mark-read-only beg
(point))))
1082 (defun muse-publish-determine-dl-indent (continue indent-sym determine-sym
)
1083 ;; If the caller doesn't know how much indentation to use, figure it
1084 ;; out ourselves. It is assumed that `muse-forward-list-item' has
1085 ;; been called just before this to set the match data.
1087 (symbol-value determine-sym
))
1089 ;; snarf all leading whitespace
1090 (let ((indent (and (match-beginning 2)
1091 (buffer-substring (match-beginning 1)
1092 (match-beginning 2)))))
1094 (not (string= indent
"")))
1095 (set indent-sym indent
)
1096 (set determine-sym nil
))))))
1098 (defun muse-publish-surround-dl (indent post-indent
)
1099 (let* ((beg-item (muse-markup-text 'begin-dl-item
))
1100 (end-item (muse-markup-text 'end-dl-item
))
1101 (beg-ddt (muse-markup-text 'begin-ddt
)) ;; term
1102 (end-ddt (muse-markup-text 'end-ddt
))
1103 (beg-dde (muse-markup-text 'begin-dde
)) ;; definition
1104 (end-dde (muse-markup-text 'end-dde
))
1106 def-on-same-line beg
)
1108 ;; envelope this as one term+definitions unit -- HTML does not
1109 ;; need this, but DocBook and Muse's custom XML format do
1110 (muse-insert-markup beg-item
)
1111 (when (looking-at muse-dl-term-regexp
)
1112 ;; find the term and wrap it with published markup
1114 (goto-char (match-end 1))
1115 (delete-region (point) (match-end 0))
1116 (muse-insert-markup-end-list end-ddt
)
1117 ;; if definition is immediately after term, move to next line
1118 (unless (eq (char-after) ?
\n)
1122 (delete-region (point) (match-beginning 1))
1123 (muse-insert-markup beg-ddt
)))
1125 ;; move past current item
1126 continue
(muse-forward-list-item 'dl-term indent
))
1128 (narrow-to-region beg
(point))
1129 (goto-char (point-min))
1130 ;; publish each definition that we find, defaulting to an
1131 ;; empty definition if none are found
1132 (muse-publish-surround-text beg-dde end-dde
1134 (muse-forward-list-item 'dl-entry indent
))
1136 #'muse-publish-determine-dl-indent
)
1137 (goto-char (point-max))
1138 (skip-chars-backward (concat muse-regexp-blank
"\n"))
1139 (muse-insert-markup-end-list end-item
)
1141 (goto-char (point-max)))))))
1143 (defun muse-publish-strip-list-indentation (list-item empty-line indent post-indent
)
1144 (let ((list-nested nil
)
1146 (while (< (point) (point-max))
1147 (when (and (looking-at list-item
)
1148 (not (or (get-text-property
1149 (muse-list-item-critical-point) 'read-only
)
1151 (muse-list-item-critical-point) 'muse-link
))))
1152 ;; if we encounter a list item, allow no post-indent space
1153 (setq list-nested t
))
1154 (when (and (not (looking-at empty-line
))
1155 (looking-at (concat indent
"\\("
1156 (or (and list-nested
"")
1159 ;; if list is not nested, remove indentation
1160 (unless indent-found
1161 (setq post-indent
(match-string 1)
1166 (defun muse-publish-surround-text (beg-tag end-tag move-func
&optional indent post-indent determine-indent-func list-item
)
1168 (setq list-item
(format muse-list-item-regexp
1169 (concat "[" muse-regexp-blank
"]*"))))
1171 (empty-line (concat "^[" muse-regexp-blank
"]*\n"))
1172 (determine-indent (if determine-indent-func t nil
))
1177 (setq indent
(concat "[" muse-regexp-blank
"]+")))
1179 (setq post-indent
(concat " \\{0," (number-to-string post-indent
)
1181 (setq post-indent
""))
1183 (if (or (not end-tag
) (string= end-tag
""))
1184 ;; if no end of list item markup exists, treat the beginning
1185 ;; of list item markup as it if it were the end -- this
1186 ;; prevents multiple-level lists from being confused
1187 (muse-insert-markup-end-list beg-tag
)
1188 (muse-insert-markup beg-tag
))
1190 ;; move past current item; continue is non-nil if there
1191 ;; are more like items to be processed
1192 continue
(if (and determine-indent-func first
)
1193 (funcall move-func
(concat indent post-indent
))
1194 (funcall move-func indent
)))
1195 (when determine-indent-func
1196 (funcall determine-indent-func continue
'new-indent
'determine-indent
))
1198 ;; remove list markup if we encountered another item of the
1200 (replace-match "" t t nil
1))
1202 (narrow-to-region beg
(point))
1203 ;; narrow to current item
1204 (goto-char (point-min))
1206 (muse-publish-strip-list-indentation list-item empty-line
1208 (skip-chars-backward (concat muse-regexp-blank
"\n"))
1209 (muse-insert-markup-end-list end-tag
)
1210 (when determine-indent-func
1211 (setq indent new-indent
))
1215 (goto-char (point-max)))))))
1217 (defun muse-publish-markup-list ()
1218 "Markup a list entry.
1219 This function works by marking up items of the same list level
1220 and type, respecting the end-of-list property."
1221 (let* ((str (match-string 1))
1222 (type (muse-list-item-type str
))
1223 (indent (buffer-substring (muse-line-beginning-position)
1224 (match-beginning 1)))
1225 (post-indent (length str
))
1226 (last (match-beginning 0)))
1228 ((or (get-text-property (muse-list-item-critical-point) 'read-only
)
1229 (get-text-property (muse-list-item-critical-point) 'muse-link
))
1232 (unless (eq (char-after (match-end 1)) ?-
)
1233 (delete-region (match-beginning 0) (match-end 0))
1234 (muse-insert-markup (muse-markup-text 'begin-uli
))
1236 (muse-publish-surround-text
1237 (muse-markup-text 'begin-uli-item
)
1238 (muse-markup-text 'end-uli-item
)
1240 (muse-forward-list-item 'ul indent
))
1242 (muse-insert-markup-end-list (muse-markup-text 'end-uli
)))
1245 (delete-region (match-beginning 0) (match-end 0))
1246 (muse-insert-markup (muse-markup-text 'begin-oli
))
1248 (muse-publish-surround-text
1249 (muse-markup-text 'begin-oli-item
)
1250 (muse-markup-text 'end-oli-item
)
1252 (muse-forward-list-item 'ol indent
))
1254 (muse-insert-markup-end-list (muse-markup-text 'end-oli
)))
1256 ((not (string= (match-string 2) ""))
1257 ;; must have an initial term
1258 (goto-char (match-beginning 0))
1259 (muse-insert-markup (muse-markup-text 'begin-dl
))
1261 (muse-publish-surround-dl indent post-indent
)
1262 (muse-insert-markup-end-list (muse-markup-text 'end-dl
)))
1266 (defun muse-publish-markup-quote ()
1267 "Markup a quoted paragraph.
1268 The reason this function is so funky, is to prevent text properties
1269 like read-only from being inadvertently deleted."
1270 (let* ((ws (match-string 1))
1271 (centered (>= (string-width ws
) 6))
1272 (begin-elem (if centered
'begin-center
'begin-quote-item
))
1273 (end-elem (if centered
'end-center
'end-quote-item
)))
1274 (replace-match "" t t nil
1)
1276 (muse-insert-markup (muse-markup-text 'begin-quote
)))
1277 (muse-publish-surround-text (muse-markup-text begin-elem
)
1278 (muse-markup-text end-elem
)
1279 (function (lambda (indent)
1280 (muse-forward-paragraph)
1283 (muse-insert-markup (muse-markup-text 'end-quote
)))))
1285 (defun muse-publish-markup-leading-space (markup-space multiple
)
1287 (when (and markup-space
1288 (>= (setq count
(skip-chars-forward " ")) 0))
1289 (delete-region (muse-line-beginning-position) (point))
1291 (muse-insert-markup markup-space
)
1292 (setq count
(- count multiple
))))))
1294 (defun muse-publish-markup-verse ()
1295 (let ((leader (match-string 0)))
1296 (goto-char (match-beginning 0))
1297 (muse-insert-markup (muse-markup-text 'begin-verse
))
1298 (while (looking-at leader
)
1300 (muse-publish-markup-leading-space (muse-markup-text 'verse-space
) 2)
1301 (let ((beg (point)))
1305 (let ((text (muse-markup-text 'empty-verse-line
)))
1306 (when text
(muse-insert-markup text
))))
1310 (or (looking-at (concat leader
"["
1313 (not (looking-at leader
)))))
1314 (let ((begin-text (muse-markup-text 'begin-last-stanza-line
))
1315 (end-text (muse-markup-text 'end-last-stanza-line
)))
1316 (when end-text
(muse-insert-markup end-text
))
1318 (when begin-text
(muse-insert-markup begin-text
))
1321 (let ((begin-text (muse-markup-text 'begin-verse-line
))
1322 (end-text (muse-markup-text 'end-verse-line
)))
1323 (when end-text
(muse-insert-markup end-text
))
1325 (when begin-text
(muse-insert-markup begin-text
))
1328 (muse-insert-markup (muse-markup-text 'end-verse
))
1331 (defun muse-publish-table-fields (beg end
)
1332 "Parse given region as a table, returning a cons cell.
1333 The car is the length of the longest row.
1335 The cdr is a list of the fields of the table, with the first
1336 element indicating the type of the row:
1337 1: body, 2: header, 3: footer.
1339 The existing region will be removed, except for initial blank lines."
1340 (unless (muse-publishing-directive "disable-tables")
1345 (narrow-to-region beg end
)
1346 (goto-char (point-min))
1347 (while (looking-at (concat "^[" muse-regexp-blank
"]*$"))
1351 (when (looking-at muse-table-line-regexp
)
1352 (setq fields
(cons (length (match-string 1))
1353 (mapcar #'muse-trim-whitespace
1354 (split-string (match-string 0)
1355 muse-table-field-regexp
)))
1356 field-list
(cons fields field-list
)
1357 longest
(max (length fields
) longest
)))
1358 (setq left
(forward-line 1))))
1359 (delete-region beg end
)
1362 (cons (1- longest
) (nreverse field-list
))))))
1364 (defun muse-publish-markup-table ()
1365 "Style does not support tables.")
1367 (defun muse-publish-escape-specials-in-string (string &optional context
)
1368 "Escape specials in STRING using style-specific :specials.
1369 CONTEXT is used to figure out what kind of specials to escape.
1371 See the documentation of the `muse-publish-escape-specials'
1372 function for the list of available contexts."
1375 (let ((specials (muse-style-element :specials nil t
)))
1376 (cond ((functionp specials
)
1377 (setq specials
(funcall specials context
)))
1379 (setq specials
(symbol-value specials
))))
1380 (if (functionp specials
)
1381 (funcall specials string
)
1382 (apply (function concat
)
1385 (let ((repl (or (assoc ch specials
)
1386 (assoc ch muse-publish-markup-specials
))))
1390 (append string nil
))))))
1392 (defun muse-publish-markup-email ()
1393 (let* ((beg (match-end 1))
1394 (addr (buffer-substring-no-properties beg
(match-end 0))))
1395 (setq addr
(muse-publish-escape-specials-in-string addr
'email
))
1397 (delete-region beg
(match-end 0))
1398 (if (or (eq (char-before (match-beginning 0)) ?
\")
1399 (eq (char-after (match-end 0)) ?
\"))
1401 (insert (format (muse-markup-text 'email-addr
) addr addr
)))
1402 (muse-publish-mark-read-only beg
(point))))
1404 (defun muse-publish-classify-url (target)
1405 "Transform anchors and get published name, if TARGET is a page.
1406 The return value is a cons cell. The car is the type of link,
1407 the cadr is the page name, and the cddr is the anchor."
1409 (cond ((or (null target
) (string= target
""))
1411 ((string-match muse-image-regexp target
)
1412 (cons 'image
(cons target nil
)))
1413 ((string-match muse-url-regexp target
)
1414 (cons 'url
(cons target nil
)))
1415 ((string-match muse-file-regexp target
)
1416 (cons 'file
(cons target nil
)))
1417 ((string-match "#" target
)
1418 (if (eq (aref target
0) ?\
#)
1419 (cons 'anchor-ref
(cons nil
(substring target
1)))
1420 (cons 'link-and-anchor
1421 (cons (muse-publish-link-page
1422 (substring target
0 (match-beginning 0)))
1423 (substring target
(match-end 0))))))
1425 (cons 'link
(cons (muse-publish-link-page target
) nil
))))))
1427 (defun muse-publish-url-desc (desc explicit
)
1429 (dolist (transform muse-publish-desc-transforms
)
1430 (setq desc
(save-match-data
1431 (when desc
(funcall transform desc explicit
)))))
1432 (setq desc
(muse-link-unescape desc
))
1433 (muse-publish-escape-specials-in-string desc
'url-desc
)))
1435 (defun muse-publish-url (url &optional desc orig-url explicit
)
1436 "Resolve a URL into its final <a href> form."
1438 (dolist (transform muse-publish-url-transforms
)
1439 (setq url
(save-match-data (when url
(funcall transform url explicit
)))))
1441 (setq desc
(muse-publish-url-desc desc explicit
))
1443 (setq orig-url
(muse-publish-url-desc orig-url explicit
))))
1444 (let ((target (muse-publish-classify-url url
)))
1445 (setq type
(car target
)
1446 url
(if (eq type
'image
)
1447 (muse-publish-escape-specials-in-string (cadr target
)
1449 (muse-publish-escape-specials-in-string (cadr target
) 'url
))
1450 anchor
(muse-publish-escape-specials-in-string
1451 (cddr target
) 'url
)))
1452 (cond ((eq type
'anchor-ref
)
1453 (muse-markup-text 'anchor-ref anchor
(or desc orig-url
)))
1457 (let ((ext (or (file-name-extension url
) "")))
1458 (setq url
(muse-path-sans-extension url
))
1460 (muse-markup-text 'image-with-desc url ext desc
)
1461 (muse-markup-text 'image url ext
))))
1462 ((eq type
'link-and-anchor
)
1463 (muse-markup-text 'link-and-anchor url anchor
1464 (or desc orig-url
)))
1465 ((and desc
(string-match muse-image-regexp desc
))
1466 (let ((ext (or (file-name-extension desc
) "")))
1467 (setq desc
(muse-path-sans-extension desc
))
1468 (muse-markup-text 'image-link url desc ext
)))
1470 (muse-markup-text 'link url
(or desc orig-url
)))
1473 (not (string= url orig-url
)))
1474 (let ((text (muse-markup-text 'url-and-desc url
1475 (or desc orig-url
))))
1476 (and (not (string= text
""))
1478 (muse-markup-text 'url url
(or desc orig-url
)))))))
1480 (defun muse-publish-insert-url (url &optional desc orig-url explicit
)
1481 "Resolve a URL into its final <a href> form."
1482 (delete-region (match-beginning 0) (match-end 0))
1483 (let ((text (muse-publish-url url desc orig-url explicit
)))
1485 (muse-insert-markup text
))))
1487 (defun muse-publish-markup-link ()
1488 (let (desc explicit orig-link link
)
1489 (setq explicit
(save-match-data
1490 (if (string-match muse-explicit-link-regexp
1493 (setq orig-link
(if explicit
(match-string 1) (match-string 0)))
1494 (setq desc
(when explicit
(match-string 2)))
1495 (setq link
(if explicit
1496 (muse-handle-explicit-link orig-link
)
1497 (muse-handle-implicit-link orig-link
)))
1500 (not (or (eq (char-before (match-beginning 0)) ?
\")
1501 (eq (char-after (match-end 0)) ?
\")))))
1502 ;; if explicit link has no user-provided description, treat it
1503 ;; as if it were an implicit link
1504 (when (and explicit
(not desc
))
1505 (setq explicit nil
))
1506 (muse-publish-insert-url link desc orig-link explicit
))))
1508 (defun muse-publish-markup-url ()
1509 (unless (or (eq (char-before (match-beginning 0)) ?
\")
1510 (eq (char-after (match-end 0)) ?
\"))
1511 (let ((url (match-string 0)))
1512 (muse-publish-insert-url url nil url
))))
1514 ;; Default publishing tags
1516 (defcustom muse-publish-contents-depth
2
1517 "The number of heading levels to include with <contents> tags."
1519 :group
'muse-publish
)
1521 (defmacro muse-publish-ensure-block-tag
(beg)
1522 "Ensure that block-level tag at BEG is published with at least one
1523 blank line. BEG is modified to be the new position."
1526 (cond ((not (bolp)) (insert "\n\n"))
1527 ((eq (point) (point-min)) nil
)
1528 ((prog2 (backward-char) (bolp) (forward-char)) nil
)
1530 (setq ,beg
(point))))
1532 (defun muse-publish-contents-tag (beg end attrs
)
1533 (set (make-local-variable 'muse-publish-generate-contents
)
1534 (cons (copy-marker (point) t
)
1535 (let ((depth (cdr (assoc "depth" attrs
))))
1536 (or (and depth
(string-to-number depth
))
1537 muse-publish-contents-depth
)))))
1539 (defun muse-publish-verse-tag (beg end
)
1542 (narrow-to-region beg end
)
1543 (goto-char (point-min))
1544 (while (eq ?\
(char-syntax (char-after)))
1546 (while (< (point) (point-max))
1549 (if (eq ?\
(char-syntax (char-before)))
1550 (delete-char -
1)))))
1552 (defun muse-publish-mark-read-only (beg end
)
1553 "Add read-only properties to the given region."
1554 (add-text-properties beg end
'(rear-nonsticky (read-only) read-only t
))
1557 (defun muse-publish-mark-link (&optional beg end
)
1558 "Indicate that the given region is a Muse link, so that other
1559 markup elements respect it. If a region is not specified, use
1560 the 0th match data to determine it.
1562 This is usually applied to explicit links."
1563 (unless beg
(setq beg
(match-beginning 0)))
1564 (unless end
(setq end
(match-end 0)))
1565 (add-text-properties beg end
'(muse-link t
))
1568 (defun muse-publish-quote-tag (beg end
)
1571 (narrow-to-region beg end
)
1572 (let ((quote-regexp "^\\(<\\(/?\\)quote>\\)"))
1573 (muse-insert-markup (muse-markup-text 'begin-quote
))
1575 (unless (looking-at (concat "[" muse-regexp-blank
"\n]*"
1577 (muse-publish-surround-text
1578 (muse-markup-text 'begin-quote-item
)
1579 (muse-markup-text 'end-quote-item
)
1582 (muse-forward-paragraph)
1583 (goto-char (match-end 0))
1584 (and (< (point) (point-max))
1585 (not (looking-at quote-regexp
)))))
1588 (if (>= (point) (point-max))
1590 (and (search-forward "<quote>" nil t
)
1591 (muse-goto-tag-end "quote" t
)
1592 (progn (forward-line 1) t
)
1593 (< (point) (point-max))))))
1594 (goto-char (point-max))
1595 (muse-insert-markup (muse-markup-text 'end-quote
))))))
1597 (defun muse-publish-code-tag (beg end
)
1598 (muse-publish-escape-specials beg end nil
'literal
)
1600 (insert (muse-markup-text 'begin-literal
))
1602 (insert (muse-markup-text 'end-literal
))
1603 (muse-publish-mark-read-only beg
(point)))
1605 (defun muse-publish-src-tag (beg end attrs
)
1606 (muse-publish-example-tag beg end
))
1608 (defun muse-publish-example-tag (beg end
)
1609 (muse-publish-ensure-block-tag beg
)
1610 (muse-publish-escape-specials beg end nil
'example
)
1612 (insert (muse-markup-text 'begin-example
))
1614 (insert (muse-markup-text 'end-example
))
1615 (muse-publish-mark-read-only beg
(point)))
1617 (defun muse-publish-literal-tag (beg end attrs
)
1618 "Ensure that the text between BEG and END is not interpreted later on.
1620 ATTRS is an alist of attributes.
1622 If it contains a \"style\" element, delete the region if the
1623 current style is neither derived from nor equal to this style.
1625 If it contains both a \"style\" element and an \"exact\" element
1626 with the value \"t\", delete the region only if the current style
1627 is exactly this style."
1628 (let* ((style (cdr (assoc "style" attrs
)))
1629 (exact (cdr (assoc "exact" attrs
)))
1630 (exactp (and (stringp exact
) (string= exact
"t"))))
1632 (and exactp
(equal (muse-style style
)
1633 muse-publishing-current-style
))
1634 (and (not exactp
) (muse-style-derived-p style
)))
1635 (muse-publish-mark-read-only beg end
)
1636 (delete-region beg end
))))
1638 (defun muse-publish-verbatim-tag (beg end
)
1639 (muse-publish-escape-specials beg end nil
'verbatim
)
1640 (muse-publish-mark-read-only beg end
))
1642 (defalias 'muse-publish-class-tag
'ignore
)
1644 (defun muse-publish-call-tag-on-buffer (tag &optional attrs
)
1645 "Transform the current buffer as if it were surrounded by the tag TAG.
1646 If attributes ATTRS are given, pass them to the tag function."
1647 (let ((tag-info (if muse-inhibit-style-tags
1648 (assoc tag muse-publish-markup-tags
)
1649 (muse-markup-tag-info tag
))))
1651 (let* ((end (progn (goto-char (point-max)) (point-marker)))
1652 (args (list (point-min) end
))
1653 (muse-inhibit-style-tags nil
))
1654 (when (nth 2 tag-info
)
1655 (nconc args
(list attrs
)))
1656 (apply (nth 4 tag-info
) args
)))))
1658 (defun muse-publish-examplify-buffer (&optional attrs
)
1659 "Transform the current buffer as if it were an <example> region."
1660 (muse-publish-call-tag-on-buffer "example" attrs
))
1662 (defun muse-publish-srcify-buffer (&optional attrs
)
1663 "Transform the current buffer as if it were a <src> region."
1664 (muse-publish-call-tag-on-buffer "src" attrs
))
1666 (defun muse-publish-versify-buffer (&optional attrs
)
1667 "Transform the current buffer as if it were a <verse> region."
1668 (muse-publish-call-tag-on-buffer "verse" attrs
)
1669 (muse-publish-markup ""
1670 `((100 ,(concat "^[" muse-regexp-blank
"]*> ") 0
1671 muse-publish-markup-verse
)))
1672 (goto-char (point-min)))
1674 (defmacro muse-publish-get-and-delete-attr
(attr attrs
)
1675 "Delete attribute ATTR from ATTRS only once, destructively.
1677 This function returns the matching attribute value, if found."
1678 (let ((last (make-symbol "last"))
1679 (found (make-symbol "found"))
1680 (vals (make-symbol "vals")))
1681 `(let ((,vals
,attrs
))
1682 (if (string= (caar ,vals
) ,attr
)
1684 (setq ,attrs
(cdr ,vals
)))
1688 (setq ,vals
(cdr ,vals
))
1689 (when (string= (caar ,vals
) ,attr
)
1690 (setq ,found
(cdar ,vals
))
1691 (setcdr ,last
(cdr ,vals
))
1696 (defmacro muse-publish-markup-attribute
(beg end attrs reinterp
&rest body
)
1697 "Evaluate BODY within the bounds of BEG and END.
1698 ATTRS is an alist. Only the \"markup\" element of ATTRS is acted
1701 If it is omitted, publish the region with the normal Muse rules.
1702 If RE-INTERP is specified, this is done immediately in a new
1703 publishing process. Currently, RE-INTERP is specified only by
1706 If \"nil\", do not mark up the region at all, but prevent it from
1707 being further interpreted by Muse.
1709 If \"example\", treat the region as if it was surrounded by the
1712 If \"src\", treat the region as if it was surrounded by the
1715 If \"verse\", treat the region as if it was surrounded by the
1716 <verse> tag, to preserve newlines.
1718 Otherwise, it should be the name of a function to call in the
1719 narrowed region after evaluating BODY. The function should
1720 take the ATTRS parameter."
1721 (let ((markup (make-symbol "markup"))
1722 (markup-function (make-symbol "markup-function")))
1723 `(let ((,markup
(muse-publish-get-and-delete-attr "markup" ,attrs
)))
1725 (narrow-to-region ,beg
,end
)
1726 (goto-char (point-min))
1730 (muse-publish-markup-region (point-min) (point-max))
1731 (muse-publish-mark-read-only (point-min) (point-max))
1732 (goto-char (point-max)))
1733 (let ((,markup-function
(read ,markup
)))
1734 (cond ((eq ,markup-function
'example
)
1735 (setq ,markup-function
#'muse-publish-examplify-buffer
))
1736 ((eq ,markup-function
'src
)
1737 (setq ,markup-function
#'muse-publish-srcify-buffer
))
1738 ((eq ,markup-function
'verse
)
1739 (setq ,markup-function
#'muse-publish-versify-buffer
))
1740 ((and ,markup-function
(not (functionp ,markup-function
)))
1741 (error "Invalid markup function `%s'" ,markup
))
1743 (if ,markup-function
1744 (funcall ,markup-function
,attrs
)
1745 (muse-publish-mark-read-only (point-min) (point-max))
1746 (goto-char (point-max)))))))))
1748 (put 'muse-publish-markup-attribute
'lisp-indent-function
4)
1749 (put 'muse-publish-markup-attribute
'edebug-form-spec
1750 '(form form form form body
))
1752 (defun muse-publish-lisp-tag (beg end attrs
)
1753 (muse-publish-markup-attribute beg end attrs nil
1755 (let ((str (muse-eval-lisp
1758 (buffer-substring-no-properties (point-min)
1761 (delete-region beg end
)))))
1762 (set-text-properties 0 (length str
) nil str
)
1765 (defun muse-publish-command-tag (beg end attrs
)
1766 (muse-publish-markup-attribute beg end attrs nil
1767 (while (looking-at "\\s-*$")
1769 (let ((interp (muse-publish-get-and-delete-attr "interp" attrs
)))
1771 (shell-command-on-region (point) (point-max) interp t t
)
1774 (buffer-substring-no-properties (point) (point-max))
1775 (delete-region (point-min) (point-max)))
1777 ;; make sure there is a newline at end
1778 (goto-char (point-max))
1780 (unless (looking-at "\\s-*$")
1781 (goto-char (point-max))
1783 (goto-char (point-min))))
1785 (defun muse-publish-perl-tag (beg end attrs
)
1786 (muse-publish-command-tag beg end
1787 (cons (cons "interp" (executable-find "perl"))
1790 (defun muse-publish-python-tag (beg end attrs
)
1791 (muse-publish-command-tag beg end
1792 (cons (cons "interp" (executable-find "python"))
1795 (defun muse-publish-ruby-tag (beg end attrs
)
1796 (muse-publish-command-tag beg end
1797 (cons (cons "interp" (executable-find "ruby"))
1800 (defun muse-publish-comment-tag (beg end
)
1801 (if (null muse-publish-comments-p
)
1802 (delete-region beg end
)
1804 (muse-insert-markup (muse-markup-text 'comment-end
))
1805 (muse-publish-mark-read-only beg end
)
1807 (muse-insert-markup (muse-markup-text 'comment-begin
))))
1809 (defun muse-publish-include-tag (beg end attrs
)
1810 "Include the named file at the current location during publishing.
1812 <include file=\"...\" markup=\"...\">
1814 The `markup' attribute controls how this file is marked up after
1815 being inserted. See `muse-publish-markup-attribute' for an
1816 explanation of how it works."
1817 (let ((filename (muse-publish-get-and-delete-attr "file" attrs
))
1818 (muse-publishing-directives muse-publishing-directives
))
1820 (setq filename
(expand-file-name
1822 (file-name-directory muse-publishing-current-file
)))
1823 (error "No file attribute specified in <include> tag"))
1824 (muse-publish-markup-attribute beg end attrs t
1825 (insert-file-contents filename
))))
1827 (defun muse-publish-mark-up-tag (beg end attrs
)
1828 "Run an Emacs Lisp function on the region delimted by this tag.
1830 <markup function=\"...\" style=\"...\" exact=\"...\">
1832 The optional \"function\" attribute controls how this section is
1833 marked up. If used, it should be the name of a function to call
1834 with the buffer narrowed to the delimited region. Note that no
1835 further marking-up will be performed on this region.
1837 If \"function\" is omitted, use the standard Muse markup function.
1838 This is useful for marking up content in headers and footers.
1840 The optional \"style\" attribute causes the region to be deleted
1841 if the current style is neither derived from nor equal to this
1844 If both a \"style\" attribute and an \"exact\" attribute are
1845 provided, and \"exact\" is \"t\", delete the region only if the
1846 current style is exactly this style."
1847 (let* ((style (cdr (assoc "style" attrs
)))
1848 (exact (cdr (assoc "exact" attrs
)))
1849 (exactp (and (stringp exact
) (string= exact
"t"))))
1851 (and exactp
(equal (muse-style style
)
1852 muse-publishing-current-style
))
1853 (and (not exactp
) (muse-style-derived-p style
)))
1854 (let* ((function (cdr (assoc "function" attrs
)))
1855 (muse-publishing-directives muse-publishing-directives
)
1856 (markup-function (and function
(intern function
))))
1857 (if (and markup-function
(functionp markup-function
))
1859 (narrow-to-region beg end
)
1860 (funcall markup-function
)
1861 (goto-char (point-max)))
1862 (let ((muse-publish-inhibit-style-hooks t
))
1863 (muse-publish-markup-region beg end
)))
1864 (muse-publish-mark-read-only beg
(point)))
1865 (delete-region beg end
))))
1867 ;; Miscellaneous helper functions
1869 (defun muse-publish-strip-tags (string)
1870 "Remove all tags from the string."
1871 (while (string-match "<.*?>" string
)
1872 (setq string
(replace-match "" nil t string
)))
1875 (defun muse-publish-markup-type (category default-func
)
1876 (let ((rule (muse-find-markup-element :overrides category
(muse-style))))
1877 (funcall (or rule default-func
))))
1879 (defun muse-published-buffer-contents (buffer)
1880 (with-current-buffer buffer
1881 (goto-char (point-min))
1882 (let ((beg (and (search-forward "Emacs Muse begins here")
1883 (muse-line-end-position)))
1884 (end (and (search-forward "Emacs Muse ends here")
1885 (muse-line-beginning-position))))
1886 (buffer-substring-no-properties beg end
))))
1888 (defun muse-published-contents (file)
1889 (when (file-readable-p file
)
1890 (muse-with-temp-buffer
1891 (insert-file-contents file
)
1892 (muse-published-buffer-contents (current-buffer)))))
1894 (defun muse-publish-transform-output
1895 (file temp-file output-path name gen-func
&rest cleanup-exts
)
1896 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1897 (setq file
(muse-page-name file
))
1898 (message "Generating %s output for %s..." name file
)
1899 (if (not (funcall gen-func temp-file output-path
))
1900 (message "Generating %s from %s...failed" name file
)
1901 (message "Generating %s output for %s...done" name file
)
1902 (muse-delete-file-if-exists temp-file
)
1903 (dolist (ext cleanup-exts
)
1904 (muse-delete-file-if-exists
1905 (expand-file-name (concat file ext
)
1906 (file-name-directory output-path
))))
1907 (message "Wrote %s" output-path
)))
1909 (defun muse-publish-read-only (string)
1910 (let ((end (1- (length string
))))
1911 (add-text-properties 0 end
1912 '(rear-nonsticky (read-only) read-only t
)
1916 ;;; muse-publish.el ends here