Make C-> and C-< keybindings not indent nested items.
[muse-el.git] / lisp / muse-publish.el
blobbd47352ce4fbcea7afac79a8922ef155ed8433c9
1 ;;; muse-publish.el --- base publishing implementation
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; This file is not part of GNU Emacs.
7 ;; This is free software; you can redistribute it and/or modify it under
8 ;; the terms of the GNU General Public License as published by the Free
9 ;; Software Foundation; either version 2, or (at your option) any later
10 ;; version.
12 ;; This is distributed in the hope that it will be useful, but WITHOUT
13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 ;; for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;; Yann Hodique (yann DOT hodique AT gmail DOT com) fixed an
27 ;; unnecessary URL description transform in `muse-publish-url'.
29 ;; Peter K. Lee (saint AT corenova DOT com) provided the
30 ;; `muse-style-elements-list' function.
32 ;; Jim Ottaway (j DOT ottaway AT lse DOT ac DOT uk) provided an
33 ;; 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-noemphasis)
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.
143 (2200 ,(format muse-list-item-regexp (concat "[" muse-regexp-blank "]*"))
144 0 list)
146 (2400 ,(concat "^\\([" muse-regexp-blank "]+\\).+") 0 quote)
148 (2500 ,(concat "\\(^\\|[" muse-regexp-blank "]+\\)--\\($\\|["
149 muse-regexp-blank "]+\\)")
150 0 emdash)
152 ;; "verse" text is indicated the same way as a quoted e-mail
153 ;; response: "> text", where text may contain initial whitespace
154 ;; (see below).
155 (2600 ,(concat "^[" muse-regexp-blank "]*> ") 0 verse)
157 ;; simple table markup is supported, nothing fancy. use | to
158 ;; separate cells, || to separate header cells, and ||| for footer
159 ;; cells
160 (2700 ,(concat "\\(\\([" muse-regexp-blank "]*\n\\)?"
161 "\\(" muse-table-line-regexp "\\(?:\n\\|\\'\\)\\)\\)+")
162 0 table)
164 ;; replace links in the buffer (links to other pages)
165 (2900 muse-explicit-link-regexp 0 link)
167 ;; bare URLs
168 (3000 muse-url-regexp 0 url)
170 ;; bare email addresses
171 (3500
172 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email)
174 "List of markup rules for publishing a page with Muse.
175 The rules given in this variable are invoked first, followed by
176 whatever rules are specified by the current style.
178 Each member of the list is either a function, or a list of the form:
180 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
182 REGEXP is a regular expression, or symbol whose value is a regular
183 expression, which is searched for using `re-search-forward'.
184 TEXT-BEGIN-GROUP is the matching group within that regexp which
185 denotes the beginning of the actual text to be marked up.
186 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
187 If it is not a string, but a function, it will be called to determine
188 what the replacement text should be (it must return a string). If it
189 is a symbol, the value of that symbol should be a string.
191 The replacements are done in order, one rule at a time. Writing
192 the regular expressions can be a tricky business. Note that case
193 is never ignored. `case-fold-search' is always bound to nil
194 while processing the markup rules."
195 :type '(repeat (choice
196 (list :tag "Markup rule"
197 integer
198 (choice regexp symbol)
199 integer
200 (choice string function symbol))
201 function))
202 :group 'muse-publish)
204 (defcustom muse-publish-markup-functions
205 '((directive . muse-publish-markup-directive)
206 (comment . muse-publish-markup-comment)
207 (anchor . muse-publish-markup-anchor)
208 (tag . muse-publish-markup-tag)
209 (word . muse-publish-markup-word)
210 (emdash . muse-publish-markup-emdash)
211 (enddots . muse-publish-markup-enddots)
212 (dots . muse-publish-markup-dots)
213 (rule . muse-publish-markup-rule)
214 (no-break-space . muse-publish-markup-no-break-space)
215 (heading . muse-publish-markup-heading)
216 (footnote . muse-publish-markup-footnote)
217 (fn-sep . muse-publish-markup-fn-sep)
218 (list . muse-publish-markup-list)
219 (quote . muse-publish-markup-quote)
220 (verse . muse-publish-markup-verse)
221 (table . muse-publish-markup-table)
222 (email . muse-publish-markup-email)
223 (link . muse-publish-markup-link)
224 (url . muse-publish-markup-url))
225 "An alist of style types to custom functions for that kind of text.
227 Each member of the list is of the form:
229 (SYMBOL FUNCTION)
231 SYMBOL describes the type of text to associate with this rule.
232 `muse-publish-markup-regexps' maps regexps to these symbols.
234 FUNCTION is the function to use to mark up this kind of rule if
235 no suitable function is found through the :functions tag of the
236 current style."
237 :type '(alist :key-type symbol :value-type function)
238 :group 'muse-publish)
240 (defcustom muse-publish-markup-tags
241 '(("contents" nil t muse-publish-contents-tag)
242 ("verse" t nil muse-publish-verse-tag)
243 ("example" t nil muse-publish-example-tag)
244 ("code" t nil muse-publish-code-tag)
245 ("quote" t nil muse-publish-quote-tag)
246 ("literal" t nil muse-publish-mark-read-only)
247 ("verbatim" t nil muse-publish-verbatim-tag)
248 ("lisp" t nil muse-publish-lisp-tag)
249 ("class" t t muse-publish-class-tag)
250 ("command" t t muse-publish-command-tag)
251 ("comment" t nil muse-publish-comment-tag))
252 "A list of tag specifications, for specially marking up text.
253 XML-style tags are the best way to add custom markup to Muse.
254 This is easily accomplished by customizing this list of markup tags.
256 For each entry, the name of the tag is given, whether it expects
257 a closing tag and/or an optional set of attributes, and a
258 function that performs whatever action is desired within the
259 delimited region.
261 The tags themselves are deleted during publishing, before the
262 function is called. The function is called with three arguments,
263 the beginning and end of the region surrounded by the tags. If
264 properties are allowed, they are passed as a third argument in
265 the form of an alist. The `end' argument to the function is
266 always a marker.
268 Point is always at the beginning of the region within the tags, when
269 the function is called. Wherever point is when the function finishes
270 is where tag markup will resume.
272 These tag rules are processed once at the beginning of markup, and
273 once at the end, to catch any tags which may have been inserted
274 in-between."
275 :type '(repeat (list (string :tag "Markup tag")
276 (boolean :tag "Expect closing tag" :value t)
277 (boolean :tag "Parse attributes" :value nil)
278 function))
279 :group 'muse-publish)
281 (defcustom muse-publish-markup-specials nil
282 "A table of characters which must be represented specially."
283 :type '(alist :key-type character :value-type string)
284 :group 'muse-publish)
286 (defvar muse-publishing-p nil
287 "Set to t while a page is being published.")
288 (defvar muse-batch-publishing-p nil
289 "Set to t while a page is being batch published.")
290 (defvar muse-publishing-styles nil
291 "The publishing styles that Muse recognizes.
292 This is automatically generated when loading publishing styles.")
293 (defvar muse-publishing-current-file nil
294 "The file that is currently being published.")
295 (defvar muse-publishing-current-output-path nil
296 "The path where the current file will be published to.")
297 (defvar muse-publishing-current-style nil
298 "The style of the file that is currently being published.")
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.")
306 (defvar muse-publishing-last-position nil
307 "Last position of the point when publishing.
308 This is used to make sure that publishing doesn't get stalled.")
310 ;; Functions for handling style information
312 (defsubst muse-style (&optional style)
313 "Resolve the given STYLE into a Muse style, if it is a string."
314 (if (null style)
315 muse-publishing-current-style
316 (if (stringp style)
317 (assoc style muse-publishing-styles)
318 (muse-assert (consp style))
319 style)))
321 (defun muse-define-style (name &rest elements)
322 (let ((entry (assoc name muse-publishing-styles)))
323 (if entry
324 (error "There is already a style named %s." name)
325 (setq muse-publishing-styles
326 (cons (append (list name) elements)
327 muse-publishing-styles)))))
329 (defun muse-derive-style (new-name base-name &rest elements)
330 (let ((entry (assoc new-name muse-publishing-styles)))
331 (if entry
332 (error "There is already a style named %s." new-name)
333 (apply 'muse-define-style new-name
334 (append elements (list :base base-name))))))
336 (defsubst muse-get-keyword (keyword list &optional direct)
337 (let ((value (cadr (memq keyword list))))
338 (if (and (not direct) (symbolp value))
339 (symbol-value value)
340 value)))
342 (defun muse-style-elements-list (elem &optional style)
343 "Return a list all references to ELEM in STYLE, including base styles.
344 If STYLE is not specified, use current style."
345 (let (base elements)
346 (while style
347 (setq style (muse-style style))
348 (setq elements (append elements
349 (muse-get-keyword elem style)))
350 (setq style (muse-get-keyword :base style)))
351 elements))
353 (defsubst muse-style-element (elem &optional style direct)
354 "Search for ELEM in STYLE, including base styles.
355 If STYLE is not specified, use current style."
356 (setq style (muse-style style))
357 (let ((value (muse-get-keyword elem style direct)))
358 (if value
359 value
360 (let ((base (muse-get-keyword :base style)))
361 (if base
362 (muse-style-element elem base direct))))))
364 (defun muse-find-markup-element (keyword ident style)
365 (let ((def (assq ident (muse-style-element keyword style))))
366 (if def
367 (cdr def)
368 (let ((base (muse-style-element :base style)))
369 (if base
370 (muse-find-markup-element keyword ident base))))))
372 (defun muse-markup-text (ident &rest args)
373 "Insert ARGS into the text markup associated with IDENT.
374 If the markup text has sections like %N%, this will be replaced
375 with the N-1th argument in ARGS. After that, `format' is applied
376 to the text with ARGS as parameters."
377 (let ((text (muse-find-markup-element :strings ident (muse-style))))
378 (if (and text args)
379 (progn
380 (let (start repl-text)
381 (while (setq start (string-match "%\\([1-9][0-9]*\\)%" text start))
382 ;; escape '%' in the argument text, since we will be
383 ;; using format on it
384 (setq repl-text (muse-replace-regexp-in-string
385 "%" "%%"
386 (nth (1- (string-to-number
387 (match-string 1 text))) args)
388 t t)
389 start (+ start (length repl-text))
390 text (replace-match repl-text t t text))))
391 (apply 'format text args))
392 (or text ""))))
394 (defun muse-insert-markup (&rest args)
395 (let ((beg (point)))
396 (apply 'insert args)
397 (muse-publish-mark-read-only beg (point))))
399 (defun muse-find-markup-tag (keyword tagname style)
400 (let ((def (assoc tagname (muse-style-element keyword style))))
401 (or def
402 (let ((base (muse-style-element :base style)))
403 (if base
404 (muse-find-markup-tag keyword tagname base))))))
406 (defsubst muse-markup-tag-info (tagname &rest args)
407 (let ((tag-info (muse-find-markup-tag :tags tagname (muse-style))))
408 (or tag-info
409 (assoc (match-string 1) muse-publish-markup-tags))))
411 (defsubst muse-markup-function (category)
412 (let ((func (muse-find-markup-element :functions category (muse-style))))
413 (or func
414 (cdr (assq category muse-publish-markup-functions)))))
416 ;; Publishing routines
418 (defun muse-publish-markup (name rules)
419 (let* ((case-fold-search nil)
420 (inhibit-read-only t)
421 (limit (* (length rules) (point-max)))
422 (verbose (and muse-publish-report-threshhold
423 (> (point-max) muse-publish-report-threshhold)))
424 (base 0))
425 (while rules
426 (goto-char (point-min))
427 (let ((regexp (nth 1 (car rules)))
428 (group (nth 2 (car rules)))
429 (repl (nth 3 (car rules)))
430 pos)
431 (setq muse-publishing-last-position nil)
432 (if (symbolp regexp)
433 (setq regexp (symbol-value regexp)))
434 (if (and verbose (not muse-batch-publishing-p))
435 (message "Publishing %s...%d%%" name
436 (* (/ (float (+ (point) base)) limit) 100)))
437 (while (and regexp (setq pos (re-search-forward regexp nil t)))
438 (if (and verbose (not muse-batch-publishing-p))
439 (message "Publishing %s...%d%%" name
440 (* (/ (float (+ (point) base)) limit) 100)))
441 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
442 (get-text-property (match-beginning group) 'read-only))
443 (let* (func
444 (text (cond
445 ((and (symbolp repl)
446 (setq func (muse-markup-function repl)))
447 (funcall func))
448 ((functionp repl)
449 (funcall repl))
450 ((symbolp repl)
451 (symbol-value repl))
452 (t repl))))
453 (if (stringp text)
454 (replace-match text t))))
455 (if (and muse-publishing-last-position
456 (= pos muse-publishing-last-position))
457 (if (eobp)
458 (setq regexp nil)
459 (forward-char 1)))
460 (setq muse-publishing-last-position pos)))
461 (setq rules (cdr rules)
462 base (+ base (point-max))))
463 (if (and verbose (not muse-batch-publishing-p))
464 (message "Publishing %s...done" name))))
466 (defun muse-insert-file-or-string (file-or-string &optional title)
467 (let ((beg (point)) end)
468 (if (and (not (string-equal file-or-string ""))
469 (file-readable-p file-or-string))
470 (setq end (+ beg (cadr (insert-file-contents file-or-string))))
471 (insert file-or-string)
472 (setq end (point)))
473 (save-restriction
474 (narrow-to-region beg end)
475 (remove-text-properties (point-min) (point-max)
476 '(read-only nil rear-nonsticky nil))
477 (goto-char (point-min))
478 (muse-publish-markup (or title "")
479 '((100 "<\\(lisp\\)>" 0
480 muse-publish-markup-tag))))))
482 (defun muse-style-run-hooks (keyword style &rest args)
483 (let (handled)
484 (while (and style (not handled))
485 (setq style (muse-style style))
486 (let ((func (muse-get-keyword keyword style t)))
487 (if func
488 (if (apply func args)
489 (setq handled t))))
490 (unless handled
491 (setq style (muse-style-element :base style))))
492 handled))
494 (defun muse-publish-markup-region (beg end title style)
495 "Apply the given STYLE's markup rules to the given region."
496 (save-restriction
497 (narrow-to-region beg end)
498 (muse-style-run-hooks :before style)
499 (muse-publish-markup
500 title
501 (sort (copy-alist (append muse-publish-markup-regexps
502 (muse-style-elements-list :regexps style)))
503 (function
504 (lambda (l r)
505 (< (car l) (car r))))))
506 (muse-style-run-hooks :before-end style)
507 (muse-publish-escape-specials (point-min) (point-max) nil 'document)))
509 (defun muse-publish-markup-buffer (title style)
510 "Apply the given STYLE's markup rules to the current buffer."
511 (setq style (muse-style style))
512 (let ((style-header (muse-style-element :header style))
513 (style-footer (muse-style-element :footer style))
514 (muse-publishing-current-style style)
515 (muse-publishing-directives
516 (list (cons "title" title)
517 (cons "author" (user-full-name))
518 (cons "date" (format-time-string
519 "%B %e, %Y"
520 (nth 5 (file-attributes
521 muse-publishing-current-file))))))
522 (muse-publishing-p t)
523 (inhibit-read-only t))
524 (run-hooks 'muse-before-publish-hook)
525 (muse-publish-markup-region (point-min) (point-max) title style)
526 (goto-char (point-min))
527 (when style-header
528 (muse-insert-file-or-string style-header title))
529 (goto-char (point-max))
530 (when style-footer
531 (muse-insert-file-or-string style-footer title))
532 (muse-style-run-hooks :after style)
533 (run-hooks 'muse-after-publish-hook)))
535 (defun muse-publish-markup-string (string &optional style)
536 "Markup STRING using the given STYLE's markup rules."
537 (setq style (muse-style style))
538 (muse-with-temp-buffer
539 (insert string)
540 (let ((muse-publishing-current-style style)
541 (muse-publishing-p t))
542 (muse-publish-markup "*string*" (muse-style-element :rules style)))
543 (buffer-string)))
545 ;; Commands for publishing files
547 (defsubst muse-publish-get-style ()
548 (if (= 1 (length muse-publishing-styles))
549 (car muse-publishing-styles)
550 (assoc (completing-read "Publish with style: "
551 muse-publishing-styles nil t)
552 muse-publishing-styles)))
554 (defsubst muse-publish-get-output-dir (style)
555 (let ((default-directory (or (muse-style-element :path style)
556 default-directory)))
557 (muse-read-directory-name "Publish to directory: " nil default-directory)))
559 (defsubst muse-publish-get-info ()
560 (let ((style (muse-publish-get-style)))
561 (list style (muse-publish-get-output-dir style)
562 current-prefix-arg)))
564 (defsubst muse-publish-output-name (&optional file style)
565 (setq style (muse-style style))
566 (concat (muse-style-element :prefix style)
567 (muse-page-name file)
568 (muse-style-element :suffix style)))
570 (defsubst muse-publish-output-file (file &optional output-dir style)
571 (setq style (muse-style style))
572 (if output-dir
573 (expand-file-name (muse-publish-output-name file style) output-dir)
574 (concat (file-name-directory file)
575 (muse-publish-output-name file style))))
577 (defsubst muse-publish-link-name (&optional file style)
578 (setq style (muse-style style))
579 (concat (muse-style-element :prefix style)
580 (muse-page-name file)
581 (or (muse-style-element :link-suffix style)
582 (muse-style-element :suffix style))))
584 (defsubst muse-publish-link-file (file &optional output-dir style)
585 (setq style (muse-style style))
586 (if output-dir
587 (expand-file-name (muse-publish-link-name file style) output-dir)
588 (concat (file-name-directory file)
589 (muse-publish-link-name file style))))
591 ;;;###autoload
592 (defun muse-publish-file (file style &optional output-dir force)
593 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
594 If the argument FORCE is nil, each file is only published if it is
595 newer than the published version. If the argument FORCE is non-nil,
596 the file is published no matter what."
597 (interactive (cons (read-file-name "Publish file: ")
598 (muse-publish-get-info)))
599 (setq style (muse-style style))
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-name style output-dir)
636 (setq style-name (car command-line-args-left)
637 style (muse-style style-name)
638 command-line-args-left (cdr command-line-args-left)
639 output-dir (car command-line-args-left)
640 output-dir
641 (if (string-match "\\`--output-dir=" output-dir)
642 (prog1
643 (substring output-dir (match-end 0))
644 (setq command-line-args-left (cdr command-line-args-left)))))
645 (unless style
646 (error "There is no style '%s' defined." style-name))
647 (dolist (file command-line-args-left)
648 (muse-publish-file file style output-dir t))))
650 ;; Default publishing rules
652 (defun muse-publish-section-close (depth)
653 "Seach forward for the closing tag of given DEPTH."
654 (let (not-end)
655 (save-excursion
656 (while (and (setq not-end (re-search-forward
657 (concat "^\\*\\{1," (number-to-string depth)
658 "\\}\\s-+")
659 nil t))
660 (get-text-property (match-beginning 0) 'read-only)))
661 (if not-end
662 (forward-line 0)
663 (goto-char (point-max)))
664 (cond ((not (eq (char-before) ?\n))
665 (insert "\n\n"))
666 ((not (eq (char-before (1- (point))) ?\n))
667 (insert "\n")))
668 (muse-insert-markup (muse-markup-text 'section-close depth))
669 (insert "\n"))))
671 (defun muse-publish-markup-directive (&optional name value)
672 (unless name (setq name (match-string 1)))
673 (unless value (setq value (match-string 2)))
674 (let ((elem (assoc name muse-publishing-directives)))
675 (if elem
676 (setcdr elem value)
677 (setq muse-publishing-directives
678 (cons (cons name value)
679 muse-publishing-directives))))
680 ;; Make sure we don't ever try to move the point forward (past the
681 ;; beginning of buffer) while we're still searching for directives.
682 (setq muse-publishing-last-position nil)
683 (delete-region (match-beginning 0) (match-end 0)))
685 (defun muse-publish-markup-anchor ()
686 (unless (get-text-property (match-end 1) 'noemphasis)
687 (let ((text (muse-markup-text 'anchor (match-string 2))))
688 (unless (string= text "")
689 (save-match-data
690 (skip-chars-forward (concat muse-regexp-blank "\n"))
691 (muse-insert-markup text)))
692 (match-string 1))))
694 (defun muse-publish-markup-comment ()
695 (if (null muse-publish-comments-p)
697 (goto-char (match-end 0))
698 (muse-insert-markup (muse-markup-text 'comment-end))
699 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
700 (delete-region (match-beginning 0) (match-beginning 1))
701 (goto-char (match-beginning 0))
702 (muse-insert-markup (muse-markup-text 'comment-begin))))
704 (defun muse-publish-markup-tag ()
705 (let ((tag-info (muse-markup-tag-info (match-string 1))))
706 (when (and tag-info
707 (not (get-text-property (match-beginning 0) 'read-only)))
708 (let ((closed-tag (match-string 3))
709 (start (match-beginning 0))
710 (beg (point))
711 end attrs)
712 (when (nth 2 tag-info)
713 (let ((attrstr (match-string 2)))
714 (while (and attrstr
715 (string-match (concat "\\([^"
716 muse-regexp-blank
717 "=\n]+\\)\\(=\"\\"
718 "([^\"]+\\)\"\\)?")
719 attrstr))
720 (let ((attr (cons (downcase
721 (muse-match-string-no-properties 1 attrstr))
722 (muse-match-string-no-properties 3 attrstr))))
723 (setq attrstr (replace-match "" t t attrstr))
724 (if attrs
725 (nconc attrs (list attr))
726 (setq attrs (list attr)))))))
727 (if (and (cadr tag-info) (not closed-tag))
728 (if (search-forward (concat "</" (car tag-info) ">") nil t)
729 (delete-region (match-beginning 0) (point))
730 (setq tag-info nil)))
731 (when tag-info
732 (setq end (point-marker))
733 (delete-region start beg)
734 (goto-char start)
735 (let ((args (list start end)))
736 (if (nth 2 tag-info)
737 (nconc args (list attrs)))
738 (apply (nth 3 tag-info) args))))))
739 nil)
741 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
742 "Escape specials from BEG to END using style-specific :specials.
743 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
744 CONTEXT is used to figure out what kind of specials to escape.
746 The following contexts exist in Muse.
747 'underline _underlined text_
748 'literal =monospaced text= or <code>region</code> (monospaced, escaped)
749 'emphasis *emphasized text*
750 'email email@example.com
751 'url http://example.com
752 'url-desc [[...][description of an extended link]]
753 'example <example>region</example> (monospaced, block context, escaped)
754 'verbatim <verbatim>region</verbatim> (escaped)
755 'document normal text"
756 (let ((specials (muse-style-element :specials nil t)))
757 (cond ((functionp specials)
758 (setq specials (funcall specials context)))
759 ((symbolp specials)
760 (setq specials (symbol-value specials))))
761 (save-excursion
762 (save-restriction
763 (narrow-to-region beg end)
764 (goto-char (point-min))
765 (while (< (point) (point-max))
766 (if (and (not ignore-read-only)
767 (get-text-property (point) 'read-only))
768 (goto-char (or (next-single-property-change (point) 'read-only)
769 (point-max)))
770 (let ((repl (or (assoc (char-after) specials)
771 (assoc (char-after)
772 muse-publish-markup-specials))))
773 (if (null repl)
774 (forward-char 1)
775 (delete-char 1)
776 (insert-before-markers (cdr repl))))))))))
778 (defun muse-publish-markup-word ()
779 (let* ((beg (match-beginning 2))
780 (end (1- (match-end 2)))
781 (leader (buffer-substring-no-properties beg end))
782 open-tag close-tag mark-read-only loc context)
783 (cond
784 ((string= leader "_")
785 (setq context 'underline
786 open-tag (muse-markup-text 'begin-underline)
787 close-tag (muse-markup-text 'end-underline)))
788 ((string= leader "=")
789 (setq context 'literal
790 open-tag (muse-markup-text 'begin-literal)
791 close-tag (muse-markup-text 'end-literal))
792 (setq mark-read-only t))
794 (let ((l (length leader)))
795 (setq context 'emphasis)
796 (cond
797 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
798 close-tag (muse-markup-text 'end-emph)))
799 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
800 close-tag (muse-markup-text 'end-more-emph)))
801 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
802 close-tag (muse-markup-text 'end-most-emph)))
803 (t (setq context nil))))))
804 (if (and context
805 (not (get-text-property beg 'noemphasis))
806 (setq loc (search-forward leader nil t))
807 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
808 (not (eq (char-syntax (char-before (point))) ?\ ))
809 (not (get-text-property (point) 'noemphasis)))
810 (progn
811 (replace-match "")
812 (delete-region beg end)
813 (setq end (point-marker))
814 (muse-insert-markup close-tag)
815 (goto-char beg)
816 (muse-insert-markup open-tag)
817 (setq beg (point))
818 (when mark-read-only
819 (muse-publish-escape-specials beg end t context)
820 (muse-publish-mark-read-only beg end)))
821 (backward-char))
822 nil))
824 (defun muse-publish-markup-emdash ()
825 (delete-region (match-beginning 0) (match-end 0))
826 (muse-insert-markup (muse-markup-text 'emdash))
827 (if (eq (char-after) ?\<)
828 (insert ?\n)))
830 (defun muse-publish-markup-enddots ()
831 (unless (get-text-property (match-beginning 0) 'noemphasis)
832 (delete-region (match-beginning 0) (match-end 0))
833 (muse-insert-markup (muse-markup-text 'enddots))))
835 (defun muse-publish-markup-dots ()
836 (unless (get-text-property (match-beginning 0) 'noemphasis)
837 (delete-region (match-beginning 0) (match-end 0))
838 (muse-insert-markup (muse-markup-text 'dots))))
840 (defun muse-publish-markup-rule ()
841 (unless (get-text-property (match-beginning 0) 'noemphasis)
842 (delete-region (match-beginning 0) (match-end 0))
843 (muse-insert-markup (muse-markup-text 'rule))))
845 (defun muse-publish-markup-no-break-space ()
846 (unless (get-text-property (match-beginning 0) 'noemphasis)
847 (delete-region (match-beginning 0) (match-end 0))
848 (muse-insert-markup (muse-markup-text 'no-break-space))))
850 (defun muse-publish-markup-heading ()
851 (let* ((len (length (match-string 1)))
852 (start (muse-markup-text
853 (cond ((= len 1) 'section)
854 ((= len 2) 'subsection)
855 ((= len 3) 'subsubsection)
856 (t 'section-other))
857 len))
858 (end (muse-markup-text
859 (cond ((= len 1) 'section-end)
860 ((= len 2) 'subsection-end)
861 ((= len 3) 'subsubsection-end)
862 (t 'section-other-end))
863 len)))
864 (delete-region (match-beginning 0) (match-end 0))
865 (muse-insert-markup start)
866 (end-of-line)
867 (when end
868 (muse-insert-markup end))
869 (muse-publish-section-close len)))
871 (defvar muse-publish-footnotes nil)
873 (defun muse-publish-markup-footnote ()
874 "Scan ahead and snarf up the footnote body"
875 (cond
876 ((get-text-property (match-beginning 0) 'noemphasis)
877 nil)
878 ((= (muse-line-beginning-position) (match-beginning 0))
881 (let ((footnote (save-match-data
882 (string-to-number (match-string 1))))
883 footnotemark)
884 (delete-region (match-beginning 0) (match-end 0))
885 (save-excursion
886 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
887 (let* ((start (match-beginning 0))
888 (beg (goto-char (match-end 0)))
889 (end (save-excursion
890 (if (search-forward "\n\n" nil t)
891 (copy-marker (match-beginning 0))
892 (goto-char (point-max))
893 (skip-chars-backward "\n")
894 (point-marker)))))
895 (while (re-search-forward
896 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
897 end t)
898 (replace-match "\\1" t))
899 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
900 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
901 (if (string= "" footnotemark-cmd)
902 (setq footnotemark
903 (concat (muse-markup-text 'footnote)
904 (buffer-substring-no-properties beg end)
905 (muse-markup-text 'footnote-end)))
906 (setq footnotemark (format footnotemark-cmd footnote
907 footnotemark-end-cmd))
908 (unless muse-publish-footnotes
909 (set (make-local-variable 'muse-publish-footnotes)
910 (make-vector 256 nil)))
911 (unless (aref muse-publish-footnotes footnote)
912 (setq footnotemark
913 (concat
914 footnotemark
915 (concat (format (muse-markup-text 'footnotetext)
916 footnote)
917 (buffer-substring-no-properties beg end)
918 (muse-markup-text 'footnotetext-end))))
919 (aset muse-publish-footnotes footnote footnotemark))))
920 (goto-char end)
921 (skip-chars-forward "\n")
922 (delete-region start (point)))))
923 (muse-insert-markup (or footnotemark footnote))))))
925 (defun muse-publish-markup-fn-sep ()
926 (delete-region (match-beginning 0) (match-end 0))
927 (muse-insert-markup (muse-markup-text 'fn-sep)))
929 (defun muse-insert-markup-end-list (&rest args)
930 (let ((beg (point)))
931 (apply 'insert args)
932 (add-text-properties beg (point) '(end-list t))
933 (muse-publish-mark-read-only beg (point))))
935 (defun muse-publish-surround-dl (indent post-indent)
936 (let* ((beg-item (muse-markup-text 'begin-dl-item))
937 (end-item (muse-markup-text 'end-dl-item))
938 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
939 (end-ddt (muse-markup-text 'end-ddt))
940 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
941 (end-dde (muse-markup-text 'end-dde))
942 (continue t)
943 def-on-same-line beg)
944 (while continue
945 ;; envelope this as one term+definitions unit -- HTML does not
946 ;; need this, but DocBook and Muse's custom XML format do
947 (muse-insert-markup beg-item)
948 (when (looking-at muse-dl-term-regexp)
949 ;; find the term and wrap it with published markup
950 (setq beg (point))
951 (goto-char (match-end 1))
952 (delete-region (point) (match-end 0))
953 (muse-insert-markup-end-list end-ddt)
954 ;; if definition is immediately after term, move to next line
955 (unless (eq (char-after) ?\n)
956 (insert ?\n))
957 (save-excursion
958 (goto-char beg)
959 (delete-region (point) (match-beginning 1))
960 (muse-insert-markup beg-ddt)))
961 (setq beg (point)
962 ;; move past current item
963 continue (muse-forward-list-item 'dl-term indent))
964 (save-restriction
965 (narrow-to-region beg (point))
966 (goto-char (point-min))
967 ;; publish each definition that we find, defaulting to an
968 ;; empty definition if none are found
969 (muse-publish-surround-text beg-dde end-dde
970 (lambda (indent)
971 (muse-forward-list-item 'dl-entry indent))
972 nil nil t)
973 (goto-char (point-max))
974 (skip-chars-backward (concat muse-regexp-blank "\n"))
975 (muse-insert-markup-end-list end-item)
976 (when continue
977 (goto-char (point-max)))))))
979 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent determine-indent)
980 (let ((continue t)
981 (list-item (format muse-list-item-regexp
982 (concat "[" muse-regexp-blank "]*")))
983 (indent-found nil)
984 init-indent beg)
985 (unless indent
986 (setq indent (concat "[" muse-regexp-blank "]+")))
987 (if post-indent
988 (setq post-indent (concat " \\{0," (number-to-string post-indent)
989 "\\}"))
990 (setq post-indent ""))
991 (while continue
992 (muse-insert-markup beg-tag)
993 (setq beg (point)
994 ;; move past current item; continue is non-nil if there
995 ;; are more like items to be processed
996 continue (funcall move-func indent))
997 (save-restriction
998 (when determine-indent
999 ;; if the caller doesn't know how much indentation
1000 ;; to use, figure it out ourselves
1001 (if (not continue)
1002 (setq indent "")
1003 (save-match-data
1004 ;; snarf all leading whitespace
1005 (let ((this-indent (and (match-beginning 2)
1006 (buffer-substring (match-beginning 1)
1007 (match-beginning 2)))))
1008 (when (and this-indent
1009 (not (string= this-indent "")))
1010 (setq indent this-indent
1011 determine-indent nil))))))
1012 (when continue
1013 ;; remove list markup if we encountered another item of the
1014 ;; same type
1015 (replace-match "" t t nil 1))
1016 (narrow-to-region beg (point))
1017 ;; narrow to current item
1018 (goto-char (point-min))
1019 (forward-line 1)
1020 (let ((list-nested nil))
1021 (while (< (point) (point-max))
1022 (when (looking-at list-item)
1023 ;; if we encounter a list item, allow no post-indent
1024 ;; space
1025 (setq list-nested t))
1026 (when (looking-at (concat indent (or (and list-nested "")
1027 post-indent)))
1028 ;; if list is not nested, remove indentation
1029 (unless indent-found
1030 (setq post-indent (match-string 0)
1031 indent-found t))
1032 (replace-match ""))
1033 (forward-line 1)))
1034 (skip-chars-backward (concat muse-regexp-blank "\n"))
1035 (muse-insert-markup-end-list end-tag)
1036 (when continue
1037 (goto-char (point-max)))))))
1039 (defun muse-publish-markup-list ()
1040 "Markup a list entry.
1041 The reason this function is so funky, is to prevent text properties
1042 like read-only from being inadvertently deleted."
1043 (let* ((str (match-string 1))
1044 (type (muse-list-item-type str))
1045 (indent (buffer-substring (muse-line-beginning-position)
1046 (match-beginning 1)))
1047 (post-indent (length str))
1048 (last (match-beginning 0)))
1049 (cond
1050 ((eq type 'ul)
1051 (unless (eq (char-after (match-end 1)) ?-)
1052 (delete-region (match-beginning 0) (match-end 0))
1053 (muse-insert-markup (muse-markup-text 'begin-uli))
1054 (save-excursion
1055 (muse-publish-surround-text
1056 (muse-markup-text 'begin-uli-item)
1057 (muse-markup-text 'end-uli-item)
1058 (lambda (indent)
1059 (muse-forward-list-item 'ul indent))
1060 indent post-indent)
1061 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1062 (forward-line 1)))
1063 ((eq type 'ol)
1064 (delete-region (match-beginning 0) (match-end 0))
1065 (muse-insert-markup (muse-markup-text 'begin-oli))
1066 (save-excursion
1067 (muse-publish-surround-text
1068 (muse-markup-text 'begin-oli-item)
1069 (muse-markup-text 'end-oli-item)
1070 (lambda (indent)
1071 (muse-forward-list-item 'ol indent))
1072 indent post-indent)
1073 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1074 (forward-line 1))
1075 ((not (string= (match-string 2) ""))
1076 ;; must have an initial term
1077 (goto-char (match-beginning 0))
1078 (muse-insert-markup (muse-markup-text 'begin-dl))
1079 (save-excursion
1080 (muse-publish-surround-dl indent post-indent)
1081 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1082 (forward-line 1))))
1083 nil)
1085 (defun muse-publish-markup-quote ()
1086 "Markup a quoted paragraph.
1087 The reason this function is so funky, is to prevent text properties
1088 like read-only from being inadvertently deleted."
1089 (let* ((ws (match-string 1))
1090 (centered (>= (string-width ws) 6))
1091 (begin-elem (if centered 'begin-center 'begin-quote-item))
1092 (end-elem (if centered 'end-center 'end-quote-item)))
1093 (replace-match "" t t nil 1)
1094 (unless centered
1095 (muse-insert-markup (muse-markup-text 'begin-quote)))
1096 (muse-publish-surround-text (muse-markup-text begin-elem)
1097 (muse-markup-text end-elem)
1098 (function (lambda (indent)
1099 (muse-forward-paragraph)
1100 nil)))
1101 (unless centered
1102 (muse-insert-markup (muse-markup-text 'end-quote)))))
1104 (defun muse-publish-markup-leading-space (markup-space multiple)
1105 (let (count)
1106 (when (and markup-space
1107 (>= (setq count (skip-chars-forward " ")) 0))
1108 (delete-region (muse-line-beginning-position) (point))
1109 (while (> count 0)
1110 (muse-insert-markup markup-space)
1111 (setq count (- count multiple))))))
1113 (defun muse-publish-markup-verse ()
1114 (let ((leader (match-string 0)))
1115 (goto-char (match-beginning 0))
1116 (muse-insert-markup (muse-markup-text 'begin-verse))
1117 (while (looking-at leader)
1118 (replace-match "")
1119 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1120 (let ((beg (point)))
1121 (end-of-line)
1122 (cond
1123 ((bolp)
1124 (let ((text (muse-markup-text 'empty-verse-line)))
1125 (when text (muse-insert-markup text))))
1126 ((save-excursion
1127 (save-match-data
1128 (forward-line 1)
1129 (or (looking-at (concat leader "["
1130 muse-regexp-blank
1131 "]*$"))
1132 (not (looking-at leader)))))
1133 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1134 (end-text (muse-markup-text 'end-last-stanza-line)))
1135 (when end-text (muse-insert-markup end-text))
1136 (goto-char beg)
1137 (when begin-text (muse-insert-markup begin-text))
1138 (end-of-line)))
1140 (let ((begin-text (muse-markup-text 'begin-verse-line))
1141 (end-text (muse-markup-text 'end-verse-line)))
1142 (when end-text (muse-insert-markup end-text))
1143 (goto-char beg)
1144 (when begin-text (muse-insert-markup begin-text))
1145 (end-of-line))))
1146 (forward-line 1))))
1147 (muse-insert-markup (muse-markup-text 'end-verse) ?\n))
1149 (defun muse-publish-table-fields (beg end)
1150 "Parse given region as a table, returning a cons cell.
1151 The car is the length of the longest row.
1153 The cdr is a list of the fields of the table, with the first
1154 element indicating the type of the row:
1155 1: body, 2: header, 3: footer.
1157 The existing region will be removed, except for initial blank lines."
1158 (let ((longest 0)
1159 (left 0)
1160 fields field-list)
1161 (save-restriction
1162 (narrow-to-region beg end)
1163 (goto-char (point-min))
1164 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1165 (forward-line 1))
1166 (setq beg (point))
1167 (while (= left 0)
1168 (when (looking-at muse-table-line-regexp)
1169 (setq fields (cons (length (match-string 1))
1170 (split-string (match-string 0)
1171 muse-table-field-regexp))
1172 field-list (cons fields field-list)
1173 longest (max (length fields) longest)))
1174 (setq left (forward-line 1))))
1175 (delete-region beg end)
1176 (if (= longest 0)
1177 (cons 0 nil)
1178 (cons (1- longest) (nreverse field-list)))))
1180 (defun muse-publish-markup-table ()
1181 "Style does not support tables.")
1183 (defun muse-publish-escape-specials-in-string (string &optional context)
1184 "Escape specials in STRING using style-specific :specials.
1185 CONTEXT is used to figure out what kind of specials to escape.
1187 See the documentation of the `muse-publish-escape-specials'
1188 function for the list of available contexts."
1189 (unless string
1190 (setq string ""))
1191 (let ((specials (muse-style-element :specials nil t)))
1192 (cond ((functionp specials)
1193 (setq specials (funcall specials context)))
1194 ((symbolp specials)
1195 (setq specials (symbol-value specials))))
1196 (if (functionp specials)
1197 (funcall specials string)
1198 (apply (function concat)
1199 (mapcar
1200 (lambda (ch)
1201 (let (repl)
1202 (setq repl (or (assoc ch specials)
1203 (assoc ch muse-publish-markup-specials)))
1204 (if (null repl)
1205 (char-to-string ch)
1206 (cdr repl))))
1207 (append string nil))))))
1209 (defun muse-publish-markup-email ()
1210 (let* ((beg (match-end 1))
1211 (addr (buffer-substring-no-properties beg (match-end 0))))
1212 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1213 (goto-char beg)
1214 (delete-region beg (match-end 0))
1215 (if (or (eq (char-before (match-beginning 0)) ?\")
1216 (eq (char-after (match-end 0)) ?\"))
1217 (insert addr)
1218 (insert (format (muse-markup-text 'email-addr) addr addr)))
1219 (muse-publish-mark-read-only beg (point))))
1221 (defun muse-publish-classify-url (target)
1222 "Transform anchors and get published name, if TARGET is a page.
1223 The return value is a cons cell. The car is the type of link,
1224 the cadr is the page name, and the cddr is the anchor."
1225 (save-match-data
1226 (cond ((or (null target) (string= target ""))
1227 nil)
1228 ((string-match muse-url-regexp target)
1229 (cons 'url (cons target nil)))
1230 ((string-match muse-image-regexp target)
1231 (cons 'image (cons target nil)))
1232 ((string-match muse-file-regexp target)
1233 (cons 'file (cons target nil)))
1234 ((string-match "#" target)
1235 (if (eq (aref target 0) ?\#)
1236 (cons 'anchor-ref (cons nil (substring target 1)))
1237 (cons 'link-and-anchor
1238 (cons (muse-publish-link-name
1239 (substring target 0 (match-beginning 0)))
1240 (substring target (match-end 0))))))
1242 (cons 'link (cons (muse-publish-link-name target) nil))))))
1244 (defun muse-publish-url (url &optional desc explicit)
1245 "Resolve a URL into its final <a href> form."
1246 (let ((orig-url url)
1247 type anchor)
1248 (dolist (transform muse-publish-url-transforms)
1249 (setq url (save-match-data (when url (funcall transform url explicit)))))
1250 (dolist (transform muse-publish-desc-transforms)
1251 (setq desc (save-match-data
1252 (when desc (funcall transform desc explicit)))))
1253 (when desc
1254 (setq desc (muse-publish-escape-specials-in-string desc 'url-desc)))
1255 (setq orig-url
1256 (muse-publish-escape-specials-in-string orig-url 'url-desc))
1257 (let ((target (muse-publish-classify-url url)))
1258 (setq type (car target)
1259 url (muse-publish-escape-specials-in-string (cadr target) 'url)
1260 anchor (muse-publish-escape-specials-in-string
1261 (cddr target) 'url)))
1262 (cond ((eq type 'anchor-ref)
1263 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1264 ((string= url "")
1265 desc)
1266 ((eq type 'image)
1267 (if desc
1268 (muse-markup-text 'image-with-desc url desc)
1269 (muse-markup-text 'image-link url)))
1270 ((eq type 'link-and-anchor)
1271 (muse-markup-text 'link-and-anchor url anchor
1272 (or desc orig-url)))
1273 ((and desc (string-match muse-image-regexp desc))
1274 (muse-markup-text 'url-with-image url desc))
1275 ((eq type 'link)
1276 (muse-markup-text 'link url (or desc orig-url)))
1278 (muse-markup-text 'url url (or desc orig-url))))))
1280 (defun muse-publish-insert-url (url &optional desc explicit)
1281 "Resolve a URL into its final <a href> form."
1282 (delete-region (match-beginning 0) (match-end 0))
1283 (let ((text (muse-publish-url url desc explicit)))
1284 (when text
1285 (muse-insert-markup text))))
1287 (defun muse-publish-markup-link ()
1288 (let (desc explicit link)
1289 (setq explicit (save-match-data
1290 (if (string-match muse-explicit-link-regexp
1291 (match-string 0))
1292 t nil)))
1293 (setq desc (if explicit (match-string 2) (match-string 0)))
1294 (setq link (if explicit
1295 (muse-handle-explicit-link (match-string 1))
1296 (muse-handle-implicit-link (match-string 0))))
1297 (when (and link
1298 (or explicit
1299 (not (or (eq (char-before (match-beginning 0)) ?\")
1300 (eq (char-after (match-end 0)) ?\")))))
1301 (muse-publish-insert-url link desc explicit))))
1303 (defun muse-publish-markup-url ()
1304 (unless (or (eq (char-before (match-beginning 0)) ?\")
1305 (eq (char-after (match-end 0)) ?\"))
1306 (muse-publish-insert-url (match-string 0))))
1308 ;; Default publishing tags
1310 (defun muse-publish-contents-tag (beg end attrs)
1311 (set (make-local-variable 'muse-publish-generate-contents)
1312 (cons (copy-marker (point) t)
1313 (let ((depth (cdr (assoc "depth" attrs))))
1314 (or (and depth (string-to-number depth)) 2)))))
1316 (defun muse-publish-verse-tag (beg end)
1317 (save-excursion
1318 (save-restriction
1319 (narrow-to-region beg end)
1320 (goto-char (point-min))
1321 (while (eq ?\ (char-syntax (char-after)))
1322 (delete-char 1))
1323 (while (< (point) (point-max))
1324 (insert "> ")
1325 (forward-line))
1326 (if (eq ?\ (char-syntax (char-before)))
1327 (delete-char -1)))))
1329 (defun muse-publish-mark-read-only (beg end)
1330 "Add read-only properties to the given region."
1331 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1332 nil)
1334 (defun muse-publish-mark-noemphasis (&optional beg end)
1335 "Make sure that no emphasis characters are interpreted within
1336 the given region. If a region is not specified, use the 0th
1337 match data to determine it.
1339 This is usually applied to extended links."
1340 (unless beg (setq beg (match-beginning 0)))
1341 (unless end (setq end (match-end 0)))
1342 (add-text-properties beg end '(noemphasis t))
1343 nil)
1345 (defun muse-publish-quote-tag (beg end)
1346 (save-excursion
1347 (save-restriction
1348 (narrow-to-region beg end)
1349 (muse-insert-markup (muse-markup-text 'begin-quote))
1350 (muse-publish-surround-text (muse-markup-text 'begin-quote-item)
1351 (muse-markup-text 'end-quote-item)
1352 (function (lambda (indent)
1353 (muse-forward-paragraph)
1354 (goto-char (match-end 0))
1355 (< (point) (point-max)))))
1356 (muse-insert-markup (muse-markup-text 'end-quote)))))
1358 (defun muse-publish-code-tag (beg end)
1359 (muse-publish-escape-specials beg end nil 'literal)
1360 (goto-char beg)
1361 (insert (muse-markup-text 'begin-literal))
1362 (goto-char end)
1363 (insert (muse-markup-text 'end-literal))
1364 (muse-publish-mark-read-only beg (point)))
1366 (defun muse-publish-example-tag (beg end)
1367 (muse-publish-escape-specials beg end nil 'example)
1368 (goto-char beg)
1369 (insert (muse-markup-text 'begin-example))
1370 (goto-char end)
1371 (insert (muse-markup-text 'end-example))
1372 (muse-publish-mark-read-only beg (point)))
1374 (defun muse-publish-verbatim-tag (beg end)
1375 (muse-publish-escape-specials beg end nil 'verbatim)
1376 (muse-publish-mark-read-only beg end))
1378 (defalias 'muse-publish-class-tag 'ignore)
1380 (defun muse-publish-lisp-tag (beg end)
1381 (save-excursion
1382 (let ((str (muse-eval-lisp
1383 (prog1
1384 (concat "(progn "
1385 (buffer-substring-no-properties beg end)
1386 ")")
1387 (delete-region beg end)))))
1388 (set-text-properties 0 (length str) nil str)
1389 (insert str))))
1391 (defun muse-publish-command-tag (beg end attrs)
1392 (while (looking-at "\\s-*$")
1393 (forward-line))
1394 (let ((interp (cdr (assoc "interp" attrs))))
1395 (if (null interp)
1396 (shell-command
1397 (prog1
1398 (buffer-substring-no-properties (point) end)
1399 (delete-region beg end)) t)
1400 (shell-command-on-region beg end interp t t))
1401 (muse-publish-mark-read-only beg (point))))
1403 (defun muse-publish-comment-tag (beg end)
1404 (if (null muse-publish-comments-p)
1405 (delete-region beg end)
1406 (goto-char end)
1407 (muse-insert-markup (muse-markup-text 'comment-end))
1408 (muse-publish-mark-read-only beg end)
1409 (goto-char beg)
1410 (muse-insert-markup (muse-markup-text 'comment-begin))))
1412 ;; Miscellaneous helper functions
1414 (defsubst muse-publishing-directive (name)
1415 (cdr (assoc name muse-publishing-directives)))
1417 (defun muse-publish-strip-tags (string)
1418 "Remove all tags from the string."
1419 (while (string-match "<.*?>" string)
1420 (setq string (replace-match "" nil t string)))
1421 string)
1423 (defun muse-publish-markup-type (category default-func)
1424 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1425 (funcall (or rule default-func))))
1427 (defun muse-published-buffer-contents (buffer)
1428 (with-current-buffer buffer
1429 (goto-char (point-min))
1430 (let ((beg (and (search-forward "Emacs Muse begins here")
1431 (muse-line-end-position)))
1432 (end (and (search-forward "Emacs Muse ends here")
1433 (muse-line-beginning-position))))
1434 (buffer-substring-no-properties beg end))))
1436 (defun muse-published-contents (file)
1437 (when (file-readable-p file)
1438 (muse-with-temp-buffer
1439 (insert-file-contents file)
1440 (muse-published-buffer-contents (current-buffer)))))
1442 (defun muse-publish-transform-output
1443 (file temp-file output-path name gen-func &rest cleanup-exts)
1444 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1445 (setq file (muse-page-name file))
1446 (message "Generating %s output for %s..." name file)
1447 (if (not (funcall gen-func temp-file output-path))
1448 (message "Generating %s from %s...failed" name file)
1449 (message "Generating %s output for %s...done" name file)
1450 (muse-delete-file-if-exists temp-file)
1451 (dolist (ext cleanup-exts)
1452 (muse-delete-file-if-exists
1453 (expand-file-name (concat file ext)
1454 (file-name-directory output-path))))
1455 (message "Wrote %s" output-path)))
1457 (defun muse-publish-read-only (string)
1458 (let ((end (1- (length string))))
1459 (add-text-properties 0 end
1460 '(rear-nonsticky (read-only) read-only t)
1461 string)
1462 string))
1464 (provide 'muse-publish)
1466 ;;; muse-publish.el ends here