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.
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 (require 'muse-regexps
)
43 (defgroup muse-publish nil
44 "Options controlling the general behavior of Muse publishing."
47 (defcustom muse-before-publish-hook nil
48 "A hook run in the buffer to be published, before it is done."
52 (defcustom muse-after-publish-hook nil
53 "A hook run in the buffer to be published, after it is done."
57 (defcustom muse-publish-url-transforms
58 '(muse-publish-escape-specials-in-string
59 muse-publish-prepare-url
61 "A list of functions used to prepare URLs for publication.
62 Each is passed the URL. The transformed URL should be returned."
64 :options
'(muse-publish-escape-specials-in-string
65 muse-publish-prepare-url
69 (defcustom muse-publish-desc-transforms
70 '(muse-publish-escape-specials-in-string)
71 "A list of functions used to prepare URL desciptions for publication.
72 Each is passed the description. The modified description should
75 :options
'(muse-publish-escape-specials-in-string)
78 (defcustom muse-publish-comments-p nil
79 "If nil, remove comments before publishing.
80 If non-nil, publish comments using the markup of the current style."
84 (defcustom muse-publish-report-threshhold
100000
85 "If a file is this size or larger, report publishing progress."
89 (defcustom muse-publish-markup-regexps
90 `(;; Remove leading and trailing whitespace from the file
91 (1000 "\\(\\`\n+\\|\n+\\'\\)" 0 "")
93 ;; Remove trailing whitespace from all lines
94 (1100 ,(concat "[" muse-regexp-blank
"]+$") 0 "")
96 ;; Handle any leading #directives
97 (1200 "\\`#\\([a-zA-Z-]+\\)\\s-+\\(.+\\)\n+" 0 directive
)
100 (1300 muse-tag-regexp
0 tag
)
103 (1350 "^;\\s-+\\(.+\\)" 0 comment
)
105 ;; prevent emphasis characters in explicit links from being marked
106 (1400 muse-explicit-link-regexp
0 muse-publish-mark-noemphasis
)
108 ;; define anchor points
109 (1500 "^\\(\\W*\\)#\\(\\S-+\\)\\s-*" 0 anchor
)
111 ;; emphasized or literal text
112 (1600 ,(concat "\\(^\\|[-[" muse-regexp-blank
113 "<('`\"\n]\\)\\(=[^=" muse-regexp-blank
114 "\n]\\|_[^_" muse-regexp-blank
115 "\n]\\|\\*+[^*" muse-regexp-blank
119 ;; headings, outline-mode style
120 (1700 "^\\(\\*+\\)\\s-+" 0 heading
)
123 (1800 "\\.\\.\\.\\." 0 enddots
)
124 (1850 "\\.\\.\\." 0 dots
)
126 ;; horizontal rule, or section separator
127 (1900 "^----+" 0 rule
)
129 ;; non-breaking space
130 (1950 "~~" 0 no-break-space
)
132 ;; beginning of footnotes section
133 (2000 "^Footnotes:?\\s-*" 0 fn-sep
)
134 ;; footnote definition/reference (def if at beginning of line)
135 (2100 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote
)
137 ;; unnumbered List items begin with a -. numbered list items
138 ;; begin with number and a period. definition lists have a
139 ;; leading term separated from the body with ::. centered
140 ;; paragraphs begin with at least six columns of whitespace; any
141 ;; other whitespace at the beginning indicates a blockquote. The
142 ;; reason all of these rules are handled here, is so that
143 ;; blockquote detection doesn't interfere with indented list
156 (2300 ,(concat "^\\(\\(?:.+?\\)["
161 (2400 ,(concat "^\\(["
166 (2500 ,(concat "\\(^\\|["
173 ;; "verse" text is indicated the same way as a quoted e-mail
174 ;; response: "> text", where text may contain initial whitespace
181 ;; simple table markup is supported, nothing fancy. use | to
182 ;; separate cells, || to separate header cells, and ||| for footer
193 ;; replace links in the buffer (links to other pages)
194 (2900 muse-explicit-link-regexp
0 link
)
197 (3000 muse-url-regexp
0 url
)
199 ;; bare email addresses
201 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email
)
203 "List of markup rules for publishing a page with Muse.
204 The rules given in this variable are invoked first, followed by
205 whatever rules are specified by the current style.
207 Each member of the list is either a function, or a list of the form:
209 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
211 REGEXP is a regular expression, or symbol whose value is a regular
212 expression, which is searched for using `re-search-forward'.
213 TEXT-BEGIN-GROUP is the matching group within that regexp which
214 denotes the beginning of the actual text to be marked up.
215 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
216 If it is not a string, but a function, it will be called to determine
217 what the replacement text should be (it must return a string). If it
218 is a symbol, the value of that symbol should be a string.
220 The replacements are done in order, one rule at a time. Writing
221 the regular expressions can be a tricky business. Note that case
222 is never ignored. `case-fold-search' is always bound to nil
223 while processing the markup rules."
224 :type
'(repeat (choice
225 (list :tag
"Markup rule"
227 (choice regexp symbol
)
229 (choice string function symbol
))
231 :group
'muse-publish
)
233 (defcustom muse-publish-markup-functions
234 '((directive . muse-publish-markup-directive
)
235 (comment . muse-publish-markup-comment
)
236 (anchor . muse-publish-markup-anchor
)
237 (tag . muse-publish-markup-tag
)
238 (word . muse-publish-markup-word
)
239 (emdash . muse-publish-markup-emdash
)
240 (enddots . muse-publish-markup-enddots
)
241 (dots . muse-publish-markup-dots
)
242 (rule . muse-publish-markup-rule
)
243 (no-break-space . muse-publish-markup-no-break-space
)
244 (heading . muse-publish-markup-heading
)
245 (footnote . muse-publish-markup-footnote
)
246 (fn-sep . muse-publish-markup-fn-sep
)
247 (list . muse-publish-markup-list
)
248 (quote . muse-publish-markup-quote
)
249 (verse . muse-publish-markup-verse
)
250 (table . muse-publish-markup-table
)
251 (email . muse-publish-markup-email
)
252 (link . muse-publish-markup-link
)
253 (url . muse-publish-markup-url
))
254 "An alist of style types to custom functions for that kind of text.
256 Each member of the list is of the form:
260 SYMBOL describes the type of text to associate with this rule.
261 `muse-publish-markup-regexps' maps regexps to these symbols.
263 FUNCTION is the function to use to mark up this kind of rule if
264 no suitable function is found through the :functions tag of the
266 :type
'(alist :key-type symbol
:value-type function
)
267 :group
'muse-publish
)
269 (defcustom muse-publish-markup-tags
270 '(("contents" nil t muse-publish-contents-tag
)
271 ("verse" t nil muse-publish-verse-tag
)
272 ("example" t nil muse-publish-example-tag
)
273 ("code" t nil muse-publish-code-tag
)
274 ("literal" t nil muse-publish-mark-read-only
)
275 ("verbatim" t nil muse-publish-verbatim-tag
)
276 ("lisp" t nil muse-publish-lisp-tag
)
277 ("class" t t muse-publish-class-tag
)
278 ("command" t t muse-publish-command-tag
)
279 ("comment" t nil muse-publish-comment-tag
))
280 "A list of tag specifications, for specially marking up text.
281 XML-style tags are the best way to add custom markup to Muse.
282 This is easily accomplished by customizing this list of markup tags.
284 For each entry, the name of the tag is given, whether it expects
285 a closing tag and/or an optional set of attributes, and a
286 function that performs whatever action is desired within the
289 The tags themselves are deleted during publishing, before the
290 function is called. The function is called with three arguments,
291 the beginning and end of the region surrounded by the tags. If
292 properties are allowed, they are passed as a third argument in
293 the form of an alist. The `end' argument to the function is
296 Point is always at the beginning of the region within the tags, when
297 the function is called. Wherever point is when the function finishes
298 is where tag markup will resume.
300 These tag rules are processed once at the beginning of markup, and
301 once at the end, to catch any tags which may have been inserted
303 :type
'(repeat (list (string :tag
"Markup tag")
304 (boolean :tag
"Expect closing tag" :value t
)
305 (boolean :tag
"Parse attributes" :value nil
)
307 :group
'muse-publish
)
309 (defcustom muse-publish-markup-specials nil
310 "A table of characters which must be represented specially."
311 :type
'(alist :key-type character
:value-type string
)
312 :group
'muse-publish
)
314 (defvar muse-publishing-p nil
315 "Set to t while a page is being published.")
316 (defvar muse-batch-publishing-p nil
317 "Set to t while a page is being batch published.")
318 (defvar muse-publishing-styles nil
)
319 (defvar muse-publishing-current-file nil
)
320 (defvar muse-publishing-current-style nil
)
321 (defvar muse-publishing-directives nil
322 "An alist of publishing directives from the top of a file.")
323 (defvar muse-publish-generate-contents nil
324 "Non-nil if a table of contents should be generated.
325 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
326 tell where the <contents> was seen, and to what depth the
327 contents were requested.")
328 (defvar muse-publishing-last-position nil
329 "Last position of the point when publishing.
330 This is used to make sure that publishing doesn't get stalled.")
332 ;; Functions for handling style information
334 (defsubst muse-style
(&optional style
)
335 "Resolve the given STYLE into a Muse style, if it is a string."
337 muse-publishing-current-style
339 (assoc style muse-publishing-styles
)
340 (muse-assert (consp style
))
343 (defun muse-define-style (name &rest elements
)
344 (let ((entry (assoc name muse-publishing-styles
)))
346 (error "There is already a style named %s." name
)
347 (setq muse-publishing-styles
348 (cons (append (list name
) elements
)
349 muse-publishing-styles
)))))
351 (defun muse-derive-style (new-name base-name
&rest elements
)
352 (let ((entry (assoc new-name muse-publishing-styles
)))
354 (error "There is already a style named %s." new-name
)
355 (apply 'muse-define-style new-name
356 (append elements
(list :base base-name
))))))
358 (defsubst muse-get-keyword
(keyword list
&optional direct
)
359 (let ((value (cadr (memq keyword list
))))
360 (if (and (not direct
) (symbolp value
))
364 (defun muse-style-elements-list (elem &optional style
)
365 "Return a list all references to ELEM in STYLE, including base styles.
366 If STYLE is not specified, use current style."
369 (setq style
(muse-style style
))
370 (setq elements
(append elements
371 (muse-get-keyword elem style
)))
372 (setq style
(muse-get-keyword :base style
)))
375 (defsubst muse-style-element
(elem &optional style direct
)
376 "Search for ELEM in STYLE, including base styles.
377 If STYLE is not specified, use current style."
378 (setq style
(muse-style style
))
379 (let ((value (muse-get-keyword elem style direct
)))
382 (let ((base (muse-get-keyword :base style
)))
384 (muse-style-element elem base direct
))))))
386 (defun muse-find-markup-element (keyword ident style
)
387 (let ((def (assq ident
(muse-style-element keyword style
))))
390 (let ((base (muse-style-element :base style
)))
392 (muse-find-markup-element keyword ident base
))))))
394 (defsubst muse-markup-text
(ident &rest args
)
395 (let ((text (muse-find-markup-element :strings ident
(muse-style))))
397 (apply 'format text args
)
400 (defun muse-find-markup-tag (keyword tagname style
)
401 (let ((def (assoc tagname
(muse-style-element keyword style
))))
403 (let ((base (muse-style-element :base style
)))
405 (muse-find-markup-tag keyword tagname base
))))))
407 (defsubst muse-markup-tag-info
(tagname &rest args
)
408 (let ((tag-info (muse-find-markup-tag :tags tagname
(muse-style))))
410 (assoc (match-string 1) muse-publish-markup-tags
))))
412 (defsubst muse-markup-function
(category)
413 (let ((func (muse-find-markup-element :functions category
(muse-style))))
415 (cdr (assq category muse-publish-markup-functions
)))))
417 ;; Publishing routines
419 (defun muse-publish-markup (name rules
)
420 (let* ((case-fold-search nil
)
421 (inhibit-read-only t
)
422 (limit (* (length rules
) (point-max)))
423 (verbose (and muse-publish-report-threshhold
424 (> (point-max) muse-publish-report-threshhold
)))
427 (goto-char (point-min))
428 (let ((regexp (nth 1 (car rules
)))
429 (group (nth 2 (car rules
)))
430 (repl (nth 3 (car rules
)))
432 (setq muse-publishing-last-position nil
)
434 (setq regexp
(symbol-value regexp
)))
435 (if (and verbose
(not muse-batch-publishing-p
))
436 (message "Publishing %s...%d%%" name
437 (* (/ (float (+ (point) base
)) limit
) 100)))
438 (while (and regexp
(setq pos
(re-search-forward regexp nil t
)))
439 (if (and verbose
(not muse-batch-publishing-p
))
440 (message "Publishing %s...%d%%" name
441 (* (/ (float (+ (point) base
)) limit
) 100)))
442 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
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 (defun muse-insert-file-or-string (file-or-string &optional title
)
468 (let ((beg (point)) end
)
469 (if (and (not (string-equal file-or-string
""))
470 (file-readable-p file-or-string
))
471 (setq end
(+ beg
(cadr (insert-file-contents file-or-string
))))
472 (insert file-or-string
)
475 (narrow-to-region beg end
)
476 (muse-publish-markup (or title
"")
477 '((100 "<\\(lisp\\)>" 0
478 muse-publish-markup-tag
))))))
480 (defun muse-style-run-hooks (keyword style
&rest args
)
482 (while (and style
(not handled
))
483 (setq style
(muse-style style
))
484 (let ((func (muse-get-keyword keyword style t
)))
486 (if (apply func args
)
489 (setq style
(muse-style-element :base style
))))
492 (defun muse-publish-markup-region (beg end title style
)
493 "Apply the given STYLE's markup rules to the given region."
495 (narrow-to-region beg end
)
496 (muse-style-run-hooks :before style
)
499 (sort (copy-alist (append muse-publish-markup-regexps
500 (muse-style-elements-list :regexps style
)))
503 (< (car l
) (car r
))))))
504 (muse-style-run-hooks :before-end style
)))
506 (defun muse-publish-markup-buffer (title style
)
507 "Apply the given STYLE's markup rules to the current buffer."
508 (setq style
(muse-style style
))
509 (let ((style-header (muse-style-element :header style
))
510 (style-footer (muse-style-element :footer style
))
511 (muse-publishing-current-style style
)
512 (muse-publishing-directives
513 (list (cons "title" title
)
514 (cons "author" (user-full-name))
515 (cons "date" (format-time-string
517 (nth 5 (file-attributes
518 muse-publishing-current-file
))))))
519 (muse-publishing-p t
))
520 (run-hooks 'muse-before-publish-hook
)
521 (muse-publish-markup-region (point-min) (point-max) title style
)
522 (goto-char (point-min))
523 (if style-header
(muse-insert-file-or-string style-header title
))
524 (goto-char (point-max))
525 (if style-footer
(muse-insert-file-or-string style-footer title
))
526 (muse-style-run-hooks :after style
)
527 (run-hooks 'muse-after-publish-hook
)))
529 (defun muse-publish-markup-string (string &optional style
)
530 "Markup STRING using the given STYLE's markup rules."
531 (setq style
(muse-style style
))
532 (muse-with-temp-buffer
534 (let ((muse-publishing-current-style style
)
535 (muse-publishing-p t
))
536 (muse-publish-markup "*string*" (muse-style-element :rules style
)))
539 ;; Commands for publishing files
541 (defsubst muse-publish-get-style
()
542 (if (= 1 (length muse-publishing-styles
))
543 (car muse-publishing-styles
)
544 (assoc (completing-read "Publish with style: "
545 muse-publishing-styles nil t
)
546 muse-publishing-styles
)))
548 (defsubst muse-publish-get-output-dir
(style)
549 (let ((default-directory (or (muse-style-element :path style
)
551 (muse-read-directory-name "Publish to directory: " nil default-directory
)))
553 (defsubst muse-publish-get-info
()
554 (let ((style (muse-publish-get-style)))
555 (list style
(muse-publish-get-output-dir style
)
556 current-prefix-arg
)))
558 (defsubst muse-publish-output-name
(&optional file style
)
559 (setq style
(muse-style style
))
560 (concat (muse-style-element :prefix style
)
561 (muse-page-name file
)
562 (muse-style-element :suffix style
)))
564 (defsubst muse-publish-output-file
(file &optional output-dir style
)
565 (setq style
(muse-style style
))
567 (expand-file-name (muse-publish-output-name file style
) output-dir
)
568 (concat (file-name-directory file
)
569 (muse-publish-output-name file style
))))
571 (defsubst muse-publish-link-name
(&optional file style
)
572 (setq style
(muse-style style
))
573 (concat (muse-style-element :prefix style
)
574 (muse-page-name file
)
575 (or (muse-style-element :link-suffix style
)
576 (muse-style-element :suffix style
))))
578 (defsubst muse-publish-link-file
(file &optional output-dir style
)
579 (setq style
(muse-style style
))
581 (expand-file-name (muse-publish-link-name file style
) output-dir
)
582 (concat (file-name-directory file
)
583 (muse-publish-link-name file style
))))
586 (defun muse-publish-file (file style
&optional output-dir force
)
587 "Publish the given file in list FILES.
588 If the argument FORCE is nil, each file is only published if it is
589 newer than the published version. If the argument FORCE is non-nil,
590 the file is published no matter what."
591 (interactive (cons (read-file-name "Publish file: ")
592 (muse-publish-get-info)))
593 (setq style
(muse-style style
))
594 (let* ((output-path (muse-publish-output-file file output-dir style
))
595 (output-suffix (muse-style-element :osuffix style
))
596 (muse-publishing-current-file file
)
597 (target (if output-suffix
598 (concat (file-name-sans-extension output-path
)
601 (when (or force
(file-newer-than-file-p file target
))
602 (if (and muse-publish-report-threshhold
603 (> (nth 7 (file-attributes file
))
604 muse-publish-report-threshhold
))
605 (message "Publishing %s ..." file
))
606 (muse-with-temp-buffer
607 (insert-file-contents file
)
608 (muse-publish-markup-buffer (muse-page-name file
) style
)
609 (let ((backup-inhibited t
))
610 (write-file output-path
))
611 (muse-style-run-hooks :final style file output-path target
))
615 (defun muse-publish-this-file (style output-dir
&optional force
)
616 "Publish the page in the current file."
617 (interactive (muse-publish-get-info))
618 (unless (muse-publish-file buffer-file-name style output-dir force
)
619 (message (concat "The published version is up-to-date; use"
620 " C-u C-c C-t to force an update."))))
622 (defun muse-batch-publish-files ()
623 "Publish Muse files in batch mode."
624 (let ((muse-batch-publishing-p t
)
625 style-name style output-dir
)
626 (setq style-name
(car command-line-args-left
)
627 style
(muse-style style-name
)
628 command-line-args-left
(cdr command-line-args-left
)
629 output-dir
(car command-line-args-left
)
631 (if (string-match "\\`--output-dir=" output-dir
)
633 (substring output-dir
(match-end 0))
634 (setq command-line-args-left
(cdr command-line-args-left
)))))
636 (error "There is no style '%s' defined." style-name
))
637 (dolist (file command-line-args-left
)
638 (muse-publish-file file style output-dir t
))))
640 ;; Default publishing rules
642 (defun muse-publish-section-close (depth)
643 "Seach forward for the closing tag of given DEPTH."
646 (while (and (setq not-end
(re-search-forward
647 (concat "^\\*\\{1," (number-to-string depth
)
650 (get-text-property (match-beginning 0) 'read-only
)))
653 (goto-char (point-max)))
654 (cond ((not (eq (char-before) ?
\n))
656 ((not (eq (char-before (1- (point))) ?
\n))
658 (insert (muse-markup-text 'section-close depth
) "\n"))))
660 (defun muse-publish-markup-directive (&optional name value
)
661 (unless name
(setq name
(match-string 1)))
662 (unless value
(setq value
(match-string 2)))
663 (let ((elem (assoc name muse-publishing-directives
)))
666 (setq muse-publishing-directives
667 (cons (cons name value
)
668 muse-publishing-directives
))))
669 ;; Make sure we don't ever try to move the point forward (past the
670 ;; beginning of buffer) while we're still searching for directives.
671 (setq muse-publishing-last-position nil
)
672 (delete-region (match-beginning 0) (match-end 0)))
674 (defun muse-publish-markup-anchor () "")
676 (defun muse-publish-markup-comment ()
677 (if (null muse-publish-comments-p
)
679 (goto-char (match-end 0))
680 (insert (muse-markup-text 'comment-end
))
681 (muse-publish-mark-read-only (match-beginning 1) (point))
682 (goto-char (match-beginning 1))
683 (insert (muse-markup-text 'comment-begin
))
684 (delete-region (match-beginning 0) (1- (match-beginning 1)))))
686 (defun muse-publish-markup-tag ()
687 (let ((tag-info (muse-markup-tag-info (match-string 1))))
689 (not (get-text-property (match-beginning 0) 'read-only
)))
690 (let ((closed-tag (match-string 3))
691 (start (match-beginning 0))
694 (when (nth 2 tag-info
)
695 (let ((attrstr (match-string 2)))
697 (string-match (concat "\\([^"
702 (let ((attr (cons (downcase
703 (muse-match-string-no-properties 1 attrstr
))
704 (muse-match-string-no-properties 3 attrstr
))))
705 (setq attrstr
(replace-match "" t t attrstr
))
707 (nconc attrs
(list attr
))
708 (setq attrs
(list attr
)))))))
709 (if (and (cadr tag-info
) (not closed-tag
))
710 (if (search-forward (concat "</" (car tag-info
) ">") nil t
)
711 (delete-region (match-beginning 0) (point))
712 (setq tag-info nil
)))
714 (setq end
(point-marker))
715 (delete-region start beg
)
717 (let ((args (list start end
)))
719 (nconc args
(list attrs
)))
720 (apply (nth 3 tag-info
) args
))))))
723 (defun muse-publish-escape-specials (beg end
&optional ignore-read-only
)
724 "Escape specials from BEG to END using style-specific :specials.
725 If IGNORE-READ-ONLY is non-nil, ignore the read-only property."
728 (narrow-to-region beg end
)
729 (goto-char (point-min))
730 (while (< (point) (point-max))
731 (if (and (not ignore-read-only
) (get-text-property (point) 'read-only
))
734 (or (assoc (char-after) (muse-style-element :specials
))
735 (assoc (char-after) muse-publish-markup-specials
))))
739 (insert-before-markers (cdr repl
)))))))))
741 (defun muse-publish-markup-word ()
742 (let* ((beg (match-beginning 2))
743 (end (1- (match-end 2)))
744 (leader (buffer-substring-no-properties beg end
))
745 open-tag close-tag mark-read-only loc
)
747 ((string= leader
"_")
748 (setq open-tag
(muse-markup-text 'begin-underline
)
749 close-tag
(muse-markup-text 'end-underline
)))
750 ((string= leader
"=")
751 (setq open-tag
(muse-markup-text 'begin-literal
)
752 close-tag
(muse-markup-text 'end-literal
))
753 (setq mark-read-only t
))
755 (let ((l (length leader
)))
757 ((= l
1) (setq open-tag
(muse-markup-text 'begin-emph
)
758 close-tag
(muse-markup-text 'end-emph
)))
759 ((= l
2) (setq open-tag
(muse-markup-text 'begin-more-emph
)
760 close-tag
(muse-markup-text 'end-more-emph
)))
761 ((= l
3) (setq open-tag
(muse-markup-text 'begin-most-emph
)
762 close-tag
(muse-markup-text 'end-most-emph
)))))))
763 (if (and (not (get-text-property beg
'noemphasis
))
764 (setq loc
(search-forward leader nil t
))
765 (or (eobp) (not (eq (char-syntax (char-after loc
)) ?w
)))
766 (not (eq (char-syntax (char-before (point))) ?\
))
767 (not (get-text-property (point) 'noemphasis
)))
770 (delete-region beg end
)
771 (setq end
(point-marker))
777 (muse-publish-escape-specials beg end t
)
779 (muse-publish-mark-read-only (1- beg
) (1+ end
))))
783 (defun muse-publish-markup-emdash ()
784 (delete-region (match-beginning 0) (match-end 0))
785 (insert (muse-markup-text 'emdash
))
786 (if (eq (char-after) ?\
<)
789 (defun muse-publish-markup-enddots ()
790 (unless (get-text-property (match-beginning 0) 'noemphasis
)
791 (delete-region (match-beginning 0) (match-end 0))
792 (insert (muse-markup-text 'enddots
))))
794 (defun muse-publish-markup-dots ()
795 (unless (get-text-property (match-beginning 0) 'noemphasis
)
796 (delete-region (match-beginning 0) (match-end 0))
797 (insert (muse-markup-text 'dots
))))
799 (defun muse-publish-markup-rule ()
800 (unless (get-text-property (match-beginning 0) 'noemphasis
)
801 (delete-region (match-beginning 0) (match-end 0))
802 (insert (muse-markup-text 'rule
))))
804 (defun muse-publish-markup-no-break-space ()
805 (unless (get-text-property (match-beginning 0) 'noemphasis
)
806 (delete-region (match-beginning 0) (match-end 0))
807 (insert (muse-markup-text 'no-break-space
))))
809 (defun muse-publish-markup-heading ()
810 (let* ((len (length (match-string 1)))
811 (start (muse-markup-text
812 (cond ((= len
1) 'section
)
813 ((= len
2) 'subsection
)
814 ((= len
3) 'subsubsection
)
817 (end (muse-markup-text
818 (cond ((= len
1) 'section-end
)
819 ((= len
2) 'subsection-end
)
820 ((= len
3) 'subsubsection-end
)
821 (t 'section-other-end
))
823 (delete-region (match-beginning 0) (match-end 0))
826 (if end
(insert end
))
827 (muse-publish-section-close len
)))
829 (defvar muse-publish-footnotes nil
)
831 (defun muse-publish-markup-footnote ()
832 "Scan ahead and snarf up the footnote body"
833 (if (= (muse-line-beginning-position) (match-beginning 0))
835 (let ((footnote (save-match-data
836 (string-to-number (match-string 1))))
838 (delete-region (match-beginning 0) (match-end 0))
840 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote
) nil t
)
841 (let* ((start (match-beginning 0))
842 (beg (goto-char (match-end 0)))
844 (if (search-forward "\n\n" nil t
)
845 (copy-marker (match-beginning 0))
846 (goto-char (point-max))
847 (skip-chars-backward "\n")
849 (while (re-search-forward (concat "^["
853 (replace-match "\\1" t
))
854 (let ((footnotemark-cmd (muse-markup-text 'footnotemark
))
855 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end
)))
856 (if (string= "" footnotemark-cmd
)
858 (concat (muse-markup-text 'footnote
)
859 (buffer-substring-no-properties beg end
)
860 (muse-markup-text 'footnote-end
)))
861 (setq footnotemark
(format footnotemark-cmd footnote
862 footnotemark-end-cmd
))
863 (unless muse-publish-footnotes
864 (set (make-local-variable 'muse-publish-footnotes
)
865 (make-vector 256 nil
)))
866 (unless (aref muse-publish-footnotes footnote
)
870 (concat (format (muse-markup-text 'footnotetext
)
872 (buffer-substring-no-properties beg end
)
873 (muse-markup-text 'footnotetext-end
))))
874 (aset muse-publish-footnotes footnote footnotemark
))))
876 (skip-chars-forward "\n")
877 (delete-region start
(point)))))
878 (insert (or footnotemark footnote
)))))
880 (defun muse-publish-markup-fn-sep ()
881 (delete-region (match-beginning 0) (match-end 0))
882 (insert (muse-markup-text 'fn-sep
)))
884 (defun muse-publish-surround-text (beg-tag end-tag move-func
)
886 (skip-chars-backward (concat muse-regexp-blank
"\n"))
887 (delete-region (point) beg
)
888 (insert "\n\n" beg-tag
)
892 (narrow-to-region beg
(point))
893 (goto-char (point-min))
894 (while (< (point) (point-max))
895 (if (looking-at "^\\s-+")
899 (skip-chars-backward (concat muse-regexp-blank
"\n"))
900 (delete-region (point) beg
))
901 (insert end-tag
"\n"))
903 (defsubst muse-forward-paragraph
(&optional pattern
)
904 (if (re-search-forward (if pattern
905 (concat "^\\(" pattern
"["
908 "^\\s-*\\($\\|\\'\\)") nil t
)
909 (goto-char (match-beginning 0))
910 (goto-char (point-max))))
912 (defun muse-publish-markup-list ()
913 "Markup a list entry or quoted paragraph.
914 The reason this function is so funky, is to prevent text properties
915 like read-only from being inadvertently deleted."
916 (let ((str (match-string 1)))
918 ((and (eq (aref str
0) ?-
))
919 (delete-region (match-beginning 0) (match-end 0))
920 (muse-publish-surround-text
921 (muse-markup-text 'begin-uli
)
922 (muse-markup-text 'end-uli
)
925 (muse-forward-paragraph (concat "["
929 (string-match "\\`[0-9]+\\." str
))
930 (delete-region (match-beginning 0) (match-end 0))
931 (muse-publish-surround-text
932 (muse-markup-text 'begin-oli
)
933 (muse-markup-text 'end-oli
)
936 (muse-forward-paragraph (concat "["
940 (goto-char (match-beginning 1))
941 (insert (muse-markup-text 'begin-ddt
))
945 (while (looking-at (concat "^\\([" muse-regexp-blank
946 "]*\\)[^" muse-regexp-blank
948 (delete-region (match-beginning 1) (match-end 1))
951 (when (re-search-forward (concat "[" muse-regexp-blank
952 "\n]+::[" muse-regexp-blank
955 (replace-match (muse-markup-text 'start-dde
))))
956 (muse-forward-paragraph)
957 (insert (muse-markup-text 'end-ddt
) ?
\n)))))
959 (defun muse-publish-markup-quote ()
960 "Markup a list entry or quoted paragraph.
961 The reason this function is so funky, is to prevent text properties
962 like read-only from being inadvertently deleted."
963 (let* ((ws (match-string 0))
964 (centered (>= (string-width ws
) 6))
965 (begin-elem (if centered
'begin-center
'begin-quote
))
966 (end-elem (if centered
'end-center
'end-quote
)))
967 (muse-publish-surround-text (muse-markup-text begin-elem
)
968 (muse-markup-text end-elem
)
969 'muse-forward-paragraph
)))
971 (defun muse-publish-markup-leading-space ()
972 (let ((markup-space (muse-markup-text 'verse-space
))
974 (when (and markup-space
975 (>= (setq count
(skip-chars-forward " ")) 0))
976 (delete-region (muse-line-beginning-position) (point))
978 (insert markup-space
)
979 (setq count
(- count
2))))))
981 (defun muse-publish-markup-verse ()
982 (let ((leader (match-string 0)))
983 (goto-char (match-beginning 0))
984 (insert (muse-markup-text 'begin-verse
))
985 (while (looking-at leader
)
987 (muse-publish-markup-leading-space)
992 (let ((text (muse-markup-text 'empty-verse-line
)))
993 (if text
(insert text
))))
997 (or (looking-at (concat leader
"["
1000 (not (looking-at leader
)))))
1001 (let ((begin-text (muse-markup-text 'begin-last-stanza-line
))
1002 (end-text (muse-markup-text 'end-last-stanza-line
)))
1003 (when end-text
(insert end-text
))
1005 (when begin-text
(insert begin-text
))
1008 (let ((begin-text (muse-markup-text 'begin-verse-line
))
1009 (end-text (muse-markup-text 'end-verse-line
)))
1010 (when end-text
(insert end-text
))
1012 (when begin-text
(insert begin-text
))
1015 (insert (muse-markup-text 'end-verse
) ?
\n))
1017 (defun muse-publish-markup-table ()
1018 "Style does not support tables.")
1020 (defun muse-publish-escape-specials-in-string (string &rest ignored
)
1021 "Escape specials in STRING using style-specific :specials."
1023 (apply (function concat
)
1027 (or (assoc ch
(muse-style-element :specials
))
1028 (assoc ch muse-publish-markup-specials
))))
1032 (append string nil
)))))
1034 (defun muse-publish-markup-email ()
1035 (let* ((beg (match-end 1))
1036 (addr (buffer-substring-no-properties beg
(match-end 0))))
1037 (setq addr
(muse-publish-escape-specials-in-string addr
))
1039 (delete-region beg
(match-end 0))
1040 (if (or (eq (char-before (match-beginning 0)) ?
\")
1041 (eq (char-after (match-end 0)) ?
\"))
1043 (insert (format (muse-markup-text 'email-addr
) addr addr
)))
1044 (muse-publish-mark-read-only beg
(point))))
1046 (defun muse-publish-url (url &optional desc explicit
)
1047 "Resolve a URL into its final <a href> form."
1048 (let ((orig-url url
))
1049 (dolist (transform muse-publish-url-transforms
)
1050 (setq url
(save-match-data (when url
(funcall transform url explicit
)))))
1051 (dolist (transform muse-publish-desc-transforms
)
1052 (setq desc
(save-match-data
1053 (when desc
(funcall transform desc explicit
)))))
1055 (cond ((string-match muse-image-regexp url
)
1057 (muse-markup-text 'image-with-desc url desc
)
1058 (muse-markup-text 'image-link url
)))
1059 ((and desc
(string-match muse-image-regexp desc
))
1060 (muse-markup-text 'url-with-image url desc
))
1061 ((eq (aref url
0) ?\
#)
1062 (muse-markup-text 'internal-link
(substring url
1)
1063 (or desc orig-url
)))
1065 (muse-markup-text 'url-link url
(or desc orig-url
))))
1068 (defun muse-publish-insert-url (url &optional desc explicit
)
1069 "Resolve a URL into its final <a href> form."
1070 (delete-region (match-beginning 0) (match-end 0))
1072 (text (muse-publish-url url desc explicit
)))
1075 (muse-publish-mark-read-only beg
(point)))))
1077 (defun muse-publish-markup-link ()
1078 (let (desc explicit link
)
1079 (setq explicit
(save-match-data
1080 (if (string-match muse-explicit-link-regexp
1083 (setq desc
(if explicit
(match-string 2) (match-string 0)))
1084 (setq link
(if explicit
1085 (muse-handle-explicit-link (match-string 1))
1086 (muse-handle-implicit-link (match-string 0))))
1089 (not (or (eq (char-before (match-beginning 0)) ?
\")
1090 (eq (char-after (match-end 0)) ?
\")))))
1091 (muse-publish-insert-url link desc explicit
))))
1093 (defun muse-publish-markup-url ()
1094 (if (not (or (eq (char-before (match-beginning 0)) ?
\")
1095 (eq (char-after (match-end 0)) ?
\")))
1096 (muse-publish-insert-url (match-string 0))
1097 (let ((beg (match-beginning 0))
1098 (url (match-string 0)))
1099 (delete-region (match-beginning 0) (match-end 0))
1100 (insert (muse-publish-escape-specials-in-string url
))
1101 (muse-publish-mark-read-only beg
(point)))))
1103 ;; Default publishing tags
1105 (defun muse-publish-contents-tag (beg end attrs
)
1106 (set (make-local-variable 'muse-publish-generate-contents
)
1107 (cons (copy-marker (point) t
)
1108 (let ((depth (cdr (assoc "depth" attrs
))))
1109 (or (and depth
(string-to-number depth
)) 2)))))
1111 (defun muse-publish-verse-tag (beg end
)
1114 (narrow-to-region beg end
)
1115 (goto-char (point-min))
1116 (while (eq ?\
(char-syntax (char-after)))
1118 (while (< (point) (point-max))
1121 (if (eq ?\
(char-syntax (char-before)))
1122 (delete-char -
1)))))
1124 (defun muse-publish-mark-read-only (beg end
)
1125 (add-text-properties beg end
'(rear-nonsticky (read-only) read-only t
))
1128 (defun muse-publish-mark-noemphasis (&optional beg end
)
1129 (unless beg
(setq beg
(match-beginning 0)))
1130 (unless end
(setq end
(match-end 0)))
1131 (add-text-properties beg end
'(noemphasis t
))
1134 (defun muse-publish-code-tag (beg end
)
1135 (muse-publish-escape-specials beg end
)
1137 (insert (muse-markup-text 'begin-literal
))
1139 (insert (muse-markup-text 'end-literal
))
1140 (muse-publish-mark-read-only beg
(point)))
1142 (defun muse-publish-example-tag (beg end
)
1143 (muse-publish-escape-specials beg end
)
1145 (insert (muse-markup-text 'begin-example
))
1147 (insert (muse-markup-text 'end-example
))
1148 (muse-publish-mark-read-only beg
(point)))
1150 (defun muse-publish-verbatim-tag (beg end
)
1151 (muse-publish-escape-specials beg end
)
1152 (muse-publish-mark-read-only beg end
))
1154 (defalias 'muse-publish-class-tag
'ignore
)
1156 (defun muse-publish-lisp-tag (beg end
)
1158 (let ((str (muse-eval-lisp
1161 (buffer-substring-no-properties beg end
)
1163 (delete-region beg end
)))))
1164 (set-text-properties 0 (length str
) nil str
)
1167 (defun muse-publish-command-tag (beg end attrs
)
1168 (while (looking-at "\\s-*$")
1170 (let ((interp (cdr (assoc "interp" attrs
))))
1174 (buffer-substring-no-properties (point) end
)
1175 (delete-region beg end
)) t
)
1176 (shell-command-on-region beg end interp t t
))
1177 (muse-publish-mark-read-only beg
(point))))
1179 (defun muse-publish-comment-tag (beg end
)
1180 (if (null muse-publish-comments-p
)
1181 (delete-region beg end
)
1183 (insert (muse-markup-text 'comment-end
))
1184 (muse-publish-mark-read-only beg
(point))
1186 (insert (muse-markup-text 'comment-begin
))))
1188 ;; Miscellaneous helper functions
1190 (defsubst muse-publishing-directive
(name)
1191 (cdr (assoc name muse-publishing-directives
)))
1193 (defun muse-publish-strip-tags (string)
1194 "Remove all tags from the string."
1195 (while (string-match "<.*?>" string
)
1196 (setq string
(replace-match "" nil t string
)))
1199 (defun muse-publish-markup-type (category default-func
)
1200 (let ((rule (muse-find-markup-element :overrides category
(muse-style))))
1201 (funcall (or rule default-func
))))
1203 (defun muse-published-buffer-contents (buffer)
1204 (with-current-buffer buffer
1205 (goto-char (point-min))
1206 (let ((beg (and (search-forward "Emacs Muse begins here")
1207 (muse-line-end-position)))
1208 (end (and (search-forward "Emacs Muse ends here")
1209 (muse-line-beginning-position))))
1210 (buffer-substring-no-properties beg end
))))
1212 (defun muse-published-contents (file)
1213 (when (file-readable-p file
)
1214 (muse-with-temp-buffer
1215 (insert-file-contents file
)
1216 (muse-published-buffer-contents (current-buffer)))))
1218 (defun muse-publish-transform-output
1219 (file temp-file output-path name gen-func
&rest cleanup-exts
)
1220 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1221 (setq file
(muse-page-name file
))
1222 (message "Generating %s output for %s..." name file
)
1223 (if (not (funcall gen-func temp-file output-path
))
1224 (message "Generating %s from %s...failed" name file
)
1225 (message "Generating %s output for %s...done" name file
)
1226 (muse-delete-file-if-exists temp-file
)
1227 (dolist (ext cleanup-exts
)
1228 (muse-delete-file-if-exists
1229 (expand-file-name (concat file ext
)
1230 (file-name-directory output-path
))))
1231 (message "Wrote %s" output-path
)))
1233 (defun muse-publish-read-only (string)
1234 (let ((end (1- (length string
))))
1235 (add-text-properties 0 end
1236 '(rear-nonsticky (read-only) read-only t
)
1240 (defun muse-publish-prepare-url (target &rest ignored
)
1241 "Transform anchors and get published name, if TARGET is a page."
1243 (unless (or (string-match muse-url-regexp target
)
1244 (string-match muse-image-regexp target
)
1245 (string-match muse-file-regexp target
))
1246 (setq target
(if (string-match "#" target
)
1247 (if (eq (aref target
0) ?\
#)
1249 (concat (muse-publish-link-name
1250 (substring target
0 (match-beginning 0)))
1251 "#" (substring target
(match-end 0))))
1252 (muse-publish-link-name target
)))))
1255 (provide 'muse-publish
)
1257 ;;; muse-publish.el ends here