Merged from mwolson@gnu.org--2005 (patch 263, 266-268)
[muse-el.git] / lisp / muse-publish.el
blobcb4d2f2ee103ac8005fc10bcd3715cb1a6529a5b
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 ;; Peter K. Lee (saint AT corenova DOT com) provided the
30 ;; `muse-style-elements-list' function.
32 ;;; Code:
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 ;; Muse Publishing
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
40 (require 'muse)
41 (require 'muse-regexps)
43 (defgroup muse-publish nil
44 "Options controlling the general behavior of Muse publishing."
45 :group 'muse)
47 (defcustom muse-before-publish-hook nil
48 "A hook run in the buffer to be published, before it is done."
49 :type 'hook
50 :group 'muse-publish)
52 (defcustom muse-after-publish-hook nil
53 "A hook run in the buffer to be published, after it is done."
54 :type 'hook
55 :group 'muse-publish)
57 (defcustom muse-publish-url-transforms
58 '(muse-publish-escape-specials-in-string
59 muse-publish-prepare-url
60 muse-resolve-url)
61 "A list of functions used to prepare URLs for publication.
62 Each is passed the URL. The transformed URL should be returned."
63 :type 'hook
64 :options '(muse-publish-escape-specials-in-string
65 muse-publish-prepare-url
66 muse-resolve-url)
67 :group 'muse-publish)
69 (defcustom muse-publish-desc-transforms
70 '(muse-publish-escape-specials-in-string)
71 "A list of functions used to prepare URL desciptions for publication.
72 Each is passed the description. The modified description should
73 be returned."
74 :type 'hook
75 :options '(muse-publish-escape-specials-in-string)
76 :group 'muse-publish)
78 (defcustom muse-publish-comments-p nil
79 "If nil, remove comments before publishing.
80 If non-nil, publish comments using the markup of the current style."
81 :type 'boolean
82 :group 'muse-publish)
84 (defcustom muse-publish-report-threshhold 100000
85 "If a file is this size or larger, report publishing progress."
86 :type 'integer
87 :group 'muse-publish)
89 (defcustom muse-publish-markup-regexps
90 `(;; Remove leading and trailing whitespace from the file
91 (1000 "\\(\\`\n+\\|\n+\\'\\)" 0 "")
93 ;; Remove trailing whitespace from all lines
94 (1100 ,(concat "[" muse-regexp-blank "]+$") 0 "")
96 ;; Handle any leading #directives
97 (1200 "\\`#\\([a-zA-Z-]+\\)\\s-+\\(.+\\)\n+" 0 directive)
99 ;; markup tags
100 (1300 muse-tag-regexp 0 tag)
102 ;; commented lines
103 (1350 "^;\\s-+\\(.+\\)" 0 comment)
105 ;; define anchor points
106 (1400 "^\\(\\W*\\)#\\(\\S-+\\)\\s-*" 0 anchor)
108 ;; prevent emphasis characters in explicit links from being marked
109 (1500 muse-explicit-link-regexp 0 muse-publish-mark-noemphasis)
111 ;; emphasized or literal text
112 (1600 ,(concat
113 "\\(^\\|[-["
114 muse-regexp-space
115 "<('`\"]\\)\\(=[^="
116 muse-regexp-space
117 "]\\|_[^_"
118 muse-regexp-space
119 "]\\|\\*+[^*"
120 muse-regexp-space
121 "]\\)")
122 2 word)
124 ;; headings, outline-mode style
125 (1700 "^\\(\\*+\\)\\s-+" 0 heading)
127 ;; ellipses
128 (1800 "\\.\\.\\.\\." 0 enddots)
129 (1850 "\\.\\.\\." 0 dots)
131 ;; horizontal rule, or section separator
132 (1900 "^----+" 0 rule)
134 ;; beginning of footnotes section
135 (2000 "^Footnotes:?\\s-*" 0 fn-sep)
136 ;; footnote definition/reference (def if at beginning of line)
137 (2100 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote)
139 ;; unnumbered List items begin with a -. numbered list items
140 ;; begin with number and a period. definition lists have a
141 ;; leading term separated from the body with ::. centered
142 ;; paragraphs begin with at least six columns of whitespace; any
143 ;; other whitespace at the beginning indicates a blockquote. The
144 ;; reason all of these rules are handled here, is so that
145 ;; blockquote detection doesn't interfere with indented list
146 ;; members.
147 (2200 ,(concat "^["
148 muse-regexp-blank
149 "]+\\(-["
150 muse-regexp-blank
151 "]*\\|[0-9]+\\.["
152 muse-regexp-blank
153 "]*\\|\\(?:.+?\\)["
154 muse-regexp-blank
155 "]+::\n?\\)")
156 1 list)
158 (2300 ,(concat "^\\(\\(?:.+?\\)["
159 muse-regexp-blank
160 "]+::\n?\\)")
161 0 list)
163 (2400 ,(concat "^\\(["
164 muse-regexp-blank
165 "]+\\)")
166 0 quote)
168 (2500 ,(concat "\\(^\\|["
169 muse-regexp-blank
170 "]+\\)--\\($\\|["
171 muse-regexp-blank
172 "]+\\)")
173 0 emdash)
175 ;; "verse" text is indicated the same way as a quoted e-mail
176 ;; response: "> text", where text may contain initial whitespace
177 ;; (see below).
178 (2600 ,(concat "^["
179 muse-regexp-blank
180 "]*> ")
181 0 verse)
183 ;; simple table markup is supported, nothing fancy. use | to
184 ;; separate cells, || to separate header cells, and ||| for footer
185 ;; cells
186 (2700 ,(concat "^["
187 muse-regexp-blank
188 "]*\\(.+?\\(["
189 muse-regexp-blank
190 "]+|+["
191 muse-regexp-blank
192 "]+.+?\\)\\)$")
193 0 table)
195 ;; replace links in the buffer (links to other pages)
196 (2900 muse-explicit-link-regexp 0 link)
198 ;; bare URLs
199 (3000 muse-url-regexp 0 url)
201 ;; bare email addresses
202 (3500
203 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email)
205 "List of markup rules for publishing a page with Muse.
206 The rules given in this variable are invoked first, followed by
207 whatever rules are specified by the current style.
209 Each member of the list is either a function, or a list of the form:
211 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
213 REGEXP is a regular expression, or symbol whose value is a regular
214 expression, which is searched for using `re-search-forward'.
215 TEXT-BEGIN-GROUP is the matching group within that regexp which
216 denotes the beginning of the actual text to be marked up.
217 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
218 If it is not a string, but a function, it will be called to determine
219 what the replacement text should be (it must return a string). If it
220 is a symbol, the value of that symbol should be a string.
222 The replacements are done in order, one rule at a time. Writing
223 the regular expressions can be a tricky business. Note that case
224 is never ignored. `case-fold-search' is always bound to nil
225 while processing the markup rules."
226 :type '(repeat (choice
227 (list :tag "Markup rule"
228 integer
229 (choice regexp symbol)
230 integer
231 (choice string function symbol))
232 function))
233 :group 'muse-publish)
235 (defcustom muse-publish-markup-functions
236 '((directive . muse-publish-markup-directive)
237 (comment . muse-publish-markup-comment)
238 (anchor . muse-publish-markup-anchor)
239 (tag . muse-publish-markup-tag)
240 (word . muse-publish-markup-word)
241 (emdash . muse-publish-markup-emdash)
242 (enddots . muse-publish-markup-enddots)
243 (dots . muse-publish-markup-dots)
244 (rule . muse-publish-markup-rule)
245 (heading . muse-publish-markup-heading)
246 (footnote . muse-publish-markup-footnote)
247 (fn-sep . muse-publish-markup-fn-sep)
248 (list . muse-publish-markup-list)
249 (quote . muse-publish-markup-quote)
250 (verse . muse-publish-markup-verse)
251 (table . muse-publish-markup-table)
252 (email . muse-publish-markup-email)
253 (link . muse-publish-markup-link)
254 (url . muse-publish-markup-url))
255 "An alist of style types to custom functions for that kind of text.
257 Each member of the list is of the form:
259 (SYMBOL FUNCTION)
261 SYMBOL describes the type of text to associate with this rule.
262 `muse-publish-markup-regexps' maps regexps to these symbols.
264 FUNCTION is the function to use to mark up this kind of rule if
265 no suitable function is found through the :functions tag of the
266 current style."
267 :type '(alist :key-type symbol :value-type function)
268 :group 'muse-publish)
270 (defcustom muse-publish-markup-tags
271 '(("contents" nil t muse-publish-contents-tag)
272 ("verse" t nil muse-publish-verse-tag)
273 ("example" t nil muse-publish-example-tag)
274 ("code" t nil muse-publish-code-tag)
275 ("literal" t nil muse-publish-mark-read-only)
276 ("verbatim" t nil muse-publish-verbatim-tag)
277 ("lisp" t nil muse-publish-lisp-tag)
278 ("class" t t muse-publish-class-tag)
279 ("command" t t muse-publish-command-tag)
280 ("comment" t nil muse-publish-comment-tag))
281 "A list of tag specifications, for specially marking up text.
282 XML-style tags are the best way to add custom markup to Muse.
283 This is easily accomplished by customizing this list of markup tags.
285 For each entry, the name of the tag is given, whether it expects
286 a closing tag and/or an optional set of attributes, and a
287 function that performs whatever action is desired within the
288 delimited region.
290 The tags themselves are deleted during publishing, before the
291 function is called. The function is called with three arguments,
292 the beginning and end of the region surrounded by the tags. If
293 properties are allowed, they are passed as a third argument in
294 the form of an alist. The `end' argument to the function is
295 always a marker.
297 Point is always at the beginning of the region within the tags, when
298 the function is called. Wherever point is when the function finishes
299 is where tag markup will resume.
301 These tag rules are processed once at the beginning of markup, and
302 once at the end, to catch any tags which may have been inserted
303 in-between."
304 :type '(repeat (list (string :tag "Markup tag")
305 (boolean :tag "Expect closing tag" :value t)
306 (boolean :tag "Parse attributes" :value nil)
307 function))
308 :group 'muse-publish)
310 (defcustom muse-publish-markup-specials nil
311 "A table of characters which must be represented specially."
312 :type '(alist :key-type character :value-type string)
313 :group 'muse-publish)
315 (defvar muse-publishing-p nil
316 "Set to t while a page is being published.")
317 (defvar muse-batch-publishing-p nil
318 "Set to t while a page is being batch published.")
319 (defvar muse-publishing-styles nil)
320 (defvar muse-publishing-current-file nil)
321 (defvar muse-publishing-current-style nil)
322 (defvar muse-publishing-directives nil
323 "An alist of publishing directives from the top of a file.")
324 (defvar muse-publish-generate-contents nil
325 "Non-nil if a table of contents should be generated.
326 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
327 tell where the <contents> was seen, and to what depth the
328 contents were requested.")
329 (defvar muse-publishing-last-position nil
330 "Last position of the point when publishing.
331 This is used to make sure that publishing doesn't get stalled.")
333 ;; Functions for handling style information
335 (defsubst muse-style (&optional style)
336 "Resolve the given STYLE into a Muse style, if it is a string."
337 (if (null style)
338 muse-publishing-current-style
339 (if (stringp style)
340 (assoc style muse-publishing-styles)
341 (muse-assert (consp style))
342 style)))
344 (defun muse-define-style (name &rest elements)
345 (let ((entry (assoc name muse-publishing-styles)))
346 (if entry
347 (error "There is already a style named %s." name)
348 (setq muse-publishing-styles
349 (cons (append (list name) elements)
350 muse-publishing-styles)))))
352 (defun muse-derive-style (new-name base-name &rest elements)
353 (let ((entry (assoc new-name muse-publishing-styles)))
354 (if entry
355 (error "There is already a style named %s." new-name)
356 (apply 'muse-define-style new-name
357 (append elements (list :base base-name))))))
359 (defsubst muse-get-keyword (keyword list &optional direct)
360 (let ((value (cadr (memq keyword list))))
361 (if (and (not direct) (symbolp value))
362 (symbol-value value)
363 value)))
365 (defun muse-style-elements-list (elem &optional style)
366 "Return a list all references to ELEM in STYLE, including base styles.
367 If STYLE is not specified, use current style."
368 (let (base elements)
369 (while style
370 (setq style (muse-style style))
371 (setq elements (append elements
372 (muse-get-keyword elem style)))
373 (setq style (muse-get-keyword :base style)))
374 elements))
376 (defsubst muse-style-element (elem &optional style direct)
377 "Search for ELEM in STYLE, including base styles.
378 If STYLE is not specified, use current style."
379 (setq style (muse-style style))
380 (let ((value (muse-get-keyword elem style direct)))
381 (if value
382 value
383 (let ((base (muse-get-keyword :base style)))
384 (if base
385 (muse-style-element elem base direct))))))
387 (defun muse-find-markup-element (keyword ident style)
388 (let ((def (assq ident (muse-style-element keyword style))))
389 (if def
390 (cdr def)
391 (let ((base (muse-style-element :base style)))
392 (if base
393 (muse-find-markup-element keyword ident base))))))
395 (defsubst muse-markup-text (ident &rest args)
396 (let ((text (muse-find-markup-element :strings ident (muse-style))))
397 (if (and text args)
398 (apply 'format text args)
399 (or text ""))))
401 (defun muse-find-markup-tag (keyword tagname style)
402 (let ((def (assoc tagname (muse-style-element keyword style))))
403 (or def
404 (let ((base (muse-style-element :base style)))
405 (if base
406 (muse-find-markup-tag keyword tagname base))))))
408 (defsubst muse-markup-tag-info (tagname &rest args)
409 (let ((tag-info (muse-find-markup-tag :tags tagname (muse-style))))
410 (or tag-info
411 (assoc (match-string 1) muse-publish-markup-tags))))
413 (defsubst muse-markup-function (category)
414 (let ((func (muse-find-markup-element :functions category (muse-style))))
415 (or func
416 (cdr (assq category muse-publish-markup-functions)))))
418 ;; Publishing routines
420 (defun muse-publish-markup (name rules)
421 (let* ((case-fold-search nil)
422 (inhibit-read-only t)
423 (limit (* (length rules) (point-max)))
424 (verbose (and muse-publish-report-threshhold
425 (> (point-max) muse-publish-report-threshhold)))
426 (base 0))
427 (while rules
428 (goto-char (point-min))
429 (let ((regexp (nth 1 (car rules)))
430 (group (nth 2 (car rules)))
431 (repl (nth 3 (car rules)))
432 pos)
433 (setq muse-publishing-last-position nil)
434 (if (symbolp regexp)
435 (setq regexp (symbol-value regexp)))
436 (if (and verbose (not muse-batch-publishing-p))
437 (message "Publishing %s...%d%%" name
438 (* (/ (float (+ (point) base)) limit) 100)))
439 (while (and regexp (setq pos (re-search-forward regexp nil t)))
440 (if (and verbose (not muse-batch-publishing-p))
441 (message "Publishing %s...%d%%" name
442 (* (/ (float (+ (point) base)) limit) 100)))
443 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
444 (get-text-property (match-beginning group) 'read-only))
445 (let* (func
446 (text (cond
447 ((and (symbolp repl)
448 (setq func (muse-markup-function repl)))
449 (funcall func))
450 ((functionp repl)
451 (funcall repl))
452 ((symbolp repl)
453 (symbol-value repl))
454 (t repl))))
455 (if text
456 (replace-match text t))))
457 (if (and muse-publishing-last-position
458 (= pos muse-publishing-last-position))
459 (if (eobp)
460 (setq regexp nil)
461 (forward-char 1)))
462 (setq muse-publishing-last-position pos)))
463 (setq rules (cdr rules)
464 base (+ base (point-max))))
465 (if (and verbose (not muse-batch-publishing-p))
466 (message "Publishing %s...done" name))))
468 (defun muse-insert-file-or-string (file-or-string &optional title)
469 (let ((beg (point)) end)
470 (if (and (not (string-equal file-or-string ""))
471 (file-readable-p file-or-string))
472 (setq end (+ beg (cadr (insert-file-contents file-or-string))))
473 (insert file-or-string)
474 (setq end (point)))
475 (save-restriction
476 (narrow-to-region beg end)
477 (muse-publish-markup (or title "")
478 '((100 "<\\(lisp\\)>" 0
479 muse-publish-markup-tag))))))
481 (defun muse-style-run-hooks (keyword style &rest args)
482 (let (handled)
483 (while (and style (not handled))
484 (setq style (muse-style style))
485 (let ((func (muse-get-keyword keyword style t)))
486 (if func
487 (if (apply func args)
488 (setq handled t))))
489 (unless handled
490 (setq style (muse-style-element :base style))))
491 handled))
493 (defun muse-publish-markup-region (beg end title style)
494 "Apply the given STYLE's markup rules to the given region."
495 (save-restriction
496 (narrow-to-region beg end)
497 (muse-style-run-hooks :before style)
498 (muse-publish-markup
499 title
500 (sort (copy-alist (append muse-publish-markup-regexps
501 (muse-style-elements-list :regexps style)))
502 (function
503 (lambda (l r)
504 (< (car l) (car r))))))
505 (muse-style-run-hooks :before-end style)))
507 (defun muse-publish-markup-buffer (title style)
508 "Apply the given STYLE's markup rules to the current buffer."
509 (setq style (muse-style style))
510 (let ((style-header (muse-style-element :header style))
511 (style-footer (muse-style-element :footer style))
512 (muse-publishing-current-style style)
513 (muse-publishing-directives
514 (list (cons "title" title)
515 (cons "author" (user-full-name))
516 (cons "date" (format-time-string
517 "%B %e, %Y"
518 (nth 5 (file-attributes
519 muse-publishing-current-file))))))
520 (muse-publishing-p t))
521 (run-hooks 'muse-before-publish-hook)
522 (muse-publish-markup-region (point-min) (point-max) title style)
523 (goto-char (point-min))
524 (if style-header (muse-insert-file-or-string style-header title))
525 (goto-char (point-max))
526 (if style-footer (muse-insert-file-or-string style-footer title))
527 (muse-style-run-hooks :after style)
528 (run-hooks 'muse-after-publish-hook)))
530 (defun muse-publish-markup-string (string &optional style)
531 "Markup STRING using the given STYLE's markup rules."
532 (setq style (muse-style style))
533 (muse-with-temp-buffer
534 (insert string)
535 (let ((muse-publishing-current-style style)
536 (muse-publishing-p t))
537 (muse-publish-markup "*string*" (muse-style-element :rules style)))
538 (buffer-string)))
540 ;; Commands for publishing files
542 (defsubst muse-publish-get-style ()
543 (if (= 1 (length muse-publishing-styles))
544 (car muse-publishing-styles)
545 (assoc (completing-read "Publish with style: "
546 muse-publishing-styles nil t)
547 muse-publishing-styles)))
549 (defsubst muse-publish-get-output-dir (style)
550 (let ((default-directory (or (muse-style-element :path style)
551 default-directory)))
552 (muse-read-directory-name "Publish to directory: " nil default-directory)))
554 (defsubst muse-publish-get-info ()
555 (let ((style (muse-publish-get-style)))
556 (list style (muse-publish-get-output-dir style)
557 current-prefix-arg)))
559 (defsubst muse-publish-output-name (&optional file style)
560 (setq style (muse-style style))
561 (concat (muse-style-element :prefix style)
562 (muse-page-name file)
563 (muse-style-element :suffix style)))
565 (defsubst muse-publish-output-file (file &optional output-dir style)
566 (setq style (muse-style style))
567 (if output-dir
568 (expand-file-name (muse-publish-output-name file style) output-dir)
569 (concat (file-name-directory file)
570 (muse-publish-output-name file style))))
572 (defsubst muse-publish-link-name (&optional file style)
573 (setq style (muse-style style))
574 (concat (muse-style-element :prefix style)
575 (muse-page-name file)
576 (or (muse-style-element :link-suffix style)
577 (muse-style-element :suffix style))))
579 (defsubst muse-publish-link-file (file &optional output-dir style)
580 (setq style (muse-style style))
581 (if output-dir
582 (expand-file-name (muse-publish-link-name file style) output-dir)
583 (concat (file-name-directory file)
584 (muse-publish-link-name file style))))
586 (defun muse-publish-file (file style &optional output-dir force)
587 "Publish the given file in list FILES.
588 If the argument FORCE is nil, each file is only published if it is
589 newer than the published version. If the argument FORCE is non-nil,
590 the file is published no matter what."
591 (interactive (cons (read-file-name "Publish file: ")
592 (muse-publish-get-info)))
593 (setq style (muse-style style))
594 (let* ((output-path (muse-publish-output-file file output-dir style))
595 (output-suffix (muse-style-element :osuffix style))
596 (muse-publishing-current-file file)
597 (target (if output-suffix
598 (concat (file-name-sans-extension output-path)
599 output-suffix)
600 output-path)))
601 (when (or force (file-newer-than-file-p file target))
602 (if (and muse-publish-report-threshhold
603 (> (nth 7 (file-attributes file))
604 muse-publish-report-threshhold))
605 (message "Publishing %s ..." file))
606 (muse-with-temp-buffer
607 (insert-file-contents file)
608 (muse-publish-markup-buffer (muse-page-name file) style)
609 (let ((backup-inhibited t))
610 (write-file output-path))
611 (muse-style-run-hooks :final style file output-path target))
612 t)))
614 (defun muse-publish-this-file (style output-dir &optional force)
615 "Publish the page in the current file."
616 (interactive (muse-publish-get-info))
617 (unless (muse-publish-file buffer-file-name style output-dir force)
618 (message (concat "The published version is up-to-date; use"
619 " C-u C-c C-t to force an update."))))
621 (defun muse-batch-publish-files ()
622 "Publish Muse files in batch mode."
623 (let ((muse-batch-publishing-p t)
624 style-name style output-dir)
625 (setq style-name (car command-line-args-left)
626 style (muse-style style-name)
627 command-line-args-left (cdr command-line-args-left)
628 output-dir (car command-line-args-left)
629 output-dir
630 (if (string-match "\\`--output-dir=" output-dir)
631 (prog1
632 (substring output-dir (match-end 0))
633 (setq command-line-args-left (cdr command-line-args-left)))))
634 (unless style
635 (error "There is no style '%s' defined." style-name))
636 (dolist (file command-line-args-left)
637 (muse-publish-file file style output-dir t))))
639 ;; Default publishing rules
641 (defun muse-publish-section-close (depth)
642 "Seach forward for the closing tag of given DEPTH."
643 (let (not-end)
644 (save-excursion
645 (while (and (setq not-end (re-search-forward
646 (concat "^\\*\\{1," (number-to-string depth)
647 "\\}\\s-+")
648 nil t))
649 (get-text-property (match-beginning 0) 'read-only)))
650 (if not-end
651 (forward-line 0)
652 (goto-char (point-max)))
653 (cond ((not (eq (char-before) ?\n))
654 (insert "\n\n"))
655 ((not (eq (char-before (1- (point))) ?\n))
656 (insert "\n")))
657 (insert (muse-markup-text 'section-close depth) "\n"))))
659 (defun muse-publish-markup-directive (&optional name value)
660 (unless name (setq name (match-string 1)))
661 (unless value (setq value (match-string 2)))
662 (let ((elem (assoc name muse-publishing-directives)))
663 (if elem
664 (setcdr elem value)
665 (setq muse-publishing-directives
666 (cons (cons name value)
667 muse-publishing-directives))))
668 ;; Make sure we don't ever try to move the point forward (past the
669 ;; beginning of buffer) while we're still searching for directives.
670 (setq muse-publishing-last-position nil)
671 (delete-region (match-beginning 0) (match-end 0)))
673 (defun muse-publish-markup-anchor () "")
675 (defun muse-publish-markup-comment ()
676 (if (null muse-publish-comments-p)
678 (goto-char (match-end 0))
679 (muse-insert-markup (muse-markup-text 'comment-end))
680 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
681 (goto-char (match-beginning 1))
682 (muse-insert-markup (muse-markup-text 'comment-begin))
683 (delete-region (match-beginning 0) (1- (match-beginning 1)))))
685 (defun muse-publish-markup-tag ()
686 (let ((tag-info (muse-markup-tag-info (match-string 1))))
687 (when (and tag-info
688 (not (get-text-property (match-beginning 0) 'read-only)))
689 (let ((closed-tag (match-string 3))
690 (start (match-beginning 0))
691 (beg (point)) end attrs)
692 (when (nth 2 tag-info)
693 (let ((attrstr (match-string 2)))
694 (while (and attrstr
695 (string-match (concat "\\([^"
696 muse-regexp-space
697 "=]+\\)\\(=\"\\"
698 "([^\"]+\\)\"\\)?")
699 attrstr))
700 (let ((attr (cons (downcase
701 (muse-match-string-no-properties 1 attrstr))
702 (muse-match-string-no-properties 3 attrstr))))
703 (setq attrstr (replace-match "" t t attrstr))
704 (if attrs
705 (nconc attrs (list attr))
706 (setq attrs (list attr)))))))
707 (if (and (cadr tag-info) (not closed-tag))
708 (if (search-forward (concat "</" (car tag-info) ">") nil t)
709 (delete-region (match-beginning 0) (point))
710 (setq tag-info nil)))
711 (when tag-info
712 (setq end (point-marker))
713 (delete-region start beg)
714 (goto-char start)
715 (let ((args (list start end)))
716 (if (nth 2 tag-info)
717 (nconc args (list attrs)))
718 (apply (nth 3 tag-info) args))))))
719 nil)
721 (defun muse-publish-escape-specials (beg end &optional ignore-read-only)
722 "Escape specials from BEG to END using style-specific :specials.
723 If IGNORE-READ-ONLY is non-nil, ignore the read-only property."
724 (save-excursion
725 (goto-char beg)
726 (while (< (point) end)
727 (if (and (not ignore-read-only) (get-text-property (point) 'read-only))
728 (forward-char 1)
729 (let ((repl
730 (or (assoc (char-after) (muse-style-element :specials))
731 (assoc (char-after) muse-publish-markup-specials))))
732 (if (null repl)
733 (forward-char 1)
734 (delete-char 1)
735 (insert-before-markers (cdr repl))))))))
737 (defun muse-publish-markup-word ()
738 (let* ((beg (match-beginning 2))
739 (end (1- (match-end 2)))
740 (leader (buffer-substring-no-properties beg end))
741 open-tag close-tag mark-read-only loc)
742 (cond
743 ((string= leader "_")
744 (setq open-tag (muse-markup-text 'begin-underline)
745 close-tag (muse-markup-text 'end-underline)))
746 ((string= leader "=")
747 (setq open-tag (muse-markup-text 'begin-literal)
748 close-tag (muse-markup-text 'end-literal))
749 (setq mark-read-only t))
751 (let ((l (length leader)))
752 (cond
753 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
754 close-tag (muse-markup-text 'end-emph)))
755 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
756 close-tag (muse-markup-text 'end-more-emph)))
757 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
758 close-tag (muse-markup-text 'end-most-emph)))))))
759 (if (and (not (get-text-property beg 'noemphasis))
760 (setq loc (search-forward leader nil t))
761 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
762 (not (eq (char-syntax (char-before (point))) ?\ ))
763 (not (get-text-property (point) 'noemphasis)))
764 (progn
765 (replace-match "")
766 (delete-region beg end)
767 (setq end (point-marker))
768 (insert close-tag)
769 (save-excursion
770 (goto-char beg)
771 (insert open-tag)
772 (setq beg (point)))
773 (when mark-read-only
774 (muse-publish-escape-specials beg end t)
775 (muse-publish-mark-read-only (1- beg) (1+ end))))
776 (backward-char))
777 nil))
779 (defun muse-publish-markup-emdash ()
780 (delete-region (match-beginning 0) (match-end 0))
781 (insert (muse-markup-text 'emdash))
782 (if (eq (char-after) ?\<)
783 (insert ?\n)))
785 (defun muse-publish-markup-enddots ()
786 (delete-region (match-beginning 0) (match-end 0))
787 (insert (muse-markup-text 'enddots)))
789 (defun muse-publish-markup-dots ()
790 (delete-region (match-beginning 0) (match-end 0))
791 (insert (muse-markup-text 'dots)))
793 (defun muse-publish-markup-rule ()
794 (delete-region (match-beginning 0) (match-end 0))
795 (insert (muse-markup-text 'rule)))
797 (defun muse-publish-markup-heading ()
798 (let* ((len (length (match-string 1)))
799 (start (muse-markup-text
800 (cond ((= len 1) 'section)
801 ((= len 2) 'subsection)
802 ((= len 3) 'subsubsection)
803 (t 'section-other))
804 len))
805 (end (muse-markup-text
806 (cond ((= len 1) 'section-end)
807 ((= len 2) 'subsection-end)
808 ((= len 3) 'subsubsection-end)
809 (t 'section-other-end))
810 len)))
811 (delete-region (match-beginning 0) (match-end 0))
812 (insert start)
813 (end-of-line)
814 (if end (insert end))
815 (muse-publish-section-close len)))
817 (defvar muse-publish-footnotes nil)
819 (defun muse-publish-markup-footnote ()
820 "Scan ahead and snarf up the footnote body"
821 (if (= (muse-line-beginning-position) (match-beginning 0))
823 (let ((footnote (save-match-data
824 (string-to-number (match-string 1))))
825 footnotemark)
826 (delete-region (match-beginning 0) (match-end 0))
827 (save-excursion
828 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
829 (let* ((start (match-beginning 0))
830 (beg (goto-char (match-end 0)))
831 (end (save-excursion
832 (if (search-forward "\n\n" nil t)
833 (copy-marker (match-beginning 0))
834 (goto-char (point-max))
835 (skip-chars-backward "\n")
836 (point-marker)))))
837 (while (re-search-forward (concat "^["
838 muse-regexp-blank
839 "]+\\([^\n]\\)")
840 end t)
841 (replace-match "\\1" t))
842 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
843 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
844 (if (string= "" footnotemark-cmd)
845 (setq footnotemark
846 (concat (muse-markup-text 'footnote)
847 (buffer-substring-no-properties beg end)
848 (muse-markup-text 'footnote-end)))
849 (setq footnotemark (format footnotemark-cmd footnote
850 footnotemark-end-cmd))
851 (unless muse-publish-footnotes
852 (set (make-local-variable 'muse-publish-footnotes)
853 (make-vector 256 nil)))
854 (unless (aref muse-publish-footnotes footnote)
855 (setq footnotemark
856 (concat
857 footnotemark
858 (concat (format (muse-markup-text 'footnotetext)
859 footnote)
860 (buffer-substring-no-properties beg end)
861 (muse-markup-text 'footnotetext-end))))
862 (aset muse-publish-footnotes footnote footnotemark))))
863 (goto-char end)
864 (skip-chars-forward "\n")
865 (delete-region start (point)))))
866 (insert (or footnotemark footnote)))))
868 (defun muse-publish-markup-fn-sep ()
869 (delete-region (match-beginning 0) (match-end 0))
870 (insert (muse-markup-text 'fn-sep)))
872 (defun muse-publish-surround-text (beg-tag end-tag move-func)
873 (let ((beg (point)) end)
874 (skip-chars-backward muse-regexp-space)
875 (delete-region (point) beg)
876 (insert "\n\n" beg-tag)
877 (funcall move-func)
878 (setq end (point-marker))
879 (goto-char beg)
880 (while (< (point) end)
881 (if (looking-at "^\\s-+")
882 (replace-match ""))
883 (forward-line 1))
884 (goto-char end)
885 (setq beg (point))
886 (skip-chars-backward muse-regexp-space)
887 (delete-region (point) beg))
888 (insert end-tag "\n"))
890 (defsubst muse-forward-paragraph (&optional pattern)
891 (if (re-search-forward (if pattern
892 (concat "^\\(" pattern "["
893 muse-regexp-blank
894 "]+\\|$\\|\\'\\)")
895 "^\\s-*\\($\\|\\'\\)") nil t)
896 (goto-char (match-beginning 0))
897 (goto-char (point-max))))
899 (defun muse-publish-markup-list ()
900 "Markup a list entry or quoted paragraph.
901 The reason this function is so funky, is to prevent text properties
902 like read-only from being inadvertently deleted."
903 (let ((str (match-string 1)))
904 (cond
905 ((and (eq (aref str 0) ?-))
906 (delete-region (match-beginning 0) (match-end 0))
907 (muse-publish-surround-text
908 (muse-markup-text 'begin-uli)
909 (muse-markup-text 'end-uli)
910 (function
911 (lambda ()
912 (muse-forward-paragraph (concat "["
913 muse-regexp-blank
914 "]+-"))))))
915 ((and (>= (aref str 0) ?0)
916 (<= (aref str 0) ?9))
917 (delete-region (match-beginning 0) (match-end 0))
918 (muse-publish-surround-text
919 (muse-markup-text 'begin-oli)
920 (muse-markup-text 'end-oli)
921 (function
922 (lambda ()
923 (muse-forward-paragraph (concat "["
924 muse-regexp-blank
925 "]+[0-9]+\\."))))))
927 (goto-char (match-beginning 1))
928 (insert (muse-markup-text 'begin-ddt))
929 (save-match-data
930 (save-excursion
931 (forward-line 1)
932 (while (looking-at (concat "^\\(["
933 muse-regexp-blank
934 "]*\\)[^"
935 muse-regexp-space
936 "]"))
937 (delete-region (match-beginning 1) (match-end 1))
938 (forward-line 1))))
939 (save-match-data
940 (when (re-search-forward (concat "["
941 muse-regexp-space
942 "]+::["
943 muse-regexp-space
944 "]+")
945 nil t)
946 (replace-match (muse-markup-text 'start-dde))))
947 (muse-forward-paragraph)
948 (insert (muse-markup-text 'end-ddt) ?\n)))))
950 (defun muse-publish-markup-quote ()
951 "Markup a list entry or quoted paragraph.
952 The reason this function is so funky, is to prevent text properties
953 like read-only from being inadvertently deleted."
954 (let* ((ws (match-string 0))
955 (centered (>= (string-width ws) 6))
956 (begin-elem (if centered 'begin-center 'begin-quote))
957 (end-elem (if centered 'end-center 'end-quote)))
958 (muse-publish-surround-text (muse-markup-text begin-elem)
959 (muse-markup-text end-elem)
960 'muse-forward-paragraph)))
962 (defun muse-publish-markup-leading-space ()
963 (let ((markup-space (muse-markup-text 'verse-space))
964 count)
965 (when (and markup-space
966 (>= (setq count (skip-chars-forward " ")) 0))
967 (delete-region (muse-line-beginning-position) (point))
968 (while (> count 0)
969 (insert markup-space)
970 (setq count (- count 2))))))
972 (defun muse-publish-markup-verse ()
973 (let ((leader (match-string 0)))
974 (goto-char (match-beginning 0))
975 (insert (muse-markup-text 'begin-verse))
976 (while (looking-at leader)
977 (replace-match "")
978 (muse-publish-markup-leading-space)
979 (let ((beg (point)))
980 (end-of-line)
981 (cond
982 ((bolp)
983 (let ((text (muse-markup-text 'empty-verse-line)))
984 (if text (insert text))))
985 ((save-excursion
986 (save-match-data
987 (forward-line 1)
988 (or (looking-at (concat leader "["
989 muse-regexp-blank
990 "]*$"))
991 (not (looking-at leader)))))
992 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
993 (end-text (muse-markup-text 'end-last-stanza-line)))
994 (when end-text (insert end-text))
995 (goto-char beg)
996 (when begin-text (insert begin-text))
997 (end-of-line)))
999 (let ((begin-text (muse-markup-text 'begin-verse-line))
1000 (end-text (muse-markup-text 'end-verse-line)))
1001 (when end-text (insert end-text))
1002 (goto-char beg)
1003 (when begin-text (insert begin-text))
1004 (end-of-line))))
1005 (forward-line 1))))
1006 (insert (muse-markup-text 'end-verse) ?\n))
1008 (defun muse-publish-markup-table ()
1009 "Style does not support tables.")
1011 (defun muse-publish-escape-specials-in-string (string &rest ignored)
1012 "Escape specials in STRING using style-specific :specials."
1013 (save-excursion
1014 (apply (function concat)
1015 (mapcar
1016 (lambda (ch)
1017 (let ((repl
1018 (or (assoc ch (muse-style-element :specials))
1019 (assoc ch muse-publish-markup-specials))))
1020 (if (null repl)
1021 (char-to-string ch)
1022 (cdr repl))))
1023 (append string nil)))))
1025 (defun muse-publish-markup-email ()
1026 (let* ((beg (match-end 1))
1027 (addr (buffer-substring-no-properties beg (match-end 0))))
1028 (setq addr (muse-publish-escape-specials-in-string addr))
1029 (goto-char beg)
1030 (delete-region beg (match-end 0))
1031 (if (or (eq (char-before (match-beginning 0)) ?\")
1032 (eq (char-after (match-end 0)) ?\"))
1033 (insert addr)
1034 (insert (format (muse-markup-text 'email-addr) addr addr)))
1035 (muse-publish-mark-read-only beg (point))))
1037 (defun muse-publish-url (url &optional desc explicit)
1038 "Resolve a URL into its final <a href> form."
1039 (let ((orig-url url))
1040 (dolist (transform muse-publish-url-transforms)
1041 (setq url (save-match-data (when url (funcall transform url explicit)))))
1042 (dolist (transform muse-publish-desc-transforms)
1043 (setq desc (save-match-data
1044 (when desc (funcall transform desc explicit)))))
1045 (if url
1046 (cond ((string-match muse-image-regexp url)
1047 (if desc
1048 (muse-markup-text 'image-with-desc url desc)
1049 (muse-markup-text 'image-link url)))
1050 ((and desc (string-match muse-image-regexp desc))
1051 (muse-markup-text 'url-with-image url desc))
1052 ((eq (aref url 0) ?\#)
1053 (muse-markup-text 'internal-link (substring url 1)
1054 (or desc orig-url)))
1056 (muse-markup-text 'url-link url (or desc orig-url))))
1057 desc)))
1059 (defun muse-publish-insert-url (url &optional desc explicit)
1060 "Resolve a URL into its final <a href> form."
1061 (delete-region (match-beginning 0) (match-end 0))
1062 (let ((beg (point))
1063 (text (muse-publish-url url desc explicit)))
1064 (when text
1065 (insert text)
1066 (muse-publish-mark-read-only beg (point)))))
1068 (defun muse-publish-markup-link ()
1069 (let (desc explicit link)
1070 (setq explicit (save-match-data
1071 (if (string-match muse-explicit-link-regexp
1072 (match-string 0))
1073 t nil)))
1074 (setq desc (if explicit (match-string 2) (match-string 0)))
1075 (setq link (if explicit
1076 (muse-handle-explicit-link (match-string 1))
1077 (muse-handle-implicit-link (match-string 0))))
1078 (when (and link
1079 (or explicit
1080 (not (or (eq (char-before (match-beginning 0)) ?\")
1081 (eq (char-after (match-end 0)) ?\")))))
1082 (muse-publish-insert-url link desc explicit))))
1084 (defun muse-publish-markup-url ()
1085 (if (not (or (eq (char-before (match-beginning 0)) ?\")
1086 (eq (char-after (match-end 0)) ?\")))
1087 (muse-publish-insert-url (match-string 0))
1088 (let ((beg (match-beginning 0))
1089 (url (match-string 0)))
1090 (delete-region (match-beginning 0) (match-end 0))
1091 (insert (muse-publish-escape-specials-in-string url))
1092 (muse-publish-mark-read-only beg (point)))))
1094 ;; Default publishing tags
1096 (defun muse-publish-contents-tag (beg end attrs)
1097 (set (make-local-variable 'muse-publish-generate-contents)
1098 (cons (copy-marker (point) t)
1099 (let ((depth (cdr (assoc "depth" attrs))))
1100 (or (and depth (string-to-number depth)) 2)))))
1102 (defun muse-publish-verse-tag (beg end)
1103 (save-excursion
1104 (goto-char beg)
1105 (while (eq ?\ (char-syntax (char-after)))
1106 (delete-char 1))
1107 (while (< (point) end)
1108 (insert "> ")
1109 (forward-line))
1110 (if (eq ?\ (char-syntax (char-before)))
1111 (delete-char -1))))
1113 (defun muse-publish-mark-read-only (beg end)
1114 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1115 nil)
1117 (defun muse-publish-mark-noemphasis (&optional beg end)
1118 (unless beg (setq beg (match-beginning 0)))
1119 (unless end (setq end (match-end 0)))
1120 (add-text-properties beg end '(noemphasis t))
1121 nil)
1123 (defun muse-publish-code-tag (beg end)
1124 (muse-publish-escape-specials beg end)
1125 (goto-char beg)
1126 (insert (muse-markup-text 'begin-literal))
1127 (goto-char end)
1128 (insert (muse-markup-text 'end-literal))
1129 (muse-publish-mark-read-only beg (point)))
1131 (defun muse-publish-example-tag (beg end)
1132 (muse-publish-escape-specials beg end)
1133 (goto-char beg)
1134 (insert (muse-markup-text 'begin-example))
1135 (goto-char end)
1136 (insert (muse-markup-text 'end-example))
1137 (muse-publish-mark-read-only beg (point)))
1139 (defun muse-publish-verbatim-tag (beg end)
1140 (muse-publish-escape-specials beg end)
1141 (muse-publish-mark-read-only beg end))
1143 (defalias 'muse-publish-class-tag 'ignore)
1145 (defun muse-publish-lisp-tag (beg end)
1146 (save-excursion
1147 (let ((str (muse-eval-lisp
1148 (prog1
1149 (buffer-substring-no-properties beg end)
1150 (delete-region beg end)))))
1151 (set-text-properties 0 (length str) nil str)
1152 (insert str))))
1154 (defun muse-publish-command-tag (beg end attrs)
1155 (while (looking-at "\\s-*$")
1156 (forward-line))
1157 (let ((interp (cdr (assoc "interp" attrs))))
1158 (if (null interp)
1159 (shell-command
1160 (prog1
1161 (buffer-substring-no-properties (point) end)
1162 (delete-region beg end)) t)
1163 (shell-command-on-region beg end interp t t))
1164 (muse-publish-mark-read-only beg (point))))
1166 (defun muse-publish-comment-tag (beg end)
1167 (if (null muse-publish-comments-p)
1168 (delete-region beg end)
1169 (goto-char end)
1170 (muse-insert-markup (muse-markup-text 'comment-end))
1171 (goto-char beg)
1172 (muse-insert-markup (muse-markup-text 'comment-begin))))
1174 ;; Miscellaneous helper functions
1176 (defsubst muse-publishing-directive (name)
1177 (cdr (assoc name muse-publishing-directives)))
1179 (defun muse-publish-strip-tags (string)
1180 "Remove all tags from the string."
1181 (while (string-match "<.*?>" string)
1182 (setq string (replace-match "" nil t string)))
1183 string)
1185 (defun muse-publish-markup-type (category default-func)
1186 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1187 (funcall (or rule default-func))))
1189 (defun muse-published-buffer-contents (buffer)
1190 (with-current-buffer buffer
1191 (goto-char (point-min))
1192 (let ((beg (and (search-forward "Emacs Muse begins here")
1193 (muse-line-end-position)))
1194 (end (and (search-forward "Emacs Muse ends here")
1195 (muse-line-beginning-position))))
1196 (buffer-substring-no-properties beg end))))
1198 (defun muse-published-contents (file)
1199 (when (file-readable-p file)
1200 (muse-with-temp-buffer
1201 (insert-file-contents file)
1202 (muse-published-buffer-contents (current-buffer)))))
1204 (defun muse-publish-transform-output
1205 (file temp-file output-path name gen-func &rest cleanup-exts)
1206 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1207 (setq file (muse-page-name file))
1208 (message "Generating %s output for %s..." name file)
1209 (if (not (funcall gen-func temp-file output-path))
1210 (message "Generating %s from %s...failed" name file)
1211 (message "Generating %s output for %s...done" name file)
1212 (muse-delete-file-if-exists temp-file)
1213 (dolist (ext cleanup-exts)
1214 (muse-delete-file-if-exists
1215 (expand-file-name (concat file ext)
1216 (file-name-directory output-path))))
1217 (message "Wrote %s" output-path)))
1219 (defun muse-publish-read-only (string)
1220 (let ((end (1- (length string))))
1221 (add-text-properties 0 end
1222 '(rear-nonsticky (read-only) read-only t)
1223 string)
1224 string))
1226 (defun muse-publish-prepare-url (target &rest ignored)
1227 "Transform anchors and get published name, if TARGET is a page."
1228 (save-match-data
1229 (unless (or (string-match muse-url-regexp target)
1230 (string-match muse-image-regexp target)
1231 (string-match muse-file-regexp target))
1232 (setq target (if (string-match "#" target)
1233 (if (eq (aref target 0) ?\#)
1234 target
1235 (concat (muse-publish-link-name
1236 (substring target 0 (match-beginning 0)))
1237 "#" (substring target (match-end 0))))
1238 (muse-publish-link-name target)))))
1239 target)
1241 (provide 'muse-publish)
1243 ;;; muse-publish.el ends here