1 ;;; muse-publish.el --- Base publishing implementation.
3 ;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 ;; This file is not part of GNU Emacs.
7 ;; This is free software; you can redistribute it and/or modify it under
8 ;; the terms of the GNU General Public License as published by the Free
9 ;; Software Foundation; either version 2, or (at your option) any later
12 ;; This is distributed in the hope that it will be useful, but WITHOUT
13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; 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'.
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 (require 'muse-regexps
)
40 (defgroup muse-publish nil
41 "Options controlling the general behaviour of Muse publishing.
42 See `muse-publish' for more information."
45 (defcustom muse-before-publish-hook nil
46 "A hook run in the buffer to be published, before it is done."
50 (defcustom muse-after-publish-hook nil
51 "A hook run in the buffer to be published, after it is done."
55 (defcustom muse-publish-url-transforms
'(muse-publish-prepare-url)
56 "A list of functions used to prepare URLs for publication.
57 Each is passed the URL and expects a URL to be returned."
61 (defcustom muse-publish-report-threshhold
100000
62 "If a file is this size or larger, report publishing progress."
66 (defcustom muse-publish-markup-regexps
67 `(;; Remove leading and trailing whitespace from the file
68 (1000 "\\(\\`\n+\\|\n+\\'\\)" 0 "")
70 ;; Remove trailing whitespace from all lines
71 (1100 ,(concat "[" muse-regexp-blank
"]+$") 0 "")
73 ;; Handle any leading #directives
74 (1200 "\\`#\\([a-zA-Z]+\\)\\s-+\\(.+\\)\n+" 0 directive
)
77 (1300 muse-tag-regexp
0 tag
)
80 (1350 "^;\\s-+\\(.+\\)" 0 comment
)
82 ;; define anchor points
83 (1400 "^#\\(\\S-+\\)\\s-*" 0 anchor
)
85 ;; emphasized or literal text
98 ;; headings, outline-mode style
99 (1600 "^\\(\\*+\\)\\s-+" 0 heading
)
102 (1700 "\\.\\.\\.\\." 0 enddots
)
103 (1800 "\\.\\.\\." 0 dots
)
105 ;; horizontal rule, or section separator
106 (1900 "^----+" 0 rule
)
108 (2000 ,(concat "\n*\\(^\\|["
115 ;; beginning of footnotes section
116 (2100 "^Footnotes:?\\s-*" 0 fn-sep
)
117 ;; footnote definition/reference (def if at beginning of line)
118 (2200 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote
)
120 ;; unnumbered List items begin with a -. numbered list items
121 ;; begin with number and a period. definition lists have a
122 ;; leading term separated from the body with ::. centered
123 ;; paragraphs begin with at least six columns of whitespace; any
124 ;; other whitespace at the beginning indicates a blockquote. The
125 ;; reason all of these rules are handled here, is so that
126 ;; blockquote detection doesn't interfere with indented list
139 (2400 ,(concat "^\\(\\(?:.+?\\)["
144 (2500 ,(concat "^\\(["
149 ;; "verse" text is indicated the same way as a quoted e-mail
150 ;; response: "> text", where text may contain initial whitespace
157 ;; simple table markup is supported, nothing fancy. use | to
158 ;; separate cells, || to separate header cells, and ||| for footer
169 ;; bare email addresses
171 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email
)
173 ;; replace links in the buffer (links to other pages)
174 (2900 muse-link-regexp
0 link
)
175 (3000 muse-url-regexp
0 url
))
176 "List of markup rules for publishing a page with Muse.
177 The rules given in this variable are invoked first, followed by
178 whatever rules are specified by the current style.
180 Each member of the list is either a function, or a list of the form:
182 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
184 REGEXP is a regular expression, or symbol whose value is a regular
185 expression, which is searched for using `re-search-forward'.
186 TEXT-BEGIN-GROUP is the matching group within that regexp which
187 denotes the beginning of the actual text to be marked up.
188 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
189 If it is not a string, but a function, it will be called to determine
190 what the replacement text should be (it must return a string). If it
191 is a symbol, the value of that symbol should be a string.
193 The replacements are done in order, one rule at a time. Writing
194 the regular expressions can be a tricky business. Note that case
195 is never ignored. `case-fold-search' is always bound to nil
196 while processing the markup rules."
197 :type
'(repeat (choice
198 (list :tag
"Markup rule"
200 (choice regexp symbol
)
202 (choice string function symbol
))
204 :group
'muse-publish
)
206 (defcustom muse-publish-markup-functions
207 '((directive . muse-publish-markup-directive
)
208 (comment . muse-publish-markup-comment
)
209 (anchor . muse-publish-markup-anchor
)
210 (tag . muse-publish-markup-tag
)
211 (word . muse-publish-markup-word
)
212 (emdash . muse-publish-markup-emdash
)
213 (enddots . muse-publish-markup-enddots
)
214 (dots . muse-publish-markup-dots
)
215 (rule . muse-publish-markup-rule
)
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."
227 :type
'(alist :key-type symbol
:value-type function
)
228 :group
'muse-publish
)
230 (defcustom muse-publish-markup-tags
231 '(("contents" nil t muse-publish-contents-tag
)
232 ("verse" t nil muse-publish-verse-tag
)
233 ("example" t nil muse-publish-example-tag
)
234 ("literal" t nil muse-publish-mark-read-only
)
235 ("verbatim" t nil muse-publish-verbatim-tag
)
236 ("lisp" t nil muse-publish-lisp-tag
)
237 ("class" t t muse-publish-class-tag
)
238 ("command" t t muse-publish-command-tag
)
239 ("comment" t nil muse-publish-comment-tag
))
240 "A list of tag specifications, for specially marking up text.
241 XML-style tags are the best way to add custom markup to Muse.
242 This is easily accomplished by customizing this list of markup tags.
244 For each entry, the name of the tag is given, whether it expects
245 a closing tag and/or an optional set of attributes, and a
246 function that performs whatever action is desired within the
249 The tags themselves are deleted during publishing, before the
250 function is called. The function is called with three arguments,
251 the beginning and end of the region surrounded by the tags. If
252 properties are allowed, they are passed as a third argument in
253 the form of an alist. The `end' argument to the function is
256 Point is always at the beginning of the region within the tags, when
257 the function is called. Wherever point is when the function finishes
258 is where tag markup will resume.
260 These tag rules are processed once at the beginning of markup, and
261 once at the end, to catch any tags which may have been inserted
263 :type
'(repeat (list (string :tag
"Markup tag")
264 (boolean :tag
"Expect closing tag" :value t
)
265 (boolean :tag
"Parse attributes" :value nil
)
267 :group
'muse-publish
)
269 (defcustom muse-publish-markup-specials nil
270 "A table of characters which must be represented specially."
271 :type
'(alist :key-type character
:value-type string
)
272 :group
'muse-publish
)
274 (defvar muse-publishing-p nil
275 "Set to t while a page is being published.")
276 (defvar muse-batch-publishing-p nil
277 "Set to t while a page is being batch published.")
278 (defvar muse-publishing-styles nil
)
279 (defvar muse-publishing-current-file nil
)
280 (defvar muse-publishing-current-style nil
)
281 (defvar muse-publishing-directives nil
282 "An alist of publishing directives from the top of a file.")
283 (defvar muse-publish-generate-contents nil
284 "Non-nil if a table of contents should be generated.
285 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
286 tell where the <contents> was seen, and to what depth the
287 contents were requested.")
289 ;; Functions for handling style information
291 (defsubst muse-style
(&optional style
)
292 "Resolve the given STYLE into a Muse style, if it is a string."
294 muse-publishing-current-style
296 (assoc style muse-publishing-styles
)
297 (muse-assert (consp style
))
300 (defun muse-define-style (name &rest elements
)
301 (let ((entry (assoc name muse-publishing-styles
)))
303 (error "There is already a style named %s." name
)
304 (setq muse-publishing-styles
305 (cons (append (list name
) elements
)
306 muse-publishing-styles
)))))
308 (defun muse-derive-style (new-name base-name
&rest elements
)
309 (let ((entry (assoc new-name muse-publishing-styles
)))
311 (error "There is already a style named %s." new-name
)
312 (apply 'muse-define-style new-name
313 (append elements
(list :base base-name
))))))
315 (defsubst muse-get-keyword
(keyword list
&optional direct
)
316 (let ((value (cadr (memq keyword list
))))
317 (if (and (not direct
) (symbolp value
))
321 (defsubst muse-style-element
(elem &optional style direct
)
322 (setq style
(muse-style style
))
323 (let ((value (muse-get-keyword elem style direct
)))
326 (let ((base (muse-get-keyword :base style
)))
328 (muse-style-element elem base direct
))))))
330 (defun muse-find-markup-element (keyword ident style
)
331 (let ((def (assq ident
(muse-style-element keyword style
))))
334 (let ((base (muse-style-element :base style
)))
336 (muse-find-markup-element keyword ident base
))))))
338 (defsubst muse-markup-text
(ident &rest args
)
339 (let ((text (muse-find-markup-element :strings ident
(muse-style))))
341 (apply 'format text args
)
344 (defun muse-find-markup-tag (keyword tagname style
)
345 (let ((def (assoc tagname
(muse-style-element keyword style
))))
347 (let ((base (muse-style-element :base style
)))
349 (muse-find-markup-tag keyword tagname base
))))))
351 (defsubst muse-markup-tag-info
(tagname &rest args
)
352 (let ((tag-info (muse-find-markup-tag :tags tagname
(muse-style))))
354 (assoc (match-string 1) muse-publish-markup-tags
))))
356 (defsubst muse-markup-function
(category)
357 (let ((func (muse-find-markup-element :functions category
(muse-style))))
359 (cdr (assq category muse-publish-markup-functions
)))))
361 ;; Publishing routines
363 (defun muse-publish-markup (name rules
)
364 (let* ((case-fold-search nil
)
365 (inhibit-read-only t
)
366 (limit (* (length rules
) (point-max)))
367 (verbose (and muse-publish-report-threshhold
368 (> (point-max) muse-publish-report-threshhold
)))
371 (goto-char (point-min))
372 (let ((regexp (nth 1 (car rules
)))
373 (group (nth 2 (car rules
)))
374 (repl (nth 3 (car rules
)))
377 (setq regexp
(symbol-value regexp
)))
378 (if (and verbose
(not muse-batch-publishing-p
))
379 (message "Publishing %s...%d%%" name
380 (* (/ (float (+ (point) base
)) limit
) 100)))
381 (while (and regexp
(setq pos
(re-search-forward regexp nil t
)))
382 (if (and verbose
(not muse-batch-publishing-p
))
383 (message "Publishing %s...%d%%" name
384 (* (/ (float (+ (point) base
)) limit
) 100)))
385 (unless (get-text-property (match-beginning group
) 'read-only
)
389 (setq func
(muse-markup-function repl
)))
397 (replace-match text t
))))
398 (if (and last-pos
(= pos last-pos
))
402 (setq last-pos pos
)))
403 (setq rules
(cdr rules
)
404 base
(+ base
(point-max))))
405 (if (and verbose
(not muse-batch-publishing-p
))
406 (message "Publishing %s...done" name
))))
408 (defun muse-insert-file-or-string (file-or-string &optional title
)
409 (let ((beg (point)) end
)
410 (if (file-readable-p file-or-string
)
411 (setq end
(+ beg
(cadr (insert-file-contents file-or-string
))))
412 (insert file-or-string
)
415 (narrow-to-region beg end
)
416 (muse-publish-markup (or title
"")
417 '((100 "<\\(lisp\\)>" 0
418 muse-publish-markup-tag
))))))
420 (defun muse-style-run-hooks (keyword style
&rest args
)
422 (while (and style
(not handled
))
423 (setq style
(muse-style style
))
424 (let ((func (muse-get-keyword keyword style t
)))
426 (if (apply func args
)
429 (setq style
(muse-style-element :base style
))))
432 (defun muse-publish-markup-region (beg end title style
)
433 "Apply the given STYLE's markup rules to the given region."
435 (narrow-to-region beg end
)
436 (muse-style-run-hooks :before style
)
439 (sort (copy-alist (append muse-publish-markup-regexps
440 (muse-style-element :regexps style
)))
443 (< (car l
) (car r
))))))
444 (muse-style-run-hooks :before-end style
)))
446 (defun muse-publish-markup-buffer (title style
)
447 "Apply the given STYLE's markup rules to the current buffer."
448 (setq style
(muse-style style
))
449 (let ((style-header (muse-style-element :header style
))
450 (style-footer (muse-style-element :footer style
))
451 (muse-publishing-current-style style
)
452 (muse-publishing-directives
453 (list (cons "title" title
)
454 (cons "author" (user-full-name))
455 (cons "date" (format-time-string "%B %e, %Y"))))
456 (muse-publishing-p t
))
457 (run-hooks 'muse-before-publish-hook
)
458 (muse-publish-markup-region (point-min) (point-max) title style
)
459 (goto-char (point-min))
460 (if style-header
(muse-insert-file-or-string style-header title
))
461 (goto-char (point-max))
462 (if style-footer
(muse-insert-file-or-string style-footer title
))
463 (muse-style-run-hooks :after style
)
464 (run-hooks 'muse-after-publish-hook
)))
466 (defun muse-publish-markup-string (string &optional style
)
467 "Markup STRING using the given STYLE's markup rules."
468 (setq style
(muse-style style
))
471 (let ((muse-publishing-current-style style
)
472 (muse-publishing-p t
))
473 (muse-publish-markup "*string*" (muse-style-element :rules style
)))
476 ;; Commands for publishing files
478 (defsubst muse-publish-get-style
()
479 (if (= 1 (length muse-publishing-styles
))
480 (car muse-publishing-styles
)
481 (assoc (completing-read "Publish with style: "
482 muse-publishing-styles nil t
)
483 muse-publishing-styles
)))
485 (defsubst muse-publish-get-output-dir
(style)
486 (let ((default-directory (or (muse-style-element :path style
)
488 (read-file-name "Publish to directory: " nil default-directory
)))
490 (defsubst muse-publish-get-info
()
491 (let ((style (muse-publish-get-style)))
492 (list style
(muse-publish-get-output-dir style
)
493 current-prefix-arg
)))
495 (defsubst muse-publish-output-name
(&optional file style
)
496 (setq style
(muse-style style
))
497 (concat (muse-style-element :prefix style
)
498 (muse-page-name (or file muse-publishing-current-file
))
499 (muse-style-element :suffix style
)))
501 (defsubst muse-publish-output-file
(file output-dir
&optional style
)
502 (setq style
(muse-style style
))
503 (expand-file-name (muse-publish-output-name file style
) output-dir
))
505 (defun muse-publish-file (file style
&optional output-dir force
)
506 "Publish the given file in list FILES.
507 If the argument FORCE is nil, each file is only published if it is
508 newer than the published version. If the argument FORCE is non-nil,
509 the file is published no matter what."
510 (interactive (cons (read-file-name "Publish file: ")
511 (muse-publish-get-info)))
512 (setq style
(muse-style style
))
513 (let* ((output-path (muse-publish-output-file file output-dir style
))
514 (output-suffix (muse-style-element :osuffix style
))
515 (muse-publishing-current-file file
)
516 (target (if output-suffix
517 (concat (file-name-sans-extension output-path
)
520 (when (or force
(file-newer-than-file-p file target
))
521 (if (and muse-publish-report-threshhold
522 (> (nth 7 (file-attributes file
))
523 muse-publish-report-threshhold
))
524 (message "Publishing %s ..." file
))
526 (insert-file-contents file t
)
527 (muse-publish-markup-buffer (muse-page-name file
) style
)
528 (let ((backup-inhibited t
))
529 (write-file output-path
))
530 (muse-style-run-hooks :final style file output-path target
))
533 (defun muse-publish-this-file (style output-dir
&optional force
)
534 "Publish the page in the current file."
535 (interactive (muse-publish-get-info))
536 (muse-publish-file buffer-file-name style output-dir force
))
538 (defun muse-batch-publish-files ()
539 "Publish Muse files in batch mode."
540 (let ((muse-batch-publishing-p t
)
541 style-name style output-dir
)
542 (setq style-name
(car command-line-args-left
)
543 style
(muse-style style-name
)
544 command-line-args-left
(cdr command-line-args-left
)
545 output-dir
(car command-line-args-left
)
547 (if (string-match "\\`--output-dir=" output-dir
)
549 (substring output-dir
(match-end 0))
550 (setq command-line-args-left
(cdr command-line-args-left
)))))
552 (error "There is no style '%s' defined." style-name
))
553 (dolist (file command-line-args-left
)
554 (muse-publish-file file style output-dir t
))))
556 ;; Default publishing rules
558 (defun muse-publish-markup-directive (&optional name value
)
559 (unless name
(setq name
(match-string 1)))
560 (unless value
(setq value
(match-string 2)))
561 (let ((elem (assoc name muse-publishing-directives
)))
564 (setq muse-publishing-directives
565 (cons (cons name value
)
566 muse-publishing-directives
))))
567 (delete-region (match-beginning 0) (match-end 0)))
569 (defun muse-publish-markup-anchor () "")
570 (defun muse-publish-markup-comment () "")
572 (defun muse-publish-markup-tag ()
573 (let ((tag-info (muse-markup-tag-info (match-string 1))))
575 (not (get-text-property (match-beginning 0) 'read-only
)))
576 (let ((closed-tag (match-string 3))
577 (start (match-beginning 0))
578 (beg (point)) end attrs
)
579 (when (nth 2 tag-info
)
580 (let ((attrstr (match-string 2)))
582 (string-match (concat "\\([^"
587 (let ((attr (cons (downcase
588 (match-string-no-properties 1 attrstr
))
589 (match-string-no-properties 3 attrstr
))))
590 (setq attrstr
(replace-match "" t t attrstr
))
592 (nconc attrs
(list attr
))
593 (setq attrs
(list attr
)))))))
594 (if (and (cadr tag-info
) (not closed-tag
))
595 (if (search-forward (concat "</" (car tag-info
) ">") nil t
)
596 (delete-region (match-beginning 0) (point))
597 (setq tag-info nil
)))
599 (setq end
(point-marker))
600 (delete-region start beg
)
602 (let ((args (list start end
)))
604 (nconc args
(list attrs
)))
605 (apply (nth 3 tag-info
) args
))))))
608 (defun muse-publish-escape-specials (beg end
)
611 (while (< (point) end
)
612 (if (get-text-property (point) 'read-only
)
615 (or (assoc (char-after) (muse-style-element :specials
))
616 (assoc (char-after) muse-publish-markup-specials
))))
620 (insert (cdr repl
))))))))
622 (defun muse-publish-markup-word ()
623 (let* ((beg (match-beginning 2))
624 (end (1- (match-end 2)))
625 (leader (buffer-substring-no-properties beg end
))
626 open-tag close-tag mark-read-only loc multi-line
)
628 ((string= leader
"_")
630 (setq open-tag
(muse-markup-text 'begin-underline
)
631 close-tag
(muse-markup-text 'end-underline
)))
632 ((string= leader
"=")
633 (setq open-tag
(muse-markup-text 'begin-literal
)
634 close-tag
(muse-markup-text 'end-literal
))
635 (setq mark-read-only t
))
638 (let ((l (length leader
)))
640 ((= l
1) (setq open-tag
(muse-markup-text 'begin-emph
)
641 close-tag
(muse-markup-text 'end-emph
)))
642 ((= l
2) (setq open-tag
(muse-markup-text 'begin-more-emph
)
643 close-tag
(muse-markup-text 'end-more-emph
)))
644 ((= l
3) (setq open-tag
(muse-markup-text 'begin-most-emph
)
645 close-tag
(muse-markup-text 'end-most-emph
)))))))
646 (if (and (setq loc
(search-forward leader nil t
))
647 (eq 0 (skip-syntax-forward "w" (1+ loc
)))
648 (or multi-line
(= 1 (count-lines beg loc
))))
651 (delete-region beg end
)
652 (setq end
(point-marker))
659 (muse-publish-escape-specials beg end
)
660 (muse-publish-mark-read-only (1- beg
) (1+ end
))))
664 (defun muse-publish-markup-emdash ()
665 (delete-region (match-beginning 0) (match-end 0))
666 (insert (muse-markup-text 'emdash
))
667 (if (eq (char-after) ?\
<)
670 (defun muse-publish-markup-enddots ()
671 (delete-region (match-beginning 0) (match-end 0))
672 (insert (muse-markup-text 'enddots
)))
674 (defun muse-publish-markup-dots ()
675 (delete-region (match-beginning 0) (match-end 0))
676 (insert (muse-markup-text 'dots
)))
678 (defun muse-publish-markup-rule ()
679 (delete-region (match-beginning 0) (match-end 0))
680 (insert (muse-markup-text 'rule
)))
682 (defun muse-publish-markup-heading ()
683 (let* ((len (length (match-string 1)))
684 (start (muse-markup-text
685 (cond ((= len
1) 'section
)
686 ((= len
2) 'subsection
)
687 ((= len
3) 'subsubsection
))))
688 (end (muse-markup-text
689 (cond ((= len
1) 'section-end
)
690 ((= len
2) 'subsection-end
)
691 ((= len
3) 'subsubsection-end
)))))
692 (delete-region (match-beginning 0) (match-end 0))
695 (if end
(insert end
))))
697 (defvar muse-publish-footnotes nil
)
699 (defun muse-publish-markup-footnote ()
700 "Scan ahead and snarf up the footnote body"
701 (if (= (line-beginning-position) (match-beginning 0))
703 (let ((footnote (save-match-data
704 (string-to-number (match-string 1))))
706 (delete-region (match-beginning 0) (match-end 0))
708 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote
) nil t
)
709 (let* ((start (match-beginning 0))
710 (beg (goto-char (match-end 0)))
712 (if (search-forward "\n\n" nil t
)
713 (copy-marker (match-beginning 0))
714 (goto-char (point-max))
715 (skip-chars-backward "\n")
717 (while (re-search-forward (concat "^["
721 (replace-match "\\1" t
))
722 (let ((footnotemark-cmd (muse-markup-text 'footnotemark
))
723 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end
)))
724 (if (string= "" footnotemark-cmd
)
726 (concat (muse-markup-text 'footnote
)
727 (buffer-substring-no-properties beg end
)
728 (muse-markup-text 'footnote-end
)))
729 (setq footnotemark
(format footnotemark-cmd footnote
730 footnotemark-end-cmd
))
731 (unless muse-publish-footnotes
732 (set (make-local-variable 'muse-publish-footnotes
)
733 (make-vector 256 nil
)))
734 (unless (aref muse-publish-footnotes footnote
)
738 (concat (format (muse-markup-text 'footnotetext
)
740 (buffer-substring-no-properties beg end
)
741 (muse-markup-text 'footnotetext-end
))))
742 (aset muse-publish-footnotes footnote footnotemark
))))
744 (skip-chars-forward "\n")
745 (delete-region start
(point)))))
746 (insert (or footnotemark footnote
)))))
748 (defun muse-publish-markup-fn-sep ()
749 (delete-region (match-beginning 0) (match-end 0))
750 (insert (muse-markup-text 'fn-sep
)))
752 (defun muse-publish-surround-text (beg-tag end-tag move-func
)
753 (let ((beg (point)) end
)
754 (skip-chars-backward muse-regexp-space
)
755 (delete-region (point) beg
)
756 (insert "\n\n" beg-tag
)
758 (setq end
(point-marker))
760 (while (< (point) end
)
761 (if (looking-at "^\\s-+")
766 (skip-chars-backward muse-regexp-space
)
767 (delete-region (point) beg
))
768 (insert end-tag
"\n"))
770 (defsubst muse-forward-paragraph
(&optional pattern
)
771 (if (re-search-forward (if pattern
772 (concat "^\\(" pattern
"["
775 "^\\s-*\\($\\|\\'\\)") nil t
)
776 (goto-char (match-beginning 0))
777 (goto-char (point-max))))
779 (defun muse-publish-markup-list ()
780 "Markup a list entry or quoted paragraph.
781 The reason this function is so funky, is to prevent text properties
782 like read-only from being inadvertently deleted."
783 (let ((str (match-string 1)))
785 ((and (eq (aref str
0) ?-
))
786 (delete-region (match-beginning 0) (match-end 0))
787 (muse-publish-surround-text
788 (muse-markup-text 'begin-uli
)
789 (muse-markup-text 'end-uli
)
792 (muse-forward-paragraph (concat "["
795 ((and (>= (aref str
0) ?
0)
796 (<= (aref str
0) ?
9))
797 (delete-region (match-beginning 0) (match-end 0))
798 (muse-publish-surround-text
799 (muse-markup-text 'begin-oli
)
800 (muse-markup-text 'end-oli
)
803 (muse-forward-paragraph (concat "["
807 (goto-char (match-beginning 1))
808 (insert (muse-markup-text 'begin-ddt
))
812 (while (looking-at (concat "^\\(["
817 (delete-region (match-beginning 1) (match-end 1))
820 (when (re-search-forward (concat "["
826 (replace-match (muse-markup-text 'start-dde
))))
827 (muse-forward-paragraph)
828 (insert (muse-markup-text 'end-ddt
) ?
\n)))))
830 (defun muse-publish-markup-quote ()
831 "Markup a list entry or quoted paragraph.
832 The reason this function is so funky, is to prevent text properties
833 like read-only from being inadvertently deleted."
834 (let* ((ws (match-string 0))
835 (centered (>= (string-width ws
) 6))
836 (begin-elem (if centered
'begin-center
'begin-quote
))
837 (end-elem (if centered
'end-center
'end-quote
)))
838 (muse-publish-surround-text (muse-markup-text begin-elem
)
839 (muse-markup-text end-elem
)
840 'muse-forward-paragraph
)))
842 (defun muse-publish-markup-leading-space ()
843 (let ((markup-space (muse-markup-text 'verse-space
))
845 (when (and markup-space
846 (>= (setq count
(skip-chars-forward " ")) 0))
847 (delete-region (line-beginning-position) (point))
849 (insert markup-space
)
850 (setq count
(- count
2))))))
852 (defun muse-publish-markup-verse ()
853 (let ((leader (match-string 0)))
854 (goto-char (match-beginning 0))
855 (insert (muse-markup-text 'begin-verse
))
856 (while (looking-at leader
)
858 (muse-publish-markup-leading-space)
862 (let ((text (muse-markup-text 'empty-verse-line
)))
863 (if text
(insert text
))))
867 (or (looking-at (concat leader
"["
870 (not (looking-at leader
)))))
871 (let ((text (muse-markup-text 'last-stanza-end
)))
872 (if text
(insert text
))))
874 (let ((text (muse-markup-text 'end-verse-line
)))
875 (if text
(insert text
)))))
877 (insert (muse-markup-text 'end-verse
) ?
\n))
879 (defun muse-publish-markup-table ()
880 "Style does not support tables.")
882 (defun muse-publish-markup-email ()
883 (let* ((beg (match-end 1))
884 (addr (buffer-substring-no-properties beg
(match-end 0))))
887 (muse-publish-escape-specials (point-min) (point-max))
888 (setq addr
(buffer-string)))
890 (delete-region beg
(match-end 0))
891 (insert (format (muse-markup-text 'email-addr
) addr addr
))
892 (muse-publish-mark-read-only beg
(point))))
894 (defun muse-publish-url (url &optional desc
)
895 "Resolve a URL into its final <a href> form."
896 (let ((orig-url url
))
897 (dolist (transform muse-publish-url-transforms
)
898 (setq url
(funcall transform url
)))
899 (if (string-match muse-image-regexp url
)
901 (muse-markup-text 'image-with-desc url desc
)
902 (muse-markup-text 'image-link url
))
903 (if (and desc
(string-match muse-image-regexp desc
))
904 (muse-markup-text 'url-with-image url desc
)
905 (muse-markup-text 'url-link url
(or desc orig-url
))))))
907 (defun muse-publish-insert-url (url &optional desc
)
908 "Resolve a URL into its final <a href> form."
909 (delete-region (match-beginning 0) (match-end 0))
910 (insert (muse-publish-url url desc
)))
912 (defun muse-publish-markup-link ()
913 (muse-publish-insert-url (match-string 1) (match-string 2)))
915 (defun muse-publish-markup-url ()
916 (muse-publish-insert-url (match-string 0)))
918 ;; Default publishing tags
920 (defun muse-publish-contents-tag (beg end attrs
)
921 (set (make-local-variable 'muse-publish-generate-contents
)
922 (cons (copy-marker (point) t
)
923 (let ((depth (cdr (assoc "depth" attrs
))))
924 (or (and depth
(string-to-number depth
)) 2)))))
926 (defun muse-publish-verse-tag (beg end
)
929 (while (eq ?\
(char-syntax (char-after)))
931 (while (< (point) end
)
934 (if (eq ?\
(char-syntax (char-before)))
937 (defun muse-publish-example-tag (beg end
)
938 (muse-publish-escape-specials beg end
)
940 (insert (muse-markup-text 'begin-example
))
942 (insert (muse-markup-text 'end-example
))
943 (muse-publish-mark-read-only beg
(point)))
945 (defun muse-publish-mark-read-only (beg end
)
946 (add-text-properties beg end
'(rear-nonsticky (read-only) read-only t
))
949 (defun muse-publish-verbatim-tag (beg end
)
950 (muse-publish-escape-specials beg end
)
951 (muse-publish-mark-read-only beg end
))
953 (defalias 'muse-publish-class-tag
'ignore
)
955 (defun muse-publish-lisp-tag (beg end
)
957 (let ((str (muse-eval-lisp
959 (buffer-substring-no-properties beg end
)
960 (delete-region beg end
)))))
961 (set-text-properties 0 (length str
) nil str
)
964 (defun muse-publish-command-tag (beg end attrs
)
965 (while (looking-at "\\s-*$")
967 (let ((interp (cdr (assoc "interp" attrs
))))
971 (buffer-substring-no-properties (point) end
)
972 (delete-region beg end
)) t
)
973 (shell-command-on-region beg end interp t t
))
974 (muse-publish-mark-read-only beg
(point))))
976 (defun muse-publish-comment-tag (beg end
)
977 (delete-region beg end
))
979 ;; Miscellaneous helper functions
981 (defsubst muse-publishing-directive
(name)
982 (cdr (assoc name muse-publishing-directives
)))
984 (defun muse-publish-strip-tags (string)
985 "Remove all tags from the string."
986 (while (string-match "<.*?>" string
)
987 (setq string
(replace-match "" nil t string
)))
990 (defun muse-publish-markup-type (category default-func
)
991 (let ((rule (muse-find-markup-element :overrides category
(muse-style))))
992 (funcall (or rule default-func
))))
994 (defun muse-published-buffer-contents (buffer)
995 (with-current-buffer buffer
996 (goto-char (point-min))
997 (let ((beg (and (search-forward "Emacs Muse begins here")
998 (line-end-position)))
999 (end (and (search-forward "Emacs Muse ends here")
1000 (line-beginning-position))))
1001 (buffer-substring-no-properties beg end
))))
1003 (defun muse-published-contents (file)
1004 (when (file-readable-p file
)
1006 (insert-file-contents file
)
1007 (muse-published-buffer-contents (current-buffer)))))
1009 (defun muse-publish-transform-output
1010 (file temp-file output-path name gen-func
&rest cleanup-exts
)
1011 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1012 (setq file
(muse-page-name file
))
1013 (message "Generating %s output for %s..." name file
)
1014 (if (not (funcall gen-func temp-file output-path
))
1015 (message "Generating %s from %s...failed" name file
)
1016 (message "Generating %s output for %s...done" name file
)
1017 (muse-delete-file-if-exists temp-file
)
1018 (dolist (ext cleanup-exts
)
1019 (muse-delete-file-if-exists
1020 (expand-file-name (concat file ext
)
1021 (file-name-directory output-path
))))
1022 (message "Wrote %s" output-path
)))
1024 (defun muse-publish-read-only (string)
1025 (add-text-properties 0 (1- (length string
))
1026 '(rear-nonsticky (read-only) read-only t
)
1030 (defun muse-publish-prepare-url (target)
1032 (unless (or (string-match muse-url-regexp target
)
1033 (string-match muse-image-regexp target
)
1034 (string-match muse-file-regexp target
))
1035 (setq target
(if (string-match "#" target
)
1036 (concat (muse-publish-output-name
1037 (substring target
0 (match-beginning 0)))
1038 "#" (substring target
(match-end 0)))
1039 (muse-publish-output-name target
)))))
1040 (muse-publish-read-only target
))
1042 (provide 'muse-publish
)
1044 ;;; muse-publish.el ends here