Nested list fix.
[muse-el.git] / lisp / muse-publish.el
blob7b11a7a648b0b575a52d095ae24a8fa4c9043834
1 ;;; muse-publish.el --- base publishing implementation
3 ;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 ;; This file is not part of GNU Emacs.
7 ;; This is free software; you can redistribute it and/or modify it under
8 ;; the terms of the GNU General Public License as published by the Free
9 ;; Software Foundation; either version 2, or (at your option) any later
10 ;; version.
12 ;; This is distributed in the hope that it will be useful, but WITHOUT
13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 ;; for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;; Yann Hodique (yann DOT hodique AT gmail DOT com) fixed an
27 ;; unnecessary URL description transform in `muse-publish-url'.
29 ;; Peter K. Lee (saint AT corenova DOT com) provided the
30 ;; `muse-style-elements-list' function.
32 ;; 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 1 list)
146 (2300 ,(concat "^\\(\\(?:.+?\\)[" muse-regexp-blank "]+::\n?\\)")
147 0 list)
149 (2400 ,(concat "^\\([" muse-regexp-blank "]+\\).+") 0 quote)
151 (2500 ,(concat "\\(^\\|[" muse-regexp-blank "]+\\)--\\($\\|["
152 muse-regexp-blank "]+\\)")
153 0 emdash)
155 ;; "verse" text is indicated the same way as a quoted e-mail
156 ;; response: "> text", where text may contain initial whitespace
157 ;; (see below).
158 (2600 ,(concat "^[" muse-regexp-blank "]*> ") 0 verse)
160 ;; simple table markup is supported, nothing fancy. use | to
161 ;; separate cells, || to separate header cells, and ||| for footer
162 ;; cells
163 (2700 ,(concat "\\(" muse-table-line-regexp "\n"
164 "\\(" muse-regexp-blank "*\n\\)?\\)+")
165 0 table)
167 ;; replace links in the buffer (links to other pages)
168 (2900 muse-explicit-link-regexp 0 link)
170 ;; bare URLs
171 (3000 muse-url-regexp 0 url)
173 ;; bare email addresses
174 (3500
175 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email)
177 "List of markup rules for publishing a page with Muse.
178 The rules given in this variable are invoked first, followed by
179 whatever rules are specified by the current style.
181 Each member of the list is either a function, or a list of the form:
183 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
185 REGEXP is a regular expression, or symbol whose value is a regular
186 expression, which is searched for using `re-search-forward'.
187 TEXT-BEGIN-GROUP is the matching group within that regexp which
188 denotes the beginning of the actual text to be marked up.
189 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
190 If it is not a string, but a function, it will be called to determine
191 what the replacement text should be (it must return a string). If it
192 is a symbol, the value of that symbol should be a string.
194 The replacements are done in order, one rule at a time. Writing
195 the regular expressions can be a tricky business. Note that case
196 is never ignored. `case-fold-search' is always bound to nil
197 while processing the markup rules."
198 :type '(repeat (choice
199 (list :tag "Markup rule"
200 integer
201 (choice regexp symbol)
202 integer
203 (choice string function symbol))
204 function))
205 :group 'muse-publish)
207 (defcustom muse-publish-markup-functions
208 '((directive . muse-publish-markup-directive)
209 (comment . muse-publish-markup-comment)
210 (anchor . muse-publish-markup-anchor)
211 (tag . muse-publish-markup-tag)
212 (word . muse-publish-markup-word)
213 (emdash . muse-publish-markup-emdash)
214 (enddots . muse-publish-markup-enddots)
215 (dots . muse-publish-markup-dots)
216 (rule . muse-publish-markup-rule)
217 (no-break-space . muse-publish-markup-no-break-space)
218 (heading . muse-publish-markup-heading)
219 (footnote . muse-publish-markup-footnote)
220 (fn-sep . muse-publish-markup-fn-sep)
221 (list . muse-publish-markup-list)
222 (quote . muse-publish-markup-quote)
223 (verse . muse-publish-markup-verse)
224 (table . muse-publish-markup-table)
225 (email . muse-publish-markup-email)
226 (link . muse-publish-markup-link)
227 (url . muse-publish-markup-url))
228 "An alist of style types to custom functions for that kind of text.
230 Each member of the list is of the form:
232 (SYMBOL FUNCTION)
234 SYMBOL describes the type of text to associate with this rule.
235 `muse-publish-markup-regexps' maps regexps to these symbols.
237 FUNCTION is the function to use to mark up this kind of rule if
238 no suitable function is found through the :functions tag of the
239 current style."
240 :type '(alist :key-type symbol :value-type function)
241 :group 'muse-publish)
243 (defcustom muse-publish-markup-tags
244 '(("contents" nil t muse-publish-contents-tag)
245 ("verse" t nil muse-publish-verse-tag)
246 ("example" t nil muse-publish-example-tag)
247 ("code" t nil muse-publish-code-tag)
248 ("quote" t nil muse-publish-quote-tag)
249 ("literal" t nil muse-publish-mark-read-only)
250 ("verbatim" t nil muse-publish-verbatim-tag)
251 ("lisp" t nil muse-publish-lisp-tag)
252 ("class" t t muse-publish-class-tag)
253 ("command" t t muse-publish-command-tag)
254 ("comment" t nil muse-publish-comment-tag))
255 "A list of tag specifications, for specially marking up text.
256 XML-style tags are the best way to add custom markup to Muse.
257 This is easily accomplished by customizing this list of markup tags.
259 For each entry, the name of the tag is given, whether it expects
260 a closing tag and/or an optional set of attributes, and a
261 function that performs whatever action is desired within the
262 delimited region.
264 The tags themselves are deleted during publishing, before the
265 function is called. The function is called with three arguments,
266 the beginning and end of the region surrounded by the tags. If
267 properties are allowed, they are passed as a third argument in
268 the form of an alist. The `end' argument to the function is
269 always a marker.
271 Point is always at the beginning of the region within the tags, when
272 the function is called. Wherever point is when the function finishes
273 is where tag markup will resume.
275 These tag rules are processed once at the beginning of markup, and
276 once at the end, to catch any tags which may have been inserted
277 in-between."
278 :type '(repeat (list (string :tag "Markup tag")
279 (boolean :tag "Expect closing tag" :value t)
280 (boolean :tag "Parse attributes" :value nil)
281 function))
282 :group 'muse-publish)
284 (defcustom muse-publish-markup-specials nil
285 "A table of characters which must be represented specially."
286 :type '(alist :key-type character :value-type string)
287 :group 'muse-publish)
289 (defvar muse-publishing-p nil
290 "Set to t while a page is being published.")
291 (defvar muse-batch-publishing-p nil
292 "Set to t while a page is being batch published.")
293 (defvar muse-publishing-styles nil
294 "The publishing styles that Muse recognizes.
295 This is automatically generated when loading publishing styles.")
296 (defvar muse-publishing-current-file nil
297 "The file that is currently being published.")
298 (defvar muse-publishing-current-output-path nil
299 "The path where the current file will be published to.")
300 (defvar muse-publishing-current-style nil
301 "The style of the file that is currently being published.")
302 (defvar muse-publishing-directives nil
303 "An alist of publishing directives from the top of a file.")
304 (defvar muse-publish-generate-contents nil
305 "Non-nil if a table of contents should be generated.
306 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
307 tell where the <contents> was seen, and to what depth the
308 contents were requested.")
309 (defvar muse-publishing-last-position nil
310 "Last position of the point when publishing.
311 This is used to make sure that publishing doesn't get stalled.")
313 ;; Functions for handling style information
315 (defsubst muse-style (&optional style)
316 "Resolve the given STYLE into a Muse style, if it is a string."
317 (if (null style)
318 muse-publishing-current-style
319 (if (stringp style)
320 (assoc style muse-publishing-styles)
321 (muse-assert (consp style))
322 style)))
324 (defun muse-define-style (name &rest elements)
325 (let ((entry (assoc name muse-publishing-styles)))
326 (if entry
327 (error "There is already a style named %s." name)
328 (setq muse-publishing-styles
329 (cons (append (list name) elements)
330 muse-publishing-styles)))))
332 (defun muse-derive-style (new-name base-name &rest elements)
333 (let ((entry (assoc new-name muse-publishing-styles)))
334 (if entry
335 (error "There is already a style named %s." new-name)
336 (apply 'muse-define-style new-name
337 (append elements (list :base base-name))))))
339 (defsubst muse-get-keyword (keyword list &optional direct)
340 (let ((value (cadr (memq keyword list))))
341 (if (and (not direct) (symbolp value))
342 (symbol-value value)
343 value)))
345 (defun muse-style-elements-list (elem &optional style)
346 "Return a list all references to ELEM in STYLE, including base styles.
347 If STYLE is not specified, use current style."
348 (let (base elements)
349 (while style
350 (setq style (muse-style style))
351 (setq elements (append elements
352 (muse-get-keyword elem style)))
353 (setq style (muse-get-keyword :base style)))
354 elements))
356 (defsubst muse-style-element (elem &optional style direct)
357 "Search for ELEM in STYLE, including base styles.
358 If STYLE is not specified, use current style."
359 (setq style (muse-style style))
360 (let ((value (muse-get-keyword elem style direct)))
361 (if value
362 value
363 (let ((base (muse-get-keyword :base style)))
364 (if base
365 (muse-style-element elem base direct))))))
367 (defun muse-find-markup-element (keyword ident style)
368 (let ((def (assq ident (muse-style-element keyword style))))
369 (if def
370 (cdr def)
371 (let ((base (muse-style-element :base style)))
372 (if base
373 (muse-find-markup-element keyword ident base))))))
375 (defun muse-markup-text (ident &rest args)
376 "Insert ARGS into the text markup associated with IDENT.
377 If the markup text has sections like %N%, this will be replaced
378 with the N-1th argument in ARGS. After that, `format' is applied
379 to the text with ARGS as parameters."
380 (let ((text (muse-find-markup-element :strings ident (muse-style))))
381 (if (and text args)
382 (progn
383 (let (start repl-text)
384 (while (setq start (string-match "%\\([1-9][0-9]*\\)%" text start))
385 ;; escape '%' in the argument text, since we will be
386 ;; using format on it
387 (setq repl-text (muse-replace-regexp-in-string
388 "%" "%%"
389 (nth (1- (string-to-number
390 (match-string 1 text))) args)
391 t t)
392 start (+ start (length repl-text))
393 text (replace-match repl-text t t text))))
394 (apply 'format text args))
395 (or text ""))))
397 (defun muse-insert-markup (&rest args)
398 (let ((beg (point)))
399 (apply 'insert args)
400 (muse-publish-mark-read-only beg (point))))
402 (defun muse-find-markup-tag (keyword tagname style)
403 (let ((def (assoc tagname (muse-style-element keyword style))))
404 (or def
405 (let ((base (muse-style-element :base style)))
406 (if base
407 (muse-find-markup-tag keyword tagname base))))))
409 (defsubst muse-markup-tag-info (tagname &rest args)
410 (let ((tag-info (muse-find-markup-tag :tags tagname (muse-style))))
411 (or tag-info
412 (assoc (match-string 1) muse-publish-markup-tags))))
414 (defsubst muse-markup-function (category)
415 (let ((func (muse-find-markup-element :functions category (muse-style))))
416 (or func
417 (cdr (assq category muse-publish-markup-functions)))))
419 ;; Publishing routines
421 (defun muse-publish-markup (name rules)
422 (let* ((case-fold-search nil)
423 (inhibit-read-only t)
424 (limit (* (length rules) (point-max)))
425 (verbose (and muse-publish-report-threshhold
426 (> (point-max) muse-publish-report-threshhold)))
427 (base 0))
428 (while rules
429 (goto-char (point-min))
430 (let ((regexp (nth 1 (car rules)))
431 (group (nth 2 (car rules)))
432 (repl (nth 3 (car rules)))
433 pos)
434 (setq muse-publishing-last-position nil)
435 (if (symbolp regexp)
436 (setq regexp (symbol-value regexp)))
437 (if (and verbose (not muse-batch-publishing-p))
438 (message "Publishing %s...%d%%" name
439 (* (/ (float (+ (point) base)) limit) 100)))
440 (while (and regexp (setq pos (re-search-forward regexp nil t)))
441 (if (and verbose (not muse-batch-publishing-p))
442 (message "Publishing %s...%d%%" name
443 (* (/ (float (+ (point) base)) limit) 100)))
444 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
445 (get-text-property (match-beginning group) 'read-only))
446 (let* (func
447 (text (cond
448 ((and (symbolp repl)
449 (setq func (muse-markup-function repl)))
450 (funcall func))
451 ((functionp repl)
452 (funcall repl))
453 ((symbolp repl)
454 (symbol-value repl))
455 (t repl))))
456 (if (stringp text)
457 (replace-match text t))))
458 (if (and muse-publishing-last-position
459 (= pos muse-publishing-last-position))
460 (if (eobp)
461 (setq regexp nil)
462 (forward-char 1)))
463 (setq muse-publishing-last-position pos)))
464 (setq rules (cdr rules)
465 base (+ base (point-max))))
466 (if (and verbose (not muse-batch-publishing-p))
467 (message "Publishing %s...done" name))))
469 (defun muse-insert-file-or-string (file-or-string &optional title)
470 (let ((beg (point)) end)
471 (if (and (not (string-equal file-or-string ""))
472 (file-readable-p file-or-string))
473 (setq end (+ beg (cadr (insert-file-contents file-or-string))))
474 (insert file-or-string)
475 (setq end (point)))
476 (save-restriction
477 (narrow-to-region beg end)
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 (when (or force (file-newer-than-file-p file target))
609 (if (and muse-publish-report-threshhold
610 (> (nth 7 (file-attributes file))
611 muse-publish-report-threshhold))
612 (message "Publishing %s ..." file))
613 (muse-with-temp-buffer
614 (insert-file-contents file)
615 (muse-publish-markup-buffer (muse-page-name file) style)
616 (let ((backup-inhibited t))
617 (write-file output-path))
618 (muse-style-run-hooks :final style file output-path target))
619 t)))
621 ;;;###autoload
622 (defun muse-publish-this-file (style output-dir &optional force)
623 "Publish the page in the current file."
624 (interactive (muse-publish-get-info))
625 (unless (muse-publish-file buffer-file-name style output-dir force)
626 (message (concat "The published version is up-to-date; use"
627 " C-u C-c C-t to force an update."))))
629 (defun muse-batch-publish-files ()
630 "Publish Muse files in batch mode."
631 (let ((muse-batch-publishing-p t)
632 style-name style output-dir)
633 (setq style-name (car command-line-args-left)
634 style (muse-style style-name)
635 command-line-args-left (cdr command-line-args-left)
636 output-dir (car command-line-args-left)
637 output-dir
638 (if (string-match "\\`--output-dir=" output-dir)
639 (prog1
640 (substring output-dir (match-end 0))
641 (setq command-line-args-left (cdr command-line-args-left)))))
642 (unless style
643 (error "There is no style '%s' defined." style-name))
644 (dolist (file command-line-args-left)
645 (muse-publish-file file style output-dir t))))
647 ;; Default publishing rules
649 (defun muse-publish-section-close (depth)
650 "Seach forward for the closing tag of given DEPTH."
651 (let (not-end)
652 (save-excursion
653 (while (and (setq not-end (re-search-forward
654 (concat "^\\*\\{1," (number-to-string depth)
655 "\\}\\s-+")
656 nil t))
657 (get-text-property (match-beginning 0) 'read-only)))
658 (if not-end
659 (forward-line 0)
660 (goto-char (point-max)))
661 (cond ((not (eq (char-before) ?\n))
662 (insert "\n\n"))
663 ((not (eq (char-before (1- (point))) ?\n))
664 (insert "\n")))
665 (muse-insert-markup (muse-markup-text 'section-close depth))
666 (insert "\n"))))
668 (defun muse-publish-markup-directive (&optional name value)
669 (unless name (setq name (match-string 1)))
670 (unless value (setq value (match-string 2)))
671 (let ((elem (assoc name muse-publishing-directives)))
672 (if elem
673 (setcdr elem value)
674 (setq muse-publishing-directives
675 (cons (cons name value)
676 muse-publishing-directives))))
677 ;; Make sure we don't ever try to move the point forward (past the
678 ;; beginning of buffer) while we're still searching for directives.
679 (setq muse-publishing-last-position nil)
680 (delete-region (match-beginning 0) (match-end 0)))
682 (defun muse-publish-markup-anchor ()
683 (unless (get-text-property (match-end 1) 'noemphasis)
684 (let ((text (muse-markup-text 'anchor (match-string 2))))
685 (unless (string= text "")
686 (save-match-data
687 (skip-chars-forward (concat muse-regexp-blank "\n"))
688 (muse-insert-markup text)))
689 (match-string 1))))
691 (defun muse-publish-markup-comment ()
692 (if (null muse-publish-comments-p)
694 (goto-char (match-end 0))
695 (muse-insert-markup (muse-markup-text 'comment-end))
696 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
697 (delete-region (match-beginning 0) (match-beginning 1))
698 (goto-char (match-beginning 0))
699 (muse-insert-markup (muse-markup-text 'comment-begin))))
701 (defun muse-publish-markup-tag ()
702 (let ((tag-info (muse-markup-tag-info (match-string 1))))
703 (when (and tag-info
704 (not (get-text-property (match-beginning 0) 'read-only)))
705 (let ((closed-tag (match-string 3))
706 (start (match-beginning 0))
707 (beg (point))
708 end attrs)
709 (when (nth 2 tag-info)
710 (let ((attrstr (match-string 2)))
711 (while (and attrstr
712 (string-match (concat "\\([^"
713 muse-regexp-blank
714 "=\n]+\\)\\(=\"\\"
715 "([^\"]+\\)\"\\)?")
716 attrstr))
717 (let ((attr (cons (downcase
718 (muse-match-string-no-properties 1 attrstr))
719 (muse-match-string-no-properties 3 attrstr))))
720 (setq attrstr (replace-match "" t t attrstr))
721 (if attrs
722 (nconc attrs (list attr))
723 (setq attrs (list attr)))))))
724 (if (and (cadr tag-info) (not closed-tag))
725 (if (search-forward (concat "</" (car tag-info) ">") nil t)
726 (delete-region (match-beginning 0) (point))
727 (setq tag-info nil)))
728 (when tag-info
729 (setq end (point-marker))
730 (delete-region start beg)
731 (goto-char start)
732 (let ((args (list start end)))
733 (if (nth 2 tag-info)
734 (nconc args (list attrs)))
735 (apply (nth 3 tag-info) args))))))
736 nil)
738 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
739 "Escape specials from BEG to END using style-specific :specials.
740 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
741 CONTEXT is used to figure out what kind of specials to escape.
743 The following contexts exist in Muse.
744 'underline _underlined text_
745 'literal =monospaced text= or <code>region</code> (monospaced, escaped)
746 'emphasis *emphasized text*
747 'email email@example.com
748 'url http://example.com
749 'url-desc [[...][description of an extended link]]
750 'example <example>region</example> (monospaced, block context, escaped)
751 'verbatim <verbatim>region</verbatim> (escaped)
752 'document normal text"
753 (let ((specials (muse-style-element :specials nil t)))
754 (cond ((functionp specials)
755 (setq specials (funcall specials context)))
756 ((symbolp specials)
757 (setq specials (symbol-value specials))))
758 (save-excursion
759 (save-restriction
760 (narrow-to-region beg end)
761 (goto-char (point-min))
762 (while (< (point) (point-max))
763 (if (and (not ignore-read-only)
764 (get-text-property (point) 'read-only))
765 (goto-char (or (next-single-property-change (point) 'read-only)
766 (point-max)))
767 (let ((repl (or (assoc (char-after) specials)
768 (assoc (char-after)
769 muse-publish-markup-specials))))
770 (if (null repl)
771 (forward-char 1)
772 (delete-char 1)
773 (insert-before-markers (cdr repl))))))))))
775 (defun muse-publish-markup-word ()
776 (let* ((beg (match-beginning 2))
777 (end (1- (match-end 2)))
778 (leader (buffer-substring-no-properties beg end))
779 open-tag close-tag mark-read-only loc context)
780 (cond
781 ((string= leader "_")
782 (setq context 'underline
783 open-tag (muse-markup-text 'begin-underline)
784 close-tag (muse-markup-text 'end-underline)))
785 ((string= leader "=")
786 (setq context 'literal
787 open-tag (muse-markup-text 'begin-literal)
788 close-tag (muse-markup-text 'end-literal))
789 (setq mark-read-only t))
791 (let ((l (length leader)))
792 (setq context 'emphasis)
793 (cond
794 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
795 close-tag (muse-markup-text 'end-emph)))
796 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
797 close-tag (muse-markup-text 'end-more-emph)))
798 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
799 close-tag (muse-markup-text 'end-most-emph)))
800 (t (setq context nil))))))
801 (if (and context
802 (not (get-text-property beg 'noemphasis))
803 (setq loc (search-forward leader nil t))
804 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
805 (not (eq (char-syntax (char-before (point))) ?\ ))
806 (not (get-text-property (point) 'noemphasis)))
807 (progn
808 (replace-match "")
809 (delete-region beg end)
810 (setq end (point-marker))
811 (muse-insert-markup close-tag)
812 (save-excursion
813 (goto-char beg)
814 (muse-insert-markup open-tag)
815 (setq beg (point)))
816 (when mark-read-only
817 (muse-publish-escape-specials beg end t context)
818 (muse-publish-mark-read-only beg end)))
819 (backward-char))
820 nil))
822 (defun muse-publish-markup-emdash ()
823 (delete-region (match-beginning 0) (match-end 0))
824 (muse-insert-markup (muse-markup-text 'emdash))
825 (if (eq (char-after) ?\<)
826 (insert ?\n)))
828 (defun muse-publish-markup-enddots ()
829 (unless (get-text-property (match-beginning 0) 'noemphasis)
830 (delete-region (match-beginning 0) (match-end 0))
831 (muse-insert-markup (muse-markup-text 'enddots))))
833 (defun muse-publish-markup-dots ()
834 (unless (get-text-property (match-beginning 0) 'noemphasis)
835 (delete-region (match-beginning 0) (match-end 0))
836 (muse-insert-markup (muse-markup-text 'dots))))
838 (defun muse-publish-markup-rule ()
839 (unless (get-text-property (match-beginning 0) 'noemphasis)
840 (delete-region (match-beginning 0) (match-end 0))
841 (muse-insert-markup (muse-markup-text 'rule))))
843 (defun muse-publish-markup-no-break-space ()
844 (unless (get-text-property (match-beginning 0) 'noemphasis)
845 (delete-region (match-beginning 0) (match-end 0))
846 (muse-insert-markup (muse-markup-text 'no-break-space))))
848 (defun muse-publish-markup-heading ()
849 (let* ((len (length (match-string 1)))
850 (start (muse-markup-text
851 (cond ((= len 1) 'section)
852 ((= len 2) 'subsection)
853 ((= len 3) 'subsubsection)
854 (t 'section-other))
855 len))
856 (end (muse-markup-text
857 (cond ((= len 1) 'section-end)
858 ((= len 2) 'subsection-end)
859 ((= len 3) 'subsubsection-end)
860 (t 'section-other-end))
861 len)))
862 (delete-region (match-beginning 0) (match-end 0))
863 (muse-insert-markup start)
864 (end-of-line)
865 (when end
866 (muse-insert-markup end))
867 (muse-publish-section-close len)))
869 (defvar muse-publish-footnotes nil)
871 (defun muse-publish-markup-footnote ()
872 "Scan ahead and snarf up the footnote body"
873 (cond
874 ((get-text-property (match-beginning 0) 'noemphasis)
875 nil)
876 ((= (muse-line-beginning-position) (match-beginning 0))
879 (let ((footnote (save-match-data
880 (string-to-number (match-string 1))))
881 footnotemark)
882 (delete-region (match-beginning 0) (match-end 0))
883 (save-excursion
884 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
885 (let* ((start (match-beginning 0))
886 (beg (goto-char (match-end 0)))
887 (end (save-excursion
888 (if (search-forward "\n\n" nil t)
889 (copy-marker (match-beginning 0))
890 (goto-char (point-max))
891 (skip-chars-backward "\n")
892 (point-marker)))))
893 (while (re-search-forward
894 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
895 end t)
896 (replace-match "\\1" t))
897 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
898 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
899 (if (string= "" footnotemark-cmd)
900 (setq footnotemark
901 (concat (muse-markup-text 'footnote)
902 (buffer-substring-no-properties beg end)
903 (muse-markup-text 'footnote-end)))
904 (setq footnotemark (format footnotemark-cmd footnote
905 footnotemark-end-cmd))
906 (unless muse-publish-footnotes
907 (set (make-local-variable 'muse-publish-footnotes)
908 (make-vector 256 nil)))
909 (unless (aref muse-publish-footnotes footnote)
910 (setq footnotemark
911 (concat
912 footnotemark
913 (concat (format (muse-markup-text 'footnotetext)
914 footnote)
915 (buffer-substring-no-properties beg end)
916 (muse-markup-text 'footnotetext-end))))
917 (aset muse-publish-footnotes footnote footnotemark))))
918 (goto-char end)
919 (skip-chars-forward "\n")
920 (delete-region start (point)))))
921 (muse-insert-markup (or footnotemark footnote))))))
923 (defun muse-publish-markup-fn-sep ()
924 (delete-region (match-beginning 0) (match-end 0))
925 (muse-insert-markup (muse-markup-text 'fn-sep)))
927 (defun muse-insert-markup-end-list (&rest args)
928 (let ((beg (point)))
929 (apply 'insert args)
930 (add-text-properties beg (point) '(end-list t))
931 (muse-publish-mark-read-only beg (point))))
933 (defun muse-publish-surround-dl (move-func &optional indent post-indent)
934 (unless indent
935 (setq indent (concat "[" muse-regexp-blank "]+")))
936 (when post-indent
937 (setq post-indent (concat " \\{0," (number-to-string post-indent) "\\}")))
938 (let ((continue t)
939 (beg-item (muse-markup-text 'begin-item))
940 (end-item (muse-markup-text 'end-item))
941 (beg-dde (muse-markup-text 'begin-dde))
942 (end-dde (muse-markup-text 'end-dde))
943 beg)
944 (while continue
945 (muse-insert-markup beg-tag)
946 (setq beg (point)
947 continue (funcall move-func))
948 (save-restriction
949 (narrow-to-region beg (point))
950 (goto-char (point-min))
951 (while (< (point) (point-max))
952 (when (looking-at indent)
953 (replace-match ""))
954 (forward-line 1))
955 (skip-chars-backward (concat muse-regexp-blank "\n"))
956 (muse-insert-markup end-tag)
957 (when continue
958 (goto-char (point-max)))))))
960 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent)
961 (unless indent
962 (setq indent (concat "[" muse-regexp-blank "]+")))
963 (when post-indent
964 (setq post-indent (concat indent " \\{0," (number-to-string post-indent)
965 "\\}")))
966 (let ((continue t)
967 (list-item (format muse-list-item-regexp
968 (concat "[" muse-regexp-blank "]+")))
969 beg)
970 (while continue
971 (muse-insert-markup beg-tag)
972 (setq beg (point)
973 continue (funcall move-func))
974 (save-restriction
975 (narrow-to-region beg (point))
976 (goto-char (point-min))
977 (while (< (point) (point-max))
978 (when (and (not (looking-at list-item))
979 (looking-at post-indent))
980 (replace-match ""))
981 (forward-line 1))
982 (skip-chars-backward (concat muse-regexp-blank "\n"))
983 (muse-insert-markup-end-list end-tag)
984 (when continue
985 (goto-char (point-max)))))))
987 (defun muse-list-item-type (str)
988 "Determine the type of list given STR.
989 Returns either 'ul, 'ol, or 'dl."
990 (cond ((= (aref str 0) ?-) 'ul)
991 ((save-match-data
992 (string-match "\\`[0-9]+\\." str))
993 'ol)
994 (t 'dl)))
996 (defsubst muse-forward-paragraph (&optional pattern)
997 (if (re-search-forward (if pattern
998 (concat "^\\(" pattern "\\|\n\\|\\'\\)")
999 "^\\s-*\\(\n\\|\\'\\)") nil t)
1000 (goto-char (match-beginning 0))
1001 (goto-char (point-max))))
1003 (defun muse-forward-list-item (type indent)
1004 "Move forward to the next item of TYPE.
1005 Return non-nil if successful, nil otherwise.
1006 The beginning indentation is given by INDENT."
1007 (let ((list-item (format muse-list-item-regexp indent))
1008 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
1009 (next-list-end (or (next-single-property-change (point) 'end-list)
1010 (point-max))))
1011 (muse-forward-paragraph (concat "\\(?:" empty-line "\\)?"
1012 "\\(" list-item "\\)"))
1013 (cond ((> (point) next-list-end)
1014 (goto-char next-list-end)
1015 nil)
1016 ((and (match-string 3)
1017 (eq type (muse-list-item-type (match-string 3))))
1018 (unless (eq type 'dl)
1019 (replace-match "" t t nil 3))
1020 (goto-char (match-beginning 2)))
1021 (t nil))))
1023 (defun muse-publish-markup-list ()
1024 "Markup a list entry.
1025 The reason this function is so funky, is to prevent text properties
1026 like read-only from being inadvertently deleted."
1027 (let* ((str (match-string 1))
1028 (type (muse-list-item-type str))
1029 (indent (buffer-substring (muse-line-beginning-position)
1030 (match-beginning 1)))
1031 (post-indent (length str))
1032 (last (match-beginning 0)))
1033 (cond
1034 ((eq type 'ul)
1035 (unless (eq (char-after (match-end 1)) ?-)
1036 (delete-region (match-beginning 0) (match-end 0))
1037 (muse-insert-markup (muse-markup-text 'begin-uli))
1038 (save-excursion
1039 (muse-publish-surround-text
1040 (muse-markup-text 'begin-uli-item)
1041 (muse-markup-text 'end-uli-item)
1042 (function (lambda ()
1043 (muse-forward-list-item type indent)))
1044 indent post-indent)
1045 (muse-insert-markup (muse-markup-text 'end-uli)))
1046 (forward-line 1)))
1047 ((eq type 'ol)
1048 (delete-region (match-beginning 0) (match-end 0))
1049 (muse-insert-markup (muse-markup-text 'begin-oli))
1050 (save-excursion
1051 (muse-publish-surround-text
1052 (muse-markup-text 'begin-oli-item)
1053 (muse-markup-text 'end-oli-item)
1054 (function (lambda ()
1055 (muse-forward-list-item type indent)))
1056 indent post-indent)
1057 (muse-insert-markup (muse-markup-text 'end-oli)))
1058 (forward-line 1))
1060 (goto-char (match-beginning 1))
1061 (muse-insert-markup (muse-markup-text 'begin-ddt))
1062 (save-match-data
1063 (save-excursion
1064 (forward-line 1)
1065 (while (looking-at (concat "^\\([" muse-regexp-blank
1066 "]*\\)[^" muse-regexp-blank "\n]"))
1067 (delete-region (match-beginning 1) (match-end 1))
1068 (forward-line 1))))
1069 (save-match-data
1070 (when (re-search-forward (concat "[" muse-regexp-blank "\n]+::["
1071 muse-regexp-blank "\n]+")
1072 nil t)
1073 (replace-match "")
1074 (muse-insert-markup (muse-markup-text 'start-dde))))
1075 (muse-forward-paragraph)
1076 (muse-insert-markup (muse-markup-text 'end-ddt) ?\n))))
1077 nil)
1079 (defun muse-publish-markup-quote ()
1080 "Markup a quoted paragraph.
1081 The reason this function is so funky, is to prevent text properties
1082 like read-only from being inadvertently deleted."
1083 (let* ((ws (match-string 1))
1084 (centered (>= (string-width ws) 6))
1085 (begin-elem (if centered 'begin-center 'begin-quote-item))
1086 (end-elem (if centered 'end-center 'end-quote-item)))
1087 (replace-match "" t t nil 1)
1088 (unless centered
1089 (muse-insert-markup (muse-markup-text 'begin-quote)))
1090 (muse-publish-surround-text (muse-markup-text begin-elem)
1091 (muse-markup-text end-elem)
1092 (function (lambda ()
1093 (muse-forward-paragraph)
1094 nil)))
1095 (unless centered
1096 (muse-insert-markup (muse-markup-text 'end-quote)))))
1098 (defun muse-publish-markup-leading-space (markup-space multiple)
1099 (let (count)
1100 (when (and markup-space
1101 (>= (setq count (skip-chars-forward " ")) 0))
1102 (delete-region (muse-line-beginning-position) (point))
1103 (while (> count 0)
1104 (muse-insert-markup markup-space)
1105 (setq count (- count multiple))))))
1107 (defun muse-publish-markup-verse ()
1108 (let ((leader (match-string 0)))
1109 (goto-char (match-beginning 0))
1110 (muse-insert-markup (muse-markup-text 'begin-verse))
1111 (while (looking-at leader)
1112 (replace-match "")
1113 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1114 (let ((beg (point)))
1115 (end-of-line)
1116 (cond
1117 ((bolp)
1118 (let ((text (muse-markup-text 'empty-verse-line)))
1119 (when text (muse-insert-markup text))))
1120 ((save-excursion
1121 (save-match-data
1122 (forward-line 1)
1123 (or (looking-at (concat leader "["
1124 muse-regexp-blank
1125 "]*$"))
1126 (not (looking-at leader)))))
1127 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1128 (end-text (muse-markup-text 'end-last-stanza-line)))
1129 (when end-text (muse-insert-markup end-text))
1130 (goto-char beg)
1131 (when begin-text (muse-insert-markup begin-text))
1132 (end-of-line)))
1134 (let ((begin-text (muse-markup-text 'begin-verse-line))
1135 (end-text (muse-markup-text 'end-verse-line)))
1136 (when end-text (muse-insert-markup end-text))
1137 (goto-char beg)
1138 (when begin-text (muse-insert-markup begin-text))
1139 (end-of-line))))
1140 (forward-line 1))))
1141 (muse-insert-markup (muse-markup-text 'end-verse) ?\n))
1143 (defun muse-publish-table-fields (beg end)
1144 "Parse given region as a table, returning a cons cell.
1145 The car is the length of the longest row.
1147 The cdr is a list of the fields of the table, with the first
1148 element indicating the type of the row:
1149 1: body, 2: header, 3: footer.
1151 The existing region will be removed."
1152 (let ((longest 0)
1153 (left 0)
1154 fields field-list)
1155 (save-restriction
1156 (narrow-to-region beg end)
1157 (goto-char (point-min))
1158 (while (= left 0)
1159 (when (looking-at muse-table-line-regexp)
1160 (setq fields (cons (length (match-string 1))
1161 (split-string (match-string 0)
1162 muse-table-field-regexp))
1163 field-list (cons fields field-list)
1164 longest (max (length fields) longest)))
1165 (setq left (forward-line 1))))
1166 (delete-region beg end)
1167 (if (= longest 0)
1168 (cons 0 nil)
1169 (cons (1- longest) (nreverse field-list)))))
1171 (defun muse-publish-markup-table ()
1172 "Style does not support tables.")
1174 (defun muse-publish-escape-specials-in-string (string &optional context)
1175 "Escape specials in STRING using style-specific :specials.
1176 CONTEXT is used to figure out what kind of specials to escape.
1178 See the documentation of the `muse-publish-escape-specials'
1179 function for the list of available contexts."
1180 (unless string
1181 (setq string ""))
1182 (let ((specials (muse-style-element :specials nil t)))
1183 (cond ((functionp specials)
1184 (setq specials (funcall specials context)))
1185 ((symbolp specials)
1186 (setq specials (symbol-value specials))))
1187 (if (functionp specials)
1188 (funcall specials string)
1189 (apply (function concat)
1190 (mapcar
1191 (lambda (ch)
1192 (let (repl)
1193 (setq repl (or (assoc ch specials)
1194 (assoc ch muse-publish-markup-specials)))
1195 (if (null repl)
1196 (char-to-string ch)
1197 (cdr repl))))
1198 (append string nil))))))
1200 (defun muse-publish-markup-email ()
1201 (let* ((beg (match-end 1))
1202 (addr (buffer-substring-no-properties beg (match-end 0))))
1203 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1204 (goto-char beg)
1205 (delete-region beg (match-end 0))
1206 (if (or (eq (char-before (match-beginning 0)) ?\")
1207 (eq (char-after (match-end 0)) ?\"))
1208 (insert addr)
1209 (insert (format (muse-markup-text 'email-addr) addr addr)))
1210 (muse-publish-mark-read-only beg (point))))
1212 (defun muse-publish-classify-url (target)
1213 "Transform anchors and get published name, if TARGET is a page.
1214 The return value is a cons cell. The car is the type of link,
1215 the cadr is the page name, and the cddr is the anchor."
1216 (save-match-data
1217 (cond ((or (null target) (string= target ""))
1218 nil)
1219 ((string-match muse-url-regexp target)
1220 (cons 'url (cons target nil)))
1221 ((string-match muse-image-regexp target)
1222 (cons 'image (cons target nil)))
1223 ((string-match muse-file-regexp target)
1224 (cons 'file (cons target nil)))
1225 ((string-match "#" target)
1226 (if (eq (aref target 0) ?\#)
1227 (cons 'anchor-ref (cons nil (substring target 1)))
1228 (cons 'link-and-anchor
1229 (cons (muse-publish-link-name
1230 (substring target 0 (match-beginning 0)))
1231 (substring target (match-end 0))))))
1233 (cons 'link (cons (muse-publish-link-name target) nil))))))
1235 (defun muse-publish-url (url &optional desc explicit)
1236 "Resolve a URL into its final <a href> form."
1237 (let ((orig-url url)
1238 type anchor)
1239 (dolist (transform muse-publish-url-transforms)
1240 (setq url (save-match-data (when url (funcall transform url explicit)))))
1241 (dolist (transform muse-publish-desc-transforms)
1242 (setq desc (save-match-data
1243 (when desc (funcall transform desc explicit)))))
1244 (when desc
1245 (setq desc (muse-publish-escape-specials-in-string desc 'url-desc)))
1246 (setq orig-url
1247 (muse-publish-escape-specials-in-string orig-url 'url-desc))
1248 (let ((target (muse-publish-classify-url url)))
1249 (setq type (car target)
1250 url (muse-publish-escape-specials-in-string (cadr target) 'url)
1251 anchor (muse-publish-escape-specials-in-string
1252 (cddr target) 'url)))
1253 (cond ((eq type 'anchor-ref)
1254 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1255 ((string= url "")
1256 desc)
1257 ((eq type 'image)
1258 (if desc
1259 (muse-markup-text 'image-with-desc url desc)
1260 (muse-markup-text 'image-link url)))
1261 ((eq type 'link-and-anchor)
1262 (muse-markup-text 'link-and-anchor url anchor
1263 (or desc orig-url)))
1264 ((and desc (string-match muse-image-regexp desc))
1265 (muse-markup-text 'url-with-image url desc))
1266 ((eq type 'link)
1267 (muse-markup-text 'link url (or desc orig-url)))
1269 (muse-markup-text 'url url (or desc orig-url))))))
1271 (defun muse-publish-insert-url (url &optional desc explicit)
1272 "Resolve a URL into its final <a href> form."
1273 (delete-region (match-beginning 0) (match-end 0))
1274 (let ((text (muse-publish-url url desc explicit)))
1275 (when text
1276 (muse-insert-markup text))))
1278 (defun muse-publish-markup-link ()
1279 (let (desc explicit link)
1280 (setq explicit (save-match-data
1281 (if (string-match muse-explicit-link-regexp
1282 (match-string 0))
1283 t nil)))
1284 (setq desc (if explicit (match-string 2) (match-string 0)))
1285 (setq link (if explicit
1286 (muse-handle-explicit-link (match-string 1))
1287 (muse-handle-implicit-link (match-string 0))))
1288 (when (and link
1289 (or explicit
1290 (not (or (eq (char-before (match-beginning 0)) ?\")
1291 (eq (char-after (match-end 0)) ?\")))))
1292 (muse-publish-insert-url link desc explicit))))
1294 (defun muse-publish-markup-url ()
1295 (unless (or (eq (char-before (match-beginning 0)) ?\")
1296 (eq (char-after (match-end 0)) ?\"))
1297 (muse-publish-insert-url (match-string 0))))
1299 ;; Default publishing tags
1301 (defun muse-publish-contents-tag (beg end attrs)
1302 (set (make-local-variable 'muse-publish-generate-contents)
1303 (cons (copy-marker (point) t)
1304 (let ((depth (cdr (assoc "depth" attrs))))
1305 (or (and depth (string-to-number depth)) 2)))))
1307 (defun muse-publish-verse-tag (beg end)
1308 (save-excursion
1309 (save-restriction
1310 (narrow-to-region beg end)
1311 (goto-char (point-min))
1312 (while (eq ?\ (char-syntax (char-after)))
1313 (delete-char 1))
1314 (while (< (point) (point-max))
1315 (insert "> ")
1316 (forward-line))
1317 (if (eq ?\ (char-syntax (char-before)))
1318 (delete-char -1)))))
1320 (defun muse-publish-mark-read-only (beg end)
1321 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1322 nil)
1324 (defun muse-publish-mark-noemphasis (&optional beg end)
1325 (unless beg (setq beg (match-beginning 0)))
1326 (unless end (setq end (match-end 0)))
1327 (add-text-properties beg end '(noemphasis t))
1328 nil)
1330 (defun muse-publish-quote-tag (beg end)
1331 (save-excursion
1332 (save-restriction
1333 (narrow-to-region beg end)
1334 (muse-insert-markup (muse-markup-text 'begin-quote))
1335 (muse-publish-surround-text (muse-markup-text 'begin-quote-item)
1336 (muse-markup-text 'end-quote-item)
1337 (function (lambda ()
1338 (muse-forward-paragraph)
1339 (goto-char (match-end 0))
1340 (< (point) (point-max)))))
1341 (muse-insert-markup (muse-markup-text 'end-quote)))))
1343 (defun muse-publish-code-tag (beg end)
1344 (muse-publish-escape-specials beg end nil 'literal)
1345 (goto-char beg)
1346 (insert (muse-markup-text 'begin-literal))
1347 (goto-char end)
1348 (insert (muse-markup-text 'end-literal))
1349 (muse-publish-mark-read-only beg (point)))
1351 (defun muse-publish-example-tag (beg end)
1352 (muse-publish-escape-specials beg end nil 'example)
1353 (goto-char beg)
1354 (insert (muse-markup-text 'begin-example))
1355 (goto-char end)
1356 (insert (muse-markup-text 'end-example))
1357 (muse-publish-mark-read-only beg (point)))
1359 (defun muse-publish-verbatim-tag (beg end)
1360 (muse-publish-escape-specials beg end nil 'verbatim)
1361 (muse-publish-mark-read-only beg end))
1363 (defalias 'muse-publish-class-tag 'ignore)
1365 (defun muse-publish-lisp-tag (beg end)
1366 (save-excursion
1367 (let ((str (muse-eval-lisp
1368 (prog1
1369 (buffer-substring-no-properties beg end)
1370 (delete-region beg end)))))
1371 (set-text-properties 0 (length str) nil str)
1372 (insert str))))
1374 (defun muse-publish-command-tag (beg end attrs)
1375 (while (looking-at "\\s-*$")
1376 (forward-line))
1377 (let ((interp (cdr (assoc "interp" attrs))))
1378 (if (null interp)
1379 (shell-command
1380 (prog1
1381 (buffer-substring-no-properties (point) end)
1382 (delete-region beg end)) t)
1383 (shell-command-on-region beg end interp t t))
1384 (muse-publish-mark-read-only beg (point))))
1386 (defun muse-publish-comment-tag (beg end)
1387 (if (null muse-publish-comments-p)
1388 (delete-region beg end)
1389 (goto-char end)
1390 (muse-insert-markup (muse-markup-text 'comment-end))
1391 (muse-publish-mark-read-only beg end)
1392 (goto-char beg)
1393 (muse-insert-markup (muse-markup-text 'comment-begin))))
1395 ;; Miscellaneous helper functions
1397 (defsubst muse-publishing-directive (name)
1398 (cdr (assoc name muse-publishing-directives)))
1400 (defun muse-publish-strip-tags (string)
1401 "Remove all tags from the string."
1402 (while (string-match "<.*?>" string)
1403 (setq string (replace-match "" nil t string)))
1404 string)
1406 (defun muse-publish-markup-type (category default-func)
1407 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1408 (funcall (or rule default-func))))
1410 (defun muse-published-buffer-contents (buffer)
1411 (with-current-buffer buffer
1412 (goto-char (point-min))
1413 (let ((beg (and (search-forward "Emacs Muse begins here")
1414 (muse-line-end-position)))
1415 (end (and (search-forward "Emacs Muse ends here")
1416 (muse-line-beginning-position))))
1417 (buffer-substring-no-properties beg end))))
1419 (defun muse-published-contents (file)
1420 (when (file-readable-p file)
1421 (muse-with-temp-buffer
1422 (insert-file-contents file)
1423 (muse-published-buffer-contents (current-buffer)))))
1425 (defun muse-publish-transform-output
1426 (file temp-file output-path name gen-func &rest cleanup-exts)
1427 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1428 (setq file (muse-page-name file))
1429 (message "Generating %s output for %s..." name file)
1430 (if (not (funcall gen-func temp-file output-path))
1431 (message "Generating %s from %s...failed" name file)
1432 (message "Generating %s output for %s...done" name file)
1433 (muse-delete-file-if-exists temp-file)
1434 (dolist (ext cleanup-exts)
1435 (muse-delete-file-if-exists
1436 (expand-file-name (concat file ext)
1437 (file-name-directory output-path))))
1438 (message "Wrote %s" output-path)))
1440 (defun muse-publish-read-only (string)
1441 (let ((end (1- (length string))))
1442 (add-text-properties 0 end
1443 '(rear-nonsticky (read-only) read-only t)
1444 string)
1445 string))
1447 (provide 'muse-publish)
1449 ;;; muse-publish.el ends here