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'.
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.
45 See `muse-publish' for more information."
48 (defcustom muse-before-publish-hook nil
49 "A hook run in the buffer to be published, before it is done."
53 (defcustom muse-after-publish-hook nil
54 "A hook run in the buffer to be published, after it is done."
58 (defcustom muse-publish-url-transforms
'(muse-publish-escape-specials-in-string
59 muse-publish-prepare-url
)
60 "A list of functions used to prepare URLs for publication.
61 Each is passed the URL and expects a URL to be returned."
63 :options
'(muse-publish-escape-specials-in-string
64 muse-publish-prepare-url
)
67 (defcustom muse-publish-report-threshhold
100000
68 "If a file is this size or larger, report publishing progress."
72 (defcustom muse-publish-markup-regexps
73 `(;; Remove leading and trailing whitespace from the file
74 (1000 "\\(\\`\n+\\|\n+\\'\\)" 0 "")
76 ;; Remove trailing whitespace from all lines
77 (1100 ,(concat "[" muse-regexp-blank
"]+$") 0 "")
79 ;; Handle any leading #directives
80 (1200 "\\`#\\([a-zA-Z]+\\)\\s-+\\(.+\\)\n+" 0 directive
)
83 (1300 muse-tag-regexp
0 tag
)
86 (1350 "^;\\s-+\\(.+\\)" 0 comment
)
88 ;; define anchor points
89 (1400 "^#\\(\\S-+\\)\\s-*" 0 anchor
)
91 ;; prevent emphasis characters in explicit links from being marked
92 (1500 muse-explicit-link-regexp
0 muse-publish-mark-noemphasis
)
94 ;; emphasized or literal text
107 ;; headings, outline-mode style
108 (1700 "^\\(\\*+\\)\\s-+" 0 heading
)
111 (1800 "\\.\\.\\.\\." 0 enddots
)
112 (1850 "\\.\\.\\." 0 dots
)
114 ;; horizontal rule, or section separator
115 (1900 "^----+" 0 rule
)
117 ;; beginning of footnotes section
118 (2000 "^Footnotes:?\\s-*" 0 fn-sep
)
119 ;; footnote definition/reference (def if at beginning of line)
120 (2100 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote
)
122 ;; unnumbered List items begin with a -. numbered list items
123 ;; begin with number and a period. definition lists have a
124 ;; leading term separated from the body with ::. centered
125 ;; paragraphs begin with at least six columns of whitespace; any
126 ;; other whitespace at the beginning indicates a blockquote. The
127 ;; reason all of these rules are handled here, is so that
128 ;; blockquote detection doesn't interfere with indented list
141 (2300 ,(concat "^\\(\\(?:.+?\\)["
146 (2400 ,(concat "^\\(["
151 (2500 ,(concat "\\(^\\|["
158 ;; "verse" text is indicated the same way as a quoted e-mail
159 ;; response: "> text", where text may contain initial whitespace
166 ;; simple table markup is supported, nothing fancy. use | to
167 ;; separate cells, || to separate header cells, and ||| for footer
178 ;; replace links in the buffer (links to other pages)
179 (2900 muse-explicit-link-regexp
0 link
)
182 (3000 muse-url-regexp
0 url
)
184 ;; bare email addresses
186 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email
)
188 "List of markup rules for publishing a page with Muse.
189 The rules given in this variable are invoked first, followed by
190 whatever rules are specified by the current style.
192 Each member of the list is either a function, or a list of the form:
194 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
196 REGEXP is a regular expression, or symbol whose value is a regular
197 expression, which is searched for using `re-search-forward'.
198 TEXT-BEGIN-GROUP is the matching group within that regexp which
199 denotes the beginning of the actual text to be marked up.
200 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
201 If it is not a string, but a function, it will be called to determine
202 what the replacement text should be (it must return a string). If it
203 is a symbol, the value of that symbol should be a string.
205 The replacements are done in order, one rule at a time. Writing
206 the regular expressions can be a tricky business. Note that case
207 is never ignored. `case-fold-search' is always bound to nil
208 while processing the markup rules."
209 :type
'(repeat (choice
210 (list :tag
"Markup rule"
212 (choice regexp symbol
)
214 (choice string function symbol
))
216 :group
'muse-publish
)
218 (defcustom muse-publish-markup-functions
219 '((directive . muse-publish-markup-directive
)
220 (comment . muse-publish-markup-comment
)
221 (anchor . muse-publish-markup-anchor
)
222 (tag . muse-publish-markup-tag
)
223 (word . muse-publish-markup-word
)
224 (emdash . muse-publish-markup-emdash
)
225 (enddots . muse-publish-markup-enddots
)
226 (dots . muse-publish-markup-dots
)
227 (rule . muse-publish-markup-rule
)
228 (heading . muse-publish-markup-heading
)
229 (footnote . muse-publish-markup-footnote
)
230 (fn-sep . muse-publish-markup-fn-sep
)
231 (list . muse-publish-markup-list
)
232 (quote . muse-publish-markup-quote
)
233 (verse . muse-publish-markup-verse
)
234 (table . muse-publish-markup-table
)
235 (email . muse-publish-markup-email
)
236 (link . muse-publish-markup-link
)
237 (url . muse-publish-markup-url
))
238 "An alist of style types to custom functions for that kind of text.
240 Each member of the list is of the form:
244 SYMBOL describes the type of text to associate with this rule.
245 `muse-publish-markup-regexps' maps regexps to these symbols.
247 FUNCTION is the function to use to mark up this kind of rule if
248 no suitable function is found through the :functions tag of the
250 :type
'(alist :key-type symbol
:value-type function
)
251 :group
'muse-publish
)
253 (defcustom muse-publish-markup-tags
254 '(("contents" nil t muse-publish-contents-tag
)
255 ("verse" t nil muse-publish-verse-tag
)
256 ("example" t nil muse-publish-example-tag
)
257 ("literal" t nil muse-publish-mark-read-only
)
258 ("verbatim" t nil muse-publish-verbatim-tag
)
259 ("lisp" t nil muse-publish-lisp-tag
)
260 ("class" t t muse-publish-class-tag
)
261 ("command" t t muse-publish-command-tag
)
262 ("comment" t nil muse-publish-comment-tag
))
263 "A list of tag specifications, for specially marking up text.
264 XML-style tags are the best way to add custom markup to Muse.
265 This is easily accomplished by customizing this list of markup tags.
267 For each entry, the name of the tag is given, whether it expects
268 a closing tag and/or an optional set of attributes, and a
269 function that performs whatever action is desired within the
272 The tags themselves are deleted during publishing, before the
273 function is called. The function is called with three arguments,
274 the beginning and end of the region surrounded by the tags. If
275 properties are allowed, they are passed as a third argument in
276 the form of an alist. The `end' argument to the function is
279 Point is always at the beginning of the region within the tags, when
280 the function is called. Wherever point is when the function finishes
281 is where tag markup will resume.
283 These tag rules are processed once at the beginning of markup, and
284 once at the end, to catch any tags which may have been inserted
286 :type
'(repeat (list (string :tag
"Markup tag")
287 (boolean :tag
"Expect closing tag" :value t
)
288 (boolean :tag
"Parse attributes" :value nil
)
290 :group
'muse-publish
)
292 (defcustom muse-publish-markup-specials nil
293 "A table of characters which must be represented specially."
294 :type
'(alist :key-type character
:value-type string
)
295 :group
'muse-publish
)
297 (defvar muse-publishing-p nil
298 "Set to t while a page is being published.")
299 (defvar muse-batch-publishing-p nil
300 "Set to t while a page is being batch published.")
301 (defvar muse-publishing-styles nil
)
302 (defvar muse-publishing-current-file nil
)
303 (defvar muse-publishing-current-style nil
)
304 (defvar muse-publishing-directives nil
305 "An alist of publishing directives from the top of a file.")
306 (defvar muse-publish-generate-contents nil
307 "Non-nil if a table of contents should be generated.
308 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
309 tell where the <contents> was seen, and to what depth the
310 contents were requested.")
312 ;; Functions for handling style information
314 (defsubst muse-style
(&optional style
)
315 "Resolve the given STYLE into a Muse style, if it is a string."
317 muse-publishing-current-style
319 (assoc style muse-publishing-styles
)
320 (muse-assert (consp style
))
323 (defun muse-define-style (name &rest elements
)
324 (let ((entry (assoc name muse-publishing-styles
)))
326 (error "There is already a style named %s." name
)
327 (setq muse-publishing-styles
328 (cons (append (list name
) elements
)
329 muse-publishing-styles
)))))
331 (defun muse-derive-style (new-name base-name
&rest elements
)
332 (let ((entry (assoc new-name muse-publishing-styles
)))
334 (error "There is already a style named %s." new-name
)
335 (apply 'muse-define-style new-name
336 (append elements
(list :base base-name
))))))
338 (defsubst muse-get-keyword
(keyword list
&optional direct
)
339 (let ((value (cadr (memq keyword list
))))
340 (if (and (not direct
) (symbolp value
))
344 (defun muse-style-elements-list (elem &optional style
)
345 "Return a list all references to ELEM in STYLE, including base styles.
346 If STYLE is not specified, use current style."
349 (setq style
(muse-style style
))
350 (setq elements
(append elements
351 (muse-get-keyword elem style
)))
352 (setq style
(muse-get-keyword :base style
)))
355 (defsubst muse-style-element
(elem &optional style direct
)
356 "Search for ELEM in STYLE, including base styles.
357 If STYLE is not specified, use current style."
358 (setq style
(muse-style style
))
359 (let ((value (muse-get-keyword elem style direct
)))
362 (let ((base (muse-get-keyword :base style
)))
364 (muse-style-element elem base direct
))))))
366 (defun muse-find-markup-element (keyword ident style
)
367 (let ((def (assq ident
(muse-style-element keyword style
))))
370 (let ((base (muse-style-element :base style
)))
372 (muse-find-markup-element keyword ident base
))))))
374 (defsubst muse-markup-text
(ident &rest args
)
375 (let ((text (muse-find-markup-element :strings ident
(muse-style))))
377 (apply 'format text args
)
380 (defun muse-find-markup-tag (keyword tagname style
)
381 (let ((def (assoc tagname
(muse-style-element keyword style
))))
383 (let ((base (muse-style-element :base style
)))
385 (muse-find-markup-tag keyword tagname base
))))))
387 (defsubst muse-markup-tag-info
(tagname &rest args
)
388 (let ((tag-info (muse-find-markup-tag :tags tagname
(muse-style))))
390 (assoc (match-string 1) muse-publish-markup-tags
))))
392 (defsubst muse-markup-function
(category)
393 (let ((func (muse-find-markup-element :functions category
(muse-style))))
395 (cdr (assq category muse-publish-markup-functions
)))))
397 ;; Publishing routines
399 (defun muse-publish-markup (name rules
)
400 (let* ((case-fold-search nil
)
401 (inhibit-read-only t
)
402 (limit (* (length rules
) (point-max)))
403 (verbose (and muse-publish-report-threshhold
404 (> (point-max) muse-publish-report-threshhold
)))
407 (goto-char (point-min))
408 (let ((regexp (nth 1 (car rules
)))
409 (group (nth 2 (car rules
)))
410 (repl (nth 3 (car rules
)))
413 (setq regexp
(symbol-value regexp
)))
414 (if (and verbose
(not muse-batch-publishing-p
))
415 (message "Publishing %s...%d%%" name
416 (* (/ (float (+ (point) base
)) limit
) 100)))
417 (while (and regexp
(setq pos
(re-search-forward regexp nil t
)))
418 (if (and verbose
(not muse-batch-publishing-p
))
419 (message "Publishing %s...%d%%" name
420 (* (/ (float (+ (point) base
)) limit
) 100)))
421 (unless (get-text-property (match-beginning group
) 'read-only
)
425 (setq func
(muse-markup-function repl
)))
433 (replace-match text t
))))
434 (if (and last-pos
(= pos last-pos
))
438 (setq last-pos pos
)))
439 (setq rules
(cdr rules
)
440 base
(+ base
(point-max))))
441 (if (and verbose
(not muse-batch-publishing-p
))
442 (message "Publishing %s...done" name
))))
444 (defun muse-insert-file-or-string (file-or-string &optional title
)
445 (let ((beg (point)) end
)
446 (if (and (not (string-equal file-or-string
""))
447 (file-readable-p file-or-string
))
448 (setq end
(+ beg
(cadr (insert-file-contents file-or-string
))))
449 (insert file-or-string
)
452 (narrow-to-region beg end
)
453 (muse-publish-markup (or title
"")
454 '((100 "<\\(lisp\\)>" 0
455 muse-publish-markup-tag
))))))
457 (defun muse-style-run-hooks (keyword style
&rest args
)
459 (while (and style
(not handled
))
460 (setq style
(muse-style style
))
461 (let ((func (muse-get-keyword keyword style t
)))
463 (if (apply func args
)
466 (setq style
(muse-style-element :base style
))))
469 (defun muse-publish-markup-region (beg end title style
)
470 "Apply the given STYLE's markup rules to the given region."
472 (narrow-to-region beg end
)
473 (muse-style-run-hooks :before style
)
476 (sort (copy-alist (append muse-publish-markup-regexps
477 (muse-style-elements-list :regexps style
)))
480 (< (car l
) (car r
))))))
481 (muse-style-run-hooks :before-end style
)))
483 (defun muse-publish-markup-buffer (title style
)
484 "Apply the given STYLE's markup rules to the current buffer."
485 (setq style
(muse-style style
))
486 (let ((style-header (muse-style-element :header style
))
487 (style-footer (muse-style-element :footer style
))
488 (muse-publishing-current-style style
)
489 (muse-publishing-directives
490 (list (cons "title" title
)
491 (cons "author" (user-full-name))
492 (cons "date" (format-time-string "%B %e, %Y"))))
493 (muse-publishing-p t
))
494 (run-hooks 'muse-before-publish-hook
)
495 (muse-publish-markup-region (point-min) (point-max) title style
)
496 (goto-char (point-min))
497 (if style-header
(muse-insert-file-or-string style-header title
))
498 (goto-char (point-max))
499 (if style-footer
(muse-insert-file-or-string style-footer title
))
500 (muse-style-run-hooks :after style
)
501 (run-hooks 'muse-after-publish-hook
)))
503 (defun muse-publish-markup-string (string &optional style
)
504 "Markup STRING using the given STYLE's markup rules."
505 (setq style
(muse-style style
))
506 (muse-with-temp-buffer
508 (let ((muse-publishing-current-style style
)
509 (muse-publishing-p t
))
510 (muse-publish-markup "*string*" (muse-style-element :rules style
)))
513 ;; Commands for publishing files
515 (defsubst muse-publish-get-style
()
516 (if (= 1 (length muse-publishing-styles
))
517 (car muse-publishing-styles
)
518 (assoc (completing-read "Publish with style: "
519 muse-publishing-styles nil t
)
520 muse-publishing-styles
)))
522 (defsubst muse-publish-get-output-dir
(style)
523 (let ((default-directory (or (muse-style-element :path style
)
525 (read-file-name "Publish to directory: " nil default-directory
)))
527 (defsubst muse-publish-get-info
()
528 (let ((style (muse-publish-get-style)))
529 (list style
(muse-publish-get-output-dir style
)
530 current-prefix-arg
)))
532 (defsubst muse-publish-output-name
(&optional file style
)
533 (setq style
(muse-style style
))
534 (concat (muse-style-element :prefix style
)
535 (muse-page-name file
)
536 (muse-style-element :suffix style
)))
538 (defsubst muse-publish-output-file
(file &optional output-dir style
)
539 (setq style
(muse-style style
))
541 (expand-file-name (muse-publish-output-name file style
) output-dir
)
542 (concat (file-name-directory file
)
543 (muse-publish-output-name file style
))))
545 (defun muse-publish-file (file style
&optional output-dir force
)
546 "Publish the given file in list FILES.
547 If the argument FORCE is nil, each file is only published if it is
548 newer than the published version. If the argument FORCE is non-nil,
549 the file is published no matter what."
550 (interactive (cons (read-file-name "Publish file: ")
551 (muse-publish-get-info)))
552 (setq style
(muse-style style
))
553 (let* ((output-path (muse-publish-output-file file output-dir style
))
554 (output-suffix (muse-style-element :osuffix style
))
555 (muse-publishing-current-file file
)
556 (target (if output-suffix
557 (concat (file-name-sans-extension output-path
)
560 (when (or force
(file-newer-than-file-p file target
))
561 (if (and muse-publish-report-threshhold
562 (> (nth 7 (file-attributes file
))
563 muse-publish-report-threshhold
))
564 (message "Publishing %s ..." file
))
565 (muse-with-temp-buffer
566 (insert-file-contents file
)
567 (muse-publish-markup-buffer (muse-page-name file
) style
)
568 (let ((backup-inhibited t
))
569 (write-file output-path
))
570 (muse-style-run-hooks :final style file output-path target
))
573 (defun muse-publish-this-file (style output-dir
&optional force
)
574 "Publish the page in the current file."
575 (interactive (muse-publish-get-info))
576 (unless (muse-publish-file buffer-file-name style output-dir force
)
577 (message (concat "The published version is up-to-date; use"
578 " C-u C-c C-t to force an update."))))
580 (defun muse-batch-publish-files ()
581 "Publish Muse files in batch mode."
582 (let ((muse-batch-publishing-p t
)
583 style-name style output-dir
)
584 (setq style-name
(car command-line-args-left
)
585 style
(muse-style style-name
)
586 command-line-args-left
(cdr command-line-args-left
)
587 output-dir
(car command-line-args-left
)
589 (if (string-match "\\`--output-dir=" output-dir
)
591 (substring output-dir
(match-end 0))
592 (setq command-line-args-left
(cdr command-line-args-left
)))))
594 (error "There is no style '%s' defined." style-name
))
595 (dolist (file command-line-args-left
)
596 (muse-publish-file file style output-dir t
))))
598 ;; Default publishing rules
600 (defun muse-publish-section-close (depth)
601 "Seach forward for the closing tag of given DEPTH."
604 (while (and (setq not-end
(re-search-forward
605 (concat "^\\*\\{1," (number-to-string depth
)
608 (get-text-property (match-beginning 0) 'read-only
)))
611 (goto-char (point-max)))
612 (cond ((not (eq (char-before) ?
\n))
614 ((not (eq (char-before (1- (point))) ?
\n))
616 (insert (muse-markup-text 'section-close depth
) "\n"))))
618 (defun muse-publish-markup-directive (&optional name value
)
619 (unless name
(setq name
(match-string 1)))
620 (unless value
(setq value
(match-string 2)))
621 (let ((elem (assoc name muse-publishing-directives
)))
624 (setq muse-publishing-directives
625 (cons (cons name value
)
626 muse-publishing-directives
))))
627 (delete-region (match-beginning 0) (match-end 0)))
629 (defun muse-publish-markup-anchor () "")
630 (defun muse-publish-markup-comment () "")
632 (defun muse-publish-markup-tag ()
633 (let ((tag-info (muse-markup-tag-info (match-string 1))))
635 (not (get-text-property (match-beginning 0) 'read-only
)))
636 (let ((closed-tag (match-string 3))
637 (start (match-beginning 0))
638 (beg (point)) end attrs
)
639 (when (nth 2 tag-info
)
640 (let ((attrstr (match-string 2)))
642 (string-match (concat "\\([^"
647 (let ((attr (cons (downcase
648 (muse-match-string-no-properties 1 attrstr
))
649 (muse-match-string-no-properties 3 attrstr
))))
650 (setq attrstr
(replace-match "" t t attrstr
))
652 (nconc attrs
(list attr
))
653 (setq attrs
(list attr
)))))))
654 (if (and (cadr tag-info
) (not closed-tag
))
655 (if (search-forward (concat "</" (car tag-info
) ">") nil t
)
656 (delete-region (match-beginning 0) (point))
657 (setq tag-info nil
)))
659 (setq end
(point-marker))
660 (delete-region start beg
)
662 (let ((args (list start end
)))
664 (nconc args
(list attrs
)))
665 (apply (nth 3 tag-info
) args
))))))
668 (defun muse-publish-escape-specials (beg end
&optional ignore-read-only
)
669 "Escape specials from BEG to END using style-specific :specials.
670 If IGNORE-READ-ONLY is non-nil, ignore the read-only property."
673 (while (< (point) end
)
674 (if (and (not ignore-read-only
) (get-text-property (point) 'read-only
))
677 (or (assoc (char-after) (muse-style-element :specials
))
678 (assoc (char-after) muse-publish-markup-specials
))))
682 (insert (cdr repl
))))))))
684 (defun muse-publish-markup-word ()
685 (let* ((beg (match-beginning 2))
686 (end (1- (match-end 2)))
687 (leader (buffer-substring-no-properties beg end
))
688 open-tag close-tag mark-read-only loc
)
690 ((string= leader
"_")
691 (setq open-tag
(muse-markup-text 'begin-underline
)
692 close-tag
(muse-markup-text 'end-underline
)))
693 ((string= leader
"=")
694 (setq open-tag
(muse-markup-text 'begin-literal
)
695 close-tag
(muse-markup-text 'end-literal
))
696 (setq mark-read-only t
))
698 (let ((l (length leader
)))
700 ((= l
1) (setq open-tag
(muse-markup-text 'begin-emph
)
701 close-tag
(muse-markup-text 'end-emph
)))
702 ((= l
2) (setq open-tag
(muse-markup-text 'begin-more-emph
)
703 close-tag
(muse-markup-text 'end-more-emph
)))
704 ((= l
3) (setq open-tag
(muse-markup-text 'begin-most-emph
)
705 close-tag
(muse-markup-text 'end-most-emph
)))))))
706 (if (and (not (get-text-property beg
'noemphasis
))
707 (setq loc
(search-forward leader nil t
))
708 (not (eq (char-syntax (char-after loc
)) ?w
))
709 (not (eq (char-syntax (char-before (point))) ?\
))
710 (not (get-text-property (point) 'noemphasis
)))
713 (delete-region beg end
)
714 (setq end
(point-marker))
721 (muse-publish-escape-specials beg end t
)
722 (muse-publish-mark-read-only (1- beg
) (1+ end
))))
726 (defun muse-publish-markup-emdash ()
727 (delete-region (match-beginning 0) (match-end 0))
728 (insert (muse-markup-text 'emdash
))
729 (if (eq (char-after) ?\
<)
732 (defun muse-publish-markup-enddots ()
733 (delete-region (match-beginning 0) (match-end 0))
734 (insert (muse-markup-text 'enddots
)))
736 (defun muse-publish-markup-dots ()
737 (delete-region (match-beginning 0) (match-end 0))
738 (insert (muse-markup-text 'dots
)))
740 (defun muse-publish-markup-rule ()
741 (delete-region (match-beginning 0) (match-end 0))
742 (insert (muse-markup-text 'rule
)))
744 (defun muse-publish-markup-heading ()
745 (let* ((len (length (match-string 1)))
746 (start (muse-markup-text
747 (cond ((= len
1) 'section
)
748 ((= len
2) 'subsection
)
749 ((= len
3) 'subsubsection
)
752 (end (muse-markup-text
753 (cond ((= len
1) 'section-end
)
754 ((= len
2) 'subsection-end
)
755 ((= len
3) 'subsubsection-end
)
756 (t 'section-other-end
))
758 (delete-region (match-beginning 0) (match-end 0))
761 (if end
(insert end
))
762 (muse-publish-section-close len
)))
764 (defvar muse-publish-footnotes nil
)
766 (defun muse-publish-markup-footnote ()
767 "Scan ahead and snarf up the footnote body"
768 (if (= (muse-line-beginning-position) (match-beginning 0))
770 (let ((footnote (save-match-data
771 (string-to-number (match-string 1))))
773 (delete-region (match-beginning 0) (match-end 0))
775 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote
) nil t
)
776 (let* ((start (match-beginning 0))
777 (beg (goto-char (match-end 0)))
779 (if (search-forward "\n\n" nil t
)
780 (copy-marker (match-beginning 0))
781 (goto-char (point-max))
782 (skip-chars-backward "\n")
784 (while (re-search-forward (concat "^["
788 (replace-match "\\1" t
))
789 (let ((footnotemark-cmd (muse-markup-text 'footnotemark
))
790 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end
)))
791 (if (string= "" footnotemark-cmd
)
793 (concat (muse-markup-text 'footnote
)
794 (buffer-substring-no-properties beg end
)
795 (muse-markup-text 'footnote-end
)))
796 (setq footnotemark
(format footnotemark-cmd footnote
797 footnotemark-end-cmd
))
798 (unless muse-publish-footnotes
799 (set (make-local-variable 'muse-publish-footnotes
)
800 (make-vector 256 nil
)))
801 (unless (aref muse-publish-footnotes footnote
)
805 (concat (format (muse-markup-text 'footnotetext
)
807 (buffer-substring-no-properties beg end
)
808 (muse-markup-text 'footnotetext-end
))))
809 (aset muse-publish-footnotes footnote footnotemark
))))
811 (skip-chars-forward "\n")
812 (delete-region start
(point)))))
813 (insert (or footnotemark footnote
)))))
815 (defun muse-publish-markup-fn-sep ()
816 (delete-region (match-beginning 0) (match-end 0))
817 (insert (muse-markup-text 'fn-sep
)))
819 (defun muse-publish-surround-text (beg-tag end-tag move-func
)
820 (let ((beg (point)) end
)
821 (skip-chars-backward muse-regexp-space
)
822 (delete-region (point) beg
)
823 (insert "\n\n" beg-tag
)
825 (setq end
(point-marker))
827 (while (< (point) end
)
828 (if (looking-at "^\\s-+")
833 (skip-chars-backward muse-regexp-space
)
834 (delete-region (point) beg
))
835 (insert end-tag
"\n"))
837 (defsubst muse-forward-paragraph
(&optional pattern
)
838 (if (re-search-forward (if pattern
839 (concat "^\\(" pattern
"["
842 "^\\s-*\\($\\|\\'\\)") nil t
)
843 (goto-char (match-beginning 0))
844 (goto-char (point-max))))
846 (defun muse-publish-markup-list ()
847 "Markup a list entry or quoted paragraph.
848 The reason this function is so funky, is to prevent text properties
849 like read-only from being inadvertently deleted."
850 (let ((str (match-string 1)))
852 ((and (eq (aref str
0) ?-
))
853 (delete-region (match-beginning 0) (match-end 0))
854 (muse-publish-surround-text
855 (muse-markup-text 'begin-uli
)
856 (muse-markup-text 'end-uli
)
859 (muse-forward-paragraph (concat "["
862 ((and (>= (aref str
0) ?
0)
863 (<= (aref str
0) ?
9))
864 (delete-region (match-beginning 0) (match-end 0))
865 (muse-publish-surround-text
866 (muse-markup-text 'begin-oli
)
867 (muse-markup-text 'end-oli
)
870 (muse-forward-paragraph (concat "["
874 (goto-char (match-beginning 1))
875 (insert (muse-markup-text 'begin-ddt
))
879 (while (looking-at (concat "^\\(["
884 (delete-region (match-beginning 1) (match-end 1))
887 (when (re-search-forward (concat "["
893 (replace-match (muse-markup-text 'start-dde
))))
894 (muse-forward-paragraph)
895 (insert (muse-markup-text 'end-ddt
) ?
\n)))))
897 (defun muse-publish-markup-quote ()
898 "Markup a list entry or quoted paragraph.
899 The reason this function is so funky, is to prevent text properties
900 like read-only from being inadvertently deleted."
901 (let* ((ws (match-string 0))
902 (centered (>= (string-width ws
) 6))
903 (begin-elem (if centered
'begin-center
'begin-quote
))
904 (end-elem (if centered
'end-center
'end-quote
)))
905 (muse-publish-surround-text (muse-markup-text begin-elem
)
906 (muse-markup-text end-elem
)
907 'muse-forward-paragraph
)))
909 (defun muse-publish-markup-leading-space ()
910 (let ((markup-space (muse-markup-text 'verse-space
))
912 (when (and markup-space
913 (>= (setq count
(skip-chars-forward " ")) 0))
914 (delete-region (muse-line-beginning-position) (point))
916 (insert markup-space
)
917 (setq count
(- count
2))))))
919 (defun muse-publish-markup-verse ()
920 (let ((leader (match-string 0)))
921 (goto-char (match-beginning 0))
922 (insert (muse-markup-text 'begin-verse
))
923 (while (looking-at leader
)
925 (muse-publish-markup-leading-space)
929 (let ((text (muse-markup-text 'empty-verse-line
)))
930 (if text
(insert text
))))
934 (or (looking-at (concat leader
"["
937 (not (looking-at leader
)))))
938 (let ((text (muse-markup-text 'last-stanza-end
)))
939 (if text
(insert text
))))
941 (let ((text (muse-markup-text 'end-verse-line
)))
942 (if text
(insert text
)))))
944 (insert (muse-markup-text 'end-verse
) ?
\n))
946 (defun muse-publish-markup-table ()
947 "Style does not support tables.")
949 (defun muse-publish-markup-email ()
950 (let* ((beg (match-end 1))
951 (addr (buffer-substring-no-properties beg
(match-end 0))))
952 (muse-with-temp-buffer
954 (muse-publish-escape-specials (point-min) (point-max))
955 (setq addr
(buffer-string)))
957 (delete-region beg
(match-end 0))
958 (insert (format (muse-markup-text 'email-addr
) addr addr
))
959 (muse-publish-mark-read-only beg
(point))))
961 (defun muse-publish-escape-specials-in-string (string &rest ignored
)
962 "Escape specials in STRING using style-specific :specials."
964 (apply (function concat
)
968 (or (assoc ch
(muse-style-element :specials
))
969 (assoc ch muse-publish-markup-specials
))))
973 (append string nil
)))))
975 (defun muse-publish-url (url &optional desc explicit
)
976 "Resolve a URL into its final <a href> form."
977 (let ((orig-url url
))
978 (dolist (transform muse-publish-url-transforms
)
979 (setq url
(save-match-data (when url
(funcall transform url explicit
)))))
980 (setq desc
(when desc
(muse-publish-escape-specials-in-string desc
)))
982 (if (string-match muse-image-regexp url
)
984 (muse-markup-text 'image-with-desc url desc
)
985 (muse-markup-text 'image-link url
))
986 (if (and desc
(string-match muse-image-regexp desc
))
987 (muse-markup-text 'url-with-image url desc
)
988 (muse-markup-text 'url-link url
(or desc orig-url
))))
991 (defun muse-publish-insert-url (url &optional desc explicit
)
992 "Resolve a URL into its final <a href> form."
993 (delete-region (match-beginning 0) (match-end 0))
995 (insert (muse-publish-url url desc explicit
))
996 (muse-publish-mark-read-only beg
(point))))
998 (defun muse-publish-markup-link ()
999 (let (desc explicit link
)
1000 (setq explicit
(save-match-data
1001 (if (string-match muse-explicit-link-regexp
1004 (setq desc
(if explicit
(match-string 2) (match-string 0)))
1005 (setq link
(if explicit
1006 (muse-handle-explicit-link (match-string 1))
1007 (muse-handle-implicit-link (match-string 0))))
1010 (not (or (eq (char-before (match-beginning 0)) ?
\")
1011 (eq (char-after (match-end 0)) ?
\")))))
1012 (muse-publish-insert-url link desc explicit
))))
1014 (defun muse-publish-markup-url ()
1015 (muse-publish-insert-url (match-string 0)))
1017 ;; Default publishing tags
1019 (defun muse-publish-contents-tag (beg end attrs
)
1020 (set (make-local-variable 'muse-publish-generate-contents
)
1021 (cons (copy-marker (point) t
)
1022 (let ((depth (cdr (assoc "depth" attrs
))))
1023 (or (and depth
(string-to-number depth
)) 2)))))
1025 (defun muse-publish-verse-tag (beg end
)
1028 (while (eq ?\
(char-syntax (char-after)))
1030 (while (< (point) end
)
1033 (if (eq ?\
(char-syntax (char-before)))
1036 (defun muse-publish-example-tag (beg end
)
1037 (muse-publish-escape-specials beg end
)
1039 (insert (muse-markup-text 'begin-example
))
1041 (insert (muse-markup-text 'end-example
))
1042 (muse-publish-mark-read-only beg
(point)))
1044 (defun muse-publish-mark-read-only (beg end
)
1045 (add-text-properties beg end
'(rear-nonsticky (read-only) read-only t
))
1048 (defun muse-publish-mark-noemphasis (&optional beg end
)
1049 (unless beg
(setq beg
(match-beginning 0)))
1050 (unless end
(setq end
(match-end 0)))
1051 (add-text-properties beg end
'(noemphasis t
))
1054 (defun muse-publish-verbatim-tag (beg end
)
1055 (muse-publish-escape-specials beg end
)
1056 (muse-publish-mark-read-only beg end
))
1058 (defalias 'muse-publish-class-tag
'ignore
)
1060 (defun muse-publish-lisp-tag (beg end
)
1062 (let ((str (muse-eval-lisp
1064 (buffer-substring-no-properties beg end
)
1065 (delete-region beg end
)))))
1066 (set-text-properties 0 (length str
) nil str
)
1069 (defun muse-publish-command-tag (beg end attrs
)
1070 (while (looking-at "\\s-*$")
1072 (let ((interp (cdr (assoc "interp" attrs
))))
1076 (buffer-substring-no-properties (point) end
)
1077 (delete-region beg end
)) t
)
1078 (shell-command-on-region beg end interp t t
))
1079 (muse-publish-mark-read-only beg
(point))))
1081 (defun muse-publish-comment-tag (beg end
)
1082 (delete-region beg end
))
1084 ;; Miscellaneous helper functions
1086 (defsubst muse-publishing-directive
(name)
1087 (cdr (assoc name muse-publishing-directives
)))
1089 (defun muse-publish-strip-tags (string)
1090 "Remove all tags from the string."
1091 (while (string-match "<.*?>" string
)
1092 (setq string
(replace-match "" nil t string
)))
1095 (defun muse-publish-markup-type (category default-func
)
1096 (let ((rule (muse-find-markup-element :overrides category
(muse-style))))
1097 (funcall (or rule default-func
))))
1099 (defun muse-published-buffer-contents (buffer)
1100 (with-current-buffer buffer
1101 (goto-char (point-min))
1102 (let ((beg (and (search-forward "Emacs Muse begins here")
1103 (muse-line-end-position)))
1104 (end (and (search-forward "Emacs Muse ends here")
1105 (muse-line-beginning-position))))
1106 (buffer-substring-no-properties beg end
))))
1108 (defun muse-published-contents (file)
1109 (when (file-readable-p file
)
1110 (muse-with-temp-buffer
1111 (insert-file-contents file
)
1112 (muse-published-buffer-contents (current-buffer)))))
1114 (defun muse-publish-transform-output
1115 (file temp-file output-path name gen-func
&rest cleanup-exts
)
1116 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1117 (setq file
(muse-page-name file
))
1118 (message "Generating %s output for %s..." name file
)
1119 (if (not (funcall gen-func temp-file output-path
))
1120 (message "Generating %s from %s...failed" name file
)
1121 (message "Generating %s output for %s...done" name file
)
1122 (muse-delete-file-if-exists temp-file
)
1123 (dolist (ext cleanup-exts
)
1124 (muse-delete-file-if-exists
1125 (expand-file-name (concat file ext
)
1126 (file-name-directory output-path
))))
1127 (message "Wrote %s" output-path
)))
1129 (defun muse-publish-read-only (string)
1130 (let ((end (1- (length string
))))
1131 (add-text-properties 0 end
1132 '(rear-nonsticky (read-only) read-only t
)
1136 (defun muse-publish-prepare-url (target &rest ignored
)
1137 "Transform anchors and get published name, if TARGET is a page."
1139 (unless (or (string-match muse-url-regexp target
)
1140 (string-match muse-image-regexp target
)
1141 (string-match muse-file-regexp target
))
1142 (setq target
(if (string-match "#" target
)
1143 (concat (muse-publish-output-name
1144 (substring target
0 (match-beginning 0)))
1145 "#" (substring target
(match-end 0)))
1146 (muse-publish-output-name target
)))))
1149 (provide 'muse-publish
)
1151 ;;; muse-publish.el ends here