Publish captions for images that have descriptions.
[muse-el.git] / lisp / muse-publish.el
blob7fac3a63b38cbe8fe310a4d6d33d4a27030ccc3a
1 ;;; muse-publish.el --- base publishing implementation
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
7 ;; Emacs Muse is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published
9 ;; by the Free Software Foundation; either version 2, or (at your
10 ;; option) any later version.
12 ;; Emacs Muse is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Emacs Muse; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;; Yann Hodique (yann DOT hodique AT gmail DOT com) fixed an
27 ;; unnecessary URL description transform in `muse-publish-url'.
29 ;; Peter K. Lee (saint AT corenova DOT com) provided the
30 ;; `muse-style-elements-list' function.
32 ;; Jim Ottaway (j DOT ottaway AT lse DOT ac DOT uk) provided a
33 ;; reference implementation for nested lists.
35 ;;; Code:
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 ;; Muse Publishing
41 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 (require 'muse)
44 (require 'muse-regexps)
46 (defgroup muse-publish nil
47 "Options controlling the general behavior of Muse publishing."
48 :group 'muse)
50 (defcustom muse-before-publish-hook nil
51 "A hook run in the buffer to be published, before it is done."
52 :type 'hook
53 :group 'muse-publish)
55 (defcustom muse-after-publish-hook nil
56 "A hook run in the buffer to be published, after it is done."
57 :type 'hook
58 :group 'muse-publish)
60 (defcustom muse-publish-url-transforms
61 '(muse-resolve-url)
62 "A list of functions used to prepare URLs for publication.
63 Each is passed the URL. The transformed URL should be returned."
64 :type 'hook
65 :options '(muse-resolve-url)
66 :group 'muse-publish)
68 (defcustom muse-publish-desc-transforms nil
69 "A list of functions used to prepare URL desciptions for publication.
70 Each is passed the description. The modified description should
71 be returned."
72 :type 'hook
73 :group 'muse-publish)
75 (defcustom muse-publish-comments-p nil
76 "If nil, remove comments before publishing.
77 If non-nil, publish comments using the markup of the current style."
78 :type 'boolean
79 :group 'muse-publish)
81 (defcustom muse-publish-report-threshhold 100000
82 "If a file is this size or larger, report publishing progress."
83 :type 'integer
84 :group 'muse-publish)
86 (defcustom muse-publish-markup-regexps
87 `(;; Remove leading and trailing whitespace from the file
88 (1000 "\\(\\`\n+\\|\n+\\'\\)" 0 "")
90 ;; Remove trailing whitespace from all lines
91 (1100 ,(concat "[" muse-regexp-blank "]+$") 0 "")
93 ;; Handle any leading #directives
94 (1200 "\\`#\\([a-zA-Z-]+\\)\\s-+\\(.+\\)\n+" 0 directive)
96 ;; markup tags
97 (1300 muse-tag-regexp 0 tag)
99 ;; commented lines
100 (1350 "^;\\s-+\\(.+\\)" 0 comment)
102 ;; prevent emphasis characters in explicit links from being marked
103 (1400 muse-explicit-link-regexp 0 muse-publish-mark-link)
105 ;; emphasized or literal text
106 (1600 ,(concat "\\(^\\|[-[" muse-regexp-blank
107 "<('`\"\n]\\)\\(=[^=" muse-regexp-blank
108 "\n]\\|_[^_" muse-regexp-blank
109 "\n]\\|\\*+[^*" muse-regexp-blank
110 "\n]\\)")
111 2 word)
113 ;; headings, outline-mode style
114 (1700 "^\\(\\*+\\)\\s-+" 0 heading)
116 ;; ellipses
117 (1800 "\\.\\.\\.\\." 0 enddots)
118 (1850 "\\.\\.\\." 0 dots)
120 ;; horizontal rule, or section separator
121 (1900 "^----+" 0 rule)
123 ;; non-breaking space
124 (1950 "~~" 0 no-break-space)
126 ;; beginning of footnotes section
127 (2000 "^Footnotes:?\\s-*" 0 fn-sep)
128 ;; footnote definition/reference (def if at beginning of line)
129 (2100 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote)
131 ;; unnumbered List items begin with a -. numbered list items
132 ;; begin with number and a period. definition lists have a
133 ;; leading term separated from the body with ::. centered
134 ;; paragraphs begin with at least six columns of whitespace; any
135 ;; other whitespace at the beginning indicates a blockquote. The
136 ;; reason all of these rules are handled here, is so that
137 ;; blockquote detection doesn't interfere with indented list
138 ;; members.
139 (2200 ,(format muse-list-item-regexp (concat "[" muse-regexp-blank "]*"))
140 0 list)
142 ;; simple table markup is supported, nothing fancy. use | to
143 ;; separate cells, || to separate header cells, and ||| for footer
144 ;; cells
145 (2300 ,(concat "\\(\\([" muse-regexp-blank "]*\n\\)?"
146 "\\(" muse-table-line-regexp "\\(?:\n\\|\\'\\)\\)\\)+")
147 0 table)
149 ;; blockquote and centered text
150 (2400 ,(concat "^\\([" muse-regexp-blank "]+\\).+") 0 quote)
152 ;; the emdash ("--")
153 (2500 ,(concat "\\(^\\|[" muse-regexp-blank "]*\\)--\\($\\|["
154 muse-regexp-blank "]*\\)")
155 0 emdash)
157 ;; "verse" text is indicated the same way as a quoted e-mail
158 ;; response: "> text", where text may contain initial whitespace
159 ;; (see below).
160 (2600 ,(concat "^[" muse-regexp-blank "]*> ") 0 verse)
162 ;; define anchor points
163 (2700 "^\\(\\W*\\)#\\(\\S-+\\)\\s-*" 0 anchor)
165 ;; replace links in the buffer (links to other pages)
166 (2900 muse-explicit-link-regexp 0 link)
168 ;; bare URLs
169 (3000 muse-url-regexp 0 url)
171 ;; bare email addresses
172 (3500
173 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email)
175 "List of markup rules for publishing a page with Muse.
176 The rules given in this variable are invoked first, followed by
177 whatever rules are specified by the current style.
179 Each member of the list is either a function, or a list of the form:
181 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
183 REGEXP is a regular expression, or symbol whose value is a regular
184 expression, which is searched for using `re-search-forward'.
185 TEXT-BEGIN-GROUP is the matching group within that regexp which
186 denotes the beginning of the actual text to be marked up.
187 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
188 If it is not a string, but a function, it will be called to determine
189 what the replacement text should be (it must return a string). If it
190 is a symbol, the value of that symbol should be a string.
192 The replacements are done in order, one rule at a time. Writing
193 the regular expressions can be a tricky business. Note that case
194 is never ignored. `case-fold-search' is always bound to nil
195 while processing the markup rules."
196 :type '(repeat (choice
197 (list :tag "Markup rule"
198 integer
199 (choice regexp symbol)
200 integer
201 (choice string function symbol))
202 function))
203 :group 'muse-publish)
205 (defcustom muse-publish-markup-functions
206 '((directive . muse-publish-markup-directive)
207 (comment . muse-publish-markup-comment)
208 (anchor . muse-publish-markup-anchor)
209 (tag . muse-publish-markup-tag)
210 (word . muse-publish-markup-word)
211 (emdash . muse-publish-markup-emdash)
212 (enddots . muse-publish-markup-enddots)
213 (dots . muse-publish-markup-dots)
214 (rule . muse-publish-markup-rule)
215 (no-break-space . muse-publish-markup-no-break-space)
216 (heading . muse-publish-markup-heading)
217 (footnote . muse-publish-markup-footnote)
218 (fn-sep . muse-publish-markup-fn-sep)
219 (list . muse-publish-markup-list)
220 (quote . muse-publish-markup-quote)
221 (verse . muse-publish-markup-verse)
222 (table . muse-publish-markup-table)
223 (email . muse-publish-markup-email)
224 (link . muse-publish-markup-link)
225 (url . muse-publish-markup-url))
226 "An alist of style types to custom functions for that kind of text.
228 Each member of the list is of the form:
230 (SYMBOL FUNCTION)
232 SYMBOL describes the type of text to associate with this rule.
233 `muse-publish-markup-regexps' maps regexps to these symbols.
235 FUNCTION is the function to use to mark up this kind of rule if
236 no suitable function is found through the :functions tag of the
237 current style."
238 :type '(alist :key-type symbol :value-type function)
239 :group 'muse-publish)
241 (defcustom muse-publish-markup-tags
242 '(("contents" nil t muse-publish-contents-tag)
243 ("verse" t nil muse-publish-verse-tag)
244 ("example" t nil muse-publish-example-tag)
245 ("code" t nil muse-publish-code-tag)
246 ("quote" t nil muse-publish-quote-tag)
247 ("literal" t nil muse-publish-mark-read-only)
248 ("verbatim" t nil muse-publish-verbatim-tag)
249 ("lisp" t t muse-publish-lisp-tag)
250 ("class" t t muse-publish-class-tag)
251 ("command" t t muse-publish-command-tag)
252 ("perl" t t muse-publish-perl-tag)
253 ("python" t t muse-publish-python-tag)
254 ("ruby" t t muse-publish-ruby-tag)
255 ("comment" t nil muse-publish-comment-tag)
256 ("include" nil t muse-publish-include-tag)
257 ("markup" t t muse-publish-mark-up-tag))
258 "A list of tag specifications, for specially marking up text.
259 XML-style tags are the best way to add custom markup to Muse.
260 This is easily accomplished by customizing this list of markup tags.
262 For each entry, the name of the tag is given, whether it expects
263 a closing tag and/or an optional set of attributes, and a
264 function that performs whatever action is desired within the
265 delimited region.
267 The tags themselves are deleted during publishing, before the
268 function is called. The function is called with three arguments,
269 the beginning and end of the region surrounded by the tags. If
270 properties are allowed, they are passed as a third argument in
271 the form of an alist. The `end' argument to the function is
272 always a marker.
274 Point is always at the beginning of the region within the tags, when
275 the function is called. Wherever point is when the function finishes
276 is where tag markup will resume.
278 These tag rules are processed once at the beginning of markup, and
279 once at the end, to catch any tags which may have been inserted
280 in-between."
281 :type '(repeat (list (string :tag "Markup tag")
282 (boolean :tag "Expect closing tag" :value t)
283 (boolean :tag "Parse attributes" :value nil)
284 function))
285 :group 'muse-publish)
287 (defcustom muse-publish-markup-specials nil
288 "A table of characters which must be represented specially."
289 :type '(alist :key-type character :value-type string)
290 :group 'muse-publish)
292 (defvar muse-publishing-p nil
293 "Set to t while a page is being published.")
294 (defvar muse-batch-publishing-p nil
295 "Set to t while a page is being batch published.")
296 (defvar muse-publishing-styles nil
297 "The publishing styles that Muse recognizes.
298 This is automatically generated when loading publishing styles.")
299 (defvar muse-publishing-current-file nil
300 "The file that is currently being published.")
301 (defvar muse-publishing-current-output-path nil
302 "The path where the current file will be published to.")
303 (defvar muse-publishing-current-style nil
304 "The style of the file that is currently being published.")
305 (defvar muse-publishing-directives nil
306 "An alist of publishing directives from the top of a file.")
307 (defvar muse-publish-generate-contents nil
308 "Non-nil if a table of contents should be generated.
309 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
310 tell where the <contents> was seen, and to what depth the
311 contents were requested.")
312 (defvar muse-publishing-last-position nil
313 "Last position of the point when publishing.
314 This is used to make sure that publishing doesn't get stalled.")
316 ;; Functions for handling style information
318 (defsubst muse-style (&optional style)
319 "Resolve the given STYLE into a Muse style, if it is a string."
320 (if (null style)
321 muse-publishing-current-style
322 (if (stringp style)
323 (assoc style muse-publishing-styles)
324 (muse-assert (consp style))
325 style)))
327 (defun muse-define-style (name &rest elements)
328 (let ((entry (assoc name muse-publishing-styles)))
329 (if entry
330 (setcdr entry elements)
331 (setq muse-publishing-styles
332 (cons (append (list name) elements)
333 muse-publishing-styles)))))
335 (defun muse-derive-style (new-name base-name &rest elements)
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 (defun 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 (match-beginning group)
446 (get-text-property (match-beginning group) 'read-only))
447 (let* (func
448 (text (cond
449 ((and (symbolp repl)
450 (setq func (muse-markup-function repl)))
451 (funcall func))
452 ((functionp repl)
453 (funcall repl))
454 ((symbolp repl)
455 (symbol-value repl))
456 (t repl))))
457 (if (stringp text)
458 (replace-match text t))))
459 (if (and muse-publishing-last-position
460 (= pos muse-publishing-last-position))
461 (if (eobp)
462 (setq regexp nil)
463 (forward-char 1)))
464 (setq muse-publishing-last-position pos)))
465 (setq rules (cdr rules)
466 base (+ base (point-max))))
467 (if (and verbose (not muse-batch-publishing-p))
468 (message "Publishing %s...done" name))))
470 (defcustom muse-publish-markup-header-footer-tags
471 '(("lisp" t nil muse-publish-lisp-tag)
472 ("markup" t t muse-publish-mark-up-tag))
473 "Tags used when publishing headers and footers.
474 See `muse-publish-markup-tags' for details."
475 :type '(repeat (list (string :tag "Markup tag")
476 (boolean :tag "Expect closing tag" :value t)
477 (boolean :tag "Parse attributes" :value nil)
478 function))
479 :group 'muse-publish)
481 (defun muse-insert-file-or-string (file-or-string &optional title)
482 (let ((beg (point)) end)
483 (if (and (not (string-equal file-or-string ""))
484 (not (string-match "\n" file-or-string))
485 (file-readable-p file-or-string))
486 (setq end (+ beg (cadr (insert-file-contents file-or-string))))
487 (insert file-or-string)
488 (setq end (point)))
489 (save-restriction
490 (narrow-to-region beg end)
491 (remove-text-properties (point-min) (point-max)
492 '(read-only nil rear-nonsticky nil))
493 (goto-char (point-min))
494 (let ((muse-inhibit-style-tags t))
495 (muse-publish-markup (or title "")
496 '((100 muse-tag-regexp 0
497 muse-publish-markup-tag)))))))
499 (defun muse-style-run-hooks (keyword style &rest args)
500 (catch 'handled
501 (while (and style
502 (setq style (muse-style style)))
503 (let ((func (muse-style-element keyword style t)))
504 (when (and func
505 (apply func args))
506 (throw 'handled t)))
507 (setq style (muse-style-element :base style)))))
509 (defun muse-publish-markup-region (beg end &optional title style)
510 "Apply the given STYLE's markup rules to the given region.
511 TITLE is used when indicating the publishing progress; it may be nil."
512 (unless title (setq title ""))
513 (unless style
514 (or (setq style muse-publishing-current-style)
515 (error "Cannot find any publishing styles to use")))
516 (save-restriction
517 (narrow-to-region beg end)
518 (muse-style-run-hooks :before style)
519 (muse-publish-markup
520 title
521 (sort (copy-alist (append muse-publish-markup-regexps
522 (muse-style-elements-list :regexps style)))
523 (function
524 (lambda (l r)
525 (< (car l) (car r))))))
526 (muse-style-run-hooks :before-end style)
527 (muse-publish-escape-specials (point-min) (point-max) nil 'document)))
529 (defun muse-publish-markup-buffer (title style)
530 "Apply the given STYLE's markup rules to the current buffer."
531 (setq style (muse-style style))
532 (let ((style-header (muse-style-element :header style))
533 (style-footer (muse-style-element :footer style))
534 (muse-publishing-current-style style)
535 (muse-publishing-directives
536 (list (cons "title" title)
537 (cons "author" (user-full-name))
538 (cons "date" (format-time-string
539 "%B %e, %Y"
540 (if muse-publishing-current-file
541 (nth 5 (file-attributes
542 muse-publishing-current-file))
543 (current-time))))))
544 (muse-publishing-p t)
545 (inhibit-read-only t))
546 (run-hooks 'muse-update-values-hook)
547 (run-hooks 'muse-before-publish-hook)
548 (muse-publish-markup-region (point-min) (point-max) title style)
549 (goto-char (point-min))
550 (when style-header
551 (muse-insert-file-or-string style-header title))
552 (goto-char (point-max))
553 (when style-footer
554 (muse-insert-file-or-string style-footer title))
555 (muse-style-run-hooks :after style)
556 (run-hooks 'muse-after-publish-hook)))
558 (defun muse-publish-markup-string (string &optional style)
559 "Markup STRING using the given STYLE's markup rules."
560 (setq style (muse-style style))
561 (muse-with-temp-buffer
562 (insert string)
563 (let ((muse-publishing-current-style style)
564 (muse-publishing-p t))
565 (muse-publish-markup "*string*" (muse-style-element :rules style)))
566 (buffer-string)))
568 ;; Commands for publishing files
570 (defsubst muse-publish-get-style (&optional styles)
571 (unless styles (setq styles muse-publishing-styles))
572 (if (= 1 (length styles))
573 (car styles)
574 (assoc (completing-read "Publish with style: " styles nil t)
575 styles)))
577 (defsubst muse-publish-get-output-dir (style)
578 (let ((default-directory (or (muse-style-element :path style)
579 default-directory)))
580 (muse-read-directory-name "Publish to directory: " nil default-directory)))
582 (defsubst muse-publish-get-info ()
583 (let ((style (muse-publish-get-style)))
584 (list style (muse-publish-get-output-dir style)
585 current-prefix-arg)))
587 (defsubst muse-publish-output-name (&optional file style)
588 (setq style (muse-style style))
589 (concat (muse-style-element :prefix style)
590 (muse-page-name file)
591 (muse-style-element :suffix style)))
593 (defsubst muse-publish-output-file (file &optional output-dir style)
594 (setq style (muse-style style))
595 (if output-dir
596 (expand-file-name (muse-publish-output-name file style) output-dir)
597 (concat (file-name-directory file)
598 (muse-publish-output-name file style))))
600 (defsubst muse-publish-link-name (&optional file style)
601 (setq style (muse-style style))
602 (concat (muse-style-element :prefix style)
603 (muse-page-name file)
604 (or (muse-style-element :link-suffix style)
605 (muse-style-element :suffix style))))
607 (defsubst muse-publish-link-file (file &optional output-dir style)
608 (setq style (muse-style style))
609 (if output-dir
610 (expand-file-name (muse-publish-link-name file style) output-dir)
611 (concat (file-name-directory file)
612 (muse-publish-link-name file style))))
614 (defsubst muse-publish-link-page (page)
615 (if (fboundp 'muse-project-link-page)
616 (muse-project-link-page page)
617 (muse-publish-link-file page)))
619 ;;;###autoload
620 (defun muse-publish-file (file style &optional output-dir force)
621 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
622 If the argument FORCE is nil, each file is only published if it is
623 newer than the published version. If the argument FORCE is non-nil,
624 the file is published no matter what."
625 (interactive (cons (read-file-name "Publish file: ")
626 (muse-publish-get-info)))
627 (let ((style-name style))
628 (setq style (muse-style style))
629 (unless style
630 (error "There is no style '%s' defined." style-name)))
631 (let* ((output-path (muse-publish-output-file file output-dir style))
632 (output-suffix (muse-style-element :osuffix style))
633 (muse-publishing-current-file file)
634 (muse-publishing-current-output-path output-path)
635 (target (if output-suffix
636 (concat (file-name-sans-extension output-path)
637 output-suffix)
638 output-path))
639 (threshhold (nth 7 (file-attributes file))))
640 (if (not threshhold)
641 (message "Please save %s before publishing" file)
642 (when (or force (file-newer-than-file-p file target))
643 (if (and muse-publish-report-threshhold
644 (> threshhold
645 muse-publish-report-threshhold))
646 (message "Publishing %s ..." file))
647 (muse-with-temp-buffer
648 (insert-file-contents file)
649 (muse-publish-markup-buffer (muse-page-name file) style)
650 (let ((backup-inhibited t))
651 (write-file output-path))
652 (muse-style-run-hooks :final style file output-path target))
653 t))))
655 ;;;###autoload
656 (defun muse-publish-this-file (style output-dir &optional force)
657 "Publish the currently-visited file.
658 Prompt for both the STYLE and OUTPUT-DIR if they are not
659 supplied."
660 (interactive (muse-publish-get-info))
661 (unless (muse-publish-file buffer-file-name style output-dir force)
662 (message (concat "The published version is up-to-date; use"
663 " C-u C-c C-T to force an update."))))
665 (defun muse-batch-publish-files ()
666 "Publish Muse files in batch mode."
667 (let ((muse-batch-publishing-p t)
668 style output-dir)
669 (setq style (car command-line-args-left)
670 command-line-args-left (cdr command-line-args-left)
671 output-dir (car command-line-args-left)
672 output-dir
673 (if (string-match "\\`--output-dir=" output-dir)
674 (prog1
675 (substring output-dir (match-end 0))
676 (setq command-line-args-left (cdr command-line-args-left)))))
677 (setq auto-mode-alist
678 (delete (cons (concat "\\." muse-file-extension "\\'")
679 'muse-mode-choose-mode)
680 auto-mode-alist))
681 (dolist (file command-line-args-left)
682 (muse-publish-file file style output-dir t))))
684 ;; Default publishing rules
686 (defun muse-publish-section-close (depth)
687 "Seach forward for the closing tag of given DEPTH."
688 (let (not-end)
689 (save-excursion
690 (while (and (setq not-end (re-search-forward
691 (concat "^\\*\\{1," (number-to-string depth)
692 "\\}\\s-+")
693 nil t))
694 (get-text-property (match-beginning 0) 'read-only)))
695 (if not-end
696 (forward-line 0)
697 (goto-char (point-max)))
698 (cond ((not (eq (char-before) ?\n))
699 (insert "\n\n"))
700 ((not (eq (char-before (1- (point))) ?\n))
701 (insert "\n")))
702 (muse-insert-markup (muse-markup-text 'section-close depth))
703 (insert "\n"))))
705 (defun muse-publish-markup-directive (&optional name value)
706 (unless name (setq name (match-string 1)))
707 (unless value (setq value (match-string 2)))
708 (let ((elem (assoc name muse-publishing-directives)))
709 (if elem
710 (setcdr elem value)
711 (setq muse-publishing-directives
712 (cons (cons name value)
713 muse-publishing-directives))))
714 ;; Make sure we don't ever try to move the point forward (past the
715 ;; beginning of buffer) while we're still searching for directives.
716 (setq muse-publishing-last-position nil)
717 (delete-region (match-beginning 0) (match-end 0)))
719 (defsubst muse-publishing-directive (name)
720 (cdr (assoc name muse-publishing-directives)))
722 (defun muse-publish-markup-anchor ()
723 (unless (get-text-property (match-end 1) 'muse-link)
724 (let ((text (muse-markup-text 'anchor (match-string 2))))
725 (unless (string= text "")
726 (save-match-data
727 (skip-chars-forward (concat muse-regexp-blank "\n"))
728 (muse-insert-markup text)))
729 (match-string 1))))
731 (defun muse-publish-markup-comment ()
732 (if (null muse-publish-comments-p)
734 (goto-char (match-end 0))
735 (muse-insert-markup (muse-markup-text 'comment-end))
736 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
737 (delete-region (match-beginning 0) (match-beginning 1))
738 (goto-char (match-beginning 0))
739 (muse-insert-markup (muse-markup-text 'comment-begin))))
741 (defvar muse-inhibit-style-tags nil
742 "If non-nil, do not search for style-specific tags.
743 This is used when publishing headers and footers.")
745 (defun muse-publish-markup-tag ()
746 (let ((tag-info (if muse-inhibit-style-tags
747 (assoc (match-string 1) muse-publish-markup-tags)
748 (muse-markup-tag-info (match-string 1)))))
749 (when (and tag-info
750 (not (get-text-property (match-beginning 0) 'read-only)))
751 (let ((closed-tag (match-string 3))
752 (start (match-beginning 0))
753 (beg (point))
754 end attrs)
755 (when (nth 2 tag-info)
756 (let ((attrstr (match-string 2)))
757 (while (and attrstr
758 (string-match (concat "\\([^"
759 muse-regexp-blank
760 "=\n]+\\)\\(=\"\\"
761 "([^\"]+\\)\"\\)?")
762 attrstr))
763 (let ((attr (cons (downcase
764 (muse-match-string-no-properties 1 attrstr))
765 (muse-match-string-no-properties 3 attrstr))))
766 (setq attrstr (replace-match "" t t attrstr))
767 (if attrs
768 (nconc attrs (list attr))
769 (setq attrs (list attr)))))))
770 (if (and (cadr tag-info) (not closed-tag))
771 (if (search-forward (concat "</" (car tag-info) ">") nil t)
772 (delete-region (match-beginning 0) (point))
773 (setq tag-info nil)))
774 (when tag-info
775 (setq end (point-marker))
776 (delete-region start beg)
777 (goto-char start)
778 (let ((args (list start end)))
779 (if (nth 2 tag-info)
780 (nconc args (list attrs)))
781 (let ((muse-inhibit-style-tags nil))
782 ;; remove the inhibition
783 (apply (nth 3 tag-info) args)))))))
784 nil)
786 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
787 "Escape specials from BEG to END using style-specific :specials.
788 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
789 CONTEXT is used to figure out what kind of specials to escape.
791 The following contexts exist in Muse.
792 'underline _underlined text_
793 'literal =monospaced text= or <code> region (monospaced, escaped)
794 'emphasis *emphasized text*
795 'email email@example.com
796 'url http://example.com
797 'url-desc [[...][description of an explicit link]]
798 'image [[image.png]]
799 'example <example> region (monospaced, block context, escaped)
800 'verbatim <verbatim> region (escaped)
801 'document normal text"
802 (let ((specials (muse-style-element :specials nil t)))
803 (cond ((functionp specials)
804 (setq specials (funcall specials context)))
805 ((symbolp specials)
806 (setq specials (symbol-value specials))))
807 (if (functionp specials)
808 (funcall specials beg end ignore-read-only)
809 (save-excursion
810 (save-restriction
811 (narrow-to-region beg end)
812 (goto-char (point-min))
813 (while (< (point) (point-max))
814 (if (and (not ignore-read-only)
815 (get-text-property (point) 'read-only))
816 (goto-char (or (next-single-property-change (point) 'read-only)
817 (point-max)))
818 (let ((repl (or (assoc (char-after) specials)
819 (assoc (char-after)
820 muse-publish-markup-specials))))
821 (if (null repl)
822 (forward-char 1)
823 (delete-char 1)
824 (insert-before-markers (cdr repl)))))))))))
826 (defun muse-publish-markup-word ()
827 (let* ((beg (match-beginning 2))
828 (end (1- (match-end 2)))
829 (leader (buffer-substring-no-properties beg end))
830 open-tag close-tag mark-read-only loc context)
831 (cond
832 ((string= leader "_")
833 (setq context 'underline
834 open-tag (muse-markup-text 'begin-underline)
835 close-tag (muse-markup-text 'end-underline)))
836 ((string= leader "=")
837 (setq context 'literal
838 open-tag (muse-markup-text 'begin-literal)
839 close-tag (muse-markup-text 'end-literal))
840 (setq mark-read-only t))
842 (let ((l (length leader)))
843 (setq context 'emphasis)
844 (cond
845 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
846 close-tag (muse-markup-text 'end-emph)))
847 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
848 close-tag (muse-markup-text 'end-more-emph)))
849 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
850 close-tag (muse-markup-text 'end-most-emph)))
851 (t (setq context nil))))))
852 (if (and context
853 (not (get-text-property beg 'muse-link))
854 (setq loc (search-forward leader nil t))
855 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
856 (not (eq (char-syntax (char-before (point))) ?\ ))
857 (not (get-text-property (point) 'muse-link)))
858 (progn
859 (replace-match "")
860 (delete-region beg end)
861 (setq end (point-marker))
862 (muse-insert-markup close-tag)
863 (goto-char beg)
864 (muse-insert-markup open-tag)
865 (setq beg (point))
866 (when mark-read-only
867 (muse-publish-escape-specials beg end t context)
868 (muse-publish-mark-read-only beg end)))
869 (backward-char))
870 nil))
872 (defun muse-publish-markup-emdash ()
873 (unless (get-text-property (match-beginning 0) 'muse-link)
874 (let ((prespace (match-string 1))
875 (postspace (match-string 2)))
876 (delete-region (match-beginning 0) (match-end 0))
877 (muse-insert-markup (muse-markup-text 'emdash prespace postspace))
878 (when (eq (char-after) ?\<)
879 (insert ?\n)))))
881 (defun muse-publish-markup-enddots ()
882 (unless (get-text-property (match-beginning 0) 'muse-link)
883 (delete-region (match-beginning 0) (match-end 0))
884 (muse-insert-markup (muse-markup-text 'enddots))))
886 (defun muse-publish-markup-dots ()
887 (unless (get-text-property (match-beginning 0) 'muse-link)
888 (delete-region (match-beginning 0) (match-end 0))
889 (muse-insert-markup (muse-markup-text 'dots))))
891 (defun muse-publish-markup-rule ()
892 (unless (get-text-property (match-beginning 0) 'muse-link)
893 (delete-region (match-beginning 0) (match-end 0))
894 (muse-insert-markup (muse-markup-text 'rule))))
896 (defun muse-publish-markup-no-break-space ()
897 (unless (get-text-property (match-beginning 0) 'muse-link)
898 (delete-region (match-beginning 0) (match-end 0))
899 (muse-insert-markup (muse-markup-text 'no-break-space))))
901 (defun muse-publish-markup-heading ()
902 (let* ((len (length (match-string 1)))
903 (start (muse-markup-text
904 (cond ((= len 1) 'section)
905 ((= len 2) 'subsection)
906 ((= len 3) 'subsubsection)
907 (t 'section-other))
908 len))
909 (end (muse-markup-text
910 (cond ((= len 1) 'section-end)
911 ((= len 2) 'subsection-end)
912 ((= len 3) 'subsubsection-end)
913 (t 'section-other-end))
914 len)))
915 (delete-region (match-beginning 0) (match-end 0))
916 (muse-insert-markup start)
917 (end-of-line)
918 (when end
919 (muse-insert-markup end))
920 (muse-publish-section-close len)))
922 (defvar muse-publish-footnotes nil)
924 (defun muse-publish-markup-footnote ()
925 "Scan ahead and snarf up the footnote body"
926 (cond
927 ((get-text-property (match-beginning 0) 'muse-link)
928 nil)
929 ((= (muse-line-beginning-position) (match-beginning 0))
932 (let ((footnote (save-match-data
933 (string-to-number (match-string 1))))
934 footnotemark)
935 (delete-region (match-beginning 0) (match-end 0))
936 (save-excursion
937 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
938 (let* ((start (match-beginning 0))
939 (beg (goto-char (match-end 0)))
940 (end (save-excursion
941 (if (search-forward "\n\n" nil t)
942 (copy-marker (match-beginning 0))
943 (goto-char (point-max))
944 (skip-chars-backward "\n")
945 (point-marker)))))
946 (while (re-search-forward
947 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
948 end t)
949 (replace-match "\\1" t))
950 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
951 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
952 (if (string= "" footnotemark-cmd)
953 (setq footnotemark
954 (concat (muse-markup-text 'footnote)
955 (buffer-substring-no-properties beg end)
956 (muse-markup-text 'footnote-end)))
957 (setq footnotemark (format footnotemark-cmd footnote
958 footnotemark-end-cmd))
959 (unless muse-publish-footnotes
960 (set (make-local-variable 'muse-publish-footnotes)
961 (make-vector 256 nil)))
962 (unless (aref muse-publish-footnotes footnote)
963 (setq footnotemark
964 (concat
965 footnotemark
966 (concat (format (muse-markup-text 'footnotetext)
967 footnote)
968 (buffer-substring-no-properties beg end)
969 (muse-markup-text 'footnotetext-end))))
970 (aset muse-publish-footnotes footnote footnotemark))))
971 (goto-char end)
972 (skip-chars-forward "\n")
973 (delete-region start (point)))))
974 (muse-insert-markup (or footnotemark footnote))))))
976 (defun muse-publish-markup-fn-sep ()
977 (delete-region (match-beginning 0) (match-end 0))
978 (muse-insert-markup (muse-markup-text 'fn-sep)))
980 (defun muse-insert-markup-end-list (&rest args)
981 (let ((beg (point)))
982 (apply 'insert args)
983 (add-text-properties beg (point) '(end-list t))
984 (muse-publish-mark-read-only beg (point))))
986 (defun muse-publish-surround-dl (indent post-indent)
987 (let* ((beg-item (muse-markup-text 'begin-dl-item))
988 (end-item (muse-markup-text 'end-dl-item))
989 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
990 (end-ddt (muse-markup-text 'end-ddt))
991 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
992 (end-dde (muse-markup-text 'end-dde))
993 (continue t)
994 def-on-same-line beg)
995 (while continue
996 ;; envelope this as one term+definitions unit -- HTML does not
997 ;; need this, but DocBook and Muse's custom XML format do
998 (muse-insert-markup beg-item)
999 (when (looking-at muse-dl-term-regexp)
1000 ;; find the term and wrap it with published markup
1001 (setq beg (point))
1002 (goto-char (match-end 1))
1003 (delete-region (point) (match-end 0))
1004 (muse-insert-markup-end-list end-ddt)
1005 ;; if definition is immediately after term, move to next line
1006 (unless (eq (char-after) ?\n)
1007 (insert ?\n))
1008 (save-excursion
1009 (goto-char beg)
1010 (delete-region (point) (match-beginning 1))
1011 (muse-insert-markup beg-ddt)))
1012 (setq beg (point)
1013 ;; move past current item
1014 continue (muse-forward-list-item 'dl-term indent))
1015 (save-restriction
1016 (narrow-to-region beg (point))
1017 (goto-char (point-min))
1018 ;; publish each definition that we find, defaulting to an
1019 ;; empty definition if none are found
1020 (muse-publish-surround-text beg-dde end-dde
1021 (lambda (indent)
1022 (muse-forward-list-item 'dl-entry indent))
1023 nil nil t)
1024 (goto-char (point-max))
1025 (skip-chars-backward (concat muse-regexp-blank "\n"))
1026 (muse-insert-markup-end-list end-item)
1027 (when continue
1028 (goto-char (point-max)))))))
1030 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent determine-indent)
1031 (let ((continue t)
1032 (list-item (format muse-list-item-regexp
1033 (concat "[" muse-regexp-blank "]*")))
1034 init-indent beg)
1035 (unless indent
1036 (setq indent (concat "[" muse-regexp-blank "]+")))
1037 (if post-indent
1038 (setq post-indent (concat " \\{0," (number-to-string post-indent)
1039 "\\}"))
1040 (setq post-indent ""))
1041 (while continue
1042 (muse-insert-markup beg-tag)
1043 (setq beg (point)
1044 ;; move past current item; continue is non-nil if there
1045 ;; are more like items to be processed
1046 continue (funcall move-func indent))
1047 (save-restriction
1048 (when determine-indent
1049 ;; if the caller doesn't know how much indentation
1050 ;; to use, figure it out ourselves
1051 (if (not continue)
1052 (setq indent "")
1053 (save-match-data
1054 ;; snarf all leading whitespace
1055 (let ((this-indent (and (match-beginning 2)
1056 (buffer-substring (match-beginning 1)
1057 (match-beginning 2)))))
1058 (when (and this-indent
1059 (not (string= this-indent "")))
1060 (setq indent this-indent
1061 determine-indent nil))))))
1062 (when continue
1063 ;; remove list markup if we encountered another item of the
1064 ;; same type
1065 (replace-match "" t t nil 1))
1066 (narrow-to-region beg (point))
1067 ;; narrow to current item
1068 (goto-char (point-min))
1069 (forward-line 1)
1070 (let ((list-nested nil)
1071 (indent-found nil)
1072 (post-indent post-indent))
1073 (while (< (point) (point-max))
1074 (when (and (looking-at list-item)
1075 (not (or (get-text-property
1076 (muse-list-item-critical-point) 'read-only)
1077 (get-text-property
1078 (muse-list-item-critical-point) 'muse-link))))
1079 ;; if we encounter a list item, allow no post-indent
1080 ;; space
1081 (setq list-nested t))
1082 (when (looking-at (concat indent "\\("
1083 (or (and list-nested "")
1084 post-indent)
1085 "\\)"))
1086 ;; if list is not nested, remove indentation
1087 (unless indent-found
1088 (setq post-indent (match-string 1)
1089 indent-found t))
1090 (replace-match ""))
1091 (forward-line 1)))
1092 (skip-chars-backward (concat muse-regexp-blank "\n"))
1093 (muse-insert-markup-end-list end-tag)
1094 (when continue
1095 (goto-char (point-max)))))))
1097 (defun muse-publish-markup-list ()
1098 "Markup a list entry.
1099 This function works by marking up items of the same list level
1100 and type, respecting the end-of-list property."
1101 (let* ((str (match-string 1))
1102 (type (muse-list-item-type str))
1103 (indent (buffer-substring (muse-line-beginning-position)
1104 (match-beginning 1)))
1105 (post-indent (length str))
1106 (last (match-beginning 0)))
1107 (cond
1108 ((or (get-text-property (muse-list-item-critical-point) 'read-only)
1109 (get-text-property (muse-list-item-critical-point) 'muse-link))
1110 nil)
1111 ((eq type 'ul)
1112 (unless (eq (char-after (match-end 1)) ?-)
1113 (delete-region (match-beginning 0) (match-end 0))
1114 (muse-insert-markup (muse-markup-text 'begin-uli))
1115 (save-excursion
1116 (muse-publish-surround-text
1117 (muse-markup-text 'begin-uli-item)
1118 (muse-markup-text 'end-uli-item)
1119 (lambda (indent)
1120 (muse-forward-list-item 'ul indent))
1121 indent post-indent)
1122 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1123 (forward-line 1)))
1124 ((eq type 'ol)
1125 (delete-region (match-beginning 0) (match-end 0))
1126 (muse-insert-markup (muse-markup-text 'begin-oli))
1127 (save-excursion
1128 (muse-publish-surround-text
1129 (muse-markup-text 'begin-oli-item)
1130 (muse-markup-text 'end-oli-item)
1131 (lambda (indent)
1132 (muse-forward-list-item 'ol indent))
1133 indent post-indent)
1134 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1135 (forward-line 1))
1136 ((not (string= (match-string 2) ""))
1137 ;; must have an initial term
1138 (goto-char (match-beginning 0))
1139 (muse-insert-markup (muse-markup-text 'begin-dl))
1140 (save-excursion
1141 (muse-publish-surround-dl indent post-indent)
1142 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1143 (forward-line 1))))
1144 nil)
1146 (defun muse-publish-markup-quote ()
1147 "Markup a quoted paragraph.
1148 The reason this function is so funky, is to prevent text properties
1149 like read-only from being inadvertently deleted."
1150 (let* ((ws (match-string 1))
1151 (centered (>= (string-width ws) 6))
1152 (begin-elem (if centered 'begin-center 'begin-quote-item))
1153 (end-elem (if centered 'end-center 'end-quote-item)))
1154 (replace-match "" t t nil 1)
1155 (unless centered
1156 (muse-insert-markup (muse-markup-text 'begin-quote)))
1157 (muse-publish-surround-text (muse-markup-text begin-elem)
1158 (muse-markup-text end-elem)
1159 (function (lambda (indent)
1160 (muse-forward-paragraph)
1161 nil)))
1162 (unless centered
1163 (muse-insert-markup (muse-markup-text 'end-quote)))))
1165 (defun muse-publish-markup-leading-space (markup-space multiple)
1166 (let (count)
1167 (when (and markup-space
1168 (>= (setq count (skip-chars-forward " ")) 0))
1169 (delete-region (muse-line-beginning-position) (point))
1170 (while (> count 0)
1171 (muse-insert-markup markup-space)
1172 (setq count (- count multiple))))))
1174 (defun muse-publish-markup-verse ()
1175 (let ((leader (match-string 0)))
1176 (goto-char (match-beginning 0))
1177 (muse-insert-markup (muse-markup-text 'begin-verse))
1178 (while (looking-at leader)
1179 (replace-match "")
1180 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1181 (let ((beg (point)))
1182 (end-of-line)
1183 (cond
1184 ((bolp)
1185 (let ((text (muse-markup-text 'empty-verse-line)))
1186 (when text (muse-insert-markup text))))
1187 ((save-excursion
1188 (save-match-data
1189 (forward-line 1)
1190 (or (looking-at (concat leader "["
1191 muse-regexp-blank
1192 "]*$"))
1193 (not (looking-at leader)))))
1194 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1195 (end-text (muse-markup-text 'end-last-stanza-line)))
1196 (when end-text (muse-insert-markup end-text))
1197 (goto-char beg)
1198 (when begin-text (muse-insert-markup begin-text))
1199 (end-of-line)))
1201 (let ((begin-text (muse-markup-text 'begin-verse-line))
1202 (end-text (muse-markup-text 'end-verse-line)))
1203 (when end-text (muse-insert-markup end-text))
1204 (goto-char beg)
1205 (when begin-text (muse-insert-markup begin-text))
1206 (end-of-line))))
1207 (forward-line 1))))
1208 (muse-insert-markup (muse-markup-text 'end-verse))
1209 (insert ?\n))
1211 (defun muse-publish-table-fields (beg end)
1212 "Parse given region as a table, returning a cons cell.
1213 The car is the length of the longest row.
1215 The cdr is a list of the fields of the table, with the first
1216 element indicating the type of the row:
1217 1: body, 2: header, 3: footer.
1219 The existing region will be removed, except for initial blank lines."
1220 (unless (muse-publishing-directive "disable-tables")
1221 (let ((longest 0)
1222 (left 0)
1223 fields field-list)
1224 (save-restriction
1225 (narrow-to-region beg end)
1226 (goto-char (point-min))
1227 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1228 (forward-line 1))
1229 (setq beg (point))
1230 (while (= left 0)
1231 (when (looking-at muse-table-line-regexp)
1232 (setq fields (cons (length (match-string 1))
1233 (split-string (match-string 0)
1234 muse-table-field-regexp))
1235 field-list (cons fields field-list)
1236 longest (max (length fields) longest)))
1237 (setq left (forward-line 1))))
1238 (delete-region beg end)
1239 (if (= longest 0)
1240 (cons 0 nil)
1241 (cons (1- longest) (nreverse field-list))))))
1243 (defun muse-publish-markup-table ()
1244 "Style does not support tables.")
1246 (defun muse-publish-escape-specials-in-string (string &optional context)
1247 "Escape specials in STRING using style-specific :specials.
1248 CONTEXT is used to figure out what kind of specials to escape.
1250 See the documentation of the `muse-publish-escape-specials'
1251 function for the list of available contexts."
1252 (unless string
1253 (setq string ""))
1254 (let ((specials (muse-style-element :specials nil t)))
1255 (cond ((functionp specials)
1256 (setq specials (funcall specials context)))
1257 ((symbolp specials)
1258 (setq specials (symbol-value specials))))
1259 (if (functionp specials)
1260 (funcall specials string)
1261 (apply (function concat)
1262 (mapcar
1263 (lambda (ch)
1264 (let ((repl (or (assoc ch specials)
1265 (assoc ch muse-publish-markup-specials))))
1266 (if (null repl)
1267 (char-to-string ch)
1268 (cdr repl))))
1269 (append string nil))))))
1271 (defun muse-publish-markup-email ()
1272 (let* ((beg (match-end 1))
1273 (addr (buffer-substring-no-properties beg (match-end 0))))
1274 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1275 (goto-char beg)
1276 (delete-region beg (match-end 0))
1277 (if (or (eq (char-before (match-beginning 0)) ?\")
1278 (eq (char-after (match-end 0)) ?\"))
1279 (insert addr)
1280 (insert (format (muse-markup-text 'email-addr) addr addr)))
1281 (muse-publish-mark-read-only beg (point))))
1283 (defun muse-publish-classify-url (target)
1284 "Transform anchors and get published name, if TARGET is a page.
1285 The return value is a cons cell. The car is the type of link,
1286 the cadr is the page name, and the cddr is the anchor."
1287 (save-match-data
1288 (cond ((or (null target) (string= target ""))
1289 nil)
1290 ((string-match muse-url-regexp target)
1291 (cons 'url (cons target nil)))
1292 ((string-match muse-image-regexp target)
1293 (cons 'image (cons target nil)))
1294 ((string-match muse-file-regexp target)
1295 (cons 'file (cons target nil)))
1296 ((string-match "#" target)
1297 (if (eq (aref target 0) ?\#)
1298 (cons 'anchor-ref (cons nil (substring target 1)))
1299 (cons 'link-and-anchor
1300 (cons (muse-publish-link-page
1301 (substring target 0 (match-beginning 0)))
1302 (substring target (match-end 0))))))
1304 (cons 'link (cons (muse-publish-link-page target) nil))))))
1306 (defun muse-publish-url (url &optional desc explicit)
1307 "Resolve a URL into its final <a href> form."
1308 (let ((orig-url url)
1309 type anchor)
1310 (dolist (transform muse-publish-url-transforms)
1311 (setq url (save-match-data (when url (funcall transform url explicit)))))
1312 (dolist (transform muse-publish-desc-transforms)
1313 (setq desc (save-match-data
1314 (when desc (funcall transform desc explicit)))))
1315 (when desc
1316 (setq desc (muse-link-unescape desc))
1317 (setq desc (muse-publish-escape-specials-in-string desc 'url-desc)))
1318 (setq orig-url
1319 (muse-publish-escape-specials-in-string orig-url 'url-desc))
1320 (let ((target (muse-publish-classify-url url)))
1321 (setq type (car target)
1322 url (if (eq type 'image)
1323 (muse-publish-escape-specials-in-string (cadr target)
1324 'image)
1325 (muse-publish-escape-specials-in-string (cadr target) 'url))
1326 anchor (muse-publish-escape-specials-in-string
1327 (cddr target) 'url)))
1328 (cond ((eq type 'anchor-ref)
1329 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1330 ((string= url "")
1331 desc)
1332 ((eq type 'image)
1333 (let ((ext (or (file-name-extension url) "")))
1334 (setq url (file-name-sans-extension url))
1335 (if desc
1336 (muse-markup-text 'image-with-desc url ext desc)
1337 (muse-markup-text 'image url ext))))
1338 ((eq type 'link-and-anchor)
1339 (muse-markup-text 'link-and-anchor url anchor
1340 (or desc orig-url)))
1341 ((and desc (string-match muse-image-regexp desc))
1342 (let ((ext (or (file-name-extension desc) "")))
1343 (setq desc (file-name-sans-extension desc))
1344 (muse-markup-text 'image-link url desc ext)))
1345 ((eq type 'link)
1346 (muse-markup-text 'link url (or desc orig-url)))
1348 (or (and desc
1349 (let ((text (muse-markup-text 'url-and-desc url desc)))
1350 (and (not (string= text ""))
1351 text)))
1352 (muse-markup-text 'url url (or desc orig-url)))))))
1354 (defun muse-publish-insert-url (url &optional desc explicit)
1355 "Resolve a URL into its final <a href> form."
1356 (delete-region (match-beginning 0) (match-end 0))
1357 (let ((text (muse-publish-url url desc explicit)))
1358 (when text
1359 (muse-insert-markup text))))
1361 (defun muse-publish-markup-link ()
1362 (let (desc explicit link)
1363 (setq explicit (save-match-data
1364 (if (string-match muse-explicit-link-regexp
1365 (match-string 0))
1366 t nil)))
1367 (setq desc (if explicit
1368 (match-string 2)
1369 (match-string 0)))
1370 (setq link (if explicit
1371 (muse-handle-explicit-link (match-string 1))
1372 (muse-handle-implicit-link (match-string 0))))
1373 (when (and link
1374 (or explicit
1375 (not (or (eq (char-before (match-beginning 0)) ?\")
1376 (eq (char-after (match-end 0)) ?\")))))
1377 ;; if explicit link has no user-provided description, treat it
1378 ;; as if it were an implicit link
1379 (when (and explicit (not desc))
1380 (setq explicit nil))
1381 (muse-publish-insert-url link desc explicit))))
1383 (defun muse-publish-markup-url ()
1384 (unless (or (eq (char-before (match-beginning 0)) ?\")
1385 (eq (char-after (match-end 0)) ?\"))
1386 (muse-publish-insert-url (match-string 0))))
1388 ;; Default publishing tags
1390 (defcustom muse-publish-contents-depth 2
1391 "The number of heading levels to include with <contents> tags."
1392 :type 'integer
1393 :group 'muse-publish)
1395 (defun muse-publish-contents-tag (beg end attrs)
1396 (set (make-local-variable 'muse-publish-generate-contents)
1397 (cons (copy-marker (point) t)
1398 (let ((depth (cdr (assoc "depth" attrs))))
1399 (or (and depth (string-to-number depth))
1400 muse-publish-contents-depth)))))
1402 (defun muse-publish-verse-tag (beg end)
1403 (save-excursion
1404 (save-restriction
1405 (narrow-to-region beg end)
1406 (goto-char (point-min))
1407 (while (eq ?\ (char-syntax (char-after)))
1408 (delete-char 1))
1409 (while (< (point) (point-max))
1410 (insert "> ")
1411 (forward-line))
1412 (if (eq ?\ (char-syntax (char-before)))
1413 (delete-char -1)))))
1415 (defun muse-publish-mark-read-only (beg end)
1416 "Add read-only properties to the given region."
1417 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1418 nil)
1420 (defun muse-publish-mark-link (&optional beg end)
1421 "Indicate that the given region is a Muse link, so that other
1422 markup elements respect it. If a region is not specified, use
1423 the 0th match data to determine it.
1425 This is usually applied to explicit links."
1426 (unless beg (setq beg (match-beginning 0)))
1427 (unless end (setq end (match-end 0)))
1428 (add-text-properties beg end '(muse-link t))
1429 nil)
1431 (defun muse-publish-quote-tag (beg end)
1432 (save-excursion
1433 (save-restriction
1434 (narrow-to-region beg end)
1435 (muse-insert-markup (muse-markup-text 'begin-quote))
1436 (muse-publish-surround-text (muse-markup-text 'begin-quote-item)
1437 (muse-markup-text 'end-quote-item)
1438 (function (lambda (indent)
1439 (muse-forward-paragraph)
1440 (goto-char (match-end 0))
1441 (< (point) (point-max)))))
1442 (muse-insert-markup (muse-markup-text 'end-quote)))))
1444 (defun muse-publish-code-tag (beg end)
1445 (muse-publish-escape-specials beg end nil 'literal)
1446 (goto-char beg)
1447 (insert (muse-markup-text 'begin-literal))
1448 (goto-char end)
1449 (insert (muse-markup-text 'end-literal))
1450 (muse-publish-mark-read-only beg (point)))
1452 (defun muse-publish-example-tag (beg end)
1453 (muse-publish-escape-specials beg end nil 'example)
1454 (goto-char beg)
1455 (insert (muse-markup-text 'begin-example))
1456 (goto-char end)
1457 (insert (muse-markup-text 'end-example))
1458 (muse-publish-mark-read-only beg (point)))
1460 (defun muse-publish-verbatim-tag (beg end)
1461 (muse-publish-escape-specials beg end nil 'verbatim)
1462 (muse-publish-mark-read-only beg end))
1464 (defalias 'muse-publish-class-tag 'ignore)
1466 (defun muse-publish-examplify-buffer ()
1467 "Transform the current buffer as if it were an <example> region."
1468 (let ((end (progn (goto-char (point-max)) (point-marker))))
1469 (muse-publish-example-tag (point-min) end)))
1471 (defun muse-publish-versify-buffer ()
1472 "Transform the current buffer as if it were a <verse> region."
1473 (muse-publish-verse-tag (point-min) (point-max))
1474 (muse-publish-markup ""
1475 `((100 ,(concat "^[" muse-regexp-blank "]*> ") 0
1476 muse-publish-markup-verse)))
1477 (goto-char (point-min)))
1479 (defmacro muse-publish-markup-attribute (beg end attrs reinterp &rest body)
1480 "Evaluate BODY within the bounds of BEG and END.
1481 ATTRS is an alist. Only the \"markup\" element of ATTRS is acted
1484 If it is omitted, publish the region with the normal Muse rules.
1485 If RE-INTERP is specified, this is done immediately in a new
1486 publishing process. Currently, RE-INTERP is specified only by
1487 the <include> tag.
1489 If \"nil\", do not mark up the region at all, but prevent it from
1490 being further interpreted by Muse.
1492 If \"example\", treat the region as if it was surrounded by the
1493 <example> tag.
1495 If \"verse\", treat the region as if it was surrounded by the
1496 <verse> tag, to preserve newlines.
1498 Otherwise, it should be the name of a function to call in the
1499 narrowed region after evaluating BODY."
1500 (let ((markup (make-symbol "markup"))
1501 (markup-function (make-symbol "markup-function")))
1502 `(let ((,markup (cdr (assoc "markup" ,attrs))))
1503 (save-restriction
1504 (narrow-to-region ,beg ,end)
1505 (goto-char (point-min))
1506 ,@body
1507 (if (not ,markup)
1508 (when ,reinterp
1509 (muse-publish-markup-region (point-min) (point-max))
1510 (muse-publish-mark-read-only (point-min) (point-max))
1511 (goto-char (point-max)))
1512 (let ((,markup-function (read ,markup)))
1513 (cond ((eq ,markup-function 'example)
1514 (setq ,markup-function #'muse-publish-examplify-buffer))
1515 ((eq ,markup-function 'verse)
1516 (setq ,markup-function #'muse-publish-versify-buffer))
1517 ((and ,markup-function (not (functionp ,markup-function)))
1518 (error "Invalid markup function `%s'" ,markup))
1519 (t nil))
1520 (if ,markup-function
1521 (funcall ,markup-function)
1522 (muse-publish-mark-read-only (point-min) (point-max))
1523 (goto-char (point-max)))))))))
1525 (put 'muse-publish-markup-attribute 'lisp-indent-function 4)
1526 (put 'muse-publish-markup-attribute 'edebug-form-spec
1527 '(form form form form body))
1529 (defun muse-publish-lisp-tag (beg end attrs)
1530 (muse-publish-markup-attribute beg end attrs nil
1531 (save-excursion
1532 (let ((str (muse-eval-lisp
1533 (prog1
1534 (concat "(progn "
1535 (buffer-substring-no-properties (point-min)
1536 (point-max))
1537 ")")
1538 (delete-region beg end)))))
1539 (set-text-properties 0 (length str) nil str)
1540 (insert str)))))
1542 (defun muse-publish-command-tag (beg end attrs)
1543 (muse-publish-markup-attribute beg end attrs nil
1544 (while (looking-at "\\s-*$")
1545 (forward-line))
1546 (let ((interp (cdr (assoc "interp" attrs))))
1547 (if interp
1548 (shell-command-on-region (point) (point-max) interp t t)
1549 (shell-command
1550 (prog1
1551 (buffer-substring-no-properties (point) (point-max))
1552 (delete-region (point-min) (point-max)))
1553 t)))
1554 ;; make sure there is a newline at end
1555 (goto-char (point-max))
1556 (forward-line 0)
1557 (unless (looking-at "\\s-*$")
1558 (goto-char (point-max))
1559 (insert ?\n))
1560 (goto-char (point-min))))
1562 (defun muse-publish-perl-tag (beg end attrs)
1563 (muse-publish-command-tag beg end
1564 (cons (cons "interp" (executable-find "perl"))
1565 attrs)))
1567 (defun muse-publish-python-tag (beg end attrs)
1568 (muse-publish-command-tag beg end
1569 (cons (cons "interp" (executable-find "python"))
1570 attrs)))
1572 (defun muse-publish-ruby-tag (beg end attrs)
1573 (muse-publish-command-tag beg end
1574 (cons (cons "interp" (executable-find "ruby"))
1575 attrs)))
1577 (defun muse-publish-comment-tag (beg end)
1578 (if (null muse-publish-comments-p)
1579 (delete-region beg end)
1580 (goto-char end)
1581 (muse-insert-markup (muse-markup-text 'comment-end))
1582 (muse-publish-mark-read-only beg end)
1583 (goto-char beg)
1584 (muse-insert-markup (muse-markup-text 'comment-begin))))
1586 (defun muse-publish-include-tag (beg end attrs)
1587 "Include the named file at the current location during publishing.
1589 <include file=\"...\" markup=\"...\">
1591 The `markup' attribute controls how this file is marked up after
1592 being inserted. See `muse-publish-markup-attribute' for an
1593 explanation of how it works."
1594 (let ((filename (cdr (assoc "file" attrs)))
1595 (muse-publishing-directives muse-publishing-directives))
1596 (if filename
1597 (setq filename (expand-file-name
1598 filename
1599 (file-name-directory muse-publishing-current-file)))
1600 (error "No file attribute specified in <include> tag"))
1601 (muse-publish-markup-attribute beg end attrs t
1602 (insert-file-contents filename))))
1604 (defun muse-publish-mark-up-tag (beg end attrs)
1605 "Run an Emacs Lisp function on the region delimted by this tag.
1607 <markup function=\"...\">
1609 The optional `function' attribute controls how this section is
1610 marked up. If used, it should be the name of a function to call
1611 with the buffer narrowed to the delimited region. Note that no
1612 further marking-up will be performed on this region.
1614 If `function' is ommitted, use the standard Muse markup function.
1615 This is useful for marking up content in headers and footers."
1616 (let ((function (cdr (assoc "function" attrs)))
1617 (muse-publishing-directives muse-publishing-directives))
1618 (if function
1619 (let ((markup-function (intern function)))
1620 (if (and markup-function (functionp markup-function))
1621 (save-restriction
1622 (narrow-to-region beg end)
1623 (funcall markup-function)
1624 (goto-char (point-max)))
1625 (error "Invalid markup function `%s'" function)))
1626 (muse-publish-markup-region beg end))
1627 (muse-publish-mark-read-only beg (point))))
1629 ;; Miscellaneous helper functions
1631 (defun muse-publish-strip-tags (string)
1632 "Remove all tags from the string."
1633 (while (string-match "<.*?>" string)
1634 (setq string (replace-match "" nil t string)))
1635 string)
1637 (defun muse-publish-markup-type (category default-func)
1638 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1639 (funcall (or rule default-func))))
1641 (defun muse-published-buffer-contents (buffer)
1642 (with-current-buffer buffer
1643 (goto-char (point-min))
1644 (let ((beg (and (search-forward "Emacs Muse begins here")
1645 (muse-line-end-position)))
1646 (end (and (search-forward "Emacs Muse ends here")
1647 (muse-line-beginning-position))))
1648 (buffer-substring-no-properties beg end))))
1650 (defun muse-published-contents (file)
1651 (when (file-readable-p file)
1652 (muse-with-temp-buffer
1653 (insert-file-contents file)
1654 (muse-published-buffer-contents (current-buffer)))))
1656 (defun muse-publish-transform-output
1657 (file temp-file output-path name gen-func &rest cleanup-exts)
1658 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1659 (setq file (muse-page-name file))
1660 (message "Generating %s output for %s..." name file)
1661 (if (not (funcall gen-func temp-file output-path))
1662 (message "Generating %s from %s...failed" name file)
1663 (message "Generating %s output for %s...done" name file)
1664 (muse-delete-file-if-exists temp-file)
1665 (dolist (ext cleanup-exts)
1666 (muse-delete-file-if-exists
1667 (expand-file-name (concat file ext)
1668 (file-name-directory output-path))))
1669 (message "Wrote %s" output-path)))
1671 (defun muse-publish-read-only (string)
1672 (let ((end (1- (length string))))
1673 (add-text-properties 0 end
1674 '(rear-nonsticky (read-only) read-only t)
1675 string)
1676 string))
1678 (provide 'muse-publish)
1680 ;;; muse-publish.el ends here