Fix publishing error with tables and initial whitespace.
[muse-el.git] / lisp / muse-publish.el
blob514ec88f4de81184d6585ccd70e6c3541912ded2
1 ;;; muse-publish.el --- base publishing implementation
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
7 ;; Emacs Muse is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published
9 ;; by the Free Software Foundation; either version 2, or (at your
10 ;; option) any later version.
12 ;; Emacs Muse is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Emacs Muse; 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 ;; Jim Ottaway (j DOT ottaway AT lse DOT ac DOT uk) provided a
33 ;; reference implementation for nested lists.
35 ;;; Code:
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 ;; Muse Publishing
41 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 (require 'muse)
44 (require 'muse-regexps)
46 (defgroup muse-publish nil
47 "Options controlling the general behavior of Muse publishing."
48 :group 'muse)
50 (defcustom muse-before-publish-hook nil
51 "A hook run in the buffer to be published, before it is done."
52 :type 'hook
53 :group 'muse-publish)
55 (defcustom muse-after-publish-hook nil
56 "A hook run in the buffer to be published, after it is done."
57 :type 'hook
58 :group 'muse-publish)
60 (defcustom muse-publish-url-transforms
61 '(muse-resolve-url)
62 "A list of functions used to prepare URLs for publication.
63 Each is passed the URL. The transformed URL should be returned."
64 :type 'hook
65 :options '(muse-resolve-url)
66 :group 'muse-publish)
68 (defcustom muse-publish-desc-transforms nil
69 "A list of functions used to prepare URL desciptions for publication.
70 Each is passed the description. The modified description should
71 be returned."
72 :type 'hook
73 :group 'muse-publish)
75 (defcustom muse-publish-comments-p nil
76 "If nil, remove comments before publishing.
77 If non-nil, publish comments using the markup of the current style."
78 :type 'boolean
79 :group 'muse-publish)
81 (defcustom muse-publish-report-threshhold 100000
82 "If a file is this size or larger, report publishing progress."
83 :type 'integer
84 :group 'muse-publish)
86 (defcustom muse-publish-markup-regexps
87 `(;; Remove leading and trailing whitespace from the file
88 (1000 "\\(\\`\n+\\|\n+\\'\\)" 0 "")
90 ;; Remove trailing whitespace from all lines
91 (1100 ,(concat "[" muse-regexp-blank "]+$") 0 "")
93 ;; Handle any leading #directives
94 (1200 "\\`#\\([a-zA-Z-]+\\)\\s-+\\(.+\\)\n+" 0 directive)
96 ;; markup tags
97 (1300 muse-tag-regexp 0 tag)
99 ;; commented lines
100 (1350 "^;\\s-+\\(.+\\)" 0 comment)
102 ;; prevent emphasis characters in explicit links from being marked
103 (1400 muse-explicit-link-regexp 0 muse-publish-mark-link)
105 ;; define anchor points
106 (1500 "^\\(\\W*\\)#\\(\\S-+\\)\\s-*" 0 anchor)
108 ;; emphasized or literal text
109 (1600 ,(concat "\\(^\\|[-[" muse-regexp-blank
110 "<('`\"\n]\\)\\(=[^=" muse-regexp-blank
111 "\n]\\|_[^_" muse-regexp-blank
112 "\n]\\|\\*+[^*" muse-regexp-blank
113 "\n]\\)")
114 2 word)
116 ;; headings, outline-mode style
117 (1700 "^\\(\\*+\\)\\s-+" 0 heading)
119 ;; ellipses
120 (1800 "\\.\\.\\.\\." 0 enddots)
121 (1850 "\\.\\.\\." 0 dots)
123 ;; horizontal rule, or section separator
124 (1900 "^----+" 0 rule)
126 ;; non-breaking space
127 (1950 "~~" 0 no-break-space)
129 ;; beginning of footnotes section
130 (2000 "^Footnotes:?\\s-*" 0 fn-sep)
131 ;; footnote definition/reference (def if at beginning of line)
132 (2100 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote)
134 ;; unnumbered List items begin with a -. numbered list items
135 ;; begin with number and a period. definition lists have a
136 ;; leading term separated from the body with ::. centered
137 ;; paragraphs begin with at least six columns of whitespace; any
138 ;; other whitespace at the beginning indicates a blockquote. The
139 ;; reason all of these rules are handled here, is so that
140 ;; blockquote detection doesn't interfere with indented list
141 ;; members.
142 (2200 ,(format muse-list-item-regexp (concat "[" muse-regexp-blank "]*"))
143 0 list)
145 ;; simple table markup is supported, nothing fancy. use | to
146 ;; separate cells, || to separate header cells, and ||| for footer
147 ;; cells
148 (2300 ,(concat "\\(\\([" muse-regexp-blank "]*\n\\)?"
149 "\\(" muse-table-line-regexp "\\(?:\n\\|\\'\\)\\)\\)+")
150 0 table)
152 ;; blockquote and centered text
153 (2400 ,(concat "^\\([" muse-regexp-blank "]+\\).+") 0 quote)
155 ;; the emdash ("--")
156 (2500 ,(concat "\\(^\\|[" muse-regexp-blank "]+\\)--\\($\\|["
157 muse-regexp-blank "]+\\)")
158 0 emdash)
160 ;; "verse" text is indicated the same way as a quoted e-mail
161 ;; response: "> text", where text may contain initial whitespace
162 ;; (see below).
163 (2600 ,(concat "^[" muse-regexp-blank "]*> ") 0 verse)
165 ;; replace links in the buffer (links to other pages)
166 (2900 muse-explicit-link-regexp 0 link)
168 ;; bare URLs
169 (3000 muse-url-regexp 0 url)
171 ;; bare email addresses
172 (3500
173 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email)
175 "List of markup rules for publishing a page with Muse.
176 The rules given in this variable are invoked first, followed by
177 whatever rules are specified by the current style.
179 Each member of the list is either a function, or a list of the form:
181 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
183 REGEXP is a regular expression, or symbol whose value is a regular
184 expression, which is searched for using `re-search-forward'.
185 TEXT-BEGIN-GROUP is the matching group within that regexp which
186 denotes the beginning of the actual text to be marked up.
187 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
188 If it is not a string, but a function, it will be called to determine
189 what the replacement text should be (it must return a string). If it
190 is a symbol, the value of that symbol should be a string.
192 The replacements are done in order, one rule at a time. Writing
193 the regular expressions can be a tricky business. Note that case
194 is never ignored. `case-fold-search' is always bound to nil
195 while processing the markup rules."
196 :type '(repeat (choice
197 (list :tag "Markup rule"
198 integer
199 (choice regexp symbol)
200 integer
201 (choice string function symbol))
202 function))
203 :group 'muse-publish)
205 (defcustom muse-publish-markup-functions
206 '((directive . muse-publish-markup-directive)
207 (comment . muse-publish-markup-comment)
208 (anchor . muse-publish-markup-anchor)
209 (tag . muse-publish-markup-tag)
210 (word . muse-publish-markup-word)
211 (emdash . muse-publish-markup-emdash)
212 (enddots . muse-publish-markup-enddots)
213 (dots . muse-publish-markup-dots)
214 (rule . muse-publish-markup-rule)
215 (no-break-space . muse-publish-markup-no-break-space)
216 (heading . muse-publish-markup-heading)
217 (footnote . muse-publish-markup-footnote)
218 (fn-sep . muse-publish-markup-fn-sep)
219 (list . muse-publish-markup-list)
220 (quote . muse-publish-markup-quote)
221 (verse . muse-publish-markup-verse)
222 (table . muse-publish-markup-table)
223 (email . muse-publish-markup-email)
224 (link . muse-publish-markup-link)
225 (url . muse-publish-markup-url))
226 "An alist of style types to custom functions for that kind of text.
228 Each member of the list is of the form:
230 (SYMBOL FUNCTION)
232 SYMBOL describes the type of text to associate with this rule.
233 `muse-publish-markup-regexps' maps regexps to these symbols.
235 FUNCTION is the function to use to mark up this kind of rule if
236 no suitable function is found through the :functions tag of the
237 current style."
238 :type '(alist :key-type symbol :value-type function)
239 :group 'muse-publish)
241 (defcustom muse-publish-markup-tags
242 '(("contents" nil t muse-publish-contents-tag)
243 ("verse" t nil muse-publish-verse-tag)
244 ("example" t nil muse-publish-example-tag)
245 ("code" t nil muse-publish-code-tag)
246 ("quote" t nil muse-publish-quote-tag)
247 ("literal" t nil muse-publish-mark-read-only)
248 ("verbatim" t nil muse-publish-verbatim-tag)
249 ("lisp" t nil muse-publish-lisp-tag)
250 ("class" t t muse-publish-class-tag)
251 ("command" t t muse-publish-command-tag)
252 ("comment" t nil muse-publish-comment-tag))
253 "A list of tag specifications, for specially marking up text.
254 XML-style tags are the best way to add custom markup to Muse.
255 This is easily accomplished by customizing this list of markup tags.
257 For each entry, the name of the tag is given, whether it expects
258 a closing tag and/or an optional set of attributes, and a
259 function that performs whatever action is desired within the
260 delimited region.
262 The tags themselves are deleted during publishing, before the
263 function is called. The function is called with three arguments,
264 the beginning and end of the region surrounded by the tags. If
265 properties are allowed, they are passed as a third argument in
266 the form of an alist. The `end' argument to the function is
267 always a marker.
269 Point is always at the beginning of the region within the tags, when
270 the function is called. Wherever point is when the function finishes
271 is where tag markup will resume.
273 These tag rules are processed once at the beginning of markup, and
274 once at the end, to catch any tags which may have been inserted
275 in-between."
276 :type '(repeat (list (string :tag "Markup tag")
277 (boolean :tag "Expect closing tag" :value t)
278 (boolean :tag "Parse attributes" :value nil)
279 function))
280 :group 'muse-publish)
282 (defcustom muse-publish-markup-specials nil
283 "A table of characters which must be represented specially."
284 :type '(alist :key-type character :value-type string)
285 :group 'muse-publish)
287 (defvar muse-publishing-p nil
288 "Set to t while a page is being published.")
289 (defvar muse-batch-publishing-p nil
290 "Set to t while a page is being batch published.")
291 (defvar muse-publishing-styles nil
292 "The publishing styles that Muse recognizes.
293 This is automatically generated when loading publishing styles.")
294 (defvar muse-publishing-current-file nil
295 "The file that is currently being published.")
296 (defvar muse-publishing-current-output-path nil
297 "The path where the current file will be published to.")
298 (defvar muse-publishing-current-style nil
299 "The style of the file that is currently being published.")
300 (defvar muse-publishing-directives nil
301 "An alist of publishing directives from the top of a file.")
302 (defvar muse-publish-generate-contents nil
303 "Non-nil if a table of contents should be generated.
304 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
305 tell where the <contents> was seen, and to what depth the
306 contents were requested.")
307 (defvar muse-publishing-last-position nil
308 "Last position of the point when publishing.
309 This is used to make sure that publishing doesn't get stalled.")
311 ;; Functions for handling style information
313 (defsubst muse-style (&optional style)
314 "Resolve the given STYLE into a Muse style, if it is a string."
315 (if (null style)
316 muse-publishing-current-style
317 (if (stringp style)
318 (assoc style muse-publishing-styles)
319 (muse-assert (consp style))
320 style)))
322 (defun muse-define-style (name &rest elements)
323 (let ((entry (assoc name muse-publishing-styles)))
324 (if entry
325 (setcdr entry elements)
326 (setq muse-publishing-styles
327 (cons (append (list name) elements)
328 muse-publishing-styles)))))
330 (defun muse-derive-style (new-name base-name &rest elements)
331 (apply 'muse-define-style new-name
332 (append elements (list :base base-name))))
334 (defsubst muse-get-keyword (keyword list &optional direct)
335 (let ((value (cadr (memq keyword list))))
336 (if (and (not direct) (symbolp value))
337 (symbol-value value)
338 value)))
340 (defun muse-style-elements-list (elem &optional style)
341 "Return a list all references to ELEM in STYLE, including base styles.
342 If STYLE is not specified, use current style."
343 (let (base elements)
344 (while style
345 (setq style (muse-style style))
346 (setq elements (append elements
347 (muse-get-keyword elem style)))
348 (setq style (muse-get-keyword :base style)))
349 elements))
351 (defsubst muse-style-element (elem &optional style direct)
352 "Search for ELEM in STYLE, including base styles.
353 If STYLE is not specified, use current style."
354 (setq style (muse-style style))
355 (let ((value (muse-get-keyword elem style direct)))
356 (if value
357 value
358 (let ((base (muse-get-keyword :base style)))
359 (if base
360 (muse-style-element elem base direct))))))
362 (defun muse-find-markup-element (keyword ident style)
363 (let ((def (assq ident (muse-style-element keyword style))))
364 (if def
365 (cdr def)
366 (let ((base (muse-style-element :base style)))
367 (if base
368 (muse-find-markup-element keyword ident base))))))
370 (defun muse-markup-text (ident &rest args)
371 "Insert ARGS into the text markup associated with IDENT.
372 If the markup text has sections like %N%, this will be replaced
373 with the N-1th argument in ARGS. After that, `format' is applied
374 to the text with ARGS as parameters."
375 (let ((text (muse-find-markup-element :strings ident (muse-style))))
376 (if (and text args)
377 (progn
378 (let (start repl-text)
379 (while (setq start (string-match "%\\([1-9][0-9]*\\)%" text start))
380 ;; escape '%' in the argument text, since we will be
381 ;; using format on it
382 (setq repl-text (muse-replace-regexp-in-string
383 "%" "%%"
384 (nth (1- (string-to-number
385 (match-string 1 text))) args)
386 t t)
387 start (+ start (length repl-text))
388 text (replace-match repl-text t t text))))
389 (apply 'format text args))
390 (or text ""))))
392 (defun muse-insert-markup (&rest args)
393 (let ((beg (point)))
394 (apply 'insert args)
395 (muse-publish-mark-read-only beg (point))))
397 (defun muse-find-markup-tag (keyword tagname style)
398 (let ((def (assoc tagname (muse-style-element keyword style))))
399 (or def
400 (let ((base (muse-style-element :base style)))
401 (if base
402 (muse-find-markup-tag keyword tagname base))))))
404 (defsubst muse-markup-tag-info (tagname &rest args)
405 (let ((tag-info (muse-find-markup-tag :tags tagname (muse-style))))
406 (or tag-info
407 (assoc (match-string 1) muse-publish-markup-tags))))
409 (defsubst muse-markup-function (category)
410 (let ((func (muse-find-markup-element :functions category (muse-style))))
411 (or func
412 (cdr (assq category muse-publish-markup-functions)))))
414 ;; Publishing routines
416 (defun muse-publish-markup (name rules)
417 (let* ((case-fold-search nil)
418 (inhibit-read-only t)
419 (limit (* (length rules) (point-max)))
420 (verbose (and muse-publish-report-threshhold
421 (> (point-max) muse-publish-report-threshhold)))
422 (base 0))
423 (while rules
424 (goto-char (point-min))
425 (let ((regexp (nth 1 (car rules)))
426 (group (nth 2 (car rules)))
427 (repl (nth 3 (car rules)))
428 pos)
429 (setq muse-publishing-last-position nil)
430 (if (symbolp regexp)
431 (setq regexp (symbol-value regexp)))
432 (if (and verbose (not muse-batch-publishing-p))
433 (message "Publishing %s...%d%%" name
434 (* (/ (float (+ (point) base)) limit) 100)))
435 (while (and regexp (setq pos (re-search-forward regexp nil t)))
436 (if (and verbose (not muse-batch-publishing-p))
437 (message "Publishing %s...%d%%" name
438 (* (/ (float (+ (point) base)) limit) 100)))
439 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
440 (get-text-property (match-beginning group) 'read-only))
441 (let* (func
442 (text (cond
443 ((and (symbolp repl)
444 (setq func (muse-markup-function repl)))
445 (funcall func))
446 ((functionp repl)
447 (funcall repl))
448 ((symbolp repl)
449 (symbol-value repl))
450 (t repl))))
451 (if (stringp text)
452 (replace-match text t))))
453 (if (and muse-publishing-last-position
454 (= pos muse-publishing-last-position))
455 (if (eobp)
456 (setq regexp nil)
457 (forward-char 1)))
458 (setq muse-publishing-last-position pos)))
459 (setq rules (cdr rules)
460 base (+ base (point-max))))
461 (if (and verbose (not muse-batch-publishing-p))
462 (message "Publishing %s...done" name))))
464 (defun muse-insert-file-or-string (file-or-string &optional title)
465 (let ((beg (point)) end)
466 (if (and (not (string-equal file-or-string ""))
467 (file-readable-p file-or-string))
468 (setq end (+ beg (cadr (insert-file-contents file-or-string))))
469 (insert file-or-string)
470 (setq end (point)))
471 (save-restriction
472 (narrow-to-region beg end)
473 (remove-text-properties (point-min) (point-max)
474 '(read-only nil rear-nonsticky nil))
475 (goto-char (point-min))
476 (muse-publish-markup (or title "")
477 '((100 "<\\(lisp\\)>" 0
478 muse-publish-markup-tag))))))
480 (defun muse-style-run-hooks (keyword style &rest args)
481 (catch 'handled
482 (while (and style
483 (setq style (muse-style style)))
484 (let ((func (muse-get-keyword keyword style t)))
485 (when (and func
486 (apply func args))
487 (throw 'handled t)))
488 (setq style (muse-style-element :base style)))))
490 (defun muse-publish-markup-region (beg end title style)
491 "Apply the given STYLE's markup rules to the given region."
492 (save-restriction
493 (narrow-to-region beg end)
494 (muse-style-run-hooks :before style)
495 (muse-publish-markup
496 title
497 (sort (copy-alist (append muse-publish-markup-regexps
498 (muse-style-elements-list :regexps style)))
499 (function
500 (lambda (l r)
501 (< (car l) (car r))))))
502 (muse-style-run-hooks :before-end style)
503 (muse-publish-escape-specials (point-min) (point-max) nil 'document)))
505 (defun muse-publish-markup-buffer (title style)
506 "Apply the given STYLE's markup rules to the current buffer."
507 (setq style (muse-style style))
508 (let ((style-header (muse-style-element :header style))
509 (style-footer (muse-style-element :footer style))
510 (muse-publishing-current-style style)
511 (muse-publishing-directives
512 (list (cons "title" title)
513 (cons "author" (user-full-name))
514 (cons "date" (format-time-string
515 "%B %e, %Y"
516 (nth 5 (file-attributes
517 muse-publishing-current-file))))))
518 (muse-publishing-p t)
519 (inhibit-read-only t))
520 (run-hooks 'muse-update-values-hook)
521 (run-hooks 'muse-before-publish-hook)
522 (muse-publish-markup-region (point-min) (point-max) title style)
523 (goto-char (point-min))
524 (when style-header
525 (muse-insert-file-or-string style-header title))
526 (goto-char (point-max))
527 (when style-footer
528 (muse-insert-file-or-string style-footer title))
529 (muse-style-run-hooks :after style)
530 (run-hooks 'muse-after-publish-hook)))
532 (defun muse-publish-markup-string (string &optional style)
533 "Markup STRING using the given STYLE's markup rules."
534 (setq style (muse-style style))
535 (muse-with-temp-buffer
536 (insert string)
537 (let ((muse-publishing-current-style style)
538 (muse-publishing-p t))
539 (muse-publish-markup "*string*" (muse-style-element :rules style)))
540 (buffer-string)))
542 ;; Commands for publishing files
544 (defsubst muse-publish-get-style (&optional styles)
545 (unless styles (setq styles muse-publishing-styles))
546 (if (= 1 (length styles))
547 (car styles)
548 (assoc (completing-read "Publish with style: " styles nil t)
549 styles)))
551 (defsubst muse-publish-get-output-dir (style)
552 (let ((default-directory (or (muse-style-element :path style)
553 default-directory)))
554 (muse-read-directory-name "Publish to directory: " nil default-directory)))
556 (defsubst muse-publish-get-info ()
557 (let ((style (muse-publish-get-style)))
558 (list style (muse-publish-get-output-dir style)
559 current-prefix-arg)))
561 (defsubst muse-publish-output-name (&optional file style)
562 (setq style (muse-style style))
563 (concat (muse-style-element :prefix style)
564 (muse-page-name file)
565 (muse-style-element :suffix style)))
567 (defsubst muse-publish-output-file (file &optional output-dir style)
568 (setq style (muse-style style))
569 (if output-dir
570 (expand-file-name (muse-publish-output-name file style) output-dir)
571 (concat (file-name-directory file)
572 (muse-publish-output-name file style))))
574 (defsubst muse-publish-link-name (&optional file style)
575 (setq style (muse-style style))
576 (concat (muse-style-element :prefix style)
577 (muse-page-name file)
578 (or (muse-style-element :link-suffix style)
579 (muse-style-element :suffix style))))
581 (defsubst muse-publish-link-file (file &optional output-dir style)
582 (setq style (muse-style style))
583 (if output-dir
584 (expand-file-name (muse-publish-link-name file style) output-dir)
585 (concat (file-name-directory file)
586 (muse-publish-link-name file style))))
588 ;;;###autoload
589 (defun muse-publish-file (file style &optional output-dir force)
590 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
591 If the argument FORCE is nil, each file is only published if it is
592 newer than the published version. If the argument FORCE is non-nil,
593 the file is published no matter what."
594 (interactive (cons (read-file-name "Publish file: ")
595 (muse-publish-get-info)))
596 (let ((style-name style))
597 (setq style (muse-style style))
598 (unless style
599 (error "There is no style '%s' defined." style-name)))
600 (let* ((output-path (muse-publish-output-file file output-dir style))
601 (output-suffix (muse-style-element :osuffix style))
602 (muse-publishing-current-file file)
603 (muse-publishing-current-output-path output-path)
604 (target (if output-suffix
605 (concat (file-name-sans-extension output-path)
606 output-suffix)
607 output-path))
608 (threshhold (nth 7 (file-attributes file))))
609 (if (not threshhold)
610 (message "Please save %s before publishing" file)
611 (when (or force (file-newer-than-file-p file target))
612 (if (and muse-publish-report-threshhold
613 (> threshhold
614 muse-publish-report-threshhold))
615 (message "Publishing %s ..." file))
616 (muse-with-temp-buffer
617 (insert-file-contents file)
618 (muse-publish-markup-buffer (muse-page-name file) style)
619 (let ((backup-inhibited t))
620 (write-file output-path))
621 (muse-style-run-hooks :final style file output-path target))
622 t))))
624 ;;;###autoload
625 (defun muse-publish-this-file (style output-dir &optional force)
626 "Publish the page in the current file."
627 (interactive (muse-publish-get-info))
628 (unless (muse-publish-file buffer-file-name style output-dir force)
629 (message (concat "The published version is up-to-date; use"
630 " C-u C-c C-t to force an update."))))
632 (defun muse-batch-publish-files ()
633 "Publish Muse files in batch mode."
634 (let ((muse-batch-publishing-p t)
635 style output-dir)
636 (setq style (car command-line-args-left)
637 command-line-args-left (cdr command-line-args-left)
638 output-dir (car command-line-args-left)
639 output-dir
640 (if (string-match "\\`--output-dir=" output-dir)
641 (prog1
642 (substring output-dir (match-end 0))
643 (setq command-line-args-left (cdr command-line-args-left)))))
644 (setq auto-mode-alist
645 (delete (cons (concat "\\." muse-file-extension "\\'")
646 'muse-mode-choose-mode)
647 auto-mode-alist))
648 (dolist (file command-line-args-left)
649 (muse-publish-file file style output-dir t))))
651 ;; Default publishing rules
653 (defun muse-publish-section-close (depth)
654 "Seach forward for the closing tag of given DEPTH."
655 (let (not-end)
656 (save-excursion
657 (while (and (setq not-end (re-search-forward
658 (concat "^\\*\\{1," (number-to-string depth)
659 "\\}\\s-+")
660 nil t))
661 (get-text-property (match-beginning 0) 'read-only)))
662 (if not-end
663 (forward-line 0)
664 (goto-char (point-max)))
665 (cond ((not (eq (char-before) ?\n))
666 (insert "\n\n"))
667 ((not (eq (char-before (1- (point))) ?\n))
668 (insert "\n")))
669 (muse-insert-markup (muse-markup-text 'section-close depth))
670 (insert "\n"))))
672 (defun muse-publish-markup-directive (&optional name value)
673 (unless name (setq name (match-string 1)))
674 (unless value (setq value (match-string 2)))
675 (let ((elem (assoc name muse-publishing-directives)))
676 (if elem
677 (setcdr elem value)
678 (setq muse-publishing-directives
679 (cons (cons name value)
680 muse-publishing-directives))))
681 ;; Make sure we don't ever try to move the point forward (past the
682 ;; beginning of buffer) while we're still searching for directives.
683 (setq muse-publishing-last-position nil)
684 (delete-region (match-beginning 0) (match-end 0)))
686 (defsubst muse-publishing-directive (name)
687 (cdr (assoc name muse-publishing-directives)))
689 (defun muse-publish-markup-anchor ()
690 (unless (get-text-property (match-end 1) 'muse-link)
691 (let ((text (muse-markup-text 'anchor (match-string 2))))
692 (unless (string= text "")
693 (save-match-data
694 (skip-chars-forward (concat muse-regexp-blank "\n"))
695 (muse-insert-markup text)))
696 (match-string 1))))
698 (defun muse-publish-markup-comment ()
699 (if (null muse-publish-comments-p)
701 (goto-char (match-end 0))
702 (muse-insert-markup (muse-markup-text 'comment-end))
703 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
704 (delete-region (match-beginning 0) (match-beginning 1))
705 (goto-char (match-beginning 0))
706 (muse-insert-markup (muse-markup-text 'comment-begin))))
708 (defun muse-publish-markup-tag ()
709 (let ((tag-info (muse-markup-tag-info (match-string 1))))
710 (when (and tag-info
711 (not (get-text-property (match-beginning 0) 'read-only)))
712 (let ((closed-tag (match-string 3))
713 (start (match-beginning 0))
714 (beg (point))
715 end attrs)
716 (when (nth 2 tag-info)
717 (let ((attrstr (match-string 2)))
718 (while (and attrstr
719 (string-match (concat "\\([^"
720 muse-regexp-blank
721 "=\n]+\\)\\(=\"\\"
722 "([^\"]+\\)\"\\)?")
723 attrstr))
724 (let ((attr (cons (downcase
725 (muse-match-string-no-properties 1 attrstr))
726 (muse-match-string-no-properties 3 attrstr))))
727 (setq attrstr (replace-match "" t t attrstr))
728 (if attrs
729 (nconc attrs (list attr))
730 (setq attrs (list attr)))))))
731 (if (and (cadr tag-info) (not closed-tag))
732 (if (search-forward (concat "</" (car tag-info) ">") nil t)
733 (delete-region (match-beginning 0) (point))
734 (setq tag-info nil)))
735 (when tag-info
736 (setq end (point-marker))
737 (delete-region start beg)
738 (goto-char start)
739 (let ((args (list start end)))
740 (if (nth 2 tag-info)
741 (nconc args (list attrs)))
742 (apply (nth 3 tag-info) args))))))
743 nil)
745 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
746 "Escape specials from BEG to END using style-specific :specials.
747 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
748 CONTEXT is used to figure out what kind of specials to escape.
750 The following contexts exist in Muse.
751 'underline _underlined text_
752 'literal =monospaced text= or <code> region (monospaced, escaped)
753 'emphasis *emphasized text*
754 'email email@example.com
755 'url http://example.com
756 'url-desc [[...][description of an extended link]]
757 'example <example> region (monospaced, block context, escaped)
758 'verbatim <verbatim> region (escaped)
759 'document normal text"
760 (let ((specials (muse-style-element :specials nil t)))
761 (cond ((functionp specials)
762 (setq specials (funcall specials context)))
763 ((symbolp specials)
764 (setq specials (symbol-value specials))))
765 (if (functionp specials)
766 (funcall specials beg end ignore-read-only)
767 (save-excursion
768 (save-restriction
769 (narrow-to-region beg end)
770 (goto-char (point-min))
771 (while (< (point) (point-max))
772 (if (and (not ignore-read-only)
773 (get-text-property (point) 'read-only))
774 (goto-char (or (next-single-property-change (point) 'read-only)
775 (point-max)))
776 (let ((repl (or (assoc (char-after) specials)
777 (assoc (char-after)
778 muse-publish-markup-specials))))
779 (if (null repl)
780 (forward-char 1)
781 (delete-char 1)
782 (insert-before-markers (cdr repl)))))))))))
784 (defun muse-publish-markup-word ()
785 (let* ((beg (match-beginning 2))
786 (end (1- (match-end 2)))
787 (leader (buffer-substring-no-properties beg end))
788 open-tag close-tag mark-read-only loc context)
789 (cond
790 ((string= leader "_")
791 (setq context 'underline
792 open-tag (muse-markup-text 'begin-underline)
793 close-tag (muse-markup-text 'end-underline)))
794 ((string= leader "=")
795 (setq context 'literal
796 open-tag (muse-markup-text 'begin-literal)
797 close-tag (muse-markup-text 'end-literal))
798 (setq mark-read-only t))
800 (let ((l (length leader)))
801 (setq context 'emphasis)
802 (cond
803 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
804 close-tag (muse-markup-text 'end-emph)))
805 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
806 close-tag (muse-markup-text 'end-more-emph)))
807 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
808 close-tag (muse-markup-text 'end-most-emph)))
809 (t (setq context nil))))))
810 (if (and context
811 (not (get-text-property beg 'muse-link))
812 (setq loc (search-forward leader nil t))
813 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
814 (not (eq (char-syntax (char-before (point))) ?\ ))
815 (not (get-text-property (point) 'muse-link)))
816 (progn
817 (replace-match "")
818 (delete-region beg end)
819 (setq end (point-marker))
820 (muse-insert-markup close-tag)
821 (goto-char beg)
822 (muse-insert-markup open-tag)
823 (setq beg (point))
824 (when mark-read-only
825 (muse-publish-escape-specials beg end t context)
826 (muse-publish-mark-read-only beg end)))
827 (backward-char))
828 nil))
830 (defun muse-publish-markup-emdash ()
831 (unless (get-text-property (match-beginning 0) 'muse-link)
832 (let ((prespace (match-string 1))
833 (postspace (match-string 2)))
834 (delete-region (match-beginning 0) (match-end 0))
835 (muse-insert-markup (muse-markup-text 'emdash prespace postspace))
836 (when (eq (char-after) ?\<)
837 (insert ?\n)))))
839 (defun muse-publish-markup-enddots ()
840 (unless (get-text-property (match-beginning 0) 'muse-link)
841 (delete-region (match-beginning 0) (match-end 0))
842 (muse-insert-markup (muse-markup-text 'enddots))))
844 (defun muse-publish-markup-dots ()
845 (unless (get-text-property (match-beginning 0) 'muse-link)
846 (delete-region (match-beginning 0) (match-end 0))
847 (muse-insert-markup (muse-markup-text 'dots))))
849 (defun muse-publish-markup-rule ()
850 (unless (get-text-property (match-beginning 0) 'muse-link)
851 (delete-region (match-beginning 0) (match-end 0))
852 (muse-insert-markup (muse-markup-text 'rule))))
854 (defun muse-publish-markup-no-break-space ()
855 (unless (get-text-property (match-beginning 0) 'muse-link)
856 (delete-region (match-beginning 0) (match-end 0))
857 (muse-insert-markup (muse-markup-text 'no-break-space))))
859 (defun muse-publish-markup-heading ()
860 (let* ((len (length (match-string 1)))
861 (start (muse-markup-text
862 (cond ((= len 1) 'section)
863 ((= len 2) 'subsection)
864 ((= len 3) 'subsubsection)
865 (t 'section-other))
866 len))
867 (end (muse-markup-text
868 (cond ((= len 1) 'section-end)
869 ((= len 2) 'subsection-end)
870 ((= len 3) 'subsubsection-end)
871 (t 'section-other-end))
872 len)))
873 (delete-region (match-beginning 0) (match-end 0))
874 (muse-insert-markup start)
875 (end-of-line)
876 (when end
877 (muse-insert-markup end))
878 (muse-publish-section-close len)))
880 (defvar muse-publish-footnotes nil)
882 (defun muse-publish-markup-footnote ()
883 "Scan ahead and snarf up the footnote body"
884 (cond
885 ((get-text-property (match-beginning 0) 'muse-link)
886 nil)
887 ((= (muse-line-beginning-position) (match-beginning 0))
890 (let ((footnote (save-match-data
891 (string-to-number (match-string 1))))
892 footnotemark)
893 (delete-region (match-beginning 0) (match-end 0))
894 (save-excursion
895 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
896 (let* ((start (match-beginning 0))
897 (beg (goto-char (match-end 0)))
898 (end (save-excursion
899 (if (search-forward "\n\n" nil t)
900 (copy-marker (match-beginning 0))
901 (goto-char (point-max))
902 (skip-chars-backward "\n")
903 (point-marker)))))
904 (while (re-search-forward
905 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
906 end t)
907 (replace-match "\\1" t))
908 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
909 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
910 (if (string= "" footnotemark-cmd)
911 (setq footnotemark
912 (concat (muse-markup-text 'footnote)
913 (buffer-substring-no-properties beg end)
914 (muse-markup-text 'footnote-end)))
915 (setq footnotemark (format footnotemark-cmd footnote
916 footnotemark-end-cmd))
917 (unless muse-publish-footnotes
918 (set (make-local-variable 'muse-publish-footnotes)
919 (make-vector 256 nil)))
920 (unless (aref muse-publish-footnotes footnote)
921 (setq footnotemark
922 (concat
923 footnotemark
924 (concat (format (muse-markup-text 'footnotetext)
925 footnote)
926 (buffer-substring-no-properties beg end)
927 (muse-markup-text 'footnotetext-end))))
928 (aset muse-publish-footnotes footnote footnotemark))))
929 (goto-char end)
930 (skip-chars-forward "\n")
931 (delete-region start (point)))))
932 (muse-insert-markup (or footnotemark footnote))))))
934 (defun muse-publish-markup-fn-sep ()
935 (delete-region (match-beginning 0) (match-end 0))
936 (muse-insert-markup (muse-markup-text 'fn-sep)))
938 (defun muse-insert-markup-end-list (&rest args)
939 (let ((beg (point)))
940 (apply 'insert args)
941 (add-text-properties beg (point) '(end-list t))
942 (muse-publish-mark-read-only beg (point))))
944 (defun muse-publish-surround-dl (indent post-indent)
945 (let* ((beg-item (muse-markup-text 'begin-dl-item))
946 (end-item (muse-markup-text 'end-dl-item))
947 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
948 (end-ddt (muse-markup-text 'end-ddt))
949 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
950 (end-dde (muse-markup-text 'end-dde))
951 (continue t)
952 def-on-same-line beg)
953 (while continue
954 ;; envelope this as one term+definitions unit -- HTML does not
955 ;; need this, but DocBook and Muse's custom XML format do
956 (muse-insert-markup beg-item)
957 (when (looking-at muse-dl-term-regexp)
958 ;; find the term and wrap it with published markup
959 (setq beg (point))
960 (goto-char (match-end 1))
961 (delete-region (point) (match-end 0))
962 (muse-insert-markup-end-list end-ddt)
963 ;; if definition is immediately after term, move to next line
964 (unless (eq (char-after) ?\n)
965 (insert ?\n))
966 (save-excursion
967 (goto-char beg)
968 (delete-region (point) (match-beginning 1))
969 (muse-insert-markup beg-ddt)))
970 (setq beg (point)
971 ;; move past current item
972 continue (muse-forward-list-item 'dl-term indent))
973 (save-restriction
974 (narrow-to-region beg (point))
975 (goto-char (point-min))
976 ;; publish each definition that we find, defaulting to an
977 ;; empty definition if none are found
978 (muse-publish-surround-text beg-dde end-dde
979 (lambda (indent)
980 (muse-forward-list-item 'dl-entry indent))
981 nil nil t)
982 (goto-char (point-max))
983 (skip-chars-backward (concat muse-regexp-blank "\n"))
984 (muse-insert-markup-end-list end-item)
985 (when continue
986 (goto-char (point-max)))))))
988 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent determine-indent)
989 (let ((continue t)
990 (list-item (format muse-list-item-regexp
991 (concat "[" muse-regexp-blank "]*")))
992 init-indent beg)
993 (unless indent
994 (setq indent (concat "[" muse-regexp-blank "]+")))
995 (if post-indent
996 (setq post-indent (concat " \\{0," (number-to-string post-indent)
997 "\\}"))
998 (setq post-indent ""))
999 (while continue
1000 (muse-insert-markup beg-tag)
1001 (setq beg (point)
1002 ;; move past current item; continue is non-nil if there
1003 ;; are more like items to be processed
1004 continue (funcall move-func indent))
1005 (save-restriction
1006 (when determine-indent
1007 ;; if the caller doesn't know how much indentation
1008 ;; to use, figure it out ourselves
1009 (if (not continue)
1010 (setq indent "")
1011 (save-match-data
1012 ;; snarf all leading whitespace
1013 (let ((this-indent (and (match-beginning 2)
1014 (buffer-substring (match-beginning 1)
1015 (match-beginning 2)))))
1016 (when (and this-indent
1017 (not (string= this-indent "")))
1018 (setq indent this-indent
1019 determine-indent nil))))))
1020 (when continue
1021 ;; remove list markup if we encountered another item of the
1022 ;; same type
1023 (replace-match "" t t nil 1))
1024 (narrow-to-region beg (point))
1025 ;; narrow to current item
1026 (goto-char (point-min))
1027 (forward-line 1)
1028 (let ((list-nested nil)
1029 (indent-found nil)
1030 (post-indent post-indent))
1031 (while (< (point) (point-max))
1032 (when (and (looking-at list-item)
1033 (not (or (get-text-property
1034 (muse-list-item-critical-point) 'read-only)
1035 (get-text-property
1036 (muse-list-item-critical-point) 'muse-link))))
1037 ;; if we encounter a list item, allow no post-indent
1038 ;; space
1039 (setq list-nested t))
1040 (when (looking-at (concat indent "\\("
1041 (or (and list-nested "")
1042 post-indent)
1043 "\\)"))
1044 ;; if list is not nested, remove indentation
1045 (unless indent-found
1046 (setq post-indent (match-string 1)
1047 indent-found t))
1048 (replace-match ""))
1049 (forward-line 1)))
1050 (skip-chars-backward (concat muse-regexp-blank "\n"))
1051 (muse-insert-markup-end-list end-tag)
1052 (when continue
1053 (goto-char (point-max)))))))
1055 (defun muse-publish-markup-list ()
1056 "Markup a list entry.
1057 This function works by marking up items of the same list level
1058 and type, respecting the end-of-list property."
1059 (let* ((str (match-string 1))
1060 (type (muse-list-item-type str))
1061 (indent (buffer-substring (muse-line-beginning-position)
1062 (match-beginning 1)))
1063 (post-indent (length str))
1064 (last (match-beginning 0)))
1065 (cond
1066 ((or (get-text-property (muse-list-item-critical-point) 'read-only)
1067 (get-text-property (muse-list-item-critical-point) 'muse-link))
1068 nil)
1069 ((eq type 'ul)
1070 (unless (eq (char-after (match-end 1)) ?-)
1071 (delete-region (match-beginning 0) (match-end 0))
1072 (muse-insert-markup (muse-markup-text 'begin-uli))
1073 (save-excursion
1074 (muse-publish-surround-text
1075 (muse-markup-text 'begin-uli-item)
1076 (muse-markup-text 'end-uli-item)
1077 (lambda (indent)
1078 (muse-forward-list-item 'ul indent))
1079 indent post-indent)
1080 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1081 (forward-line 1)))
1082 ((eq type 'ol)
1083 (delete-region (match-beginning 0) (match-end 0))
1084 (muse-insert-markup (muse-markup-text 'begin-oli))
1085 (save-excursion
1086 (muse-publish-surround-text
1087 (muse-markup-text 'begin-oli-item)
1088 (muse-markup-text 'end-oli-item)
1089 (lambda (indent)
1090 (muse-forward-list-item 'ol indent))
1091 indent post-indent)
1092 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1093 (forward-line 1))
1094 ((not (string= (match-string 2) ""))
1095 ;; must have an initial term
1096 (goto-char (match-beginning 0))
1097 (muse-insert-markup (muse-markup-text 'begin-dl))
1098 (save-excursion
1099 (muse-publish-surround-dl indent post-indent)
1100 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1101 (forward-line 1))))
1102 nil)
1104 (defun muse-publish-markup-quote ()
1105 "Markup a quoted paragraph.
1106 The reason this function is so funky, is to prevent text properties
1107 like read-only from being inadvertently deleted."
1108 (let* ((ws (match-string 1))
1109 (centered (>= (string-width ws) 6))
1110 (begin-elem (if centered 'begin-center 'begin-quote-item))
1111 (end-elem (if centered 'end-center 'end-quote-item)))
1112 (replace-match "" t t nil 1)
1113 (unless centered
1114 (muse-insert-markup (muse-markup-text 'begin-quote)))
1115 (muse-publish-surround-text (muse-markup-text begin-elem)
1116 (muse-markup-text end-elem)
1117 (function (lambda (indent)
1118 (muse-forward-paragraph)
1119 nil)))
1120 (unless centered
1121 (muse-insert-markup (muse-markup-text 'end-quote)))))
1123 (defun muse-publish-markup-leading-space (markup-space multiple)
1124 (let (count)
1125 (when (and markup-space
1126 (>= (setq count (skip-chars-forward " ")) 0))
1127 (delete-region (muse-line-beginning-position) (point))
1128 (while (> count 0)
1129 (muse-insert-markup markup-space)
1130 (setq count (- count multiple))))))
1132 (defun muse-publish-markup-verse ()
1133 (let ((leader (match-string 0)))
1134 (goto-char (match-beginning 0))
1135 (muse-insert-markup (muse-markup-text 'begin-verse))
1136 (while (looking-at leader)
1137 (replace-match "")
1138 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1139 (let ((beg (point)))
1140 (end-of-line)
1141 (cond
1142 ((bolp)
1143 (let ((text (muse-markup-text 'empty-verse-line)))
1144 (when text (muse-insert-markup text))))
1145 ((save-excursion
1146 (save-match-data
1147 (forward-line 1)
1148 (or (looking-at (concat leader "["
1149 muse-regexp-blank
1150 "]*$"))
1151 (not (looking-at leader)))))
1152 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1153 (end-text (muse-markup-text 'end-last-stanza-line)))
1154 (when end-text (muse-insert-markup end-text))
1155 (goto-char beg)
1156 (when begin-text (muse-insert-markup begin-text))
1157 (end-of-line)))
1159 (let ((begin-text (muse-markup-text 'begin-verse-line))
1160 (end-text (muse-markup-text 'end-verse-line)))
1161 (when end-text (muse-insert-markup end-text))
1162 (goto-char beg)
1163 (when begin-text (muse-insert-markup begin-text))
1164 (end-of-line))))
1165 (forward-line 1))))
1166 (muse-insert-markup (muse-markup-text 'end-verse))
1167 (insert ?\n))
1169 (defun muse-publish-table-fields (beg end)
1170 "Parse given region as a table, returning a cons cell.
1171 The car is the length of the longest row.
1173 The cdr is a list of the fields of the table, with the first
1174 element indicating the type of the row:
1175 1: body, 2: header, 3: footer.
1177 The existing region will be removed, except for initial blank lines."
1178 (unless (muse-publishing-directive "disable-tables")
1179 (let ((longest 0)
1180 (left 0)
1181 fields field-list)
1182 (save-restriction
1183 (narrow-to-region beg end)
1184 (goto-char (point-min))
1185 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1186 (forward-line 1))
1187 (setq beg (point))
1188 (while (= left 0)
1189 (when (looking-at muse-table-line-regexp)
1190 (setq fields (cons (length (match-string 1))
1191 (split-string (match-string 0)
1192 muse-table-field-regexp))
1193 field-list (cons fields field-list)
1194 longest (max (length fields) longest)))
1195 (setq left (forward-line 1))))
1196 (delete-region beg end)
1197 (if (= longest 0)
1198 (cons 0 nil)
1199 (cons (1- longest) (nreverse field-list))))))
1201 (defun muse-publish-markup-table ()
1202 "Style does not support tables.")
1204 (defun muse-publish-escape-specials-in-string (string &optional context)
1205 "Escape specials in STRING using style-specific :specials.
1206 CONTEXT is used to figure out what kind of specials to escape.
1208 See the documentation of the `muse-publish-escape-specials'
1209 function for the list of available contexts."
1210 (unless string
1211 (setq string ""))
1212 (let ((specials (muse-style-element :specials nil t)))
1213 (cond ((functionp specials)
1214 (setq specials (funcall specials context)))
1215 ((symbolp specials)
1216 (setq specials (symbol-value specials))))
1217 (if (functionp specials)
1218 (funcall specials string)
1219 (apply (function concat)
1220 (mapcar
1221 (lambda (ch)
1222 (let (repl)
1223 (setq repl (or (assoc ch specials)
1224 (assoc ch muse-publish-markup-specials)))
1225 (if (null repl)
1226 (char-to-string ch)
1227 (cdr repl))))
1228 (append string nil))))))
1230 (defun muse-publish-markup-email ()
1231 (let* ((beg (match-end 1))
1232 (addr (buffer-substring-no-properties beg (match-end 0))))
1233 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1234 (goto-char beg)
1235 (delete-region beg (match-end 0))
1236 (if (or (eq (char-before (match-beginning 0)) ?\")
1237 (eq (char-after (match-end 0)) ?\"))
1238 (insert addr)
1239 (insert (format (muse-markup-text 'email-addr) addr addr)))
1240 (muse-publish-mark-read-only beg (point))))
1242 (defun muse-publish-classify-url (target)
1243 "Transform anchors and get published name, if TARGET is a page.
1244 The return value is a cons cell. The car is the type of link,
1245 the cadr is the page name, and the cddr is the anchor."
1246 (save-match-data
1247 (cond ((or (null target) (string= target ""))
1248 nil)
1249 ((string-match muse-url-regexp target)
1250 (cons 'url (cons target nil)))
1251 ((string-match muse-image-regexp target)
1252 (cons 'image (cons target nil)))
1253 ((string-match muse-file-regexp target)
1254 (cons 'file (cons target nil)))
1255 ((string-match "#" target)
1256 (if (eq (aref target 0) ?\#)
1257 (cons 'anchor-ref (cons nil (substring target 1)))
1258 (cons 'link-and-anchor
1259 (cons (muse-publish-link-name
1260 (substring target 0 (match-beginning 0)))
1261 (substring target (match-end 0))))))
1263 (cons 'link (cons (muse-publish-link-name target) nil))))))
1265 (defun muse-publish-url (url &optional desc explicit)
1266 "Resolve a URL into its final <a href> form."
1267 (let ((orig-url url)
1268 type anchor)
1269 (dolist (transform muse-publish-url-transforms)
1270 (setq url (save-match-data (when url (funcall transform url explicit)))))
1271 (dolist (transform muse-publish-desc-transforms)
1272 (setq desc (save-match-data
1273 (when desc (funcall transform desc explicit)))))
1274 (when desc
1275 (setq desc (muse-publish-escape-specials-in-string desc 'url-desc)))
1276 (setq orig-url
1277 (muse-publish-escape-specials-in-string orig-url 'url-desc))
1278 (let ((target (muse-publish-classify-url url)))
1279 (setq type (car target)
1280 url (muse-publish-escape-specials-in-string (cadr target) 'url)
1281 anchor (muse-publish-escape-specials-in-string
1282 (cddr target) 'url)))
1283 (cond ((eq type 'anchor-ref)
1284 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1285 ((string= url "")
1286 desc)
1287 ((eq type 'image)
1288 (if desc
1289 (muse-markup-text 'image-with-desc url desc)
1290 (muse-markup-text 'image-link url)))
1291 ((eq type 'link-and-anchor)
1292 (muse-markup-text 'link-and-anchor url anchor
1293 (or desc orig-url)))
1294 ((and desc (string-match muse-image-regexp desc))
1295 (muse-markup-text 'url-with-image url desc))
1296 ((eq type 'link)
1297 (muse-markup-text 'link url (or desc orig-url)))
1299 (or (and desc
1300 (let ((text (muse-markup-text 'url-and-desc url desc)))
1301 (and (not (string= text ""))
1302 text)))
1303 (muse-markup-text 'url url (or desc orig-url)))))))
1305 (defun muse-publish-insert-url (url &optional desc explicit)
1306 "Resolve a URL into its final <a href> form."
1307 (delete-region (match-beginning 0) (match-end 0))
1308 (let ((text (muse-publish-url url desc explicit)))
1309 (when text
1310 (muse-insert-markup text))))
1312 (defun muse-publish-markup-link ()
1313 (let (desc explicit link)
1314 (setq explicit (save-match-data
1315 (if (string-match muse-explicit-link-regexp
1316 (match-string 0))
1317 t nil)))
1318 (setq desc (if explicit
1319 (or (match-string 2) (match-string 1))
1320 (match-string 0)))
1321 (setq link (if explicit
1322 (muse-handle-explicit-link (match-string 1))
1323 (muse-handle-implicit-link (match-string 0))))
1324 (when (and link
1325 (or explicit
1326 (not (or (eq (char-before (match-beginning 0)) ?\")
1327 (eq (char-after (match-end 0)) ?\")))))
1328 ;; if explicit link has no user-provided description, treat it
1329 ;; as if it were an implicit link
1330 (when (and explicit (not (match-string 2)))
1331 (setq explicit nil))
1332 (muse-publish-insert-url link desc explicit))))
1334 (defun muse-publish-markup-url ()
1335 (unless (or (eq (char-before (match-beginning 0)) ?\")
1336 (eq (char-after (match-end 0)) ?\"))
1337 (muse-publish-insert-url (match-string 0))))
1339 ;; Default publishing tags
1341 (defun muse-publish-contents-tag (beg end attrs)
1342 (set (make-local-variable 'muse-publish-generate-contents)
1343 (cons (copy-marker (point) t)
1344 (let ((depth (cdr (assoc "depth" attrs))))
1345 (or (and depth (string-to-number depth)) 2)))))
1347 (defun muse-publish-verse-tag (beg end)
1348 (save-excursion
1349 (save-restriction
1350 (narrow-to-region beg end)
1351 (goto-char (point-min))
1352 (while (eq ?\ (char-syntax (char-after)))
1353 (delete-char 1))
1354 (while (< (point) (point-max))
1355 (insert "> ")
1356 (forward-line))
1357 (if (eq ?\ (char-syntax (char-before)))
1358 (delete-char -1)))))
1360 (defun muse-publish-mark-read-only (beg end)
1361 "Add read-only properties to the given region."
1362 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1363 nil)
1365 (defun muse-publish-mark-link (&optional beg end)
1366 "Indicate that the given region is a Muse link, so that other
1367 markup elements respect it. If a region is not specified, use
1368 the 0th match data to determine it.
1370 This is usually applied to extended links."
1371 (unless beg (setq beg (match-beginning 0)))
1372 (unless end (setq end (match-end 0)))
1373 (add-text-properties beg end '(muse-link t))
1374 nil)
1376 (defun muse-publish-quote-tag (beg end)
1377 (save-excursion
1378 (save-restriction
1379 (narrow-to-region beg end)
1380 (muse-insert-markup (muse-markup-text 'begin-quote))
1381 (muse-publish-surround-text (muse-markup-text 'begin-quote-item)
1382 (muse-markup-text 'end-quote-item)
1383 (function (lambda (indent)
1384 (muse-forward-paragraph)
1385 (goto-char (match-end 0))
1386 (< (point) (point-max)))))
1387 (muse-insert-markup (muse-markup-text 'end-quote)))))
1389 (defun muse-publish-code-tag (beg end)
1390 (muse-publish-escape-specials beg end nil 'literal)
1391 (goto-char beg)
1392 (insert (muse-markup-text 'begin-literal))
1393 (goto-char end)
1394 (insert (muse-markup-text 'end-literal))
1395 (muse-publish-mark-read-only beg (point)))
1397 (defun muse-publish-example-tag (beg end)
1398 (muse-publish-escape-specials beg end nil 'example)
1399 (goto-char beg)
1400 (insert (muse-markup-text 'begin-example))
1401 (goto-char end)
1402 (insert (muse-markup-text 'end-example))
1403 (muse-publish-mark-read-only beg (point)))
1405 (defun muse-publish-verbatim-tag (beg end)
1406 (muse-publish-escape-specials beg end nil 'verbatim)
1407 (muse-publish-mark-read-only beg end))
1409 (defalias 'muse-publish-class-tag 'ignore)
1411 (defun muse-publish-lisp-tag (beg end)
1412 (save-excursion
1413 (let ((str (muse-eval-lisp
1414 (prog1
1415 (concat "(progn "
1416 (buffer-substring-no-properties beg end)
1417 ")")
1418 (delete-region beg end)))))
1419 (set-text-properties 0 (length str) nil str)
1420 (insert str))))
1422 (defun muse-publish-command-tag (beg end attrs)
1423 (while (looking-at "\\s-*$")
1424 (forward-line))
1425 (let ((interp (cdr (assoc "interp" attrs))))
1426 (if (null interp)
1427 (shell-command
1428 (prog1
1429 (buffer-substring-no-properties (point) end)
1430 (delete-region beg end)) t)
1431 (shell-command-on-region beg end interp t t))
1432 (muse-publish-mark-read-only beg (point))))
1434 (defun muse-publish-comment-tag (beg end)
1435 (if (null muse-publish-comments-p)
1436 (delete-region beg end)
1437 (goto-char end)
1438 (muse-insert-markup (muse-markup-text 'comment-end))
1439 (muse-publish-mark-read-only beg end)
1440 (goto-char beg)
1441 (muse-insert-markup (muse-markup-text 'comment-begin))))
1443 ;; Miscellaneous helper functions
1445 (defun muse-publish-strip-tags (string)
1446 "Remove all tags from the string."
1447 (while (string-match "<.*?>" string)
1448 (setq string (replace-match "" nil t string)))
1449 string)
1451 (defun muse-publish-markup-type (category default-func)
1452 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1453 (funcall (or rule default-func))))
1455 (defun muse-published-buffer-contents (buffer)
1456 (with-current-buffer buffer
1457 (goto-char (point-min))
1458 (let ((beg (and (search-forward "Emacs Muse begins here")
1459 (muse-line-end-position)))
1460 (end (and (search-forward "Emacs Muse ends here")
1461 (muse-line-beginning-position))))
1462 (buffer-substring-no-properties beg end))))
1464 (defun muse-published-contents (file)
1465 (when (file-readable-p file)
1466 (muse-with-temp-buffer
1467 (insert-file-contents file)
1468 (muse-published-buffer-contents (current-buffer)))))
1470 (defun muse-publish-transform-output
1471 (file temp-file output-path name gen-func &rest cleanup-exts)
1472 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1473 (setq file (muse-page-name file))
1474 (message "Generating %s output for %s..." name file)
1475 (if (not (funcall gen-func temp-file output-path))
1476 (message "Generating %s from %s...failed" name file)
1477 (message "Generating %s output for %s...done" name file)
1478 (muse-delete-file-if-exists temp-file)
1479 (dolist (ext cleanup-exts)
1480 (muse-delete-file-if-exists
1481 (expand-file-name (concat file ext)
1482 (file-name-directory output-path))))
1483 (message "Wrote %s" output-path)))
1485 (defun muse-publish-read-only (string)
1486 (let ((end (1- (length string))))
1487 (add-text-properties 0 end
1488 '(rear-nonsticky (read-only) read-only t)
1489 string)
1490 string))
1492 (provide 'muse-publish)
1494 ;;; muse-publish.el ends here