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