Add invalid directory assertion.
[muse-el.git] / lisp / muse-publish.el
blob67443dc36727209e3ec57224b8b40ab8d0751df2
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
10 ;; version.
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
15 ;; for more details.
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.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;; Yann Hodique (yann DOT hodique AT gmail DOT com) fixed an
27 ;; unnecessary URL description transform in `muse-publish-url'.
29 ;;; Code:
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 ;; Muse Publishing
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 (require 'muse)
38 (require 'muse-regexps)
40 (defgroup muse-publish nil
41 "Options controlling the general behavior of Muse publishing.
42 See `muse-publish' for more information."
43 :group 'muse)
45 (defcustom muse-before-publish-hook nil
46 "A hook run in the buffer to be published, before it is done."
47 :type 'hook
48 :group 'muse-publish)
50 (defcustom muse-after-publish-hook nil
51 "A hook run in the buffer to be published, after it is done."
52 :type 'hook
53 :group 'muse-publish)
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."
58 :type 'hook
59 :group 'muse-publish)
61 (defcustom muse-publish-report-threshhold 100000
62 "If a file is this size or larger, report publishing progress."
63 :type 'integer
64 :group 'muse-publish)
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)
76 ;; markup tags
77 (1300 muse-tag-regexp 0 tag)
79 ;; commented lines
80 (1350 "^;\\s-+\\(.+\\)" 0 comment)
82 ;; define anchor points
83 (1400 "^#\\(\\S-+\\)\\s-*" 0 anchor)
85 ;; replace links in the buffer (links to other pages)
86 (1500 muse-explicit-link-regexp 0 link)
88 ;; emphasized or literal text
89 (1600 ,(concat
90 "\\(^\\|[-["
91 muse-regexp-space
92 "<('`\"]\\)\\(=[^="
93 muse-regexp-space
94 "]\\|_[^_"
95 muse-regexp-space
96 "]\\|\\*+[^*"
97 muse-regexp-space
98 "]\\)")
99 2 word)
101 ;; headings, outline-mode style
102 (1700 "^\\(\\*+\\)\\s-+" 0 heading)
104 ;; ellipses
105 (1800 "\\.\\.\\.\\." 0 enddots)
106 (1850 "\\.\\.\\." 0 dots)
108 ;; horizontal rule, or section separator
109 (1900 "^----+" 0 rule)
111 (2000 ,(concat "\n*\\(^\\|["
112 muse-regexp-blank
113 "]+\\)--\\($\\|["
114 muse-regexp-blank
115 "]+\\)")
116 0 emdash)
118 ;; beginning of footnotes section
119 (2100 "^Footnotes:?\\s-*" 0 fn-sep)
120 ;; footnote definition/reference (def if at beginning of line)
121 (2200 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote)
123 ;; unnumbered List items begin with a -. numbered list items
124 ;; begin with number and a period. definition lists have a
125 ;; leading term separated from the body with ::. centered
126 ;; paragraphs begin with at least six columns of whitespace; any
127 ;; other whitespace at the beginning indicates a blockquote. The
128 ;; reason all of these rules are handled here, is so that
129 ;; blockquote detection doesn't interfere with indented list
130 ;; members.
131 (2300 ,(concat "^["
132 muse-regexp-blank
133 "]+\\(-["
134 muse-regexp-blank
135 "]*\\|[0-9]+\\.["
136 muse-regexp-blank
137 "]*\\|\\(?:.+?\\)["
138 muse-regexp-blank
139 "]+::\n?\\)")
140 1 list)
142 (2400 ,(concat "^\\(\\(?:.+?\\)["
143 muse-regexp-blank
144 "]+::\n?\\)")
145 0 list)
147 (2500 ,(concat "^\\(["
148 muse-regexp-blank
149 "]+\\)")
150 0 quote)
152 ;; "verse" text is indicated the same way as a quoted e-mail
153 ;; response: "> text", where text may contain initial whitespace
154 ;; (see below).
155 (2600 ,(concat "^["
156 muse-regexp-blank
157 "]*> ")
158 0 verse)
160 ;; simple table markup is supported, nothing fancy. use | to
161 ;; separate cells, || to separate header cells, and ||| for footer
162 ;; cells
163 (2700 ,(concat "^["
164 muse-regexp-blank
165 "]*\\(.+?\\(["
166 muse-regexp-blank
167 "]+|+["
168 muse-regexp-blank
169 "]+.+?\\)\\)$")
170 0 table)
172 ;; base URLs
173 (3000 muse-url-regexp 0 url)
175 ;; bare email addresses
176 (3500
177 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email)
179 "List of markup rules for publishing a page with Muse.
180 The rules given in this variable are invoked first, followed by
181 whatever rules are specified by the current style.
183 Each member of the list is either a function, or a list of the form:
185 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
187 REGEXP is a regular expression, or symbol whose value is a regular
188 expression, which is searched for using `re-search-forward'.
189 TEXT-BEGIN-GROUP is the matching group within that regexp which
190 denotes the beginning of the actual text to be marked up.
191 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
192 If it is not a string, but a function, it will be called to determine
193 what the replacement text should be (it must return a string). If it
194 is a symbol, the value of that symbol should be a string.
196 The replacements are done in order, one rule at a time. Writing
197 the regular expressions can be a tricky business. Note that case
198 is never ignored. `case-fold-search' is always bound to nil
199 while processing the markup rules."
200 :type '(repeat (choice
201 (list :tag "Markup rule"
202 integer
203 (choice regexp symbol)
204 integer
205 (choice string function symbol))
206 function))
207 :group 'muse-publish)
209 (defcustom muse-publish-markup-functions
210 '((directive . muse-publish-markup-directive)
211 (comment . muse-publish-markup-comment)
212 (anchor . muse-publish-markup-anchor)
213 (tag . muse-publish-markup-tag)
214 (word . muse-publish-markup-word)
215 (emdash . muse-publish-markup-emdash)
216 (enddots . muse-publish-markup-enddots)
217 (dots . muse-publish-markup-dots)
218 (rule . muse-publish-markup-rule)
219 (heading . muse-publish-markup-heading)
220 (footnote . muse-publish-markup-footnote)
221 (fn-sep . muse-publish-markup-fn-sep)
222 (list . muse-publish-markup-list)
223 (quote . muse-publish-markup-quote)
224 (verse . muse-publish-markup-verse)
225 (table . muse-publish-markup-table)
226 (email . muse-publish-markup-email)
227 (link . muse-publish-markup-link)
228 (url . muse-publish-markup-url))
229 "An alist of style types to custom functions for that kind of text.
231 Each member of the list is of the form:
233 (SYMBOL FUNCTION)
235 SYMBOL describes the type of text to associate with this rule.
236 `muse-publish-markup-regexps' maps regexps to these symbols.
238 FUNCTION is the function to use to mark up this kind of rule if
239 no suitable function is found through the :functions tag of the
240 current style."
241 :type '(alist :key-type symbol :value-type function)
242 :group 'muse-publish)
244 (defcustom muse-publish-markup-tags
245 '(("contents" nil t muse-publish-contents-tag)
246 ("verse" t nil muse-publish-verse-tag)
247 ("example" t nil muse-publish-example-tag)
248 ("literal" t nil muse-publish-mark-read-only)
249 ("verbatim" t nil muse-publish-verbatim-tag)
250 ("lisp" t nil muse-publish-lisp-tag)
251 ("class" t t muse-publish-class-tag)
252 ("command" t t muse-publish-command-tag)
253 ("comment" t nil muse-publish-comment-tag))
254 "A list of tag specifications, for specially marking up text.
255 XML-style tags are the best way to add custom markup to Muse.
256 This is easily accomplished by customizing this list of markup tags.
258 For each entry, the name of the tag is given, whether it expects
259 a closing tag and/or an optional set of attributes, and a
260 function that performs whatever action is desired within the
261 delimited region.
263 The tags themselves are deleted during publishing, before the
264 function is called. The function is called with three arguments,
265 the beginning and end of the region surrounded by the tags. If
266 properties are allowed, they are passed as a third argument in
267 the form of an alist. The `end' argument to the function is
268 always a marker.
270 Point is always at the beginning of the region within the tags, when
271 the function is called. Wherever point is when the function finishes
272 is where tag markup will resume.
274 These tag rules are processed once at the beginning of markup, and
275 once at the end, to catch any tags which may have been inserted
276 in-between."
277 :type '(repeat (list (string :tag "Markup tag")
278 (boolean :tag "Expect closing tag" :value t)
279 (boolean :tag "Parse attributes" :value nil)
280 function))
281 :group 'muse-publish)
283 (defcustom muse-publish-markup-specials nil
284 "A table of characters which must be represented specially."
285 :type '(alist :key-type character :value-type string)
286 :group 'muse-publish)
288 (defvar muse-publishing-p nil
289 "Set to t while a page is being published.")
290 (defvar muse-batch-publishing-p nil
291 "Set to t while a page is being batch published.")
292 (defvar muse-publishing-styles nil)
293 (defvar muse-publishing-current-file nil)
294 (defvar muse-publishing-current-style nil)
295 (defvar muse-publishing-directives nil
296 "An alist of publishing directives from the top of a file.")
297 (defvar muse-publish-generate-contents nil
298 "Non-nil if a table of contents should be generated.
299 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
300 tell where the <contents> was seen, and to what depth the
301 contents were requested.")
303 ;; Functions for handling style information
305 (defsubst muse-style (&optional style)
306 "Resolve the given STYLE into a Muse style, if it is a string."
307 (if (null style)
308 muse-publishing-current-style
309 (if (stringp style)
310 (assoc style muse-publishing-styles)
311 (muse-assert (consp style))
312 style)))
314 (defun muse-define-style (name &rest elements)
315 (let ((entry (assoc name muse-publishing-styles)))
316 (if entry
317 (error "There is already a style named %s." name)
318 (setq muse-publishing-styles
319 (cons (append (list name) elements)
320 muse-publishing-styles)))))
322 (defun muse-derive-style (new-name base-name &rest elements)
323 (let ((entry (assoc new-name muse-publishing-styles)))
324 (if entry
325 (error "There is already a style named %s." new-name)
326 (apply 'muse-define-style new-name
327 (append elements (list :base base-name))))))
329 (defsubst muse-get-keyword (keyword list &optional direct)
330 (let ((value (cadr (memq keyword list))))
331 (if (and (not direct) (symbolp value))
332 (symbol-value value)
333 value)))
335 (defsubst muse-style-element (elem &optional style direct)
336 (setq style (muse-style style))
337 (let ((value (muse-get-keyword elem style direct)))
338 (if value
339 value
340 (let ((base (muse-get-keyword :base style)))
341 (if base
342 (muse-style-element elem base direct))))))
344 (defun muse-find-markup-element (keyword ident style)
345 (let ((def (assq ident (muse-style-element keyword style))))
346 (if def
347 (cdr def)
348 (let ((base (muse-style-element :base style)))
349 (if base
350 (muse-find-markup-element keyword ident base))))))
352 (defsubst muse-markup-text (ident &rest args)
353 (let ((text (muse-find-markup-element :strings ident (muse-style))))
354 (if (and text args)
355 (apply 'format text args)
356 (or text ""))))
358 (defun muse-find-markup-tag (keyword tagname style)
359 (let ((def (assoc tagname (muse-style-element keyword style))))
360 (or def
361 (let ((base (muse-style-element :base style)))
362 (if base
363 (muse-find-markup-tag keyword tagname base))))))
365 (defsubst muse-markup-tag-info (tagname &rest args)
366 (let ((tag-info (muse-find-markup-tag :tags tagname (muse-style))))
367 (or tag-info
368 (assoc (match-string 1) muse-publish-markup-tags))))
370 (defsubst muse-markup-function (category)
371 (let ((func (muse-find-markup-element :functions category (muse-style))))
372 (or func
373 (cdr (assq category muse-publish-markup-functions)))))
375 ;; Publishing routines
377 (defun muse-publish-markup (name rules)
378 (let* ((case-fold-search nil)
379 (inhibit-read-only t)
380 (limit (* (length rules) (point-max)))
381 (verbose (and muse-publish-report-threshhold
382 (> (point-max) muse-publish-report-threshhold)))
383 (base 0))
384 (while rules
385 (goto-char (point-min))
386 (let ((regexp (nth 1 (car rules)))
387 (group (nth 2 (car rules)))
388 (repl (nth 3 (car rules)))
389 last-pos pos)
390 (if (symbolp regexp)
391 (setq regexp (symbol-value regexp)))
392 (if (and verbose (not muse-batch-publishing-p))
393 (message "Publishing %s...%d%%" name
394 (* (/ (float (+ (point) base)) limit) 100)))
395 (while (and regexp (setq pos (re-search-forward regexp nil t)))
396 (if (and verbose (not muse-batch-publishing-p))
397 (message "Publishing %s...%d%%" name
398 (* (/ (float (+ (point) base)) limit) 100)))
399 (unless (get-text-property (match-beginning group) 'read-only)
400 (let* (func
401 (text (cond
402 ((and (symbolp repl)
403 (setq func (muse-markup-function repl)))
404 (funcall func))
405 ((functionp repl)
406 (funcall repl))
407 ((symbolp repl)
408 (symbol-value repl))
409 (t repl))))
410 (if text
411 (replace-match text t))))
412 (if (and last-pos (= pos last-pos))
413 (if (eobp)
414 (setq regexp nil)
415 (forward-char 1)))
416 (setq last-pos pos)))
417 (setq rules (cdr rules)
418 base (+ base (point-max))))
419 (if (and verbose (not muse-batch-publishing-p))
420 (message "Publishing %s...done" name))))
422 (defun muse-insert-file-or-string (file-or-string &optional title)
423 (let ((beg (point)) end)
424 (if (and (not (string-equal file-or-string ""))
425 (file-readable-p file-or-string))
426 (setq end (+ beg (cadr (insert-file-contents file-or-string))))
427 (insert file-or-string)
428 (setq end (point)))
429 (save-restriction
430 (narrow-to-region beg end)
431 (muse-publish-markup (or title "")
432 '((100 "<\\(lisp\\)>" 0
433 muse-publish-markup-tag))))))
435 (defun muse-style-run-hooks (keyword style &rest args)
436 (let (handled)
437 (while (and style (not handled))
438 (setq style (muse-style style))
439 (let ((func (muse-get-keyword keyword style t)))
440 (if func
441 (if (apply func args)
442 (setq handled t))))
443 (unless handled
444 (setq style (muse-style-element :base style))))
445 handled))
447 (defun muse-publish-markup-region (beg end title style)
448 "Apply the given STYLE's markup rules to the given region."
449 (save-restriction
450 (narrow-to-region beg end)
451 (muse-style-run-hooks :before style)
452 (muse-publish-markup
453 title
454 (sort (copy-alist (append muse-publish-markup-regexps
455 (muse-style-element :regexps style)))
456 (function
457 (lambda (l r)
458 (< (car l) (car r))))))
459 (muse-style-run-hooks :before-end style)))
461 (defun muse-publish-markup-buffer (title style)
462 "Apply the given STYLE's markup rules to the current buffer."
463 (setq style (muse-style style))
464 (let ((style-header (muse-style-element :header style))
465 (style-footer (muse-style-element :footer style))
466 (muse-publishing-current-style style)
467 (muse-publishing-directives
468 (list (cons "title" title)
469 (cons "author" (user-full-name))
470 (cons "date" (format-time-string "%B %e, %Y"))))
471 (muse-publishing-p t))
472 (run-hooks 'muse-before-publish-hook)
473 (muse-publish-markup-region (point-min) (point-max) title style)
474 (goto-char (point-min))
475 (if style-header (muse-insert-file-or-string style-header title))
476 (goto-char (point-max))
477 (if style-footer (muse-insert-file-or-string style-footer title))
478 (muse-style-run-hooks :after style)
479 (run-hooks 'muse-after-publish-hook)))
481 (defun muse-publish-markup-string (string &optional style)
482 "Markup STRING using the given STYLE's markup rules."
483 (setq style (muse-style style))
484 (with-temp-buffer
485 (insert string)
486 (let ((muse-publishing-current-style style)
487 (muse-publishing-p t))
488 (muse-publish-markup "*string*" (muse-style-element :rules style)))
489 (buffer-string)))
491 ;; Commands for publishing files
493 (defsubst muse-publish-get-style ()
494 (if (= 1 (length muse-publishing-styles))
495 (car muse-publishing-styles)
496 (assoc (completing-read "Publish with style: "
497 muse-publishing-styles nil t)
498 muse-publishing-styles)))
500 (defsubst muse-publish-get-output-dir (style)
501 (let ((default-directory (or (muse-style-element :path style)
502 default-directory)))
503 (read-file-name "Publish to directory: " nil default-directory)))
505 (defsubst muse-publish-get-info ()
506 (let ((style (muse-publish-get-style)))
507 (list style (muse-publish-get-output-dir style)
508 current-prefix-arg)))
510 (defsubst muse-publish-output-name (&optional file style)
511 (setq style (muse-style style))
512 (concat (muse-style-element :prefix style)
513 (muse-page-name file)
514 (muse-style-element :suffix style)))
516 (defsubst muse-publish-output-file (file output-dir &optional style)
517 (setq style (muse-style style))
518 (expand-file-name (muse-publish-output-name file style) output-dir))
520 (defun muse-publish-file (file style &optional output-dir force)
521 "Publish the given file in list FILES.
522 If the argument FORCE is nil, each file is only published if it is
523 newer than the published version. If the argument FORCE is non-nil,
524 the file is published no matter what."
525 (interactive (cons (read-file-name "Publish file: ")
526 (muse-publish-get-info)))
527 (setq style (muse-style style))
528 (let* ((output-path (muse-publish-output-file file output-dir style))
529 (output-suffix (muse-style-element :osuffix style))
530 (muse-publishing-current-file file)
531 (target (if output-suffix
532 (concat (file-name-sans-extension output-path)
533 output-suffix)
534 output-path)))
535 (when (or force (file-newer-than-file-p file target))
536 (if (and muse-publish-report-threshhold
537 (> (nth 7 (file-attributes file))
538 muse-publish-report-threshhold))
539 (message "Publishing %s ..." file))
540 (with-temp-buffer
541 (insert-file-contents file t)
542 (muse-publish-markup-buffer (muse-page-name file) style)
543 (let ((backup-inhibited t))
544 (write-file output-path))
545 (muse-style-run-hooks :final style file output-path target))
546 t)))
548 (defun muse-publish-this-file (style output-dir &optional force)
549 "Publish the page in the current file."
550 (interactive (muse-publish-get-info))
551 (unless (muse-publish-file buffer-file-name style output-dir force)
552 (message (concat "The published version is up-to-date; use"
553 " C-u C-c C-t to force an update."))))
555 (defun muse-batch-publish-files ()
556 "Publish Muse files in batch mode."
557 (let ((muse-batch-publishing-p t)
558 style-name style output-dir)
559 (setq style-name (car command-line-args-left)
560 style (muse-style style-name)
561 command-line-args-left (cdr command-line-args-left)
562 output-dir (car command-line-args-left)
563 output-dir
564 (if (string-match "\\`--output-dir=" output-dir)
565 (prog1
566 (substring output-dir (match-end 0))
567 (setq command-line-args-left (cdr command-line-args-left)))))
568 (unless style
569 (error "There is no style '%s' defined." style-name))
570 (dolist (file command-line-args-left)
571 (muse-publish-file file style output-dir t))))
573 ;; Default publishing rules
575 (defun muse-publish-markup-directive (&optional name value)
576 (unless name (setq name (match-string 1)))
577 (unless value (setq value (match-string 2)))
578 (let ((elem (assoc name muse-publishing-directives)))
579 (if elem
580 (setcdr elem value)
581 (setq muse-publishing-directives
582 (cons (cons name value)
583 muse-publishing-directives))))
584 (delete-region (match-beginning 0) (match-end 0)))
586 (defun muse-publish-markup-anchor () "")
587 (defun muse-publish-markup-comment () "")
589 (defun muse-publish-markup-tag ()
590 (let ((tag-info (muse-markup-tag-info (match-string 1))))
591 (when (and tag-info
592 (not (get-text-property (match-beginning 0) 'read-only)))
593 (let ((closed-tag (match-string 3))
594 (start (match-beginning 0))
595 (beg (point)) end attrs)
596 (when (nth 2 tag-info)
597 (let ((attrstr (match-string 2)))
598 (while (and attrstr
599 (string-match (concat "\\([^"
600 muse-regexp-space
601 "=]+\\)\\(=\"\\"
602 "([^\"]+\\)\"\\)?")
603 attrstr))
604 (let ((attr (cons (downcase
605 (muse-match-string-no-properties 1 attrstr))
606 (muse-match-string-no-properties 3 attrstr))))
607 (setq attrstr (replace-match "" t t attrstr))
608 (if attrs
609 (nconc attrs (list attr))
610 (setq attrs (list attr)))))))
611 (if (and (cadr tag-info) (not closed-tag))
612 (if (search-forward (concat "</" (car tag-info) ">") nil t)
613 (delete-region (match-beginning 0) (point))
614 (setq tag-info nil)))
615 (when tag-info
616 (setq end (point-marker))
617 (delete-region start beg)
618 (goto-char start)
619 (let ((args (list start end)))
620 (if (nth 2 tag-info)
621 (nconc args (list attrs)))
622 (apply (nth 3 tag-info) args))))))
623 nil)
625 (defun muse-publish-escape-specials (beg end)
626 (save-excursion
627 (goto-char beg)
628 (while (< (point) end)
629 (if (get-text-property (point) 'read-only)
630 (forward-char 1)
631 (let ((repl
632 (or (assoc (char-after) (muse-style-element :specials))
633 (assoc (char-after) muse-publish-markup-specials))))
634 (if (null repl)
635 (forward-char 1)
636 (delete-char 1)
637 (insert (cdr repl))))))))
639 (defun muse-publish-markup-word ()
640 (let* ((beg (match-beginning 2))
641 (end (1- (match-end 2)))
642 (leader (buffer-substring-no-properties beg end))
643 open-tag close-tag mark-read-only loc multi-line)
644 (cond
645 ((string= leader "_")
646 (setq multi-line t)
647 (setq open-tag (muse-markup-text 'begin-underline)
648 close-tag (muse-markup-text 'end-underline)))
649 ((string= leader "=")
650 (setq open-tag (muse-markup-text 'begin-literal)
651 close-tag (muse-markup-text 'end-literal))
652 (setq mark-read-only t))
654 (setq multi-line t)
655 (let ((l (length leader)))
656 (cond
657 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
658 close-tag (muse-markup-text 'end-emph)))
659 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
660 close-tag (muse-markup-text 'end-more-emph)))
661 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
662 close-tag (muse-markup-text 'end-most-emph)))))))
663 (if (and (setq loc (search-forward leader nil t))
664 (eq 0 (skip-syntax-forward "w" (1+ loc)))
665 (or multi-line (= 1 (count-lines beg loc))))
666 (progn
667 (replace-match "")
668 (delete-region beg end)
669 (setq end (point-marker))
670 (insert close-tag)
671 (save-excursion
672 (goto-char beg)
673 (insert open-tag)
674 (setq beg (point)))
675 (when mark-read-only
676 (muse-publish-escape-specials beg end)
677 (muse-publish-mark-read-only (1- beg) (1+ end))))
678 (backward-char))
679 nil))
681 (defun muse-publish-markup-emdash ()
682 (delete-region (match-beginning 0) (match-end 0))
683 (insert (muse-markup-text 'emdash))
684 (if (eq (char-after) ?\<)
685 (insert ?\n)))
687 (defun muse-publish-markup-enddots ()
688 (delete-region (match-beginning 0) (match-end 0))
689 (insert (muse-markup-text 'enddots)))
691 (defun muse-publish-markup-dots ()
692 (delete-region (match-beginning 0) (match-end 0))
693 (insert (muse-markup-text 'dots)))
695 (defun muse-publish-markup-rule ()
696 (delete-region (match-beginning 0) (match-end 0))
697 (insert (muse-markup-text 'rule)))
699 (defun muse-publish-markup-heading ()
700 (let* ((len (length (match-string 1)))
701 (start (muse-markup-text
702 (cond ((= len 1) 'section)
703 ((= len 2) 'subsection)
704 ((= len 3) 'subsubsection))))
705 (end (muse-markup-text
706 (cond ((= len 1) 'section-end)
707 ((= len 2) 'subsection-end)
708 ((= len 3) 'subsubsection-end)))))
709 (delete-region (match-beginning 0) (match-end 0))
710 (insert start)
711 (end-of-line)
712 (if end (insert end))))
714 (defvar muse-publish-footnotes nil)
716 (defun muse-publish-markup-footnote ()
717 "Scan ahead and snarf up the footnote body"
718 (if (= (muse-line-beginning-position) (match-beginning 0))
720 (let ((footnote (save-match-data
721 (string-to-number (match-string 1))))
722 footnotemark)
723 (delete-region (match-beginning 0) (match-end 0))
724 (save-excursion
725 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
726 (let* ((start (match-beginning 0))
727 (beg (goto-char (match-end 0)))
728 (end (save-excursion
729 (if (search-forward "\n\n" nil t)
730 (copy-marker (match-beginning 0))
731 (goto-char (point-max))
732 (skip-chars-backward "\n")
733 (point-marker)))))
734 (while (re-search-forward (concat "^["
735 muse-regexp-blank
736 "]+\\([^\n]\\)")
737 end t)
738 (replace-match "\\1" t))
739 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
740 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
741 (if (string= "" footnotemark-cmd)
742 (setq footnotemark
743 (concat (muse-markup-text 'footnote)
744 (buffer-substring-no-properties beg end)
745 (muse-markup-text 'footnote-end)))
746 (setq footnotemark (format footnotemark-cmd footnote
747 footnotemark-end-cmd))
748 (unless muse-publish-footnotes
749 (set (make-local-variable 'muse-publish-footnotes)
750 (make-vector 256 nil)))
751 (unless (aref muse-publish-footnotes footnote)
752 (setq footnotemark
753 (concat
754 footnotemark
755 (concat (format (muse-markup-text 'footnotetext)
756 footnote)
757 (buffer-substring-no-properties beg end)
758 (muse-markup-text 'footnotetext-end))))
759 (aset muse-publish-footnotes footnote footnotemark))))
760 (goto-char end)
761 (skip-chars-forward "\n")
762 (delete-region start (point)))))
763 (insert (or footnotemark footnote)))))
765 (defun muse-publish-markup-fn-sep ()
766 (delete-region (match-beginning 0) (match-end 0))
767 (insert (muse-markup-text 'fn-sep)))
769 (defun muse-publish-surround-text (beg-tag end-tag move-func)
770 (let ((beg (point)) end)
771 (skip-chars-backward muse-regexp-space)
772 (delete-region (point) beg)
773 (insert "\n\n" beg-tag)
774 (funcall move-func)
775 (setq end (point-marker))
776 (goto-char beg)
777 (while (< (point) end)
778 (if (looking-at "^\\s-+")
779 (replace-match ""))
780 (forward-line 1))
781 (goto-char end)
782 (setq beg (point))
783 (skip-chars-backward muse-regexp-space)
784 (delete-region (point) beg))
785 (insert end-tag "\n"))
787 (defsubst muse-forward-paragraph (&optional pattern)
788 (if (re-search-forward (if pattern
789 (concat "^\\(" pattern "["
790 muse-regexp-blank
791 "]+\\|$\\|\\'\\)")
792 "^\\s-*\\($\\|\\'\\)") nil t)
793 (goto-char (match-beginning 0))
794 (goto-char (point-max))))
796 (defun muse-publish-markup-list ()
797 "Markup a list entry or quoted paragraph.
798 The reason this function is so funky, is to prevent text properties
799 like read-only from being inadvertently deleted."
800 (let ((str (match-string 1)))
801 (cond
802 ((and (eq (aref str 0) ?-))
803 (delete-region (match-beginning 0) (match-end 0))
804 (muse-publish-surround-text
805 (muse-markup-text 'begin-uli)
806 (muse-markup-text 'end-uli)
807 (function
808 (lambda ()
809 (muse-forward-paragraph (concat "["
810 muse-regexp-blank
811 "]+-"))))))
812 ((and (>= (aref str 0) ?0)
813 (<= (aref str 0) ?9))
814 (delete-region (match-beginning 0) (match-end 0))
815 (muse-publish-surround-text
816 (muse-markup-text 'begin-oli)
817 (muse-markup-text 'end-oli)
818 (function
819 (lambda ()
820 (muse-forward-paragraph (concat "["
821 muse-regexp-blank
822 "]+[0-9]+\\."))))))
824 (goto-char (match-beginning 1))
825 (insert (muse-markup-text 'begin-ddt))
826 (save-match-data
827 (save-excursion
828 (forward-line 1)
829 (while (looking-at (concat "^\\(["
830 muse-regexp-blank
831 "]*\\)[^"
832 muse-regexp-space
833 "]"))
834 (delete-region (match-beginning 1) (match-end 1))
835 (forward-line 1))))
836 (save-match-data
837 (when (re-search-forward (concat "["
838 muse-regexp-space
839 "]+::["
840 muse-regexp-space
841 "]+")
842 nil t)
843 (replace-match (muse-markup-text 'start-dde))))
844 (muse-forward-paragraph)
845 (insert (muse-markup-text 'end-ddt) ?\n)))))
847 (defun muse-publish-markup-quote ()
848 "Markup a list entry or quoted paragraph.
849 The reason this function is so funky, is to prevent text properties
850 like read-only from being inadvertently deleted."
851 (let* ((ws (match-string 0))
852 (centered (>= (string-width ws) 6))
853 (begin-elem (if centered 'begin-center 'begin-quote))
854 (end-elem (if centered 'end-center 'end-quote)))
855 (muse-publish-surround-text (muse-markup-text begin-elem)
856 (muse-markup-text end-elem)
857 'muse-forward-paragraph)))
859 (defun muse-publish-markup-leading-space ()
860 (let ((markup-space (muse-markup-text 'verse-space))
861 count)
862 (when (and markup-space
863 (>= (setq count (skip-chars-forward " ")) 0))
864 (delete-region (muse-line-beginning-position) (point))
865 (while (> count 0)
866 (insert markup-space)
867 (setq count (- count 2))))))
869 (defun muse-publish-markup-verse ()
870 (let ((leader (match-string 0)))
871 (goto-char (match-beginning 0))
872 (insert (muse-markup-text 'begin-verse))
873 (while (looking-at leader)
874 (replace-match "")
875 (muse-publish-markup-leading-space)
876 (end-of-line)
877 (cond
878 ((bolp)
879 (let ((text (muse-markup-text 'empty-verse-line)))
880 (if text (insert text))))
881 ((save-excursion
882 (save-match-data
883 (forward-line 1)
884 (or (looking-at (concat leader "["
885 muse-regexp-blank
886 "]*$"))
887 (not (looking-at leader)))))
888 (let ((text (muse-markup-text 'last-stanza-end)))
889 (if text (insert text))))
891 (let ((text (muse-markup-text 'end-verse-line)))
892 (if text (insert text)))))
893 (forward-line 1)))
894 (insert (muse-markup-text 'end-verse) ?\n))
896 (defun muse-publish-markup-table ()
897 "Style does not support tables.")
899 (defun muse-publish-markup-email ()
900 (let* ((beg (match-end 1))
901 (addr (buffer-substring-no-properties beg (match-end 0))))
902 (with-temp-buffer
903 (insert addr)
904 (muse-publish-escape-specials (point-min) (point-max))
905 (setq addr (buffer-string)))
906 (goto-char beg)
907 (delete-region beg (match-end 0))
908 (insert (format (muse-markup-text 'email-addr) addr addr))
909 (muse-publish-mark-read-only beg (point))))
911 (defun muse-publish-escape-specials-in-string (string)
912 "Escape specials in STRING using style-specific :specials."
913 (save-excursion
914 (apply (function concat)
915 (mapcar
916 (lambda (ch)
917 (let ((repl
918 (or (assoc ch (muse-style-element :specials))
919 (assoc ch muse-publish-markup-specials))))
920 (if (null repl)
921 (char-to-string ch)
922 (cdr repl))))
923 (append string nil)))))
925 (defun muse-publish-url (url &optional desc explicit)
926 "Resolve a URL into its final <a href> form."
927 (let ((orig-url url))
928 (dolist (transform muse-publish-url-transforms)
929 (setq url (save-match-data (when url (funcall transform url explicit)))))
930 (setq desc (when desc (muse-publish-escape-specials-in-string desc)))
931 (if url
932 (if (string-match muse-image-regexp url)
933 (if desc
934 (muse-markup-text 'image-with-desc url desc)
935 (muse-markup-text 'image-link url))
936 (if (and desc (string-match muse-image-regexp desc))
937 (muse-markup-text 'url-with-image url desc)
938 (muse-markup-text 'url-link url (or desc orig-url))))
939 orig-url)))
941 (defun muse-publish-insert-url (url &optional desc explicit)
942 "Resolve a URL into its final <a href> form."
943 (delete-region (match-beginning 0) (match-end 0))
944 (let ((beg (point)))
945 (insert (muse-publish-url url desc explicit))
946 (muse-publish-mark-read-only beg (point))))
948 (defun muse-publish-markup-link ()
949 (let ((explicit (save-match-data
950 (if (string-match muse-explicit-link-regexp
951 (match-string 0))
952 t nil))))
953 (muse-publish-insert-url (match-string 1) (match-string 2) explicit)))
955 (defun muse-publish-markup-url ()
956 (muse-publish-insert-url (match-string 0)))
958 ;; Default publishing tags
960 (defun muse-publish-contents-tag (beg end attrs)
961 (set (make-local-variable 'muse-publish-generate-contents)
962 (cons (copy-marker (point) t)
963 (let ((depth (cdr (assoc "depth" attrs))))
964 (or (and depth (string-to-number depth)) 2)))))
966 (defun muse-publish-verse-tag (beg end)
967 (save-excursion
968 (goto-char beg)
969 (while (eq ?\ (char-syntax (char-after)))
970 (delete-char 1))
971 (while (< (point) end)
972 (insert "> ")
973 (forward-line))
974 (if (eq ?\ (char-syntax (char-before)))
975 (delete-char -1))))
977 (defun muse-publish-example-tag (beg end)
978 (muse-publish-escape-specials beg end)
979 (goto-char beg)
980 (insert (muse-markup-text 'begin-example))
981 (goto-char end)
982 (insert (muse-markup-text 'end-example))
983 (muse-publish-mark-read-only beg (point)))
985 (defun muse-publish-mark-read-only (beg end)
986 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
987 nil)
989 (defun muse-publish-verbatim-tag (beg end)
990 (muse-publish-escape-specials beg end)
991 (muse-publish-mark-read-only beg end))
993 (defalias 'muse-publish-class-tag 'ignore)
995 (defun muse-publish-lisp-tag (beg end)
996 (save-excursion
997 (let ((str (muse-eval-lisp
998 (prog1
999 (buffer-substring-no-properties beg end)
1000 (delete-region beg end)))))
1001 (set-text-properties 0 (length str) nil str)
1002 (insert str))))
1004 (defun muse-publish-command-tag (beg end attrs)
1005 (while (looking-at "\\s-*$")
1006 (forward-line))
1007 (let ((interp (cdr (assoc "interp" attrs))))
1008 (if (null interp)
1009 (shell-command
1010 (prog1
1011 (buffer-substring-no-properties (point) end)
1012 (delete-region beg end)) t)
1013 (shell-command-on-region beg end interp t t))
1014 (muse-publish-mark-read-only beg (point))))
1016 (defun muse-publish-comment-tag (beg end)
1017 (delete-region beg end))
1019 ;; Miscellaneous helper functions
1021 (defsubst muse-publishing-directive (name)
1022 (cdr (assoc name muse-publishing-directives)))
1024 (defun muse-publish-strip-tags (string)
1025 "Remove all tags from the string."
1026 (while (string-match "<.*?>" string)
1027 (setq string (replace-match "" nil t string)))
1028 string)
1030 (defun muse-publish-markup-type (category default-func)
1031 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1032 (funcall (or rule default-func))))
1034 (defun muse-published-buffer-contents (buffer)
1035 (with-current-buffer buffer
1036 (goto-char (point-min))
1037 (let ((beg (and (search-forward "Emacs Muse begins here")
1038 (muse-line-end-position)))
1039 (end (and (search-forward "Emacs Muse ends here")
1040 (muse-line-beginning-position))))
1041 (buffer-substring-no-properties beg end))))
1043 (defun muse-published-contents (file)
1044 (when (file-readable-p file)
1045 (with-temp-buffer
1046 (insert-file-contents file)
1047 (muse-published-buffer-contents (current-buffer)))))
1049 (defun muse-publish-transform-output
1050 (file temp-file output-path name gen-func &rest cleanup-exts)
1051 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1052 (setq file (muse-page-name file))
1053 (message "Generating %s output for %s..." name file)
1054 (if (not (funcall gen-func temp-file output-path))
1055 (message "Generating %s from %s...failed" name file)
1056 (message "Generating %s output for %s...done" name file)
1057 (muse-delete-file-if-exists temp-file)
1058 (dolist (ext cleanup-exts)
1059 (muse-delete-file-if-exists
1060 (expand-file-name (concat file ext)
1061 (file-name-directory output-path))))
1062 (message "Wrote %s" output-path)))
1064 (defun muse-publish-read-only (string)
1065 (let ((end (1- (length string))))
1066 (add-text-properties 0 end
1067 '(rear-nonsticky (read-only) read-only t)
1068 string)
1069 string))
1071 (defun muse-publish-prepare-url (target &rest ignored)
1072 (save-match-data
1073 (unless (or (string-match muse-url-regexp target)
1074 (string-match muse-image-regexp target)
1075 (string-match muse-file-regexp target))
1076 (setq target (if (string-match "#" target)
1077 (concat (muse-publish-output-name
1078 (substring target 0 (match-beginning 0)))
1079 "#" (substring target (match-end 0)))
1080 (muse-publish-output-name target)))))
1081 target)
1083 (provide 'muse-publish)
1085 ;;; muse-publish.el ends here