Interpret explicit links before emphasis.
[muse-el.git] / lisp / muse-publish.el
blob77bf26db3fd89cbf7ce99f01fd6d98faa2245d88
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-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 (or file muse-publishing-current-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 (setq muse-current-file file)
543 (muse-publish-markup-buffer (muse-page-name file) style)
544 (let ((backup-inhibited t))
545 (write-file output-path))
546 (muse-style-run-hooks :final style file output-path target))
547 t)))
549 (defun muse-publish-this-file (style output-dir &optional force)
550 "Publish the page in the current file."
551 (interactive (muse-publish-get-info))
552 (muse-publish-file buffer-file-name style output-dir force))
554 (defun muse-batch-publish-files ()
555 "Publish Muse files in batch mode."
556 (let ((muse-batch-publishing-p t)
557 style-name style output-dir)
558 (setq style-name (car command-line-args-left)
559 style (muse-style style-name)
560 command-line-args-left (cdr command-line-args-left)
561 output-dir (car command-line-args-left)
562 output-dir
563 (if (string-match "\\`--output-dir=" output-dir)
564 (prog1
565 (substring output-dir (match-end 0))
566 (setq command-line-args-left (cdr command-line-args-left)))))
567 (unless style
568 (error "There is no style '%s' defined." style-name))
569 (dolist (file command-line-args-left)
570 (muse-publish-file file style output-dir t))))
572 ;; Default publishing rules
574 (defun muse-publish-markup-directive (&optional name value)
575 (unless name (setq name (match-string 1)))
576 (unless value (setq value (match-string 2)))
577 (let ((elem (assoc name muse-publishing-directives)))
578 (if elem
579 (setcdr elem value)
580 (setq muse-publishing-directives
581 (cons (cons name value)
582 muse-publishing-directives))))
583 (delete-region (match-beginning 0) (match-end 0)))
585 (defun muse-publish-markup-anchor () "")
586 (defun muse-publish-markup-comment () "")
588 (defun muse-publish-markup-tag ()
589 (let ((tag-info (muse-markup-tag-info (match-string 1))))
590 (when (and tag-info
591 (not (get-text-property (match-beginning 0) 'read-only)))
592 (let ((closed-tag (match-string 3))
593 (start (match-beginning 0))
594 (beg (point)) end attrs)
595 (when (nth 2 tag-info)
596 (let ((attrstr (match-string 2)))
597 (while (and attrstr
598 (string-match (concat "\\([^"
599 muse-regexp-space
600 "=]+\\)\\(=\"\\"
601 "([^\"]+\\)\"\\)?")
602 attrstr))
603 (let ((attr (cons (downcase
604 (muse-match-string-no-properties 1 attrstr))
605 (muse-match-string-no-properties 3 attrstr))))
606 (setq attrstr (replace-match "" t t attrstr))
607 (if attrs
608 (nconc attrs (list attr))
609 (setq attrs (list attr)))))))
610 (if (and (cadr tag-info) (not closed-tag))
611 (if (search-forward (concat "</" (car tag-info) ">") nil t)
612 (delete-region (match-beginning 0) (point))
613 (setq tag-info nil)))
614 (when tag-info
615 (setq end (point-marker))
616 (delete-region start beg)
617 (goto-char start)
618 (let ((args (list start end)))
619 (if (nth 2 tag-info)
620 (nconc args (list attrs)))
621 (apply (nth 3 tag-info) args))))))
622 nil)
624 (defun muse-publish-escape-specials (beg end)
625 (save-excursion
626 (goto-char beg)
627 (while (< (point) end)
628 (if (get-text-property (point) 'read-only)
629 (forward-char 1)
630 (let ((repl
631 (or (assoc (char-after) (muse-style-element :specials))
632 (assoc (char-after) muse-publish-markup-specials))))
633 (if (null repl)
634 (forward-char 1)
635 (delete-char 1)
636 (insert (cdr repl))))))))
638 (defun muse-publish-markup-word ()
639 (let* ((beg (match-beginning 2))
640 (end (1- (match-end 2)))
641 (leader (buffer-substring-no-properties beg end))
642 open-tag close-tag mark-read-only loc multi-line)
643 (cond
644 ((string= leader "_")
645 (setq multi-line t)
646 (setq open-tag (muse-markup-text 'begin-underline)
647 close-tag (muse-markup-text 'end-underline)))
648 ((string= leader "=")
649 (setq open-tag (muse-markup-text 'begin-literal)
650 close-tag (muse-markup-text 'end-literal))
651 (setq mark-read-only t))
653 (setq multi-line t)
654 (let ((l (length leader)))
655 (cond
656 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
657 close-tag (muse-markup-text 'end-emph)))
658 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
659 close-tag (muse-markup-text 'end-more-emph)))
660 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
661 close-tag (muse-markup-text 'end-most-emph)))))))
662 (if (and (setq loc (search-forward leader nil t))
663 (eq 0 (skip-syntax-forward "w" (1+ loc)))
664 (or multi-line (= 1 (count-lines beg loc))))
665 (progn
666 (replace-match "")
667 (delete-region beg end)
668 (setq end (point-marker))
669 (insert close-tag)
670 (save-excursion
671 (goto-char beg)
672 (insert open-tag)
673 (setq beg (point)))
674 (when mark-read-only
675 (muse-publish-escape-specials beg end)
676 (muse-publish-mark-read-only (1- beg) (1+ end))))
677 (backward-char))
678 nil))
680 (defun muse-publish-markup-emdash ()
681 (delete-region (match-beginning 0) (match-end 0))
682 (insert (muse-markup-text 'emdash))
683 (if (eq (char-after) ?\<)
684 (insert ?\n)))
686 (defun muse-publish-markup-enddots ()
687 (delete-region (match-beginning 0) (match-end 0))
688 (insert (muse-markup-text 'enddots)))
690 (defun muse-publish-markup-dots ()
691 (delete-region (match-beginning 0) (match-end 0))
692 (insert (muse-markup-text 'dots)))
694 (defun muse-publish-markup-rule ()
695 (delete-region (match-beginning 0) (match-end 0))
696 (insert (muse-markup-text 'rule)))
698 (defun muse-publish-markup-heading ()
699 (let* ((len (length (match-string 1)))
700 (start (muse-markup-text
701 (cond ((= len 1) 'section)
702 ((= len 2) 'subsection)
703 ((= len 3) 'subsubsection))))
704 (end (muse-markup-text
705 (cond ((= len 1) 'section-end)
706 ((= len 2) 'subsection-end)
707 ((= len 3) 'subsubsection-end)))))
708 (delete-region (match-beginning 0) (match-end 0))
709 (insert start)
710 (end-of-line)
711 (if end (insert end))))
713 (defvar muse-publish-footnotes nil)
715 (defun muse-publish-markup-footnote ()
716 "Scan ahead and snarf up the footnote body"
717 (if (= (muse-line-beginning-position) (match-beginning 0))
719 (let ((footnote (save-match-data
720 (string-to-number (match-string 1))))
721 footnotemark)
722 (delete-region (match-beginning 0) (match-end 0))
723 (save-excursion
724 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
725 (let* ((start (match-beginning 0))
726 (beg (goto-char (match-end 0)))
727 (end (save-excursion
728 (if (search-forward "\n\n" nil t)
729 (copy-marker (match-beginning 0))
730 (goto-char (point-max))
731 (skip-chars-backward "\n")
732 (point-marker)))))
733 (while (re-search-forward (concat "^["
734 muse-regexp-blank
735 "]+\\([^\n]\\)")
736 end t)
737 (replace-match "\\1" t))
738 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
739 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
740 (if (string= "" footnotemark-cmd)
741 (setq footnotemark
742 (concat (muse-markup-text 'footnote)
743 (buffer-substring-no-properties beg end)
744 (muse-markup-text 'footnote-end)))
745 (setq footnotemark (format footnotemark-cmd footnote
746 footnotemark-end-cmd))
747 (unless muse-publish-footnotes
748 (set (make-local-variable 'muse-publish-footnotes)
749 (make-vector 256 nil)))
750 (unless (aref muse-publish-footnotes footnote)
751 (setq footnotemark
752 (concat
753 footnotemark
754 (concat (format (muse-markup-text 'footnotetext)
755 footnote)
756 (buffer-substring-no-properties beg end)
757 (muse-markup-text 'footnotetext-end))))
758 (aset muse-publish-footnotes footnote footnotemark))))
759 (goto-char end)
760 (skip-chars-forward "\n")
761 (delete-region start (point)))))
762 (insert (or footnotemark footnote)))))
764 (defun muse-publish-markup-fn-sep ()
765 (delete-region (match-beginning 0) (match-end 0))
766 (insert (muse-markup-text 'fn-sep)))
768 (defun muse-publish-surround-text (beg-tag end-tag move-func)
769 (let ((beg (point)) end)
770 (skip-chars-backward muse-regexp-space)
771 (delete-region (point) beg)
772 (insert "\n\n" beg-tag)
773 (funcall move-func)
774 (setq end (point-marker))
775 (goto-char beg)
776 (while (< (point) end)
777 (if (looking-at "^\\s-+")
778 (replace-match ""))
779 (forward-line 1))
780 (goto-char end)
781 (setq beg (point))
782 (skip-chars-backward muse-regexp-space)
783 (delete-region (point) beg))
784 (insert end-tag "\n"))
786 (defsubst muse-forward-paragraph (&optional pattern)
787 (if (re-search-forward (if pattern
788 (concat "^\\(" pattern "["
789 muse-regexp-blank
790 "]+\\|$\\|\\'\\)")
791 "^\\s-*\\($\\|\\'\\)") nil t)
792 (goto-char (match-beginning 0))
793 (goto-char (point-max))))
795 (defun muse-publish-markup-list ()
796 "Markup a list entry or quoted paragraph.
797 The reason this function is so funky, is to prevent text properties
798 like read-only from being inadvertently deleted."
799 (let ((str (match-string 1)))
800 (cond
801 ((and (eq (aref str 0) ?-))
802 (delete-region (match-beginning 0) (match-end 0))
803 (muse-publish-surround-text
804 (muse-markup-text 'begin-uli)
805 (muse-markup-text 'end-uli)
806 (function
807 (lambda ()
808 (muse-forward-paragraph (concat "["
809 muse-regexp-blank
810 "]+-"))))))
811 ((and (>= (aref str 0) ?0)
812 (<= (aref str 0) ?9))
813 (delete-region (match-beginning 0) (match-end 0))
814 (muse-publish-surround-text
815 (muse-markup-text 'begin-oli)
816 (muse-markup-text 'end-oli)
817 (function
818 (lambda ()
819 (muse-forward-paragraph (concat "["
820 muse-regexp-blank
821 "]+[0-9]+\\."))))))
823 (goto-char (match-beginning 1))
824 (insert (muse-markup-text 'begin-ddt))
825 (save-match-data
826 (save-excursion
827 (forward-line 1)
828 (while (looking-at (concat "^\\(["
829 muse-regexp-blank
830 "]*\\)[^"
831 muse-regexp-space
832 "]"))
833 (delete-region (match-beginning 1) (match-end 1))
834 (forward-line 1))))
835 (save-match-data
836 (when (re-search-forward (concat "["
837 muse-regexp-space
838 "]+::["
839 muse-regexp-space
840 "]+")
841 nil t)
842 (replace-match (muse-markup-text 'start-dde))))
843 (muse-forward-paragraph)
844 (insert (muse-markup-text 'end-ddt) ?\n)))))
846 (defun muse-publish-markup-quote ()
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* ((ws (match-string 0))
851 (centered (>= (string-width ws) 6))
852 (begin-elem (if centered 'begin-center 'begin-quote))
853 (end-elem (if centered 'end-center 'end-quote)))
854 (muse-publish-surround-text (muse-markup-text begin-elem)
855 (muse-markup-text end-elem)
856 'muse-forward-paragraph)))
858 (defun muse-publish-markup-leading-space ()
859 (let ((markup-space (muse-markup-text 'verse-space))
860 count)
861 (when (and markup-space
862 (>= (setq count (skip-chars-forward " ")) 0))
863 (delete-region (muse-line-beginning-position) (point))
864 (while (> count 0)
865 (insert markup-space)
866 (setq count (- count 2))))))
868 (defun muse-publish-markup-verse ()
869 (let ((leader (match-string 0)))
870 (goto-char (match-beginning 0))
871 (insert (muse-markup-text 'begin-verse))
872 (while (looking-at leader)
873 (replace-match "")
874 (muse-publish-markup-leading-space)
875 (end-of-line)
876 (cond
877 ((bolp)
878 (let ((text (muse-markup-text 'empty-verse-line)))
879 (if text (insert text))))
880 ((save-excursion
881 (save-match-data
882 (forward-line 1)
883 (or (looking-at (concat leader "["
884 muse-regexp-blank
885 "]*$"))
886 (not (looking-at leader)))))
887 (let ((text (muse-markup-text 'last-stanza-end)))
888 (if text (insert text))))
890 (let ((text (muse-markup-text 'end-verse-line)))
891 (if text (insert text)))))
892 (forward-line 1)))
893 (insert (muse-markup-text 'end-verse) ?\n))
895 (defun muse-publish-markup-table ()
896 "Style does not support tables.")
898 (defun muse-publish-markup-email ()
899 (let* ((beg (match-end 1))
900 (addr (buffer-substring-no-properties beg (match-end 0))))
901 (with-temp-buffer
902 (insert addr)
903 (muse-publish-escape-specials (point-min) (point-max))
904 (setq addr (buffer-string)))
905 (goto-char beg)
906 (delete-region beg (match-end 0))
907 (insert (format (muse-markup-text 'email-addr) addr addr))
908 (muse-publish-mark-read-only beg (point))))
910 (defun muse-publish-url (url &optional desc)
911 "Resolve a URL into its final <a href> form."
912 (let ((orig-url url))
913 (dolist (transform muse-publish-url-transforms)
914 (setq url (save-match-data (funcall transform url))))
915 (if (string-match muse-image-regexp url)
916 (if desc
917 (muse-markup-text 'image-with-desc url desc)
918 (muse-markup-text 'image-link url))
919 (if (and desc (string-match muse-image-regexp desc))
920 (muse-markup-text 'url-with-image url desc)
921 (muse-markup-text 'url-link url (or desc orig-url))))))
923 (defun muse-publish-insert-url (url &optional desc)
924 "Resolve a URL into its final <a href> form."
925 (delete-region (match-beginning 0) (match-end 0))
926 (let ((beg (point)))
927 (insert (muse-publish-url url desc))
928 (muse-publish-mark-read-only beg (point))))
930 (defun muse-publish-markup-link ()
931 (muse-publish-insert-url (match-string 1) (match-string 2)))
933 (defun muse-publish-markup-url ()
934 (muse-publish-insert-url (match-string 0)))
936 ;; Default publishing tags
938 (defun muse-publish-contents-tag (beg end attrs)
939 (set (make-local-variable 'muse-publish-generate-contents)
940 (cons (copy-marker (point) t)
941 (let ((depth (cdr (assoc "depth" attrs))))
942 (or (and depth (string-to-number depth)) 2)))))
944 (defun muse-publish-verse-tag (beg end)
945 (save-excursion
946 (goto-char beg)
947 (while (eq ?\ (char-syntax (char-after)))
948 (delete-char 1))
949 (while (< (point) end)
950 (insert "> ")
951 (forward-line))
952 (if (eq ?\ (char-syntax (char-before)))
953 (delete-char -1))))
955 (defun muse-publish-example-tag (beg end)
956 (muse-publish-escape-specials beg end)
957 (goto-char beg)
958 (insert (muse-markup-text 'begin-example))
959 (goto-char end)
960 (insert (muse-markup-text 'end-example))
961 (muse-publish-mark-read-only beg (point)))
963 (defun muse-publish-mark-read-only (beg end)
964 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
965 nil)
967 (defun muse-publish-verbatim-tag (beg end)
968 (muse-publish-escape-specials beg end)
969 (muse-publish-mark-read-only beg end))
971 (defalias 'muse-publish-class-tag 'ignore)
973 (defun muse-publish-lisp-tag (beg end)
974 (save-excursion
975 (let ((str (muse-eval-lisp
976 (prog1
977 (buffer-substring-no-properties beg end)
978 (delete-region beg end)))))
979 (set-text-properties 0 (length str) nil str)
980 (insert str))))
982 (defun muse-publish-command-tag (beg end attrs)
983 (while (looking-at "\\s-*$")
984 (forward-line))
985 (let ((interp (cdr (assoc "interp" attrs))))
986 (if (null interp)
987 (shell-command
988 (prog1
989 (buffer-substring-no-properties (point) end)
990 (delete-region beg end)) t)
991 (shell-command-on-region beg end interp t t))
992 (muse-publish-mark-read-only beg (point))))
994 (defun muse-publish-comment-tag (beg end)
995 (delete-region beg end))
997 ;; Miscellaneous helper functions
999 (defsubst muse-publishing-directive (name)
1000 (cdr (assoc name muse-publishing-directives)))
1002 (defun muse-publish-strip-tags (string)
1003 "Remove all tags from the string."
1004 (while (string-match "<.*?>" string)
1005 (setq string (replace-match "" nil t string)))
1006 string)
1008 (defun muse-publish-markup-type (category default-func)
1009 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1010 (funcall (or rule default-func))))
1012 (defun muse-published-buffer-contents (buffer)
1013 (with-current-buffer buffer
1014 (goto-char (point-min))
1015 (let ((beg (and (search-forward "Emacs Muse begins here")
1016 (muse-line-end-position)))
1017 (end (and (search-forward "Emacs Muse ends here")
1018 (muse-line-beginning-position))))
1019 (buffer-substring-no-properties beg end))))
1021 (defun muse-published-contents (file)
1022 (when (file-readable-p file)
1023 (with-temp-buffer
1024 (insert-file-contents file)
1025 (muse-published-buffer-contents (current-buffer)))))
1027 (defun muse-publish-transform-output
1028 (file temp-file output-path name gen-func &rest cleanup-exts)
1029 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1030 (setq file (muse-page-name file))
1031 (message "Generating %s output for %s..." name file)
1032 (if (not (funcall gen-func temp-file output-path))
1033 (message "Generating %s from %s...failed" name file)
1034 (message "Generating %s output for %s...done" name file)
1035 (muse-delete-file-if-exists temp-file)
1036 (dolist (ext cleanup-exts)
1037 (muse-delete-file-if-exists
1038 (expand-file-name (concat file ext)
1039 (file-name-directory output-path))))
1040 (message "Wrote %s" output-path)))
1042 (defun muse-publish-read-only (string)
1043 (let ((end (1- (length string))))
1044 (add-text-properties 0 end
1045 '(rear-nonsticky (read-only) read-only t)
1046 string)
1047 string))
1049 (defun muse-publish-prepare-url (target)
1050 (save-match-data
1051 (unless (or (string-match muse-url-regexp target)
1052 (string-match muse-image-regexp target)
1053 (string-match muse-file-regexp target))
1054 (setq target (if (string-match "#" target)
1055 (concat (muse-publish-output-name
1056 (substring target 0 (match-beginning 0)))
1057 "#" (substring target (match-end 0)))
1058 (muse-publish-output-name target)))))
1059 (muse-publish-read-only target))
1061 (provide 'muse-publish)
1063 ;;; muse-publish.el ends here