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 muse-publish-contents-tag
)
243 ("verse" t nil muse-publish-verse-tag
)
244 ("example" t nil muse-publish-example-tag
)
245 ("code" t nil muse-publish-code-tag
)
246 ("quote" t nil muse-publish-quote-tag
)
247 ("literal" t nil muse-publish-mark-read-only
)
248 ("verbatim" t nil muse-publish-verbatim-tag
)
249 ("lisp" t nil muse-publish-lisp-tag
)
250 ("class" t t muse-publish-class-tag
)
251 ("command" t t muse-publish-command-tag
)
252 ("comment" t nil muse-publish-comment-tag
)
253 ("include" nil t muse-publish-include-tag
)
254 ("markup" t t muse-publish-mark-up-tag
))
255 "A list of tag specifications, for specially marking up text.
256 XML-style tags are the best way to add custom markup to Muse.
257 This is easily accomplished by customizing this list of markup tags.
259 For each entry, the name of the tag is given, whether it expects
260 a closing tag and/or an optional set of attributes, and a
261 function that performs whatever action is desired within the
264 The tags themselves are deleted during publishing, before the
265 function is called. The function is called with three arguments,
266 the beginning and end of the region surrounded by the tags. If
267 properties are allowed, they are passed as a third argument in
268 the form of an alist. The `end' argument to the function is
271 Point is always at the beginning of the region within the tags, when
272 the function is called. Wherever point is when the function finishes
273 is where tag markup will resume.
275 These tag rules are processed once at the beginning of markup, and
276 once at the end, to catch any tags which may have been inserted
278 :type
'(repeat (list (string :tag
"Markup tag")
279 (boolean :tag
"Expect closing tag" :value t
)
280 (boolean :tag
"Parse attributes" :value nil
)
282 :group
'muse-publish
)
284 (defcustom muse-publish-markup-specials nil
285 "A table of characters which must be represented specially."
286 :type
'(alist :key-type character
:value-type string
)
287 :group
'muse-publish
)
289 (defvar muse-publishing-p nil
290 "Set to t while a page is being published.")
291 (defvar muse-batch-publishing-p nil
292 "Set to t while a page is being batch published.")
293 (defvar muse-publishing-styles nil
294 "The publishing styles that Muse recognizes.
295 This is automatically generated when loading publishing styles.")
296 (defvar muse-publishing-current-file nil
297 "The file that is currently being published.")
298 (defvar muse-publishing-current-output-path nil
299 "The path where the current file will be published to.")
300 (defvar muse-publishing-current-style nil
301 "The style of the file that is currently being published.")
302 (defvar muse-publishing-directives nil
303 "An alist of publishing directives from the top of a file.")
304 (defvar muse-publish-generate-contents nil
305 "Non-nil if a table of contents should be generated.
306 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
307 tell where the <contents> was seen, and to what depth the
308 contents were requested.")
309 (defvar muse-publishing-last-position nil
310 "Last position of the point when publishing.
311 This is used to make sure that publishing doesn't get stalled.")
313 ;; Functions for handling style information
315 (defsubst muse-style
(&optional style
)
316 "Resolve the given STYLE into a Muse style, if it is a string."
318 muse-publishing-current-style
320 (assoc style muse-publishing-styles
)
321 (muse-assert (consp style
))
324 (defun muse-define-style (name &rest elements
)
325 (let ((entry (assoc name muse-publishing-styles
)))
327 (setcdr entry elements
)
328 (setq muse-publishing-styles
329 (cons (append (list name
) elements
)
330 muse-publishing-styles
)))))
332 (defun muse-derive-style (new-name base-name
&rest elements
)
333 (apply 'muse-define-style new-name
334 (append elements
(list :base base-name
))))
336 (defsubst muse-get-keyword
(keyword list
&optional direct
)
337 (let ((value (cadr (memq keyword list
))))
338 (if (and (not direct
) (symbolp value
))
342 (defun muse-style-elements-list (elem &optional style
)
343 "Return a list all references to ELEM in STYLE, including base styles.
344 If STYLE is not specified, use current style."
347 (setq style
(muse-style style
))
348 (setq elements
(append elements
349 (muse-get-keyword elem style
)))
350 (setq style
(muse-get-keyword :base style
)))
353 (defun muse-style-element (elem &optional style direct
)
354 "Search for ELEM in STYLE, including base styles.
355 If STYLE is not specified, use current style."
356 (setq style
(muse-style style
))
357 (let ((value (muse-get-keyword elem style direct
)))
360 (let ((base (muse-get-keyword :base style
)))
362 (muse-style-element elem base direct
))))))
364 (defun muse-find-markup-element (keyword ident style
)
365 (let ((def (assq ident
(muse-style-element keyword style
))))
368 (let ((base (muse-style-element :base style
)))
370 (muse-find-markup-element keyword ident base
))))))
372 (defun muse-markup-text (ident &rest args
)
373 "Insert ARGS into the text markup associated with IDENT.
374 If the markup text has sections like %N%, this will be replaced
375 with the N-1th argument in ARGS. After that, `format' is applied
376 to the text with ARGS as parameters."
377 (let ((text (muse-find-markup-element :strings ident
(muse-style))))
380 (let (start repl-text
)
381 (while (setq start
(string-match "%\\([1-9][0-9]*\\)%" text start
))
382 ;; escape '%' in the argument text, since we will be
383 ;; using format on it
384 (setq repl-text
(muse-replace-regexp-in-string
386 (nth (1- (string-to-number
387 (match-string 1 text
))) args
)
389 start
(+ start
(length repl-text
))
390 text
(replace-match repl-text t t text
))))
391 (apply 'format text args
))
394 (defun muse-insert-markup (&rest args
)
397 (muse-publish-mark-read-only beg
(point))))
399 (defun muse-find-markup-tag (keyword tagname style
)
400 (let ((def (assoc tagname
(muse-style-element keyword style
))))
402 (let ((base (muse-style-element :base style
)))
404 (muse-find-markup-tag keyword tagname base
))))))
406 (defsubst muse-markup-tag-info
(tagname &rest args
)
407 (let ((tag-info (muse-find-markup-tag :tags tagname
(muse-style))))
409 (assoc (match-string 1) muse-publish-markup-tags
))))
411 (defsubst muse-markup-function
(category)
412 (let ((func (muse-find-markup-element :functions category
(muse-style))))
414 (cdr (assq category muse-publish-markup-functions
)))))
416 ;; Publishing routines
418 (defun muse-publish-markup (name rules
)
419 (let* ((case-fold-search nil
)
420 (inhibit-read-only t
)
421 (limit (* (length rules
) (point-max)))
422 (verbose (and muse-publish-report-threshhold
423 (> (point-max) muse-publish-report-threshhold
)))
426 (goto-char (point-min))
427 (let ((regexp (nth 1 (car rules
)))
428 (group (nth 2 (car rules
)))
429 (repl (nth 3 (car rules
)))
431 (setq muse-publishing-last-position nil
)
433 (setq regexp
(symbol-value regexp
)))
434 (if (and verbose
(not muse-batch-publishing-p
))
435 (message "Publishing %s...%d%%" name
436 (* (/ (float (+ (point) base
)) limit
) 100)))
437 (while (and regexp
(setq pos
(re-search-forward regexp nil t
)))
438 (if (and verbose
(not muse-batch-publishing-p
))
439 (message "Publishing %s...%d%%" name
440 (* (/ (float (+ (point) base
)) limit
) 100)))
441 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
442 (match-beginning group
)
443 (get-text-property (match-beginning group
) 'read-only
))
447 (setq func
(muse-markup-function repl
)))
455 (replace-match text t
))))
456 (if (and muse-publishing-last-position
457 (= pos muse-publishing-last-position
))
461 (setq muse-publishing-last-position pos
)))
462 (setq rules
(cdr rules
)
463 base
(+ base
(point-max))))
464 (if (and verbose
(not muse-batch-publishing-p
))
465 (message "Publishing %s...done" name
))))
467 (defcustom muse-publish-markup-header-footer-tags
468 '(("lisp" t nil muse-publish-lisp-tag
)
469 ("markup" t t muse-publish-mark-up-tag
))
470 "Tags used when publishing headers and footers.
471 See `muse-publish-markup-tags' for details."
472 :type
'(repeat (list (string :tag
"Markup tag")
473 (boolean :tag
"Expect closing tag" :value t
)
474 (boolean :tag
"Parse attributes" :value nil
)
476 :group
'muse-publish
)
478 (defun muse-insert-file-or-string (file-or-string &optional title
)
479 (let ((beg (point)) end
)
480 (if (and (not (string-equal file-or-string
""))
481 (not (string-match "\n" file-or-string
))
482 (file-readable-p file-or-string
))
483 (setq end
(+ beg
(cadr (insert-file-contents file-or-string
))))
484 (insert file-or-string
)
487 (narrow-to-region beg end
)
488 (remove-text-properties (point-min) (point-max)
489 '(read-only nil rear-nonsticky nil
))
490 (goto-char (point-min))
491 (let ((muse-inhibit-style-tags t
))
492 (muse-publish-markup (or title
"")
493 '((100 muse-tag-regexp
0
494 muse-publish-markup-tag
)))))))
496 (defun muse-style-run-hooks (keyword style
&rest args
)
499 (setq style
(muse-style style
)))
500 (let ((func (muse-get-keyword keyword style t
)))
504 (setq style
(muse-style-element :base style
)))))
506 (defun muse-publish-markup-region (beg end
&optional title style
)
507 "Apply the given STYLE's markup rules to the given region.
508 TITLE is used when indicating the publishing progress; it may be nil."
509 (unless title
(setq title
""))
511 (or (setq style muse-publishing-current-style
)
512 (error "Cannot find any publishing styles to use")))
514 (narrow-to-region beg end
)
515 (muse-style-run-hooks :before style
)
518 (sort (copy-alist (append muse-publish-markup-regexps
519 (muse-style-elements-list :regexps style
)))
522 (< (car l
) (car r
))))))
523 (muse-style-run-hooks :before-end style
)
524 (muse-publish-escape-specials (point-min) (point-max) nil
'document
)))
526 (defun muse-publish-markup-buffer (title style
)
527 "Apply the given STYLE's markup rules to the current buffer."
528 (setq style
(muse-style style
))
529 (let ((style-header (muse-style-element :header style
))
530 (style-footer (muse-style-element :footer style
))
531 (muse-publishing-current-style style
)
532 (muse-publishing-directives
533 (list (cons "title" title
)
534 (cons "author" (user-full-name))
535 (cons "date" (format-time-string
537 (if muse-publishing-current-file
538 (nth 5 (file-attributes
539 muse-publishing-current-file
))
541 (muse-publishing-p t
)
542 (inhibit-read-only t
))
543 (run-hooks 'muse-update-values-hook
)
544 (run-hooks 'muse-before-publish-hook
)
545 (muse-publish-markup-region (point-min) (point-max) title style
)
546 (goto-char (point-min))
548 (muse-insert-file-or-string style-header title
))
549 (goto-char (point-max))
551 (muse-insert-file-or-string style-footer title
))
552 (muse-style-run-hooks :after style
)
553 (run-hooks 'muse-after-publish-hook
)))
555 (defun muse-publish-markup-string (string &optional style
)
556 "Markup STRING using the given STYLE's markup rules."
557 (setq style
(muse-style style
))
558 (muse-with-temp-buffer
560 (let ((muse-publishing-current-style style
)
561 (muse-publishing-p t
))
562 (muse-publish-markup "*string*" (muse-style-element :rules style
)))
565 ;; Commands for publishing files
567 (defsubst muse-publish-get-style
(&optional styles
)
568 (unless styles
(setq styles muse-publishing-styles
))
569 (if (= 1 (length styles
))
571 (assoc (completing-read "Publish with style: " styles nil t
)
574 (defsubst muse-publish-get-output-dir
(style)
575 (let ((default-directory (or (muse-style-element :path style
)
577 (muse-read-directory-name "Publish to directory: " nil default-directory
)))
579 (defsubst muse-publish-get-info
()
580 (let ((style (muse-publish-get-style)))
581 (list style
(muse-publish-get-output-dir style
)
582 current-prefix-arg
)))
584 (defsubst muse-publish-output-name
(&optional file style
)
585 (setq style
(muse-style style
))
586 (concat (muse-style-element :prefix style
)
587 (muse-page-name file
)
588 (muse-style-element :suffix style
)))
590 (defsubst muse-publish-output-file
(file &optional output-dir style
)
591 (setq style
(muse-style style
))
593 (expand-file-name (muse-publish-output-name file style
) output-dir
)
594 (concat (file-name-directory file
)
595 (muse-publish-output-name file style
))))
597 (defsubst muse-publish-link-name
(&optional file style
)
598 (setq style
(muse-style style
))
599 (concat (muse-style-element :prefix style
)
600 (muse-page-name file
)
601 (or (muse-style-element :link-suffix style
)
602 (muse-style-element :suffix style
))))
604 (defsubst muse-publish-link-file
(file &optional output-dir style
)
605 (setq style
(muse-style style
))
607 (expand-file-name (muse-publish-link-name file style
) output-dir
)
608 (concat (file-name-directory file
)
609 (muse-publish-link-name file style
))))
611 (defsubst muse-publish-link-page
(page)
612 (if (fboundp 'muse-project-link-page
)
613 (muse-project-link-page page
)
614 (muse-publish-link-file page
)))
617 (defun muse-publish-file (file style
&optional output-dir force
)
618 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
619 If the argument FORCE is nil, each file is only published if it is
620 newer than the published version. If the argument FORCE is non-nil,
621 the file is published no matter what."
622 (interactive (cons (read-file-name "Publish file: ")
623 (muse-publish-get-info)))
624 (let ((style-name style
))
625 (setq style
(muse-style style
))
627 (error "There is no style '%s' defined." style-name
)))
628 (let* ((output-path (muse-publish-output-file file output-dir style
))
629 (output-suffix (muse-style-element :osuffix style
))
630 (muse-publishing-current-file file
)
631 (muse-publishing-current-output-path output-path
)
632 (target (if output-suffix
633 (concat (file-name-sans-extension output-path
)
636 (threshhold (nth 7 (file-attributes file
))))
638 (message "Please save %s before publishing" file
)
639 (when (or force
(file-newer-than-file-p file target
))
640 (if (and muse-publish-report-threshhold
642 muse-publish-report-threshhold
))
643 (message "Publishing %s ..." file
))
644 (muse-with-temp-buffer
645 (insert-file-contents file
)
646 (muse-publish-markup-buffer (muse-page-name file
) style
)
647 (let ((backup-inhibited t
))
648 (write-file output-path
))
649 (muse-style-run-hooks :final style file output-path target
))
653 (defun muse-publish-this-file (style output-dir
&optional force
)
654 "Publish the currently-visited file.
655 Prompt for both the STYLE and OUTPUT-DIR if they are not
657 (interactive (muse-publish-get-info))
658 (unless (muse-publish-file buffer-file-name style output-dir force
)
659 (message (concat "The published version is up-to-date; use"
660 " C-u C-c C-T to force an update."))))
662 (defun muse-batch-publish-files ()
663 "Publish Muse files in batch mode."
664 (let ((muse-batch-publishing-p t
)
666 (setq style
(car command-line-args-left
)
667 command-line-args-left
(cdr command-line-args-left
)
668 output-dir
(car command-line-args-left
)
670 (if (string-match "\\`--output-dir=" output-dir
)
672 (substring output-dir
(match-end 0))
673 (setq command-line-args-left
(cdr command-line-args-left
)))))
674 (setq auto-mode-alist
675 (delete (cons (concat "\\." muse-file-extension
"\\'")
676 'muse-mode-choose-mode
)
678 (dolist (file command-line-args-left
)
679 (muse-publish-file file style output-dir t
))))
681 ;; Default publishing rules
683 (defun muse-publish-section-close (depth)
684 "Seach forward for the closing tag of given DEPTH."
687 (while (and (setq not-end
(re-search-forward
688 (concat "^\\*\\{1," (number-to-string depth
)
691 (get-text-property (match-beginning 0) 'read-only
)))
694 (goto-char (point-max)))
695 (cond ((not (eq (char-before) ?
\n))
697 ((not (eq (char-before (1- (point))) ?
\n))
699 (muse-insert-markup (muse-markup-text 'section-close depth
))
702 (defun muse-publish-markup-directive (&optional name value
)
703 (unless name
(setq name
(match-string 1)))
704 (unless value
(setq value
(match-string 2)))
705 (let ((elem (assoc name muse-publishing-directives
)))
708 (setq muse-publishing-directives
709 (cons (cons name value
)
710 muse-publishing-directives
))))
711 ;; Make sure we don't ever try to move the point forward (past the
712 ;; beginning of buffer) while we're still searching for directives.
713 (setq muse-publishing-last-position nil
)
714 (delete-region (match-beginning 0) (match-end 0)))
716 (defsubst muse-publishing-directive
(name)
717 (cdr (assoc name muse-publishing-directives
)))
719 (defun muse-publish-markup-anchor ()
720 (unless (get-text-property (match-end 1) 'muse-link
)
721 (let ((text (muse-markup-text 'anchor
(match-string 2))))
722 (unless (string= text
"")
724 (skip-chars-forward (concat muse-regexp-blank
"\n"))
725 (muse-insert-markup text
)))
728 (defun muse-publish-markup-comment ()
729 (if (null muse-publish-comments-p
)
731 (goto-char (match-end 0))
732 (muse-insert-markup (muse-markup-text 'comment-end
))
733 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
734 (delete-region (match-beginning 0) (match-beginning 1))
735 (goto-char (match-beginning 0))
736 (muse-insert-markup (muse-markup-text 'comment-begin
))))
738 (defvar muse-inhibit-style-tags nil
739 "If non-nil, do not search for style-specific tags.
740 This is used when publishing headers and footers.")
742 (defun muse-publish-markup-tag ()
743 (let ((tag-info (if muse-inhibit-style-tags
744 (assoc (match-string 1) muse-publish-markup-tags
)
745 (muse-markup-tag-info (match-string 1)))))
747 (not (get-text-property (match-beginning 0) 'read-only
)))
748 (let ((closed-tag (match-string 3))
749 (start (match-beginning 0))
752 (when (nth 2 tag-info
)
753 (let ((attrstr (match-string 2)))
755 (string-match (concat "\\([^"
760 (let ((attr (cons (downcase
761 (muse-match-string-no-properties 1 attrstr
))
762 (muse-match-string-no-properties 3 attrstr
))))
763 (setq attrstr
(replace-match "" t t attrstr
))
765 (nconc attrs
(list attr
))
766 (setq attrs
(list attr
)))))))
767 (if (and (cadr tag-info
) (not closed-tag
))
768 (if (search-forward (concat "</" (car tag-info
) ">") nil t
)
769 (delete-region (match-beginning 0) (point))
770 (setq tag-info nil
)))
772 (setq end
(point-marker))
773 (delete-region start beg
)
775 (let ((args (list start end
)))
777 (nconc args
(list attrs
)))
778 (let ((muse-inhibit-style-tags nil
))
779 ;; remove the inhibition
780 (apply (nth 3 tag-info
) args
)))))))
783 (defun muse-publish-escape-specials (beg end
&optional ignore-read-only context
)
784 "Escape specials from BEG to END using style-specific :specials.
785 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
786 CONTEXT is used to figure out what kind of specials to escape.
788 The following contexts exist in Muse.
789 'underline _underlined text_
790 'literal =monospaced text= or <code> region (monospaced, escaped)
791 'emphasis *emphasized text*
792 'email email@example.com
793 'url http://example.com
794 'url-desc [[...][description of an explicit link]]
795 'example <example> region (monospaced, block context, escaped)
796 'verbatim <verbatim> region (escaped)
797 'document normal text"
798 (let ((specials (muse-style-element :specials nil t
)))
799 (cond ((functionp specials
)
800 (setq specials
(funcall specials context
)))
802 (setq specials
(symbol-value specials
))))
803 (if (functionp specials
)
804 (funcall specials beg end ignore-read-only
)
807 (narrow-to-region beg end
)
808 (goto-char (point-min))
809 (while (< (point) (point-max))
810 (if (and (not ignore-read-only
)
811 (get-text-property (point) 'read-only
))
812 (goto-char (or (next-single-property-change (point) 'read-only
)
814 (let ((repl (or (assoc (char-after) specials
)
816 muse-publish-markup-specials
))))
820 (insert-before-markers (cdr repl
)))))))))))
822 (defun muse-publish-markup-word ()
823 (let* ((beg (match-beginning 2))
824 (end (1- (match-end 2)))
825 (leader (buffer-substring-no-properties beg end
))
826 open-tag close-tag mark-read-only loc context
)
828 ((string= leader
"_")
829 (setq context
'underline
830 open-tag
(muse-markup-text 'begin-underline
)
831 close-tag
(muse-markup-text 'end-underline
)))
832 ((string= leader
"=")
833 (setq context
'literal
834 open-tag
(muse-markup-text 'begin-literal
)
835 close-tag
(muse-markup-text 'end-literal
))
836 (setq mark-read-only t
))
838 (let ((l (length leader
)))
839 (setq context
'emphasis
)
841 ((= l
1) (setq open-tag
(muse-markup-text 'begin-emph
)
842 close-tag
(muse-markup-text 'end-emph
)))
843 ((= l
2) (setq open-tag
(muse-markup-text 'begin-more-emph
)
844 close-tag
(muse-markup-text 'end-more-emph
)))
845 ((= l
3) (setq open-tag
(muse-markup-text 'begin-most-emph
)
846 close-tag
(muse-markup-text 'end-most-emph
)))
847 (t (setq context nil
))))))
849 (not (get-text-property beg
'muse-link
))
850 (setq loc
(search-forward leader nil t
))
851 (or (eobp) (not (eq (char-syntax (char-after loc
)) ?w
)))
852 (not (eq (char-syntax (char-before (point))) ?\
))
853 (not (get-text-property (point) 'muse-link
)))
856 (delete-region beg end
)
857 (setq end
(point-marker))
858 (muse-insert-markup close-tag
)
860 (muse-insert-markup open-tag
)
863 (muse-publish-escape-specials beg end t context
)
864 (muse-publish-mark-read-only beg end
)))
868 (defun muse-publish-markup-emdash ()
869 (unless (get-text-property (match-beginning 0) 'muse-link
)
870 (let ((prespace (match-string 1))
871 (postspace (match-string 2)))
872 (delete-region (match-beginning 0) (match-end 0))
873 (muse-insert-markup (muse-markup-text 'emdash prespace postspace
))
874 (when (eq (char-after) ?\
<)
877 (defun muse-publish-markup-enddots ()
878 (unless (get-text-property (match-beginning 0) 'muse-link
)
879 (delete-region (match-beginning 0) (match-end 0))
880 (muse-insert-markup (muse-markup-text 'enddots
))))
882 (defun muse-publish-markup-dots ()
883 (unless (get-text-property (match-beginning 0) 'muse-link
)
884 (delete-region (match-beginning 0) (match-end 0))
885 (muse-insert-markup (muse-markup-text 'dots
))))
887 (defun muse-publish-markup-rule ()
888 (unless (get-text-property (match-beginning 0) 'muse-link
)
889 (delete-region (match-beginning 0) (match-end 0))
890 (muse-insert-markup (muse-markup-text 'rule
))))
892 (defun muse-publish-markup-no-break-space ()
893 (unless (get-text-property (match-beginning 0) 'muse-link
)
894 (delete-region (match-beginning 0) (match-end 0))
895 (muse-insert-markup (muse-markup-text 'no-break-space
))))
897 (defun muse-publish-markup-heading ()
898 (let* ((len (length (match-string 1)))
899 (start (muse-markup-text
900 (cond ((= len
1) 'section
)
901 ((= len
2) 'subsection
)
902 ((= len
3) 'subsubsection
)
905 (end (muse-markup-text
906 (cond ((= len
1) 'section-end
)
907 ((= len
2) 'subsection-end
)
908 ((= len
3) 'subsubsection-end
)
909 (t 'section-other-end
))
911 (delete-region (match-beginning 0) (match-end 0))
912 (muse-insert-markup start
)
915 (muse-insert-markup end
))
916 (muse-publish-section-close len
)))
918 (defvar muse-publish-footnotes nil
)
920 (defun muse-publish-markup-footnote ()
921 "Scan ahead and snarf up the footnote body"
923 ((get-text-property (match-beginning 0) 'muse-link
)
925 ((= (muse-line-beginning-position) (match-beginning 0))
928 (let ((footnote (save-match-data
929 (string-to-number (match-string 1))))
931 (delete-region (match-beginning 0) (match-end 0))
933 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote
) nil t
)
934 (let* ((start (match-beginning 0))
935 (beg (goto-char (match-end 0)))
937 (if (search-forward "\n\n" nil t
)
938 (copy-marker (match-beginning 0))
939 (goto-char (point-max))
940 (skip-chars-backward "\n")
942 (while (re-search-forward
943 (concat "^[" muse-regexp-blank
"]+\\([^\n]\\)")
945 (replace-match "\\1" t
))
946 (let ((footnotemark-cmd (muse-markup-text 'footnotemark
))
947 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end
)))
948 (if (string= "" footnotemark-cmd
)
950 (concat (muse-markup-text 'footnote
)
951 (buffer-substring-no-properties beg end
)
952 (muse-markup-text 'footnote-end
)))
953 (setq footnotemark
(format footnotemark-cmd footnote
954 footnotemark-end-cmd
))
955 (unless muse-publish-footnotes
956 (set (make-local-variable 'muse-publish-footnotes
)
957 (make-vector 256 nil
)))
958 (unless (aref muse-publish-footnotes footnote
)
962 (concat (format (muse-markup-text 'footnotetext
)
964 (buffer-substring-no-properties beg end
)
965 (muse-markup-text 'footnotetext-end
))))
966 (aset muse-publish-footnotes footnote footnotemark
))))
968 (skip-chars-forward "\n")
969 (delete-region start
(point)))))
970 (muse-insert-markup (or footnotemark footnote
))))))
972 (defun muse-publish-markup-fn-sep ()
973 (delete-region (match-beginning 0) (match-end 0))
974 (muse-insert-markup (muse-markup-text 'fn-sep
)))
976 (defun muse-insert-markup-end-list (&rest args
)
979 (add-text-properties beg
(point) '(end-list t
))
980 (muse-publish-mark-read-only beg
(point))))
982 (defun muse-publish-surround-dl (indent post-indent
)
983 (let* ((beg-item (muse-markup-text 'begin-dl-item
))
984 (end-item (muse-markup-text 'end-dl-item
))
985 (beg-ddt (muse-markup-text 'begin-ddt
)) ;; term
986 (end-ddt (muse-markup-text 'end-ddt
))
987 (beg-dde (muse-markup-text 'begin-dde
)) ;; definition
988 (end-dde (muse-markup-text 'end-dde
))
990 def-on-same-line beg
)
992 ;; envelope this as one term+definitions unit -- HTML does not
993 ;; need this, but DocBook and Muse's custom XML format do
994 (muse-insert-markup beg-item
)
995 (when (looking-at muse-dl-term-regexp
)
996 ;; find the term and wrap it with published markup
998 (goto-char (match-end 1))
999 (delete-region (point) (match-end 0))
1000 (muse-insert-markup-end-list end-ddt
)
1001 ;; if definition is immediately after term, move to next line
1002 (unless (eq (char-after) ?
\n)
1006 (delete-region (point) (match-beginning 1))
1007 (muse-insert-markup beg-ddt
)))
1009 ;; move past current item
1010 continue
(muse-forward-list-item 'dl-term indent
))
1012 (narrow-to-region beg
(point))
1013 (goto-char (point-min))
1014 ;; publish each definition that we find, defaulting to an
1015 ;; empty definition if none are found
1016 (muse-publish-surround-text beg-dde end-dde
1018 (muse-forward-list-item 'dl-entry indent
))
1020 (goto-char (point-max))
1021 (skip-chars-backward (concat muse-regexp-blank
"\n"))
1022 (muse-insert-markup-end-list end-item
)
1024 (goto-char (point-max)))))))
1026 (defun muse-publish-surround-text (beg-tag end-tag move-func
&optional indent post-indent determine-indent
)
1028 (list-item (format muse-list-item-regexp
1029 (concat "[" muse-regexp-blank
"]*")))
1032 (setq indent
(concat "[" muse-regexp-blank
"]+")))
1034 (setq post-indent
(concat " \\{0," (number-to-string post-indent
)
1036 (setq post-indent
""))
1038 (muse-insert-markup beg-tag
)
1040 ;; move past current item; continue is non-nil if there
1041 ;; are more like items to be processed
1042 continue
(funcall move-func indent
))
1044 (when determine-indent
1045 ;; if the caller doesn't know how much indentation
1046 ;; to use, figure it out ourselves
1050 ;; snarf all leading whitespace
1051 (let ((this-indent (and (match-beginning 2)
1052 (buffer-substring (match-beginning 1)
1053 (match-beginning 2)))))
1054 (when (and this-indent
1055 (not (string= this-indent
"")))
1056 (setq indent this-indent
1057 determine-indent nil
))))))
1059 ;; remove list markup if we encountered another item of the
1061 (replace-match "" t t nil
1))
1062 (narrow-to-region beg
(point))
1063 ;; narrow to current item
1064 (goto-char (point-min))
1066 (let ((list-nested nil
)
1068 (post-indent post-indent
))
1069 (while (< (point) (point-max))
1070 (when (and (looking-at list-item
)
1071 (not (or (get-text-property
1072 (muse-list-item-critical-point) 'read-only
)
1074 (muse-list-item-critical-point) 'muse-link
))))
1075 ;; if we encounter a list item, allow no post-indent
1077 (setq list-nested t
))
1078 (when (looking-at (concat indent
"\\("
1079 (or (and list-nested
"")
1082 ;; if list is not nested, remove indentation
1083 (unless indent-found
1084 (setq post-indent
(match-string 1)
1088 (skip-chars-backward (concat muse-regexp-blank
"\n"))
1089 (muse-insert-markup-end-list end-tag
)
1091 (goto-char (point-max)))))))
1093 (defun muse-publish-markup-list ()
1094 "Markup a list entry.
1095 This function works by marking up items of the same list level
1096 and type, respecting the end-of-list property."
1097 (let* ((str (match-string 1))
1098 (type (muse-list-item-type str
))
1099 (indent (buffer-substring (muse-line-beginning-position)
1100 (match-beginning 1)))
1101 (post-indent (length str
))
1102 (last (match-beginning 0)))
1104 ((or (get-text-property (muse-list-item-critical-point) 'read-only
)
1105 (get-text-property (muse-list-item-critical-point) 'muse-link
))
1108 (unless (eq (char-after (match-end 1)) ?-
)
1109 (delete-region (match-beginning 0) (match-end 0))
1110 (muse-insert-markup (muse-markup-text 'begin-uli
))
1112 (muse-publish-surround-text
1113 (muse-markup-text 'begin-uli-item
)
1114 (muse-markup-text 'end-uli-item
)
1116 (muse-forward-list-item 'ul indent
))
1118 (muse-insert-markup-end-list (muse-markup-text 'end-uli
)))
1121 (delete-region (match-beginning 0) (match-end 0))
1122 (muse-insert-markup (muse-markup-text 'begin-oli
))
1124 (muse-publish-surround-text
1125 (muse-markup-text 'begin-oli-item
)
1126 (muse-markup-text 'end-oli-item
)
1128 (muse-forward-list-item 'ol indent
))
1130 (muse-insert-markup-end-list (muse-markup-text 'end-oli
)))
1132 ((not (string= (match-string 2) ""))
1133 ;; must have an initial term
1134 (goto-char (match-beginning 0))
1135 (muse-insert-markup (muse-markup-text 'begin-dl
))
1137 (muse-publish-surround-dl indent post-indent
)
1138 (muse-insert-markup-end-list (muse-markup-text 'end-dl
)))
1142 (defun muse-publish-markup-quote ()
1143 "Markup a quoted paragraph.
1144 The reason this function is so funky, is to prevent text properties
1145 like read-only from being inadvertently deleted."
1146 (let* ((ws (match-string 1))
1147 (centered (>= (string-width ws
) 6))
1148 (begin-elem (if centered
'begin-center
'begin-quote-item
))
1149 (end-elem (if centered
'end-center
'end-quote-item
)))
1150 (replace-match "" t t nil
1)
1152 (muse-insert-markup (muse-markup-text 'begin-quote
)))
1153 (muse-publish-surround-text (muse-markup-text begin-elem
)
1154 (muse-markup-text end-elem
)
1155 (function (lambda (indent)
1156 (muse-forward-paragraph)
1159 (muse-insert-markup (muse-markup-text 'end-quote
)))))
1161 (defun muse-publish-markup-leading-space (markup-space multiple
)
1163 (when (and markup-space
1164 (>= (setq count
(skip-chars-forward " ")) 0))
1165 (delete-region (muse-line-beginning-position) (point))
1167 (muse-insert-markup markup-space
)
1168 (setq count
(- count multiple
))))))
1170 (defun muse-publish-markup-verse ()
1171 (let ((leader (match-string 0)))
1172 (goto-char (match-beginning 0))
1173 (muse-insert-markup (muse-markup-text 'begin-verse
))
1174 (while (looking-at leader
)
1176 (muse-publish-markup-leading-space (muse-markup-text 'verse-space
) 2)
1177 (let ((beg (point)))
1181 (let ((text (muse-markup-text 'empty-verse-line
)))
1182 (when text
(muse-insert-markup text
))))
1186 (or (looking-at (concat leader
"["
1189 (not (looking-at leader
)))))
1190 (let ((begin-text (muse-markup-text 'begin-last-stanza-line
))
1191 (end-text (muse-markup-text 'end-last-stanza-line
)))
1192 (when end-text
(muse-insert-markup end-text
))
1194 (when begin-text
(muse-insert-markup begin-text
))
1197 (let ((begin-text (muse-markup-text 'begin-verse-line
))
1198 (end-text (muse-markup-text 'end-verse-line
)))
1199 (when end-text
(muse-insert-markup end-text
))
1201 (when begin-text
(muse-insert-markup begin-text
))
1204 (muse-insert-markup (muse-markup-text 'end-verse
))
1207 (defun muse-publish-table-fields (beg end
)
1208 "Parse given region as a table, returning a cons cell.
1209 The car is the length of the longest row.
1211 The cdr is a list of the fields of the table, with the first
1212 element indicating the type of the row:
1213 1: body, 2: header, 3: footer.
1215 The existing region will be removed, except for initial blank lines."
1216 (unless (muse-publishing-directive "disable-tables")
1221 (narrow-to-region beg end
)
1222 (goto-char (point-min))
1223 (while (looking-at (concat "^[" muse-regexp-blank
"]*$"))
1227 (when (looking-at muse-table-line-regexp
)
1228 (setq fields
(cons (length (match-string 1))
1229 (split-string (match-string 0)
1230 muse-table-field-regexp
))
1231 field-list
(cons fields field-list
)
1232 longest
(max (length fields
) longest
)))
1233 (setq left
(forward-line 1))))
1234 (delete-region beg end
)
1237 (cons (1- longest
) (nreverse field-list
))))))
1239 (defun muse-publish-markup-table ()
1240 "Style does not support tables.")
1242 (defun muse-publish-escape-specials-in-string (string &optional context
)
1243 "Escape specials in STRING using style-specific :specials.
1244 CONTEXT is used to figure out what kind of specials to escape.
1246 See the documentation of the `muse-publish-escape-specials'
1247 function for the list of available contexts."
1250 (let ((specials (muse-style-element :specials nil t
)))
1251 (cond ((functionp specials
)
1252 (setq specials
(funcall specials context
)))
1254 (setq specials
(symbol-value specials
))))
1255 (if (functionp specials
)
1256 (funcall specials string
)
1257 (apply (function concat
)
1260 (let ((repl (or (assoc ch specials
)
1261 (assoc ch muse-publish-markup-specials
))))
1265 (append string nil
))))))
1267 (defun muse-publish-markup-email ()
1268 (let* ((beg (match-end 1))
1269 (addr (buffer-substring-no-properties beg
(match-end 0))))
1270 (setq addr
(muse-publish-escape-specials-in-string addr
'email
))
1272 (delete-region beg
(match-end 0))
1273 (if (or (eq (char-before (match-beginning 0)) ?
\")
1274 (eq (char-after (match-end 0)) ?
\"))
1276 (insert (format (muse-markup-text 'email-addr
) addr addr
)))
1277 (muse-publish-mark-read-only beg
(point))))
1279 (defun muse-publish-classify-url (target)
1280 "Transform anchors and get published name, if TARGET is a page.
1281 The return value is a cons cell. The car is the type of link,
1282 the cadr is the page name, and the cddr is the anchor."
1284 (cond ((or (null target
) (string= target
""))
1286 ((string-match muse-url-regexp target
)
1287 (cons 'url
(cons target nil
)))
1288 ((string-match muse-image-regexp target
)
1289 (cons 'image
(cons target nil
)))
1290 ((string-match muse-file-regexp target
)
1291 (cons 'file
(cons target nil
)))
1292 ((string-match "#" target
)
1293 (if (eq (aref target
0) ?\
#)
1294 (cons 'anchor-ref
(cons nil
(substring target
1)))
1295 (cons 'link-and-anchor
1296 (cons (muse-publish-link-page
1297 (substring target
0 (match-beginning 0)))
1298 (substring target
(match-end 0))))))
1300 (cons 'link
(cons (muse-publish-link-page target
) nil
))))))
1302 (defun muse-publish-url (url &optional desc explicit
)
1303 "Resolve a URL into its final <a href> form."
1304 (let ((orig-url url
)
1306 (dolist (transform muse-publish-url-transforms
)
1307 (setq url
(save-match-data (when url
(funcall transform url explicit
)))))
1308 (dolist (transform muse-publish-desc-transforms
)
1309 (setq desc
(save-match-data
1310 (when desc
(funcall transform desc explicit
)))))
1312 (setq desc
(muse-link-unescape desc
))
1313 (setq desc
(muse-publish-escape-specials-in-string desc
'url-desc
)))
1315 (muse-publish-escape-specials-in-string orig-url
'url-desc
))
1316 (let ((target (muse-publish-classify-url url
)))
1317 (setq type
(car target
)
1318 url
(muse-publish-escape-specials-in-string (cadr target
) 'url
)
1319 anchor
(muse-publish-escape-specials-in-string
1320 (cddr target
) 'url
)))
1321 (cond ((eq type
'anchor-ref
)
1322 (muse-markup-text 'anchor-ref anchor
(or desc orig-url
)))
1327 (muse-markup-text 'image-with-desc url desc
)
1328 (muse-markup-text 'image-link url
)))
1329 ((eq type
'link-and-anchor
)
1330 (muse-markup-text 'link-and-anchor url anchor
1331 (or desc orig-url
)))
1332 ((and desc
(string-match muse-image-regexp desc
))
1333 (muse-markup-text 'url-with-image url desc
))
1335 (muse-markup-text 'link url
(or desc orig-url
)))
1338 (let ((text (muse-markup-text 'url-and-desc url desc
)))
1339 (and (not (string= text
""))
1341 (muse-markup-text 'url url
(or desc orig-url
)))))))
1343 (defun muse-publish-insert-url (url &optional desc explicit
)
1344 "Resolve a URL into its final <a href> form."
1345 (delete-region (match-beginning 0) (match-end 0))
1346 (let ((text (muse-publish-url url desc explicit
)))
1348 (muse-insert-markup text
))))
1350 (defun muse-publish-markup-link ()
1351 (let (desc explicit link
)
1352 (setq explicit
(save-match-data
1353 (if (string-match muse-explicit-link-regexp
1356 (setq desc
(if explicit
1357 (or (match-string 2) (match-string 1))
1359 (setq link
(if explicit
1360 (muse-handle-explicit-link (match-string 1))
1361 (muse-handle-implicit-link (match-string 0))))
1364 (not (or (eq (char-before (match-beginning 0)) ?
\")
1365 (eq (char-after (match-end 0)) ?
\")))))
1366 ;; if explicit link has no user-provided description, treat it
1367 ;; as if it were an implicit link
1368 (when (and explicit
(not (match-string 2)))
1369 (setq explicit nil
))
1370 (muse-publish-insert-url link desc explicit
))))
1372 (defun muse-publish-markup-url ()
1373 (unless (or (eq (char-before (match-beginning 0)) ?
\")
1374 (eq (char-after (match-end 0)) ?
\"))
1375 (muse-publish-insert-url (match-string 0))))
1377 ;; Default publishing tags
1379 (defcustom muse-publish-contents-depth
2
1380 "The number of heading levels to include with <contents> tags."
1382 :group
'muse-publish
)
1384 (defun muse-publish-contents-tag (beg end attrs
)
1385 (set (make-local-variable 'muse-publish-generate-contents
)
1386 (cons (copy-marker (point) t
)
1387 (let ((depth (cdr (assoc "depth" attrs
))))
1388 (or (and depth
(string-to-number depth
))
1389 muse-publish-contents-depth
)))))
1391 (defun muse-publish-verse-tag (beg end
)
1394 (narrow-to-region beg end
)
1395 (goto-char (point-min))
1396 (while (eq ?\
(char-syntax (char-after)))
1398 (while (< (point) (point-max))
1401 (if (eq ?\
(char-syntax (char-before)))
1402 (delete-char -
1)))))
1404 (defun muse-publish-mark-read-only (beg end
)
1405 "Add read-only properties to the given region."
1406 (add-text-properties beg end
'(rear-nonsticky (read-only) read-only t
))
1409 (defun muse-publish-mark-link (&optional beg end
)
1410 "Indicate that the given region is a Muse link, so that other
1411 markup elements respect it. If a region is not specified, use
1412 the 0th match data to determine it.
1414 This is usually applied to explicit links."
1415 (unless beg
(setq beg
(match-beginning 0)))
1416 (unless end
(setq end
(match-end 0)))
1417 (add-text-properties beg end
'(muse-link t
))
1420 (defun muse-publish-quote-tag (beg end
)
1423 (narrow-to-region beg end
)
1424 (muse-insert-markup (muse-markup-text 'begin-quote
))
1425 (muse-publish-surround-text (muse-markup-text 'begin-quote-item
)
1426 (muse-markup-text 'end-quote-item
)
1427 (function (lambda (indent)
1428 (muse-forward-paragraph)
1429 (goto-char (match-end 0))
1430 (< (point) (point-max)))))
1431 (muse-insert-markup (muse-markup-text 'end-quote
)))))
1433 (defun muse-publish-code-tag (beg end
)
1434 (muse-publish-escape-specials beg end nil
'literal
)
1436 (insert (muse-markup-text 'begin-literal
))
1438 (insert (muse-markup-text 'end-literal
))
1439 (muse-publish-mark-read-only beg
(point)))
1441 (defun muse-publish-example-tag (beg end
)
1442 (muse-publish-escape-specials beg end nil
'example
)
1444 (insert (muse-markup-text 'begin-example
))
1446 (insert (muse-markup-text 'end-example
))
1447 (muse-publish-mark-read-only beg
(point)))
1449 (defun muse-publish-verbatim-tag (beg end
)
1450 (muse-publish-escape-specials beg end nil
'verbatim
)
1451 (muse-publish-mark-read-only beg end
))
1453 (defalias 'muse-publish-class-tag
'ignore
)
1455 (defun muse-publish-lisp-tag (beg end
)
1457 (let ((str (muse-eval-lisp
1460 (buffer-substring-no-properties beg end
)
1462 (delete-region beg end
)))))
1463 (set-text-properties 0 (length str
) nil str
)
1466 (defun muse-publish-command-tag (beg end attrs
)
1467 (while (looking-at "\\s-*$")
1469 (let ((interp (cdr (assoc "interp" attrs
))))
1473 (buffer-substring-no-properties (point) end
)
1474 (delete-region beg end
)) t
)
1475 (shell-command-on-region beg end interp t t
))
1476 (muse-publish-mark-read-only beg
(point))))
1478 (defun muse-publish-comment-tag (beg end
)
1479 (if (null muse-publish-comments-p
)
1480 (delete-region beg end
)
1482 (muse-insert-markup (muse-markup-text 'comment-end
))
1483 (muse-publish-mark-read-only beg end
)
1485 (muse-insert-markup (muse-markup-text 'comment-begin
))))
1487 (defun muse-publish-examplify-buffer ()
1488 "Transform the current buffer as if it were an <example> region."
1489 (let ((end (progn (goto-char (point-max)) (point-marker))))
1490 (muse-publish-example-tag (point-min) end
)))
1492 (defun muse-publish-versify-buffer ()
1493 "Transform the current buffer as if it were a <verse> region."
1494 (muse-publish-verse-tag (point-min) (point-max))
1495 (muse-publish-markup ""
1496 `((100 ,(concat "^[" muse-regexp-blank
"]*> ") 0
1497 muse-publish-markup-verse
))))
1499 (defun muse-publish-include-tag (beg end attrs
)
1500 "Include the named file at the current location during publishing.
1502 <include file=\"...\" markup=\"...\">
1504 The `markup' attribute controls how this section is marked up.
1505 If it is omitted, publish the included text with the normal Muse
1508 If \"nil\", do not mark up the included text at all.
1510 If \"example\", treat the included text as if it was surrounded
1511 by the <example> tag.
1513 If \"verse\", treat the included text as if it was surrounded
1514 by the <verse> tag, to preserve newlines.
1516 Otherwise, it should be the name of a function to call after
1517 inserting the file with the buffer narrowed to the section
1520 Note that no further marking-up will be performed on this
1522 (let ((filename (cdr (assoc "file" attrs
)))
1523 (markup (cdr (assoc "markup" attrs
)))
1524 (muse-publishing-directives muse-publishing-directives
))
1526 (setq filename
(expand-file-name
1528 (file-name-directory muse-publishing-current-file
)))
1529 (error "No file attribute specified in <include> tag"))
1531 (let ((markup-function (read markup
)))
1532 (cond ((eq markup-function
'example
)
1533 (setq markup-function
#'muse-publish-examplify-buffer
))
1534 ((eq markup-function
'verse
)
1535 (setq markup-function
#'muse-publish-versify-buffer
))
1536 ((and markup-function
(not (functionp markup-function
)))
1537 (error "Invalid markup function `%s'" markup
))
1540 (narrow-to-region beg end
)
1541 (insert-file-contents filename
)
1542 (when markup-function
1543 (funcall markup-function
))
1544 (goto-char (point-max))))
1545 (insert-file-contents filename
)
1546 (muse-publish-markup-region beg
(point)))
1547 (muse-publish-mark-read-only beg
(point))))
1549 (defun muse-publish-mark-up-tag (beg end attrs
)
1550 "Run an Emacs Lisp function on the region delimted by this tag.
1552 <markup function=\"...\">
1554 The optional `function' attribute controls how this section is
1555 marked up. If used, it should be the name of a function to call
1556 with the buffer narrowed to the delimited region. Note that no
1557 further marking-up will be performed on this region.
1559 If `function' is ommitted, use the standard Muse markup function.
1560 This is useful for marking up content in headers and footers."
1561 (let ((function (cdr (assoc "function" attrs
)))
1562 (muse-publishing-directives muse-publishing-directives
))
1564 (let ((markup-function (intern function
)))
1565 (if (and markup-function
(functionp markup-function
))
1567 (narrow-to-region beg end
)
1568 (funcall markup-function
)
1569 (goto-char (point-max)))
1570 (error "Invalid markup function `%s'" function
)))
1571 (muse-publish-markup-region beg end
))
1572 (muse-publish-mark-read-only beg
(point))))
1574 ;; Miscellaneous helper functions
1576 (defun muse-publish-strip-tags (string)
1577 "Remove all tags from the string."
1578 (while (string-match "<.*?>" string
)
1579 (setq string
(replace-match "" nil t string
)))
1582 (defun muse-publish-markup-type (category default-func
)
1583 (let ((rule (muse-find-markup-element :overrides category
(muse-style))))
1584 (funcall (or rule default-func
))))
1586 (defun muse-published-buffer-contents (buffer)
1587 (with-current-buffer buffer
1588 (goto-char (point-min))
1589 (let ((beg (and (search-forward "Emacs Muse begins here")
1590 (muse-line-end-position)))
1591 (end (and (search-forward "Emacs Muse ends here")
1592 (muse-line-beginning-position))))
1593 (buffer-substring-no-properties beg end
))))
1595 (defun muse-published-contents (file)
1596 (when (file-readable-p file
)
1597 (muse-with-temp-buffer
1598 (insert-file-contents file
)
1599 (muse-published-buffer-contents (current-buffer)))))
1601 (defun muse-publish-transform-output
1602 (file temp-file output-path name gen-func
&rest cleanup-exts
)
1603 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1604 (setq file
(muse-page-name file
))
1605 (message "Generating %s output for %s..." name file
)
1606 (if (not (funcall gen-func temp-file output-path
))
1607 (message "Generating %s from %s...failed" name file
)
1608 (message "Generating %s output for %s...done" name file
)
1609 (muse-delete-file-if-exists temp-file
)
1610 (dolist (ext cleanup-exts
)
1611 (muse-delete-file-if-exists
1612 (expand-file-name (concat file ext
)
1613 (file-name-directory output-path
))))
1614 (message "Wrote %s" output-path
)))
1616 (defun muse-publish-read-only (string)
1617 (let ((end (1- (length string
))))
1618 (add-text-properties 0 end
1619 '(rear-nonsticky (read-only) read-only t
)
1623 (provide 'muse-publish
)
1625 ;;; muse-publish.el ends here