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.
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 (require 'muse-regexps
)
46 (defgroup muse-publish nil
47 "Options controlling the general behavior of Muse publishing."
50 (defcustom muse-before-publish-hook nil
51 "A hook run in the buffer to be published, before it is done."
55 (defcustom muse-after-publish-hook nil
56 "A hook run in the buffer to be published, after it is done."
60 (defcustom muse-publish-url-transforms
62 "A list of functions used to prepare URLs for publication.
63 Each is passed the URL. The transformed URL should be returned."
65 :options
'(muse-resolve-url)
68 (defcustom muse-publish-desc-transforms nil
69 "A list of functions used to prepare URL desciptions for publication.
70 Each is passed the description. The modified description should
75 (defcustom muse-publish-comments-p nil
76 "If nil, remove comments before publishing.
77 If non-nil, publish comments using the markup of the current style."
81 (defcustom muse-publish-report-threshhold
100000
82 "If a file is this size or larger, report publishing progress."
86 (defcustom muse-publish-markup-regexps
87 `(;; Remove leading and trailing whitespace from the file
88 (1000 "\\(\\`\n+\\|\n+\\'\\)" 0 "")
90 ;; Remove trailing whitespace from all lines
91 (1100 ,(concat "[" muse-regexp-blank
"]+$") 0 "")
93 ;; Handle any leading #directives
94 (1200 "\\`#\\([a-zA-Z-]+\\)\\s-+\\(.+\\)\n+" 0 directive
)
97 (1300 muse-tag-regexp
0 tag
)
100 (1350 "^;\\s-+\\(.+\\)" 0 comment
)
102 ;; prevent emphasis characters in explicit links from being marked
103 (1400 muse-explicit-link-regexp
0 muse-publish-mark-link
)
105 ;; emphasized or literal text
106 (1600 ,(concat "\\(^\\|[-[" muse-regexp-blank
107 "<('`\"\n]\\)\\(=[^=" muse-regexp-blank
108 "\n]\\|_[^_" muse-regexp-blank
109 "\n]\\|\\*+[^*" muse-regexp-blank
113 ;; headings, outline-mode style
114 (1700 "^\\(\\*+\\)\\s-+" 0 heading
)
117 (1800 "\\.\\.\\.\\." 0 enddots
)
118 (1850 "\\.\\.\\." 0 dots
)
120 ;; horizontal rule, or section separator
121 (1900 "^----+" 0 rule
)
123 ;; non-breaking space
124 (1950 "~~" 0 no-break-space
)
126 ;; beginning of footnotes section
127 (2000 "^Footnotes:?\\s-*" 0 fn-sep
)
128 ;; footnote definition/reference (def if at beginning of line)
129 (2100 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote
)
131 ;; unnumbered List items begin with a -. numbered list items
132 ;; begin with number and a period. definition lists have a
133 ;; leading term separated from the body with ::. centered
134 ;; paragraphs begin with at least six columns of whitespace; any
135 ;; other whitespace at the beginning indicates a blockquote. The
136 ;; reason all of these rules are handled here, is so that
137 ;; blockquote detection doesn't interfere with indented list
139 (2200 ,(format muse-list-item-regexp
(concat "[" muse-regexp-blank
"]*"))
142 ;; simple table markup is supported, nothing fancy. use | to
143 ;; separate cells, || to separate header cells, and ||| for footer
145 (2300 ,(concat "\\(\\([" muse-regexp-blank
"]*\n\\)?"
146 "\\(" muse-table-line-regexp
"\\(?:\n\\|\\'\\)\\)\\)+")
149 ;; blockquote and centered text
150 (2400 ,(concat "^\\([" muse-regexp-blank
"]+\\).+") 0 quote
)
153 (2500 ,(concat "\\(^\\|[" muse-regexp-blank
"]*\\)--\\($\\|["
154 muse-regexp-blank
"]*\\)")
157 ;; "verse" text is indicated the same way as a quoted e-mail
158 ;; response: "> text", where text may contain initial whitespace
160 (2600 ,(concat "^[" muse-regexp-blank
"]*> ") 0 verse
)
162 ;; define anchor points
163 (2700 "^\\(\\W*\\)#\\(\\S-+\\)\\s-*" 0 anchor
)
165 ;; replace links in the buffer (links to other pages)
166 (2900 muse-explicit-link-regexp
0 link
)
169 (3000 muse-url-regexp
0 url
)
171 ;; bare email addresses
173 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email
)
175 "List of markup rules for publishing a page with Muse.
176 The rules given in this variable are invoked first, followed by
177 whatever rules are specified by the current style.
179 Each member of the list is either a function, or a list of the form:
181 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
183 REGEXP is a regular expression, or symbol whose value is a regular
184 expression, which is searched for using `re-search-forward'.
185 TEXT-BEGIN-GROUP is the matching group within that regexp which
186 denotes the beginning of the actual text to be marked up.
187 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
188 If it is not a string, but a function, it will be called to determine
189 what the replacement text should be (it must return a string). If it
190 is a symbol, the value of that symbol should be a string.
192 The replacements are done in order, one rule at a time. Writing
193 the regular expressions can be a tricky business. Note that case
194 is never ignored. `case-fold-search' is always bound to nil
195 while processing the markup rules."
196 :type
'(repeat (choice
197 (list :tag
"Markup rule"
199 (choice regexp symbol
)
201 (choice string function symbol
))
203 :group
'muse-publish
)
205 (defcustom muse-publish-markup-functions
206 '((directive . muse-publish-markup-directive
)
207 (comment . muse-publish-markup-comment
)
208 (anchor . muse-publish-markup-anchor
)
209 (tag . muse-publish-markup-tag
)
210 (word . muse-publish-markup-word
)
211 (emdash . muse-publish-markup-emdash
)
212 (enddots . muse-publish-markup-enddots
)
213 (dots . muse-publish-markup-dots
)
214 (rule . muse-publish-markup-rule
)
215 (no-break-space . muse-publish-markup-no-break-space
)
216 (heading . muse-publish-markup-heading
)
217 (footnote . muse-publish-markup-footnote
)
218 (fn-sep . muse-publish-markup-fn-sep
)
219 (list . muse-publish-markup-list
)
220 (quote . muse-publish-markup-quote
)
221 (verse . muse-publish-markup-verse
)
222 (table . muse-publish-markup-table
)
223 (email . muse-publish-markup-email
)
224 (link . muse-publish-markup-link
)
225 (url . muse-publish-markup-url
))
226 "An alist of style types to custom functions for that kind of text.
228 Each member of the list is of the form:
232 SYMBOL describes the type of text to associate with this rule.
233 `muse-publish-markup-regexps' maps regexps to these symbols.
235 FUNCTION is the function to use to mark up this kind of rule if
236 no suitable function is found through the :functions tag of the
238 :type
'(alist :key-type symbol
:value-type function
)
239 :group
'muse-publish
)
241 (defcustom muse-publish-markup-tags
242 '(("contents" nil t nil muse-publish-contents-tag
)
243 ("verse" t nil nil muse-publish-verse-tag
)
244 ("example" t nil nil muse-publish-example-tag
)
245 ("code" t nil nil muse-publish-code-tag
)
246 ("quote" t nil t muse-publish-quote-tag
)
247 ("literal" t nil nil muse-publish-mark-read-only
)
248 ("verbatim" t nil nil muse-publish-verbatim-tag
)
249 ("lisp" t t nil muse-publish-lisp-tag
)
250 ("class" t t nil muse-publish-class-tag
)
251 ("command" t t nil muse-publish-command-tag
)
252 ("perl" t t nil muse-publish-perl-tag
)
253 ("python" t t nil muse-publish-python-tag
)
254 ("ruby" t t nil muse-publish-ruby-tag
)
255 ("comment" t nil nil muse-publish-comment-tag
)
256 ("include" nil t nil muse-publish-include-tag
)
257 ("markup" t t nil muse-publish-mark-up-tag
))
258 "A list of tag specifications, for specially marking up text.
259 XML-style tags are the best way to add custom markup to Muse.
260 This is easily accomplished by customizing this list of markup tags.
262 For each entry, the name of the tag is given, whether it expects
263 a closing tag and/or an optional set of attributes, whether it is
264 nestable, and a function that performs whatever action is desired
265 within the delimited region.
267 The tags themselves are deleted during publishing, before the
268 function is called. The function is called with three arguments,
269 the beginning and end of the region surrounded by the tags. If
270 properties are allowed, they are passed as a third argument in
271 the form of an alist. The `end' argument to the function is
274 Point is always at the beginning of the region within the tags, when
275 the function is called. Wherever point is when the function finishes
276 is where tag markup will resume.
278 These tag rules are processed once at the beginning of markup, and
279 once at the end, to catch any tags which may have been inserted
281 :type
'(repeat (list (string :tag
"Markup tag")
282 (boolean :tag
"Expect closing tag" :value t
)
283 (boolean :tag
"Parse attributes" :value nil
)
284 (boolean :tag
"Nestable" :value nil
)
286 :group
'muse-publish
)
288 (defcustom muse-publish-markup-specials nil
289 "A table of characters which must be represented specially."
290 :type
'(alist :key-type character
:value-type string
)
291 :group
'muse-publish
)
293 (defvar muse-publishing-p nil
294 "Set to t while a page is being published.")
295 (defvar muse-batch-publishing-p nil
296 "Set to t while a page is being batch published.")
297 (defvar muse-publishing-styles nil
298 "The publishing styles that Muse recognizes.
299 This is automatically generated when loading publishing styles.")
300 (defvar muse-publishing-current-file nil
301 "The file that is currently being published.")
302 (defvar muse-publishing-current-output-path nil
303 "The path where the current file will be published to.")
304 (defvar muse-publishing-current-style nil
305 "The style of the file that is currently being published.")
306 (defvar muse-publishing-directives nil
307 "An alist of publishing directives from the top of a file.")
308 (defvar muse-publish-generate-contents nil
309 "Non-nil if a table of contents should be generated.
310 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
311 tell where the <contents> was seen, and to what depth the
312 contents were requested.")
313 (defvar muse-publishing-last-position nil
314 "Last position of the point when publishing.
315 This is used to make sure that publishing doesn't get stalled.")
317 ;; Functions for handling style information
319 (defsubst muse-style
(&optional style
)
320 "Resolve the given STYLE into a Muse style, if it is a string."
322 muse-publishing-current-style
324 (assoc style muse-publishing-styles
)
325 (muse-assert (consp style
))
328 (defun muse-define-style (name &rest elements
)
329 (let ((entry (assoc name muse-publishing-styles
)))
331 (setcdr entry elements
)
332 (setq muse-publishing-styles
333 (cons (append (list name
) elements
)
334 muse-publishing-styles
)))))
336 (defun muse-derive-style (new-name base-name
&rest elements
)
337 (apply 'muse-define-style new-name
338 (append elements
(list :base base-name
))))
340 (defsubst muse-get-keyword
(keyword list
&optional direct
)
341 (let ((value (cadr (memq keyword list
))))
342 (if (and (not direct
) (symbolp value
))
346 (defun muse-style-elements-list (elem &optional style
)
347 "Return a list all references to ELEM in STYLE, including base styles.
348 If STYLE is not specified, use current style."
351 (setq style
(muse-style style
))
352 (setq elements
(append elements
353 (muse-get-keyword elem style
)))
354 (setq style
(muse-get-keyword :base style
)))
357 (defun muse-style-element (elem &optional style direct
)
358 "Search for ELEM in STYLE, including base styles.
359 If STYLE is not specified, use current style."
360 (setq style
(muse-style style
))
361 (let ((value (muse-get-keyword elem style direct
)))
364 (let ((base (muse-get-keyword :base style
)))
366 (muse-style-element elem base direct
))))))
368 (defun muse-find-markup-element (keyword ident style
)
369 (let ((def (assq ident
(muse-style-element keyword style
))))
372 (let ((base (muse-style-element :base style
)))
374 (muse-find-markup-element keyword ident base
))))))
376 (defun muse-markup-text (ident &rest args
)
377 "Insert ARGS into the text markup associated with IDENT.
378 If the markup text has sections like %N%, this will be replaced
379 with the N-1th argument in ARGS. After that, `format' is applied
380 to the text with ARGS as parameters."
381 (let ((text (muse-find-markup-element :strings ident
(muse-style))))
384 (let (start repl-text
)
385 (while (setq start
(string-match "%\\([1-9][0-9]*\\)%" text start
))
386 ;; escape '%' in the argument text, since we will be
387 ;; using format on it
388 (setq repl-text
(muse-replace-regexp-in-string
390 (nth (1- (string-to-number
391 (match-string 1 text
))) args
)
393 start
(+ start
(length repl-text
))
394 text
(replace-match repl-text t t text
))))
395 (apply 'format text args
))
398 (defun muse-insert-markup (&rest args
)
401 (muse-publish-mark-read-only beg
(point))))
403 (defun muse-find-markup-tag (keyword tagname style
)
404 (let ((def (assoc tagname
(muse-style-element keyword style
))))
406 (let ((base (muse-style-element :base style
)))
408 (muse-find-markup-tag keyword tagname base
))))))
410 (defsubst muse-markup-tag-info
(tagname &rest args
)
411 (let ((tag-info (muse-find-markup-tag :tags tagname
(muse-style))))
413 (assoc (match-string 1) muse-publish-markup-tags
))))
415 (defsubst muse-markup-function
(category)
416 (let ((func (muse-find-markup-element :functions category
(muse-style))))
418 (cdr (assq category muse-publish-markup-functions
)))))
420 ;; Publishing routines
422 (defun muse-publish-markup (name rules
)
423 (let* ((case-fold-search nil
)
424 (inhibit-read-only t
)
425 (limit (* (length rules
) (point-max)))
426 (verbose (and muse-publish-report-threshhold
427 (> (point-max) muse-publish-report-threshhold
)))
430 (goto-char (point-min))
431 (let ((regexp (nth 1 (car rules
)))
432 (group (nth 2 (car rules
)))
433 (repl (nth 3 (car rules
)))
435 (setq muse-publishing-last-position nil
)
437 (setq regexp
(symbol-value regexp
)))
438 (if (and verbose
(not muse-batch-publishing-p
))
439 (message "Publishing %s...%d%%" name
440 (* (/ (float (+ (point) base
)) limit
) 100)))
441 (while (and regexp
(setq pos
(re-search-forward regexp nil t
)))
442 (if (and verbose
(not muse-batch-publishing-p
))
443 (message "Publishing %s...%d%%" name
444 (* (/ (float (+ (point) base
)) limit
) 100)))
445 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
446 (match-beginning group
)
447 (get-text-property (match-beginning group
) 'read-only
))
451 (setq func
(muse-markup-function repl
)))
459 (replace-match text t
))))
460 (if (and muse-publishing-last-position
461 (= pos muse-publishing-last-position
))
465 (setq muse-publishing-last-position pos
)))
466 (setq rules
(cdr rules
)
467 base
(+ base
(point-max))))
468 (if (and verbose
(not muse-batch-publishing-p
))
469 (message "Publishing %s...done" name
))))
471 (defcustom muse-publish-markup-header-footer-tags
472 '(("lisp" t t nil muse-publish-lisp-tag
)
473 ("markup" t t nil muse-publish-mark-up-tag
))
474 "Tags used when publishing headers and footers.
475 See `muse-publish-markup-tags' for details."
476 :type
'(repeat (list (string :tag
"Markup tag")
477 (boolean :tag
"Expect closing tag" :value t
)
478 (boolean :tag
"Parse attributes" :value nil
)
479 (boolean :tag
"Nestable" :value nil
)
481 :group
'muse-publish
)
483 (defun muse-insert-file-or-string (file-or-string &optional title
)
484 (let ((beg (point)) end
)
485 (if (and (not (string-equal file-or-string
""))
486 (not (string-match "\n" file-or-string
))
487 (file-readable-p file-or-string
))
488 (setq end
(+ beg
(cadr (insert-file-contents file-or-string
))))
489 (insert file-or-string
)
492 (narrow-to-region beg end
)
493 (remove-text-properties (point-min) (point-max)
494 '(read-only nil rear-nonsticky nil
))
495 (goto-char (point-min))
496 (let ((muse-inhibit-style-tags t
))
497 (muse-publish-markup (or title
"")
498 '((100 muse-tag-regexp
0
499 muse-publish-markup-tag
)))))))
501 (defun muse-style-run-hooks (keyword style
&rest args
)
505 (setq style
(muse-style style
)))
506 (let ((func (muse-style-element keyword style t
)))
508 (not (member func cache
)))
509 (setq cache
(cons func cache
))
510 (when (apply func args
)
511 (throw 'handled t
))))
512 (setq style
(muse-style-element :base style
))))))
514 (defun muse-publish-markup-region (beg end
&optional title style
)
515 "Apply the given STYLE's markup rules to the given region.
516 TITLE is used when indicating the publishing progress; it may be nil."
517 (unless title
(setq title
""))
519 (or (setq style muse-publishing-current-style
)
520 (error "Cannot find any publishing styles to use")))
522 (narrow-to-region beg end
)
523 (muse-style-run-hooks :before style
)
526 (sort (copy-alist (append muse-publish-markup-regexps
527 (muse-style-elements-list :regexps style
)))
530 (< (car l
) (car r
))))))
531 (muse-style-run-hooks :before-end style
)
532 (muse-publish-escape-specials (point-min) (point-max) nil
'document
)))
534 (defun muse-publish-markup-buffer (title style
)
535 "Apply the given STYLE's markup rules to the current buffer."
536 (setq style
(muse-style style
))
537 (let ((style-header (muse-style-element :header style
))
538 (style-footer (muse-style-element :footer style
))
539 (muse-publishing-current-style style
)
540 (muse-publishing-directives
541 (list (cons "title" title
)
542 (cons "author" (user-full-name))
543 (cons "date" (format-time-string
545 (if muse-publishing-current-file
546 (nth 5 (file-attributes
547 muse-publishing-current-file
))
549 (muse-publishing-p t
)
550 (inhibit-read-only t
))
551 (run-hooks 'muse-update-values-hook
)
552 (run-hooks 'muse-before-publish-hook
)
553 (muse-publish-markup-region (point-min) (point-max) title style
)
554 (goto-char (point-min))
556 (muse-insert-file-or-string style-header title
))
557 (goto-char (point-max))
559 (muse-insert-file-or-string style-footer title
))
560 (muse-style-run-hooks :after style
)
561 (run-hooks 'muse-after-publish-hook
)))
563 (defun muse-publish-markup-string (string &optional style
)
564 "Markup STRING using the given STYLE's markup rules."
565 (setq style
(muse-style style
))
566 (muse-with-temp-buffer
568 (let ((muse-publishing-current-style style
)
569 (muse-publishing-p t
))
570 (muse-publish-markup "*string*" (muse-style-element :rules style
)))
573 ;; Commands for publishing files
575 (defun muse-publish-get-style (&optional styles
)
576 (unless styles
(setq styles muse-publishing-styles
))
577 (if (= 1 (length styles
))
579 (when (catch 'different
580 (let ((first (car (car styles
))))
581 (dolist (style (cdr styles
))
582 (unless (equal first
(car style
))
583 (throw 'different t
)))))
584 (setq styles
(muse-collect-alist
586 (completing-read "Publish with style: " styles nil t
))))
587 (if (or (= 1 (length styles
))
588 (not (muse-get-keyword :path
(car styles
))))
590 (setq styles
(mapcar (lambda (style)
591 (cons (muse-get-keyword :path style
)
594 (cdr (assoc (completing-read "Publish to directory: " styles nil t
)
597 (defsubst muse-publish-get-output-dir
(style)
598 (let ((default-directory (or (muse-style-element :path style
)
600 (muse-read-directory-name "Publish to directory: " nil default-directory
)))
602 (defsubst muse-publish-get-info
()
603 (let ((style (muse-publish-get-style)))
604 (list style
(muse-publish-get-output-dir style
)
605 current-prefix-arg
)))
607 (defsubst muse-publish-output-name
(&optional file style
)
608 (setq style
(muse-style style
))
609 (concat (muse-style-element :prefix style
)
610 (muse-page-name file
)
611 (muse-style-element :suffix style
)))
613 (defsubst muse-publish-output-file
(file &optional output-dir style
)
614 (setq style
(muse-style style
))
616 (expand-file-name (muse-publish-output-name file style
) output-dir
)
617 (concat (file-name-directory file
)
618 (muse-publish-output-name file style
))))
620 (defsubst muse-publish-link-name
(&optional file style
)
621 (setq style
(muse-style style
))
622 (concat (muse-style-element :prefix style
)
623 (muse-page-name file
)
624 (or (muse-style-element :link-suffix style
)
625 (muse-style-element :suffix style
))))
627 (defsubst muse-publish-link-file
(file &optional output-dir style
)
628 (setq style
(muse-style style
))
630 (expand-file-name (muse-publish-link-name file style
) output-dir
)
631 (concat (file-name-directory file
)
632 (muse-publish-link-name file style
))))
634 (defsubst muse-publish-link-page
(page)
635 (if (fboundp 'muse-project-link-page
)
636 (muse-project-link-page page
)
637 (muse-publish-link-file page
)))
640 (defun muse-publish-file (file style
&optional output-dir force
)
641 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
642 If the argument FORCE is nil, each file is only published if it is
643 newer than the published version. If the argument FORCE is non-nil,
644 the file is published no matter what."
645 (interactive (cons (read-file-name "Publish file: ")
646 (muse-publish-get-info)))
647 (let ((style-name style
))
648 (setq style
(muse-style style
))
650 (error "There is no style '%s' defined." style-name
)))
651 (let* ((output-path (muse-publish-output-file file output-dir style
))
652 (output-suffix (muse-style-element :osuffix style
))
653 (muse-publishing-current-file file
)
654 (muse-publishing-current-output-path output-path
)
655 (target (if output-suffix
656 (concat (muse-path-sans-extension output-path
)
659 (threshhold (nth 7 (file-attributes file
))))
661 (message "Please save %s before publishing" file
)
662 (when (or force
(file-newer-than-file-p file target
))
663 (if (and muse-publish-report-threshhold
665 muse-publish-report-threshhold
))
666 (message "Publishing %s ..." file
))
667 (muse-with-temp-buffer
668 (insert-file-contents file
)
669 (muse-publish-markup-buffer (muse-page-name file
) style
)
670 (let ((backup-inhibited t
))
671 (write-file output-path
))
672 (muse-style-run-hooks :final style file output-path target
))
676 (defun muse-publish-this-file (style output-dir
&optional force
)
677 "Publish the currently-visited file.
678 Prompt for both the STYLE and OUTPUT-DIR if they are not
680 (interactive (muse-publish-get-info))
682 (unless (muse-publish-file buffer-file-name style output-dir force
)
683 (message (concat "The published version is up-to-date; use"
684 " C-u C-c C-T to force an update.")))
685 (message "This buffer is not associated with any file")))
687 (defun muse-batch-publish-files ()
688 "Publish Muse files in batch mode."
689 (let ((muse-batch-publishing-p t
)
691 (setq style
(car command-line-args-left
)
692 command-line-args-left
(cdr command-line-args-left
)
693 output-dir
(car command-line-args-left
)
695 (if (string-match "\\`--output-dir=" output-dir
)
697 (substring output-dir
(match-end 0))
698 (setq command-line-args-left
(cdr command-line-args-left
)))))
699 (setq auto-mode-alist
700 (delete (cons (concat "\\." muse-file-extension
"\\'")
701 'muse-mode-choose-mode
)
703 (dolist (file command-line-args-left
)
704 (muse-publish-file file style output-dir t
))))
706 ;; Default publishing rules
708 (defun muse-publish-section-close (depth)
709 "Seach forward for the closing tag of given DEPTH."
712 (while (and (setq not-end
(re-search-forward
713 (concat "^\\*\\{1," (number-to-string depth
)
716 (get-text-property (match-beginning 0) 'read-only
)))
719 (goto-char (point-max)))
720 (cond ((not (eq (char-before) ?
\n))
722 ((not (eq (char-before (1- (point))) ?
\n))
724 (muse-insert-markup (muse-markup-text 'section-close depth
))
727 (defun muse-publish-markup-directive (&optional name value
)
728 (unless name
(setq name
(match-string 1)))
729 (unless value
(setq value
(match-string 2)))
730 (let ((elem (assoc name muse-publishing-directives
)))
733 (setq muse-publishing-directives
734 (cons (cons name value
)
735 muse-publishing-directives
))))
736 ;; Make sure we don't ever try to move the point forward (past the
737 ;; beginning of buffer) while we're still searching for directives.
738 (setq muse-publishing-last-position nil
)
739 (delete-region (match-beginning 0) (match-end 0)))
741 (defsubst muse-publishing-directive
(name)
742 (cdr (assoc name muse-publishing-directives
)))
744 (defun muse-publish-markup-anchor ()
745 (unless (get-text-property (match-end 1) 'muse-link
)
746 (let ((text (muse-markup-text 'anchor
(match-string 2))))
747 (unless (string= text
"")
749 (skip-chars-forward (concat muse-regexp-blank
"\n"))
750 (muse-insert-markup text
)))
753 (defun muse-publish-markup-comment ()
754 (if (null muse-publish-comments-p
)
756 (goto-char (match-end 0))
757 (muse-insert-markup (muse-markup-text 'comment-end
))
758 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
759 (delete-region (match-beginning 0) (match-beginning 1))
760 (goto-char (match-beginning 0))
761 (muse-insert-markup (muse-markup-text 'comment-begin
))))
763 (defun muse-publish-goto-tag-end (tag nested
)
765 (search-forward (concat "</" tag
">") nil t
)
767 (tag-regexp (concat "^\\(<\\(/?\\)" tag
">\\)"))
769 (while (and (> nesting
0)
770 (setq match-found
(re-search-forward tag-regexp nil t
)))
771 (if (string-equal (match-string 2) "/")
772 (setq nesting
(1- nesting
))
773 (setq nesting
(1+ nesting
))))
776 (defvar muse-inhibit-style-tags nil
777 "If non-nil, do not search for style-specific tags.
778 This is used when publishing headers and footers.")
780 (defun muse-publish-markup-tag ()
781 (let ((tag-info (if muse-inhibit-style-tags
782 (assoc (match-string 1) muse-publish-markup-tags
)
783 (muse-markup-tag-info (match-string 1)))))
785 (not (get-text-property (match-beginning 0) 'read-only
)))
786 (let ((closed-tag (match-string 3))
787 (start (match-beginning 0))
790 (when (nth 2 tag-info
)
791 (let ((attrstr (match-string 2)))
793 (string-match (concat "\\([^"
798 (let ((attr (cons (downcase
799 (muse-match-string-no-properties 1 attrstr
))
800 (muse-match-string-no-properties 3 attrstr
))))
801 (setq attrstr
(replace-match "" t t attrstr
))
803 (nconc attrs
(list attr
))
804 (setq attrs
(list attr
)))))))
805 (if (and (cadr tag-info
) (not closed-tag
))
806 (if (muse-publish-goto-tag-end (car tag-info
) (nth 3 tag-info
))
807 (delete-region (match-beginning 0) (point))
808 (setq tag-info nil
)))
810 (setq end
(point-marker))
811 (delete-region start beg
)
813 (let ((args (list start end
)))
815 (nconc args
(list attrs
)))
816 (let ((muse-inhibit-style-tags nil
))
817 ;; remove the inhibition
818 (apply (nth 4 tag-info
) args
)))))))
821 (defun muse-publish-escape-specials (beg end
&optional ignore-read-only context
)
822 "Escape specials from BEG to END using style-specific :specials.
823 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
824 CONTEXT is used to figure out what kind of specials to escape.
826 The following contexts exist in Muse.
827 'underline _underlined text_
828 'literal =monospaced text= or <code> region (monospaced, escaped)
829 'emphasis *emphasized text*
830 'email email@example.com
831 'url http://example.com
832 'url-desc [[...][description of an explicit link]]
834 'example <example> region (monospaced, block context, escaped)
835 'verbatim <verbatim> region (escaped)
836 'document normal text"
837 (let ((specials (muse-style-element :specials nil t
)))
838 (cond ((functionp specials
)
839 (setq specials
(funcall specials context
)))
841 (setq specials
(symbol-value specials
))))
842 (if (functionp specials
)
843 (funcall specials beg end ignore-read-only
)
846 (narrow-to-region beg end
)
847 (goto-char (point-min))
848 (while (< (point) (point-max))
849 (if (and (not ignore-read-only
)
850 (get-text-property (point) 'read-only
))
851 (goto-char (or (next-single-property-change (point) 'read-only
)
853 (let ((repl (or (assoc (char-after) specials
)
855 muse-publish-markup-specials
))))
859 (insert-before-markers (cdr repl
)))))))))))
861 (defun muse-publish-markup-word ()
862 (let* ((beg (match-beginning 2))
863 (end (1- (match-end 2)))
864 (leader (buffer-substring-no-properties beg end
))
865 open-tag close-tag mark-read-only loc context
)
867 ((string= leader
"_")
868 (setq context
'underline
869 open-tag
(muse-markup-text 'begin-underline
)
870 close-tag
(muse-markup-text 'end-underline
)))
871 ((string= leader
"=")
872 (setq context
'literal
873 open-tag
(muse-markup-text 'begin-literal
)
874 close-tag
(muse-markup-text 'end-literal
))
875 (setq mark-read-only t
))
877 (let ((l (length leader
)))
878 (setq context
'emphasis
)
880 ((= l
1) (setq open-tag
(muse-markup-text 'begin-emph
)
881 close-tag
(muse-markup-text 'end-emph
)))
882 ((= l
2) (setq open-tag
(muse-markup-text 'begin-more-emph
)
883 close-tag
(muse-markup-text 'end-more-emph
)))
884 ((= l
3) (setq open-tag
(muse-markup-text 'begin-most-emph
)
885 close-tag
(muse-markup-text 'end-most-emph
)))
886 (t (setq context nil
))))))
888 (not (get-text-property beg
'muse-link
))
889 (setq loc
(search-forward leader nil t
))
890 (or (eobp) (not (eq (char-syntax (char-after loc
)) ?w
)))
891 (not (eq (char-syntax (char-before (point))) ?\
))
892 (not (get-text-property (point) 'muse-link
)))
895 (delete-region beg end
)
896 (setq end
(point-marker))
897 (muse-insert-markup close-tag
)
899 (muse-insert-markup open-tag
)
902 (muse-publish-escape-specials beg end t context
)
903 (muse-publish-mark-read-only beg end
)))
907 (defun muse-publish-markup-emdash ()
908 (unless (get-text-property (match-beginning 0) 'muse-link
)
909 (let ((prespace (match-string 1))
910 (postspace (match-string 2)))
911 (delete-region (match-beginning 0) (match-end 0))
912 (muse-insert-markup (muse-markup-text 'emdash prespace postspace
))
913 (when (eq (char-after) ?\
<)
916 (defun muse-publish-markup-enddots ()
917 (unless (get-text-property (match-beginning 0) 'muse-link
)
918 (delete-region (match-beginning 0) (match-end 0))
919 (muse-insert-markup (muse-markup-text 'enddots
))))
921 (defun muse-publish-markup-dots ()
922 (unless (get-text-property (match-beginning 0) 'muse-link
)
923 (delete-region (match-beginning 0) (match-end 0))
924 (muse-insert-markup (muse-markup-text 'dots
))))
926 (defun muse-publish-markup-rule ()
927 (unless (get-text-property (match-beginning 0) 'muse-link
)
928 (delete-region (match-beginning 0) (match-end 0))
929 (muse-insert-markup (muse-markup-text 'rule
))))
931 (defun muse-publish-markup-no-break-space ()
932 (unless (get-text-property (match-beginning 0) 'muse-link
)
933 (delete-region (match-beginning 0) (match-end 0))
934 (muse-insert-markup (muse-markup-text 'no-break-space
))))
936 (defun muse-publish-markup-heading ()
937 (let* ((len (length (match-string 1)))
938 (start (muse-markup-text
939 (cond ((= len
1) 'section
)
940 ((= len
2) 'subsection
)
941 ((= len
3) 'subsubsection
)
944 (end (muse-markup-text
945 (cond ((= len
1) 'section-end
)
946 ((= len
2) 'subsection-end
)
947 ((= len
3) 'subsubsection-end
)
948 (t 'section-other-end
))
950 (delete-region (match-beginning 0) (match-end 0))
951 (muse-insert-markup start
)
954 (muse-insert-markup end
))
955 (muse-publish-section-close len
)))
957 (defvar muse-publish-footnotes nil
)
959 (defun muse-publish-markup-footnote ()
960 "Scan ahead and snarf up the footnote body"
962 ((get-text-property (match-beginning 0) 'muse-link
)
964 ((= (muse-line-beginning-position) (match-beginning 0))
967 (let ((footnote (save-match-data
968 (string-to-number (match-string 1))))
970 (delete-region (match-beginning 0) (match-end 0))
972 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote
) nil t
)
973 (let* ((start (match-beginning 0))
974 (beg (goto-char (match-end 0)))
976 (if (search-forward "\n\n" nil t
)
977 (copy-marker (match-beginning 0))
978 (goto-char (point-max))
979 (skip-chars-backward "\n")
981 (while (re-search-forward
982 (concat "^[" muse-regexp-blank
"]+\\([^\n]\\)")
984 (replace-match "\\1" t
))
985 (let ((footnotemark-cmd (muse-markup-text 'footnotemark
))
986 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end
)))
987 (if (string= "" footnotemark-cmd
)
989 (concat (muse-markup-text 'footnote
)
990 (buffer-substring-no-properties beg end
)
991 (muse-markup-text 'footnote-end
)))
992 (setq footnotemark
(format footnotemark-cmd footnote
993 footnotemark-end-cmd
))
994 (unless muse-publish-footnotes
995 (set (make-local-variable 'muse-publish-footnotes
)
996 (make-vector 256 nil
)))
997 (unless (aref muse-publish-footnotes footnote
)
1001 (concat (format (muse-markup-text 'footnotetext
)
1003 (buffer-substring-no-properties beg end
)
1004 (muse-markup-text 'footnotetext-end
))))
1005 (aset muse-publish-footnotes footnote footnotemark
))))
1007 (skip-chars-forward "\n")
1008 (delete-region start
(point)))))
1009 (muse-insert-markup (or footnotemark footnote
))))))
1011 (defun muse-publish-markup-fn-sep ()
1012 (delete-region (match-beginning 0) (match-end 0))
1013 (muse-insert-markup (muse-markup-text 'fn-sep
)))
1015 (defun muse-insert-markup-end-list (&rest args
)
1016 (let ((beg (point)))
1017 (apply 'insert args
)
1018 (add-text-properties beg
(point) '(end-list t
))
1019 (muse-publish-mark-read-only beg
(point))))
1021 (defun muse-publish-surround-dl (indent post-indent
)
1022 (let* ((beg-item (muse-markup-text 'begin-dl-item
))
1023 (end-item (muse-markup-text 'end-dl-item
))
1024 (beg-ddt (muse-markup-text 'begin-ddt
)) ;; term
1025 (end-ddt (muse-markup-text 'end-ddt
))
1026 (beg-dde (muse-markup-text 'begin-dde
)) ;; definition
1027 (end-dde (muse-markup-text 'end-dde
))
1029 def-on-same-line beg
)
1031 ;; envelope this as one term+definitions unit -- HTML does not
1032 ;; need this, but DocBook and Muse's custom XML format do
1033 (muse-insert-markup beg-item
)
1034 (when (looking-at muse-dl-term-regexp
)
1035 ;; find the term and wrap it with published markup
1037 (goto-char (match-end 1))
1038 (delete-region (point) (match-end 0))
1039 (muse-insert-markup-end-list end-ddt
)
1040 ;; if definition is immediately after term, move to next line
1041 (unless (eq (char-after) ?
\n)
1045 (delete-region (point) (match-beginning 1))
1046 (muse-insert-markup beg-ddt
)))
1048 ;; move past current item
1049 continue
(muse-forward-list-item 'dl-term indent
))
1051 (narrow-to-region beg
(point))
1052 (goto-char (point-min))
1053 ;; publish each definition that we find, defaulting to an
1054 ;; empty definition if none are found
1055 (muse-publish-surround-text beg-dde end-dde
1057 (muse-forward-list-item 'dl-entry indent
))
1059 (goto-char (point-max))
1060 (skip-chars-backward (concat muse-regexp-blank
"\n"))
1061 (muse-insert-markup-end-list end-item
)
1063 (goto-char (point-max)))))))
1065 (defun muse-publish-surround-text (beg-tag end-tag move-func
&optional indent post-indent determine-indent list-item
)
1067 (setq list-item
(format muse-list-item-regexp
1068 (concat "[" muse-regexp-blank
"]*"))))
1070 (empty-line (concat "^[" muse-regexp-blank
"]*\n"))
1073 (setq indent
(concat "[" muse-regexp-blank
"]+")))
1075 (setq post-indent
(concat " \\{0," (number-to-string post-indent
)
1077 (setq post-indent
""))
1079 (muse-insert-markup beg-tag
)
1081 ;; move past current item; continue is non-nil if there
1082 ;; are more like items to be processed
1083 continue
(funcall move-func indent
))
1085 (when determine-indent
1086 ;; if the caller doesn't know how much indentation
1087 ;; to use, figure it out ourselves
1091 ;; snarf all leading whitespace
1092 (let ((this-indent (and (match-beginning 2)
1093 (buffer-substring (match-beginning 1)
1094 (match-beginning 2)))))
1095 (when (and this-indent
1096 (not (string= this-indent
"")))
1097 (setq indent this-indent
1098 determine-indent nil
))))))
1100 ;; remove list markup if we encountered another item of the
1102 (replace-match "" t t nil
1))
1103 (narrow-to-region beg
(point))
1104 ;; narrow to current item
1105 (goto-char (point-min))
1107 (let ((list-nested nil
)
1109 (post-indent post-indent
))
1110 (while (< (point) (point-max))
1111 (when (and (looking-at list-item
)
1112 (not (or (get-text-property
1113 (muse-list-item-critical-point) 'read-only
)
1115 (muse-list-item-critical-point) 'muse-link
))))
1116 ;; if we encounter a list item, allow no post-indent
1118 (setq list-nested t
))
1119 (when (and (not (looking-at empty-line
))
1120 (looking-at (concat indent
"\\("
1121 (or (and list-nested
"")
1124 ;; if list is not nested, remove indentation
1125 (unless indent-found
1126 (setq post-indent
(match-string 1)
1130 (skip-chars-backward (concat muse-regexp-blank
"\n"))
1131 (muse-insert-markup-end-list end-tag
)
1133 (goto-char (point-max)))))))
1135 (defun muse-publish-markup-list ()
1136 "Markup a list entry.
1137 This function works by marking up items of the same list level
1138 and type, respecting the end-of-list property."
1139 (let* ((str (match-string 1))
1140 (type (muse-list-item-type str
))
1141 (indent (buffer-substring (muse-line-beginning-position)
1142 (match-beginning 1)))
1143 (post-indent (length str
))
1144 (last (match-beginning 0)))
1146 ((or (get-text-property (muse-list-item-critical-point) 'read-only
)
1147 (get-text-property (muse-list-item-critical-point) 'muse-link
))
1150 (unless (eq (char-after (match-end 1)) ?-
)
1151 (delete-region (match-beginning 0) (match-end 0))
1152 (muse-insert-markup (muse-markup-text 'begin-uli
))
1154 (muse-publish-surround-text
1155 (muse-markup-text 'begin-uli-item
)
1156 (muse-markup-text 'end-uli-item
)
1158 (muse-forward-list-item 'ul indent
))
1160 (muse-insert-markup-end-list (muse-markup-text 'end-uli
)))
1163 (delete-region (match-beginning 0) (match-end 0))
1164 (muse-insert-markup (muse-markup-text 'begin-oli
))
1166 (muse-publish-surround-text
1167 (muse-markup-text 'begin-oli-item
)
1168 (muse-markup-text 'end-oli-item
)
1170 (muse-forward-list-item 'ol indent
))
1172 (muse-insert-markup-end-list (muse-markup-text 'end-oli
)))
1174 ((not (string= (match-string 2) ""))
1175 ;; must have an initial term
1176 (goto-char (match-beginning 0))
1177 (muse-insert-markup (muse-markup-text 'begin-dl
))
1179 (muse-publish-surround-dl indent post-indent
)
1180 (muse-insert-markup-end-list (muse-markup-text 'end-dl
)))
1184 (defun muse-publish-markup-quote ()
1185 "Markup a quoted paragraph.
1186 The reason this function is so funky, is to prevent text properties
1187 like read-only from being inadvertently deleted."
1188 (let* ((ws (match-string 1))
1189 (centered (>= (string-width ws
) 6))
1190 (begin-elem (if centered
'begin-center
'begin-quote-item
))
1191 (end-elem (if centered
'end-center
'end-quote-item
)))
1192 (replace-match "" t t nil
1)
1194 (muse-insert-markup (muse-markup-text 'begin-quote
)))
1195 (muse-publish-surround-text (muse-markup-text begin-elem
)
1196 (muse-markup-text end-elem
)
1197 (function (lambda (indent)
1198 (muse-forward-paragraph)
1201 (muse-insert-markup (muse-markup-text 'end-quote
)))))
1203 (defun muse-publish-markup-leading-space (markup-space multiple
)
1205 (when (and markup-space
1206 (>= (setq count
(skip-chars-forward " ")) 0))
1207 (delete-region (muse-line-beginning-position) (point))
1209 (muse-insert-markup markup-space
)
1210 (setq count
(- count multiple
))))))
1212 (defun muse-publish-markup-verse ()
1213 (let ((leader (match-string 0)))
1214 (goto-char (match-beginning 0))
1215 (muse-insert-markup (muse-markup-text 'begin-verse
))
1216 (while (looking-at leader
)
1218 (muse-publish-markup-leading-space (muse-markup-text 'verse-space
) 2)
1219 (let ((beg (point)))
1223 (let ((text (muse-markup-text 'empty-verse-line
)))
1224 (when text
(muse-insert-markup text
))))
1228 (or (looking-at (concat leader
"["
1231 (not (looking-at leader
)))))
1232 (let ((begin-text (muse-markup-text 'begin-last-stanza-line
))
1233 (end-text (muse-markup-text 'end-last-stanza-line
)))
1234 (when end-text
(muse-insert-markup end-text
))
1236 (when begin-text
(muse-insert-markup begin-text
))
1239 (let ((begin-text (muse-markup-text 'begin-verse-line
))
1240 (end-text (muse-markup-text 'end-verse-line
)))
1241 (when end-text
(muse-insert-markup end-text
))
1243 (when begin-text
(muse-insert-markup begin-text
))
1246 (muse-insert-markup (muse-markup-text 'end-verse
))
1249 (defun muse-publish-table-fields (beg end
)
1250 "Parse given region as a table, returning a cons cell.
1251 The car is the length of the longest row.
1253 The cdr is a list of the fields of the table, with the first
1254 element indicating the type of the row:
1255 1: body, 2: header, 3: footer.
1257 The existing region will be removed, except for initial blank lines."
1258 (unless (muse-publishing-directive "disable-tables")
1263 (narrow-to-region beg end
)
1264 (goto-char (point-min))
1265 (while (looking-at (concat "^[" muse-regexp-blank
"]*$"))
1269 (when (looking-at muse-table-line-regexp
)
1270 (setq fields
(cons (length (match-string 1))
1271 (mapcar #'muse-trim-whitespace
1272 (split-string (match-string 0)
1273 muse-table-field-regexp
)))
1274 field-list
(cons fields field-list
)
1275 longest
(max (length fields
) longest
)))
1276 (setq left
(forward-line 1))))
1277 (delete-region beg end
)
1280 (cons (1- longest
) (nreverse field-list
))))))
1282 (defun muse-publish-markup-table ()
1283 "Style does not support tables.")
1285 (defun muse-publish-escape-specials-in-string (string &optional context
)
1286 "Escape specials in STRING using style-specific :specials.
1287 CONTEXT is used to figure out what kind of specials to escape.
1289 See the documentation of the `muse-publish-escape-specials'
1290 function for the list of available contexts."
1293 (let ((specials (muse-style-element :specials nil t
)))
1294 (cond ((functionp specials
)
1295 (setq specials
(funcall specials context
)))
1297 (setq specials
(symbol-value specials
))))
1298 (if (functionp specials
)
1299 (funcall specials string
)
1300 (apply (function concat
)
1303 (let ((repl (or (assoc ch specials
)
1304 (assoc ch muse-publish-markup-specials
))))
1308 (append string nil
))))))
1310 (defun muse-publish-markup-email ()
1311 (let* ((beg (match-end 1))
1312 (addr (buffer-substring-no-properties beg
(match-end 0))))
1313 (setq addr
(muse-publish-escape-specials-in-string addr
'email
))
1315 (delete-region beg
(match-end 0))
1316 (if (or (eq (char-before (match-beginning 0)) ?
\")
1317 (eq (char-after (match-end 0)) ?
\"))
1319 (insert (format (muse-markup-text 'email-addr
) addr addr
)))
1320 (muse-publish-mark-read-only beg
(point))))
1322 (defun muse-publish-classify-url (target)
1323 "Transform anchors and get published name, if TARGET is a page.
1324 The return value is a cons cell. The car is the type of link,
1325 the cadr is the page name, and the cddr is the anchor."
1327 (cond ((or (null target
) (string= target
""))
1329 ((string-match muse-url-regexp target
)
1330 (cons 'url
(cons target nil
)))
1331 ((string-match muse-image-regexp target
)
1332 (cons 'image
(cons target nil
)))
1333 ((string-match muse-file-regexp target
)
1334 (cons 'file
(cons target nil
)))
1335 ((string-match "#" target
)
1336 (if (eq (aref target
0) ?\
#)
1337 (cons 'anchor-ref
(cons nil
(substring target
1)))
1338 (cons 'link-and-anchor
1339 (cons (muse-publish-link-page
1340 (substring target
0 (match-beginning 0)))
1341 (substring target
(match-end 0))))))
1343 (cons 'link
(cons (muse-publish-link-page target
) nil
))))))
1345 (defun muse-publish-url-desc (desc explicit
)
1347 (dolist (transform muse-publish-desc-transforms
)
1348 (setq desc
(save-match-data
1349 (when desc
(funcall transform desc explicit
)))))
1350 (setq desc
(muse-link-unescape desc
))
1351 (muse-publish-escape-specials-in-string desc
'url-desc
)))
1353 (defun muse-publish-url (url &optional desc orig-url explicit
)
1354 "Resolve a URL into its final <a href> form."
1356 (dolist (transform muse-publish-url-transforms
)
1357 (setq url
(save-match-data (when url
(funcall transform url explicit
)))))
1359 (setq desc
(muse-publish-url-desc desc explicit
))
1361 (setq orig-url
(muse-publish-url-desc orig-url explicit
))))
1362 (let ((target (muse-publish-classify-url url
)))
1363 (setq type
(car target
)
1364 url
(if (eq type
'image
)
1365 (muse-publish-escape-specials-in-string (cadr target
)
1367 (muse-publish-escape-specials-in-string (cadr target
) 'url
))
1368 anchor
(muse-publish-escape-specials-in-string
1369 (cddr target
) 'url
)))
1370 (cond ((eq type
'anchor-ref
)
1371 (muse-markup-text 'anchor-ref anchor
(or desc orig-url
)))
1375 (let ((ext (or (file-name-extension url
) "")))
1376 (setq url
(muse-path-sans-extension url
))
1378 (muse-markup-text 'image-with-desc url ext desc
)
1379 (muse-markup-text 'image url ext
))))
1380 ((eq type
'link-and-anchor
)
1381 (muse-markup-text 'link-and-anchor url anchor
1382 (or desc orig-url
)))
1383 ((and desc
(string-match muse-image-regexp desc
))
1384 (let ((ext (or (file-name-extension desc
) "")))
1385 (setq desc
(muse-path-sans-extension desc
))
1386 (muse-markup-text 'image-link url desc ext
)))
1388 (muse-markup-text 'link url
(or desc orig-url
)))
1391 (let ((text (muse-markup-text 'url-and-desc url desc
)))
1392 (and (not (string= text
""))
1394 (muse-markup-text 'url url
(or desc orig-url
)))))))
1396 (defun muse-publish-insert-url (url &optional desc orig-url explicit
)
1397 "Resolve a URL into its final <a href> form."
1398 (delete-region (match-beginning 0) (match-end 0))
1399 (let ((text (muse-publish-url url desc orig-url explicit
)))
1401 (muse-insert-markup text
))))
1403 (defun muse-publish-markup-link ()
1404 (let (desc explicit orig-link link
)
1405 (setq explicit
(save-match-data
1406 (if (string-match muse-explicit-link-regexp
1409 (setq orig-link
(if explicit
(match-string 1) (match-string 0)))
1410 (setq desc
(when explicit
(match-string 2)))
1411 (setq link
(if explicit
1412 (muse-handle-explicit-link orig-link
)
1413 (muse-handle-implicit-link orig-link
)))
1416 (not (or (eq (char-before (match-beginning 0)) ?
\")
1417 (eq (char-after (match-end 0)) ?
\")))))
1418 ;; if explicit link has no user-provided description, treat it
1419 ;; as if it were an implicit link
1420 (when (and explicit
(not desc
))
1421 (setq explicit nil
))
1422 (muse-publish-insert-url link desc orig-link explicit
))))
1424 (defun muse-publish-markup-url ()
1425 (unless (or (eq (char-before (match-beginning 0)) ?
\")
1426 (eq (char-after (match-end 0)) ?
\"))
1427 (let ((url (match-string 0)))
1428 (muse-publish-insert-url url nil url
))))
1430 ;; Default publishing tags
1432 (defcustom muse-publish-contents-depth
2
1433 "The number of heading levels to include with <contents> tags."
1435 :group
'muse-publish
)
1437 (defun muse-publish-contents-tag (beg end attrs
)
1438 (set (make-local-variable 'muse-publish-generate-contents
)
1439 (cons (copy-marker (point) t
)
1440 (let ((depth (cdr (assoc "depth" attrs
))))
1441 (or (and depth
(string-to-number depth
))
1442 muse-publish-contents-depth
)))))
1444 (defun muse-publish-verse-tag (beg end
)
1447 (narrow-to-region beg end
)
1448 (goto-char (point-min))
1449 (while (eq ?\
(char-syntax (char-after)))
1451 (while (< (point) (point-max))
1454 (if (eq ?\
(char-syntax (char-before)))
1455 (delete-char -
1)))))
1457 (defun muse-publish-mark-read-only (beg end
)
1458 "Add read-only properties to the given region."
1459 (add-text-properties beg end
'(rear-nonsticky (read-only) read-only t
))
1462 (defun muse-publish-mark-link (&optional beg end
)
1463 "Indicate that the given region is a Muse link, so that other
1464 markup elements respect it. If a region is not specified, use
1465 the 0th match data to determine it.
1467 This is usually applied to explicit links."
1468 (unless beg
(setq beg
(match-beginning 0)))
1469 (unless end
(setq end
(match-end 0)))
1470 (add-text-properties beg end
'(muse-link t
))
1473 (defun muse-publish-quote-tag (beg end
)
1476 (narrow-to-region beg end
)
1477 (let ((quote-regexp "^\\(<\\(/?\\)quote>\\)"))
1478 (muse-insert-markup (muse-markup-text 'begin-quote
))
1480 (unless (looking-at (concat "[" muse-regexp-blank
"\n]*"
1482 (muse-publish-surround-text
1483 (muse-markup-text 'begin-quote-item
)
1484 (muse-markup-text 'end-quote-item
)
1487 (muse-forward-paragraph)
1488 (goto-char (match-end 0))
1489 (and (< (point) (point-max))
1490 (not (looking-at quote-regexp
)))))
1493 (if (>= (point) (point-max))
1495 (and (search-forward "<quote>" nil t
)
1496 (muse-publish-goto-tag-end "quote" t
)
1497 (progn (forward-line 1) t
)
1498 (< (point) (point-max))))))
1499 (goto-char (point-max))
1500 (muse-insert-markup (muse-markup-text 'end-quote
))))))
1502 (defun muse-publish-code-tag (beg end
)
1503 (muse-publish-escape-specials beg end nil
'literal
)
1505 (insert (muse-markup-text 'begin-literal
))
1507 (insert (muse-markup-text 'end-literal
))
1508 (muse-publish-mark-read-only beg
(point)))
1510 (defun muse-publish-example-tag (beg end
)
1511 (muse-publish-escape-specials beg end nil
'example
)
1513 (insert (muse-markup-text 'begin-example
))
1515 (insert (muse-markup-text 'end-example
))
1516 (muse-publish-mark-read-only beg
(point)))
1518 (defun muse-publish-verbatim-tag (beg end
)
1519 (muse-publish-escape-specials beg end nil
'verbatim
)
1520 (muse-publish-mark-read-only beg end
))
1522 (defalias 'muse-publish-class-tag
'ignore
)
1524 (defun muse-publish-examplify-buffer ()
1525 "Transform the current buffer as if it were an <example> region."
1526 (let ((end (progn (goto-char (point-max)) (point-marker))))
1527 (muse-publish-example-tag (point-min) end
)))
1529 (defun muse-publish-versify-buffer ()
1530 "Transform the current buffer as if it were a <verse> region."
1531 (muse-publish-verse-tag (point-min) (point-max))
1532 (muse-publish-markup ""
1533 `((100 ,(concat "^[" muse-regexp-blank
"]*> ") 0
1534 muse-publish-markup-verse
)))
1535 (goto-char (point-min)))
1537 (defmacro muse-publish-markup-attribute
(beg end attrs reinterp
&rest body
)
1538 "Evaluate BODY within the bounds of BEG and END.
1539 ATTRS is an alist. Only the \"markup\" element of ATTRS is acted
1542 If it is omitted, publish the region with the normal Muse rules.
1543 If RE-INTERP is specified, this is done immediately in a new
1544 publishing process. Currently, RE-INTERP is specified only by
1547 If \"nil\", do not mark up the region at all, but prevent it from
1548 being further interpreted by Muse.
1550 If \"example\", treat the region as if it was surrounded by the
1553 If \"verse\", treat the region as if it was surrounded by the
1554 <verse> tag, to preserve newlines.
1556 Otherwise, it should be the name of a function to call in the
1557 narrowed region after evaluating BODY."
1558 (let ((markup (make-symbol "markup"))
1559 (markup-function (make-symbol "markup-function")))
1560 `(let ((,markup
(cdr (assoc "markup" ,attrs
))))
1562 (narrow-to-region ,beg
,end
)
1563 (goto-char (point-min))
1567 (muse-publish-markup-region (point-min) (point-max))
1568 (muse-publish-mark-read-only (point-min) (point-max))
1569 (goto-char (point-max)))
1570 (let ((,markup-function
(read ,markup
)))
1571 (cond ((eq ,markup-function
'example
)
1572 (setq ,markup-function
#'muse-publish-examplify-buffer
))
1573 ((eq ,markup-function
'verse
)
1574 (setq ,markup-function
#'muse-publish-versify-buffer
))
1575 ((and ,markup-function
(not (functionp ,markup-function
)))
1576 (error "Invalid markup function `%s'" ,markup
))
1578 (if ,markup-function
1579 (funcall ,markup-function
)
1580 (muse-publish-mark-read-only (point-min) (point-max))
1581 (goto-char (point-max)))))))))
1583 (put 'muse-publish-markup-attribute
'lisp-indent-function
4)
1584 (put 'muse-publish-markup-attribute
'edebug-form-spec
1585 '(form form form form body
))
1587 (defun muse-publish-lisp-tag (beg end attrs
)
1588 (muse-publish-markup-attribute beg end attrs nil
1590 (let ((str (muse-eval-lisp
1593 (buffer-substring-no-properties (point-min)
1596 (delete-region beg end
)))))
1597 (set-text-properties 0 (length str
) nil str
)
1600 (defun muse-publish-command-tag (beg end attrs
)
1601 (muse-publish-markup-attribute beg end attrs nil
1602 (while (looking-at "\\s-*$")
1604 (let ((interp (cdr (assoc "interp" attrs
))))
1606 (shell-command-on-region (point) (point-max) interp t t
)
1609 (buffer-substring-no-properties (point) (point-max))
1610 (delete-region (point-min) (point-max)))
1612 ;; make sure there is a newline at end
1613 (goto-char (point-max))
1615 (unless (looking-at "\\s-*$")
1616 (goto-char (point-max))
1618 (goto-char (point-min))))
1620 (defun muse-publish-perl-tag (beg end attrs
)
1621 (muse-publish-command-tag beg end
1622 (cons (cons "interp" (executable-find "perl"))
1625 (defun muse-publish-python-tag (beg end attrs
)
1626 (muse-publish-command-tag beg end
1627 (cons (cons "interp" (executable-find "python"))
1630 (defun muse-publish-ruby-tag (beg end attrs
)
1631 (muse-publish-command-tag beg end
1632 (cons (cons "interp" (executable-find "ruby"))
1635 (defun muse-publish-comment-tag (beg end
)
1636 (if (null muse-publish-comments-p
)
1637 (delete-region beg end
)
1639 (muse-insert-markup (muse-markup-text 'comment-end
))
1640 (muse-publish-mark-read-only beg end
)
1642 (muse-insert-markup (muse-markup-text 'comment-begin
))))
1644 (defun muse-publish-include-tag (beg end attrs
)
1645 "Include the named file at the current location during publishing.
1647 <include file=\"...\" markup=\"...\">
1649 The `markup' attribute controls how this file is marked up after
1650 being inserted. See `muse-publish-markup-attribute' for an
1651 explanation of how it works."
1652 (let ((filename (cdr (assoc "file" attrs
)))
1653 (muse-publishing-directives muse-publishing-directives
))
1655 (setq filename
(expand-file-name
1657 (file-name-directory muse-publishing-current-file
)))
1658 (error "No file attribute specified in <include> tag"))
1659 (muse-publish-markup-attribute beg end attrs t
1660 (insert-file-contents filename
))))
1662 (defun muse-publish-mark-up-tag (beg end attrs
)
1663 "Run an Emacs Lisp function on the region delimted by this tag.
1665 <markup function=\"...\">
1667 The optional `function' attribute controls how this section is
1668 marked up. If used, it should be the name of a function to call
1669 with the buffer narrowed to the delimited region. Note that no
1670 further marking-up will be performed on this region.
1672 If `function' is ommitted, use the standard Muse markup function.
1673 This is useful for marking up content in headers and footers."
1674 (let ((function (cdr (assoc "function" attrs
)))
1675 (muse-publishing-directives muse-publishing-directives
))
1677 (let ((markup-function (intern function
)))
1678 (if (and markup-function
(functionp markup-function
))
1680 (narrow-to-region beg end
)
1681 (funcall markup-function
)
1682 (goto-char (point-max)))
1683 (error "Invalid markup function `%s'" function
)))
1684 (muse-publish-markup-region beg end
))
1685 (muse-publish-mark-read-only beg
(point))))
1687 ;; Miscellaneous helper functions
1689 (defun muse-publish-strip-tags (string)
1690 "Remove all tags from the string."
1691 (while (string-match "<.*?>" string
)
1692 (setq string
(replace-match "" nil t string
)))
1695 (defun muse-publish-markup-type (category default-func
)
1696 (let ((rule (muse-find-markup-element :overrides category
(muse-style))))
1697 (funcall (or rule default-func
))))
1699 (defun muse-published-buffer-contents (buffer)
1700 (with-current-buffer buffer
1701 (goto-char (point-min))
1702 (let ((beg (and (search-forward "Emacs Muse begins here")
1703 (muse-line-end-position)))
1704 (end (and (search-forward "Emacs Muse ends here")
1705 (muse-line-beginning-position))))
1706 (buffer-substring-no-properties beg end
))))
1708 (defun muse-published-contents (file)
1709 (when (file-readable-p file
)
1710 (muse-with-temp-buffer
1711 (insert-file-contents file
)
1712 (muse-published-buffer-contents (current-buffer)))))
1714 (defun muse-publish-transform-output
1715 (file temp-file output-path name gen-func
&rest cleanup-exts
)
1716 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1717 (setq file
(muse-page-name file
))
1718 (message "Generating %s output for %s..." name file
)
1719 (if (not (funcall gen-func temp-file output-path
))
1720 (message "Generating %s from %s...failed" name file
)
1721 (message "Generating %s output for %s...done" name file
)
1722 (muse-delete-file-if-exists temp-file
)
1723 (dolist (ext cleanup-exts
)
1724 (muse-delete-file-if-exists
1725 (expand-file-name (concat file ext
)
1726 (file-name-directory output-path
))))
1727 (message "Wrote %s" output-path
)))
1729 (defun muse-publish-read-only (string)
1730 (let ((end (1- (length string
))))
1731 (add-text-properties 0 end
1732 '(rear-nonsticky (read-only) read-only t
)
1736 (provide 'muse-publish
)
1738 ;;; muse-publish.el ends here