Make space around emdash optional.
[muse-el.git] / lisp / muse-publish.el
blobccea12b68a1fcafb792b6fbbce5d27f6ce3be84e
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 nil muse-publish-lisp-tag)
250 ("class" t t muse-publish-class-tag)
251 ("command" t t muse-publish-command-tag)
252 ("comment" t nil muse-publish-comment-tag)
253 ("include" nil t muse-publish-include-tag)
254 ("markup" t t muse-publish-mark-up-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 (setcdr entry elements)
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 (apply 'muse-define-style new-name
334 (append elements (list :base base-name))))
336 (defsubst muse-get-keyword (keyword list &optional direct)
337 (let ((value (cadr (memq keyword list))))
338 (if (and (not direct) (symbolp value))
339 (symbol-value value)
340 value)))
342 (defun muse-style-elements-list (elem &optional style)
343 "Return a list all references to ELEM in STYLE, including base styles.
344 If STYLE is not specified, use current style."
345 (let (base elements)
346 (while style
347 (setq style (muse-style style))
348 (setq elements (append elements
349 (muse-get-keyword elem style)))
350 (setq style (muse-get-keyword :base style)))
351 elements))
353 (defun muse-style-element (elem &optional style direct)
354 "Search for ELEM in STYLE, including base styles.
355 If STYLE is not specified, use current style."
356 (setq style (muse-style style))
357 (let ((value (muse-get-keyword elem style direct)))
358 (if value
359 value
360 (let ((base (muse-get-keyword :base style)))
361 (if base
362 (muse-style-element elem base direct))))))
364 (defun muse-find-markup-element (keyword ident style)
365 (let ((def (assq ident (muse-style-element keyword style))))
366 (if def
367 (cdr def)
368 (let ((base (muse-style-element :base style)))
369 (if base
370 (muse-find-markup-element keyword ident base))))))
372 (defun muse-markup-text (ident &rest args)
373 "Insert ARGS into the text markup associated with IDENT.
374 If the markup text has sections like %N%, this will be replaced
375 with the N-1th argument in ARGS. After that, `format' is applied
376 to the text with ARGS as parameters."
377 (let ((text (muse-find-markup-element :strings ident (muse-style))))
378 (if (and text args)
379 (progn
380 (let (start repl-text)
381 (while (setq start (string-match "%\\([1-9][0-9]*\\)%" text start))
382 ;; escape '%' in the argument text, since we will be
383 ;; using format on it
384 (setq repl-text (muse-replace-regexp-in-string
385 "%" "%%"
386 (nth (1- (string-to-number
387 (match-string 1 text))) args)
388 t t)
389 start (+ start (length repl-text))
390 text (replace-match repl-text t t text))))
391 (apply 'format text args))
392 (or text ""))))
394 (defun muse-insert-markup (&rest args)
395 (let ((beg (point)))
396 (apply 'insert args)
397 (muse-publish-mark-read-only beg (point))))
399 (defun muse-find-markup-tag (keyword tagname style)
400 (let ((def (assoc tagname (muse-style-element keyword style))))
401 (or def
402 (let ((base (muse-style-element :base style)))
403 (if base
404 (muse-find-markup-tag keyword tagname base))))))
406 (defsubst muse-markup-tag-info (tagname &rest args)
407 (let ((tag-info (muse-find-markup-tag :tags tagname (muse-style))))
408 (or tag-info
409 (assoc (match-string 1) muse-publish-markup-tags))))
411 (defsubst muse-markup-function (category)
412 (let ((func (muse-find-markup-element :functions category (muse-style))))
413 (or func
414 (cdr (assq category muse-publish-markup-functions)))))
416 ;; Publishing routines
418 (defun muse-publish-markup (name rules)
419 (let* ((case-fold-search nil)
420 (inhibit-read-only t)
421 (limit (* (length rules) (point-max)))
422 (verbose (and muse-publish-report-threshhold
423 (> (point-max) muse-publish-report-threshhold)))
424 (base 0))
425 (while rules
426 (goto-char (point-min))
427 (let ((regexp (nth 1 (car rules)))
428 (group (nth 2 (car rules)))
429 (repl (nth 3 (car rules)))
430 pos)
431 (setq muse-publishing-last-position nil)
432 (if (symbolp regexp)
433 (setq regexp (symbol-value regexp)))
434 (if (and verbose (not muse-batch-publishing-p))
435 (message "Publishing %s...%d%%" name
436 (* (/ (float (+ (point) base)) limit) 100)))
437 (while (and regexp (setq pos (re-search-forward regexp nil t)))
438 (if (and verbose (not muse-batch-publishing-p))
439 (message "Publishing %s...%d%%" name
440 (* (/ (float (+ (point) base)) limit) 100)))
441 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
442 (match-beginning group)
443 (get-text-property (match-beginning group) 'read-only))
444 (let* (func
445 (text (cond
446 ((and (symbolp repl)
447 (setq func (muse-markup-function repl)))
448 (funcall func))
449 ((functionp repl)
450 (funcall repl))
451 ((symbolp repl)
452 (symbol-value repl))
453 (t repl))))
454 (if (stringp text)
455 (replace-match text t))))
456 (if (and muse-publishing-last-position
457 (= pos muse-publishing-last-position))
458 (if (eobp)
459 (setq regexp nil)
460 (forward-char 1)))
461 (setq muse-publishing-last-position pos)))
462 (setq rules (cdr rules)
463 base (+ base (point-max))))
464 (if (and verbose (not muse-batch-publishing-p))
465 (message "Publishing %s...done" name))))
467 (defcustom muse-publish-markup-header-footer-tags
468 '(("lisp" t nil muse-publish-lisp-tag)
469 ("markup" t t muse-publish-mark-up-tag))
470 "Tags used when publishing headers and footers.
471 See `muse-publish-markup-tags' for details."
472 :type '(repeat (list (string :tag "Markup tag")
473 (boolean :tag "Expect closing tag" :value t)
474 (boolean :tag "Parse attributes" :value nil)
475 function))
476 :group 'muse-publish)
478 (defun muse-insert-file-or-string (file-or-string &optional title)
479 (let ((beg (point)) end)
480 (if (and (not (string-equal file-or-string ""))
481 (not (string-match "\n" file-or-string))
482 (file-readable-p file-or-string))
483 (setq end (+ beg (cadr (insert-file-contents file-or-string))))
484 (insert file-or-string)
485 (setq end (point)))
486 (save-restriction
487 (narrow-to-region beg end)
488 (remove-text-properties (point-min) (point-max)
489 '(read-only nil rear-nonsticky nil))
490 (goto-char (point-min))
491 (let ((muse-inhibit-style-tags t))
492 (muse-publish-markup (or title "")
493 '((100 muse-tag-regexp 0
494 muse-publish-markup-tag)))))))
496 (defun muse-style-run-hooks (keyword style &rest args)
497 (catch 'handled
498 (while (and style
499 (setq style (muse-style style)))
500 (let ((func (muse-get-keyword keyword style t)))
501 (when (and func
502 (apply func args))
503 (throw 'handled t)))
504 (setq style (muse-style-element :base style)))))
506 (defun muse-publish-markup-region (beg end &optional title style)
507 "Apply the given STYLE's markup rules to the given region.
508 TITLE is used when indicating the publishing progress; it may be nil."
509 (unless title (setq title ""))
510 (unless style
511 (or (setq style muse-publishing-current-style)
512 (error "Cannot find any publishing styles to use")))
513 (save-restriction
514 (narrow-to-region beg end)
515 (muse-style-run-hooks :before style)
516 (muse-publish-markup
517 title
518 (sort (copy-alist (append muse-publish-markup-regexps
519 (muse-style-elements-list :regexps style)))
520 (function
521 (lambda (l r)
522 (< (car l) (car r))))))
523 (muse-style-run-hooks :before-end style)
524 (muse-publish-escape-specials (point-min) (point-max) nil 'document)))
526 (defun muse-publish-markup-buffer (title style)
527 "Apply the given STYLE's markup rules to the current buffer."
528 (setq style (muse-style style))
529 (let ((style-header (muse-style-element :header style))
530 (style-footer (muse-style-element :footer style))
531 (muse-publishing-current-style style)
532 (muse-publishing-directives
533 (list (cons "title" title)
534 (cons "author" (user-full-name))
535 (cons "date" (format-time-string
536 "%B %e, %Y"
537 (if muse-publishing-current-file
538 (nth 5 (file-attributes
539 muse-publishing-current-file))
540 (current-time))))))
541 (muse-publishing-p t)
542 (inhibit-read-only t))
543 (run-hooks 'muse-update-values-hook)
544 (run-hooks 'muse-before-publish-hook)
545 (muse-publish-markup-region (point-min) (point-max) title style)
546 (goto-char (point-min))
547 (when style-header
548 (muse-insert-file-or-string style-header title))
549 (goto-char (point-max))
550 (when style-footer
551 (muse-insert-file-or-string style-footer title))
552 (muse-style-run-hooks :after style)
553 (run-hooks 'muse-after-publish-hook)))
555 (defun muse-publish-markup-string (string &optional style)
556 "Markup STRING using the given STYLE's markup rules."
557 (setq style (muse-style style))
558 (muse-with-temp-buffer
559 (insert string)
560 (let ((muse-publishing-current-style style)
561 (muse-publishing-p t))
562 (muse-publish-markup "*string*" (muse-style-element :rules style)))
563 (buffer-string)))
565 ;; Commands for publishing files
567 (defsubst muse-publish-get-style (&optional styles)
568 (unless styles (setq styles muse-publishing-styles))
569 (if (= 1 (length styles))
570 (car styles)
571 (assoc (completing-read "Publish with style: " styles nil t)
572 styles)))
574 (defsubst muse-publish-get-output-dir (style)
575 (let ((default-directory (or (muse-style-element :path style)
576 default-directory)))
577 (muse-read-directory-name "Publish to directory: " nil default-directory)))
579 (defsubst muse-publish-get-info ()
580 (let ((style (muse-publish-get-style)))
581 (list style (muse-publish-get-output-dir style)
582 current-prefix-arg)))
584 (defsubst muse-publish-output-name (&optional file style)
585 (setq style (muse-style style))
586 (concat (muse-style-element :prefix style)
587 (muse-page-name file)
588 (muse-style-element :suffix style)))
590 (defsubst muse-publish-output-file (file &optional output-dir style)
591 (setq style (muse-style style))
592 (if output-dir
593 (expand-file-name (muse-publish-output-name file style) output-dir)
594 (concat (file-name-directory file)
595 (muse-publish-output-name file style))))
597 (defsubst muse-publish-link-name (&optional file style)
598 (setq style (muse-style style))
599 (concat (muse-style-element :prefix style)
600 (muse-page-name file)
601 (or (muse-style-element :link-suffix style)
602 (muse-style-element :suffix style))))
604 (defsubst muse-publish-link-file (file &optional output-dir style)
605 (setq style (muse-style style))
606 (if output-dir
607 (expand-file-name (muse-publish-link-name file style) output-dir)
608 (concat (file-name-directory file)
609 (muse-publish-link-name file style))))
611 (defsubst muse-publish-link-page (page)
612 (if (fboundp 'muse-project-link-page)
613 (muse-project-link-page page)
614 (muse-publish-link-file page)))
616 ;;;###autoload
617 (defun muse-publish-file (file style &optional output-dir force)
618 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
619 If the argument FORCE is nil, each file is only published if it is
620 newer than the published version. If the argument FORCE is non-nil,
621 the file is published no matter what."
622 (interactive (cons (read-file-name "Publish file: ")
623 (muse-publish-get-info)))
624 (let ((style-name style))
625 (setq style (muse-style style))
626 (unless style
627 (error "There is no style '%s' defined." style-name)))
628 (let* ((output-path (muse-publish-output-file file output-dir style))
629 (output-suffix (muse-style-element :osuffix style))
630 (muse-publishing-current-file file)
631 (muse-publishing-current-output-path output-path)
632 (target (if output-suffix
633 (concat (file-name-sans-extension output-path)
634 output-suffix)
635 output-path))
636 (threshhold (nth 7 (file-attributes file))))
637 (if (not threshhold)
638 (message "Please save %s before publishing" file)
639 (when (or force (file-newer-than-file-p file target))
640 (if (and muse-publish-report-threshhold
641 (> threshhold
642 muse-publish-report-threshhold))
643 (message "Publishing %s ..." file))
644 (muse-with-temp-buffer
645 (insert-file-contents file)
646 (muse-publish-markup-buffer (muse-page-name file) style)
647 (let ((backup-inhibited t))
648 (write-file output-path))
649 (muse-style-run-hooks :final style file output-path target))
650 t))))
652 ;;;###autoload
653 (defun muse-publish-this-file (style output-dir &optional force)
654 "Publish the page in the current file."
655 (interactive (muse-publish-get-info))
656 (unless (muse-publish-file buffer-file-name style output-dir force)
657 (message (concat "The published version is up-to-date; use"
658 " C-u C-c C-t to force an update."))))
660 (defun muse-batch-publish-files ()
661 "Publish Muse files in batch mode."
662 (let ((muse-batch-publishing-p t)
663 style output-dir)
664 (setq style (car command-line-args-left)
665 command-line-args-left (cdr command-line-args-left)
666 output-dir (car command-line-args-left)
667 output-dir
668 (if (string-match "\\`--output-dir=" output-dir)
669 (prog1
670 (substring output-dir (match-end 0))
671 (setq command-line-args-left (cdr command-line-args-left)))))
672 (setq auto-mode-alist
673 (delete (cons (concat "\\." muse-file-extension "\\'")
674 'muse-mode-choose-mode)
675 auto-mode-alist))
676 (dolist (file command-line-args-left)
677 (muse-publish-file file style output-dir t))))
679 ;; Default publishing rules
681 (defun muse-publish-section-close (depth)
682 "Seach forward for the closing tag of given DEPTH."
683 (let (not-end)
684 (save-excursion
685 (while (and (setq not-end (re-search-forward
686 (concat "^\\*\\{1," (number-to-string depth)
687 "\\}\\s-+")
688 nil t))
689 (get-text-property (match-beginning 0) 'read-only)))
690 (if not-end
691 (forward-line 0)
692 (goto-char (point-max)))
693 (cond ((not (eq (char-before) ?\n))
694 (insert "\n\n"))
695 ((not (eq (char-before (1- (point))) ?\n))
696 (insert "\n")))
697 (muse-insert-markup (muse-markup-text 'section-close depth))
698 (insert "\n"))))
700 (defun muse-publish-markup-directive (&optional name value)
701 (unless name (setq name (match-string 1)))
702 (unless value (setq value (match-string 2)))
703 (let ((elem (assoc name muse-publishing-directives)))
704 (if elem
705 (setcdr elem value)
706 (setq muse-publishing-directives
707 (cons (cons name value)
708 muse-publishing-directives))))
709 ;; Make sure we don't ever try to move the point forward (past the
710 ;; beginning of buffer) while we're still searching for directives.
711 (setq muse-publishing-last-position nil)
712 (delete-region (match-beginning 0) (match-end 0)))
714 (defsubst muse-publishing-directive (name)
715 (cdr (assoc name muse-publishing-directives)))
717 (defun muse-publish-markup-anchor ()
718 (unless (get-text-property (match-end 1) 'muse-link)
719 (let ((text (muse-markup-text 'anchor (match-string 2))))
720 (unless (string= text "")
721 (save-match-data
722 (skip-chars-forward (concat muse-regexp-blank "\n"))
723 (muse-insert-markup text)))
724 (match-string 1))))
726 (defun muse-publish-markup-comment ()
727 (if (null muse-publish-comments-p)
729 (goto-char (match-end 0))
730 (muse-insert-markup (muse-markup-text 'comment-end))
731 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
732 (delete-region (match-beginning 0) (match-beginning 1))
733 (goto-char (match-beginning 0))
734 (muse-insert-markup (muse-markup-text 'comment-begin))))
736 (defvar muse-inhibit-style-tags nil
737 "If non-nil, do not search for style-specific tags.
738 This is used when publishing headers and footers.")
740 (defun muse-publish-markup-tag ()
741 (let ((tag-info (if muse-inhibit-style-tags
742 (assoc (match-string 1) muse-publish-markup-tags)
743 (muse-markup-tag-info (match-string 1)))))
744 (when (and tag-info
745 (not (get-text-property (match-beginning 0) 'read-only)))
746 (let ((closed-tag (match-string 3))
747 (start (match-beginning 0))
748 (beg (point))
749 end attrs)
750 (when (nth 2 tag-info)
751 (let ((attrstr (match-string 2)))
752 (while (and attrstr
753 (string-match (concat "\\([^"
754 muse-regexp-blank
755 "=\n]+\\)\\(=\"\\"
756 "([^\"]+\\)\"\\)?")
757 attrstr))
758 (let ((attr (cons (downcase
759 (muse-match-string-no-properties 1 attrstr))
760 (muse-match-string-no-properties 3 attrstr))))
761 (setq attrstr (replace-match "" t t attrstr))
762 (if attrs
763 (nconc attrs (list attr))
764 (setq attrs (list attr)))))))
765 (if (and (cadr tag-info) (not closed-tag))
766 (if (search-forward (concat "</" (car tag-info) ">") nil t)
767 (delete-region (match-beginning 0) (point))
768 (setq tag-info nil)))
769 (when tag-info
770 (setq end (point-marker))
771 (delete-region start beg)
772 (goto-char start)
773 (let ((args (list start end)))
774 (if (nth 2 tag-info)
775 (nconc args (list attrs)))
776 (let ((muse-inhibit-style-tags nil))
777 ;; remove the inhibition
778 (apply (nth 3 tag-info) args)))))))
779 nil)
781 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
782 "Escape specials from BEG to END using style-specific :specials.
783 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
784 CONTEXT is used to figure out what kind of specials to escape.
786 The following contexts exist in Muse.
787 'underline _underlined text_
788 'literal =monospaced text= or <code> region (monospaced, escaped)
789 'emphasis *emphasized text*
790 'email email@example.com
791 'url http://example.com
792 'url-desc [[...][description of an extended link]]
793 'example <example> region (monospaced, block context, escaped)
794 'verbatim <verbatim> region (escaped)
795 'document normal text"
796 (let ((specials (muse-style-element :specials nil t)))
797 (cond ((functionp specials)
798 (setq specials (funcall specials context)))
799 ((symbolp specials)
800 (setq specials (symbol-value specials))))
801 (if (functionp specials)
802 (funcall specials beg end ignore-read-only)
803 (save-excursion
804 (save-restriction
805 (narrow-to-region beg end)
806 (goto-char (point-min))
807 (while (< (point) (point-max))
808 (if (and (not ignore-read-only)
809 (get-text-property (point) 'read-only))
810 (goto-char (or (next-single-property-change (point) 'read-only)
811 (point-max)))
812 (let ((repl (or (assoc (char-after) specials)
813 (assoc (char-after)
814 muse-publish-markup-specials))))
815 (if (null repl)
816 (forward-char 1)
817 (delete-char 1)
818 (insert-before-markers (cdr repl)))))))))))
820 (defun muse-publish-markup-word ()
821 (let* ((beg (match-beginning 2))
822 (end (1- (match-end 2)))
823 (leader (buffer-substring-no-properties beg end))
824 open-tag close-tag mark-read-only loc context)
825 (cond
826 ((string= leader "_")
827 (setq context 'underline
828 open-tag (muse-markup-text 'begin-underline)
829 close-tag (muse-markup-text 'end-underline)))
830 ((string= leader "=")
831 (setq context 'literal
832 open-tag (muse-markup-text 'begin-literal)
833 close-tag (muse-markup-text 'end-literal))
834 (setq mark-read-only t))
836 (let ((l (length leader)))
837 (setq context 'emphasis)
838 (cond
839 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
840 close-tag (muse-markup-text 'end-emph)))
841 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
842 close-tag (muse-markup-text 'end-more-emph)))
843 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
844 close-tag (muse-markup-text 'end-most-emph)))
845 (t (setq context nil))))))
846 (if (and context
847 (not (get-text-property beg 'muse-link))
848 (setq loc (search-forward leader nil t))
849 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
850 (not (eq (char-syntax (char-before (point))) ?\ ))
851 (not (get-text-property (point) 'muse-link)))
852 (progn
853 (replace-match "")
854 (delete-region beg end)
855 (setq end (point-marker))
856 (muse-insert-markup close-tag)
857 (goto-char beg)
858 (muse-insert-markup open-tag)
859 (setq beg (point))
860 (when mark-read-only
861 (muse-publish-escape-specials beg end t context)
862 (muse-publish-mark-read-only beg end)))
863 (backward-char))
864 nil))
866 (defun muse-publish-markup-emdash ()
867 (unless (get-text-property (match-beginning 0) 'muse-link)
868 (let ((prespace (match-string 1))
869 (postspace (match-string 2)))
870 (delete-region (match-beginning 0) (match-end 0))
871 (muse-insert-markup (muse-markup-text 'emdash prespace postspace))
872 (when (eq (char-after) ?\<)
873 (insert ?\n)))))
875 (defun muse-publish-markup-enddots ()
876 (unless (get-text-property (match-beginning 0) 'muse-link)
877 (delete-region (match-beginning 0) (match-end 0))
878 (muse-insert-markup (muse-markup-text 'enddots))))
880 (defun muse-publish-markup-dots ()
881 (unless (get-text-property (match-beginning 0) 'muse-link)
882 (delete-region (match-beginning 0) (match-end 0))
883 (muse-insert-markup (muse-markup-text 'dots))))
885 (defun muse-publish-markup-rule ()
886 (unless (get-text-property (match-beginning 0) 'muse-link)
887 (delete-region (match-beginning 0) (match-end 0))
888 (muse-insert-markup (muse-markup-text 'rule))))
890 (defun muse-publish-markup-no-break-space ()
891 (unless (get-text-property (match-beginning 0) 'muse-link)
892 (delete-region (match-beginning 0) (match-end 0))
893 (muse-insert-markup (muse-markup-text 'no-break-space))))
895 (defun muse-publish-markup-heading ()
896 (let* ((len (length (match-string 1)))
897 (start (muse-markup-text
898 (cond ((= len 1) 'section)
899 ((= len 2) 'subsection)
900 ((= len 3) 'subsubsection)
901 (t 'section-other))
902 len))
903 (end (muse-markup-text
904 (cond ((= len 1) 'section-end)
905 ((= len 2) 'subsection-end)
906 ((= len 3) 'subsubsection-end)
907 (t 'section-other-end))
908 len)))
909 (delete-region (match-beginning 0) (match-end 0))
910 (muse-insert-markup start)
911 (end-of-line)
912 (when end
913 (muse-insert-markup end))
914 (muse-publish-section-close len)))
916 (defvar muse-publish-footnotes nil)
918 (defun muse-publish-markup-footnote ()
919 "Scan ahead and snarf up the footnote body"
920 (cond
921 ((get-text-property (match-beginning 0) 'muse-link)
922 nil)
923 ((= (muse-line-beginning-position) (match-beginning 0))
926 (let ((footnote (save-match-data
927 (string-to-number (match-string 1))))
928 footnotemark)
929 (delete-region (match-beginning 0) (match-end 0))
930 (save-excursion
931 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
932 (let* ((start (match-beginning 0))
933 (beg (goto-char (match-end 0)))
934 (end (save-excursion
935 (if (search-forward "\n\n" nil t)
936 (copy-marker (match-beginning 0))
937 (goto-char (point-max))
938 (skip-chars-backward "\n")
939 (point-marker)))))
940 (while (re-search-forward
941 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
942 end t)
943 (replace-match "\\1" t))
944 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
945 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
946 (if (string= "" footnotemark-cmd)
947 (setq footnotemark
948 (concat (muse-markup-text 'footnote)
949 (buffer-substring-no-properties beg end)
950 (muse-markup-text 'footnote-end)))
951 (setq footnotemark (format footnotemark-cmd footnote
952 footnotemark-end-cmd))
953 (unless muse-publish-footnotes
954 (set (make-local-variable 'muse-publish-footnotes)
955 (make-vector 256 nil)))
956 (unless (aref muse-publish-footnotes footnote)
957 (setq footnotemark
958 (concat
959 footnotemark
960 (concat (format (muse-markup-text 'footnotetext)
961 footnote)
962 (buffer-substring-no-properties beg end)
963 (muse-markup-text 'footnotetext-end))))
964 (aset muse-publish-footnotes footnote footnotemark))))
965 (goto-char end)
966 (skip-chars-forward "\n")
967 (delete-region start (point)))))
968 (muse-insert-markup (or footnotemark footnote))))))
970 (defun muse-publish-markup-fn-sep ()
971 (delete-region (match-beginning 0) (match-end 0))
972 (muse-insert-markup (muse-markup-text 'fn-sep)))
974 (defun muse-insert-markup-end-list (&rest args)
975 (let ((beg (point)))
976 (apply 'insert args)
977 (add-text-properties beg (point) '(end-list t))
978 (muse-publish-mark-read-only beg (point))))
980 (defun muse-publish-surround-dl (indent post-indent)
981 (let* ((beg-item (muse-markup-text 'begin-dl-item))
982 (end-item (muse-markup-text 'end-dl-item))
983 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
984 (end-ddt (muse-markup-text 'end-ddt))
985 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
986 (end-dde (muse-markup-text 'end-dde))
987 (continue t)
988 def-on-same-line beg)
989 (while continue
990 ;; envelope this as one term+definitions unit -- HTML does not
991 ;; need this, but DocBook and Muse's custom XML format do
992 (muse-insert-markup beg-item)
993 (when (looking-at muse-dl-term-regexp)
994 ;; find the term and wrap it with published markup
995 (setq beg (point))
996 (goto-char (match-end 1))
997 (delete-region (point) (match-end 0))
998 (muse-insert-markup-end-list end-ddt)
999 ;; if definition is immediately after term, move to next line
1000 (unless (eq (char-after) ?\n)
1001 (insert ?\n))
1002 (save-excursion
1003 (goto-char beg)
1004 (delete-region (point) (match-beginning 1))
1005 (muse-insert-markup beg-ddt)))
1006 (setq beg (point)
1007 ;; move past current item
1008 continue (muse-forward-list-item 'dl-term indent))
1009 (save-restriction
1010 (narrow-to-region beg (point))
1011 (goto-char (point-min))
1012 ;; publish each definition that we find, defaulting to an
1013 ;; empty definition if none are found
1014 (muse-publish-surround-text beg-dde end-dde
1015 (lambda (indent)
1016 (muse-forward-list-item 'dl-entry indent))
1017 nil nil t)
1018 (goto-char (point-max))
1019 (skip-chars-backward (concat muse-regexp-blank "\n"))
1020 (muse-insert-markup-end-list end-item)
1021 (when continue
1022 (goto-char (point-max)))))))
1024 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent determine-indent)
1025 (let ((continue t)
1026 (list-item (format muse-list-item-regexp
1027 (concat "[" muse-regexp-blank "]*")))
1028 init-indent beg)
1029 (unless indent
1030 (setq indent (concat "[" muse-regexp-blank "]+")))
1031 (if post-indent
1032 (setq post-indent (concat " \\{0," (number-to-string post-indent)
1033 "\\}"))
1034 (setq post-indent ""))
1035 (while continue
1036 (muse-insert-markup beg-tag)
1037 (setq beg (point)
1038 ;; move past current item; continue is non-nil if there
1039 ;; are more like items to be processed
1040 continue (funcall move-func indent))
1041 (save-restriction
1042 (when determine-indent
1043 ;; if the caller doesn't know how much indentation
1044 ;; to use, figure it out ourselves
1045 (if (not continue)
1046 (setq indent "")
1047 (save-match-data
1048 ;; snarf all leading whitespace
1049 (let ((this-indent (and (match-beginning 2)
1050 (buffer-substring (match-beginning 1)
1051 (match-beginning 2)))))
1052 (when (and this-indent
1053 (not (string= this-indent "")))
1054 (setq indent this-indent
1055 determine-indent nil))))))
1056 (when continue
1057 ;; remove list markup if we encountered another item of the
1058 ;; same type
1059 (replace-match "" t t nil 1))
1060 (narrow-to-region beg (point))
1061 ;; narrow to current item
1062 (goto-char (point-min))
1063 (forward-line 1)
1064 (let ((list-nested nil)
1065 (indent-found nil)
1066 (post-indent post-indent))
1067 (while (< (point) (point-max))
1068 (when (and (looking-at list-item)
1069 (not (or (get-text-property
1070 (muse-list-item-critical-point) 'read-only)
1071 (get-text-property
1072 (muse-list-item-critical-point) 'muse-link))))
1073 ;; if we encounter a list item, allow no post-indent
1074 ;; space
1075 (setq list-nested t))
1076 (when (looking-at (concat indent "\\("
1077 (or (and list-nested "")
1078 post-indent)
1079 "\\)"))
1080 ;; if list is not nested, remove indentation
1081 (unless indent-found
1082 (setq post-indent (match-string 1)
1083 indent-found t))
1084 (replace-match ""))
1085 (forward-line 1)))
1086 (skip-chars-backward (concat muse-regexp-blank "\n"))
1087 (muse-insert-markup-end-list end-tag)
1088 (when continue
1089 (goto-char (point-max)))))))
1091 (defun muse-publish-markup-list ()
1092 "Markup a list entry.
1093 This function works by marking up items of the same list level
1094 and type, respecting the end-of-list property."
1095 (let* ((str (match-string 1))
1096 (type (muse-list-item-type str))
1097 (indent (buffer-substring (muse-line-beginning-position)
1098 (match-beginning 1)))
1099 (post-indent (length str))
1100 (last (match-beginning 0)))
1101 (cond
1102 ((or (get-text-property (muse-list-item-critical-point) 'read-only)
1103 (get-text-property (muse-list-item-critical-point) 'muse-link))
1104 nil)
1105 ((eq type 'ul)
1106 (unless (eq (char-after (match-end 1)) ?-)
1107 (delete-region (match-beginning 0) (match-end 0))
1108 (muse-insert-markup (muse-markup-text 'begin-uli))
1109 (save-excursion
1110 (muse-publish-surround-text
1111 (muse-markup-text 'begin-uli-item)
1112 (muse-markup-text 'end-uli-item)
1113 (lambda (indent)
1114 (muse-forward-list-item 'ul indent))
1115 indent post-indent)
1116 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1117 (forward-line 1)))
1118 ((eq type 'ol)
1119 (delete-region (match-beginning 0) (match-end 0))
1120 (muse-insert-markup (muse-markup-text 'begin-oli))
1121 (save-excursion
1122 (muse-publish-surround-text
1123 (muse-markup-text 'begin-oli-item)
1124 (muse-markup-text 'end-oli-item)
1125 (lambda (indent)
1126 (muse-forward-list-item 'ol indent))
1127 indent post-indent)
1128 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1129 (forward-line 1))
1130 ((not (string= (match-string 2) ""))
1131 ;; must have an initial term
1132 (goto-char (match-beginning 0))
1133 (muse-insert-markup (muse-markup-text 'begin-dl))
1134 (save-excursion
1135 (muse-publish-surround-dl indent post-indent)
1136 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1137 (forward-line 1))))
1138 nil)
1140 (defun muse-publish-markup-quote ()
1141 "Markup a quoted paragraph.
1142 The reason this function is so funky, is to prevent text properties
1143 like read-only from being inadvertently deleted."
1144 (let* ((ws (match-string 1))
1145 (centered (>= (string-width ws) 6))
1146 (begin-elem (if centered 'begin-center 'begin-quote-item))
1147 (end-elem (if centered 'end-center 'end-quote-item)))
1148 (replace-match "" t t nil 1)
1149 (unless centered
1150 (muse-insert-markup (muse-markup-text 'begin-quote)))
1151 (muse-publish-surround-text (muse-markup-text begin-elem)
1152 (muse-markup-text end-elem)
1153 (function (lambda (indent)
1154 (muse-forward-paragraph)
1155 nil)))
1156 (unless centered
1157 (muse-insert-markup (muse-markup-text 'end-quote)))))
1159 (defun muse-publish-markup-leading-space (markup-space multiple)
1160 (let (count)
1161 (when (and markup-space
1162 (>= (setq count (skip-chars-forward " ")) 0))
1163 (delete-region (muse-line-beginning-position) (point))
1164 (while (> count 0)
1165 (muse-insert-markup markup-space)
1166 (setq count (- count multiple))))))
1168 (defun muse-publish-markup-verse ()
1169 (let ((leader (match-string 0)))
1170 (goto-char (match-beginning 0))
1171 (muse-insert-markup (muse-markup-text 'begin-verse))
1172 (while (looking-at leader)
1173 (replace-match "")
1174 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1175 (let ((beg (point)))
1176 (end-of-line)
1177 (cond
1178 ((bolp)
1179 (let ((text (muse-markup-text 'empty-verse-line)))
1180 (when text (muse-insert-markup text))))
1181 ((save-excursion
1182 (save-match-data
1183 (forward-line 1)
1184 (or (looking-at (concat leader "["
1185 muse-regexp-blank
1186 "]*$"))
1187 (not (looking-at leader)))))
1188 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1189 (end-text (muse-markup-text 'end-last-stanza-line)))
1190 (when end-text (muse-insert-markup end-text))
1191 (goto-char beg)
1192 (when begin-text (muse-insert-markup begin-text))
1193 (end-of-line)))
1195 (let ((begin-text (muse-markup-text 'begin-verse-line))
1196 (end-text (muse-markup-text 'end-verse-line)))
1197 (when end-text (muse-insert-markup end-text))
1198 (goto-char beg)
1199 (when begin-text (muse-insert-markup begin-text))
1200 (end-of-line))))
1201 (forward-line 1))))
1202 (muse-insert-markup (muse-markup-text 'end-verse))
1203 (insert ?\n))
1205 (defun muse-publish-table-fields (beg end)
1206 "Parse given region as a table, returning a cons cell.
1207 The car is the length of the longest row.
1209 The cdr is a list of the fields of the table, with the first
1210 element indicating the type of the row:
1211 1: body, 2: header, 3: footer.
1213 The existing region will be removed, except for initial blank lines."
1214 (unless (muse-publishing-directive "disable-tables")
1215 (let ((longest 0)
1216 (left 0)
1217 fields field-list)
1218 (save-restriction
1219 (narrow-to-region beg end)
1220 (goto-char (point-min))
1221 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1222 (forward-line 1))
1223 (setq beg (point))
1224 (while (= left 0)
1225 (when (looking-at muse-table-line-regexp)
1226 (setq fields (cons (length (match-string 1))
1227 (split-string (match-string 0)
1228 muse-table-field-regexp))
1229 field-list (cons fields field-list)
1230 longest (max (length fields) longest)))
1231 (setq left (forward-line 1))))
1232 (delete-region beg end)
1233 (if (= longest 0)
1234 (cons 0 nil)
1235 (cons (1- longest) (nreverse field-list))))))
1237 (defun muse-publish-markup-table ()
1238 "Style does not support tables.")
1240 (defun muse-publish-escape-specials-in-string (string &optional context)
1241 "Escape specials in STRING using style-specific :specials.
1242 CONTEXT is used to figure out what kind of specials to escape.
1244 See the documentation of the `muse-publish-escape-specials'
1245 function for the list of available contexts."
1246 (unless string
1247 (setq string ""))
1248 (let ((specials (muse-style-element :specials nil t)))
1249 (cond ((functionp specials)
1250 (setq specials (funcall specials context)))
1251 ((symbolp specials)
1252 (setq specials (symbol-value specials))))
1253 (if (functionp specials)
1254 (funcall specials string)
1255 (apply (function concat)
1256 (mapcar
1257 (lambda (ch)
1258 (let (repl)
1259 (setq repl (or (assoc ch specials)
1260 (assoc ch muse-publish-markup-specials)))
1261 (if (null repl)
1262 (char-to-string ch)
1263 (cdr repl))))
1264 (append string nil))))))
1266 (defun muse-publish-markup-email ()
1267 (let* ((beg (match-end 1))
1268 (addr (buffer-substring-no-properties beg (match-end 0))))
1269 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1270 (goto-char beg)
1271 (delete-region beg (match-end 0))
1272 (if (or (eq (char-before (match-beginning 0)) ?\")
1273 (eq (char-after (match-end 0)) ?\"))
1274 (insert addr)
1275 (insert (format (muse-markup-text 'email-addr) addr addr)))
1276 (muse-publish-mark-read-only beg (point))))
1278 (defun muse-publish-classify-url (target)
1279 "Transform anchors and get published name, if TARGET is a page.
1280 The return value is a cons cell. The car is the type of link,
1281 the cadr is the page name, and the cddr is the anchor."
1282 (save-match-data
1283 (cond ((or (null target) (string= target ""))
1284 nil)
1285 ((string-match muse-url-regexp target)
1286 (cons 'url (cons target nil)))
1287 ((string-match muse-image-regexp target)
1288 (cons 'image (cons target nil)))
1289 ((string-match muse-file-regexp target)
1290 (cons 'file (cons target nil)))
1291 ((string-match "#" target)
1292 (if (eq (aref target 0) ?\#)
1293 (cons 'anchor-ref (cons nil (substring target 1)))
1294 (cons 'link-and-anchor
1295 (cons (muse-publish-link-page
1296 (substring target 0 (match-beginning 0)))
1297 (substring target (match-end 0))))))
1299 (cons 'link (cons (muse-publish-link-page target) nil))))))
1301 (defun muse-publish-url (url &optional desc explicit)
1302 "Resolve a URL into its final <a href> form."
1303 (let ((orig-url url)
1304 type anchor)
1305 (dolist (transform muse-publish-url-transforms)
1306 (setq url (save-match-data (when url (funcall transform url explicit)))))
1307 (dolist (transform muse-publish-desc-transforms)
1308 (setq desc (save-match-data
1309 (when desc (funcall transform desc explicit)))))
1310 (when desc
1311 (setq desc (muse-publish-escape-specials-in-string desc 'url-desc)))
1312 (setq orig-url
1313 (muse-publish-escape-specials-in-string orig-url 'url-desc))
1314 (let ((target (muse-publish-classify-url url)))
1315 (setq type (car target)
1316 url (muse-publish-escape-specials-in-string (cadr target) 'url)
1317 anchor (muse-publish-escape-specials-in-string
1318 (cddr target) 'url)))
1319 (cond ((eq type 'anchor-ref)
1320 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1321 ((string= url "")
1322 desc)
1323 ((eq type 'image)
1324 (if desc
1325 (muse-markup-text 'image-with-desc url desc)
1326 (muse-markup-text 'image-link url)))
1327 ((eq type 'link-and-anchor)
1328 (muse-markup-text 'link-and-anchor url anchor
1329 (or desc orig-url)))
1330 ((and desc (string-match muse-image-regexp desc))
1331 (muse-markup-text 'url-with-image url desc))
1332 ((eq type 'link)
1333 (muse-markup-text 'link url (or desc orig-url)))
1335 (or (and desc
1336 (let ((text (muse-markup-text 'url-and-desc url desc)))
1337 (and (not (string= text ""))
1338 text)))
1339 (muse-markup-text 'url url (or desc orig-url)))))))
1341 (defun muse-publish-insert-url (url &optional desc explicit)
1342 "Resolve a URL into its final <a href> form."
1343 (delete-region (match-beginning 0) (match-end 0))
1344 (let ((text (muse-publish-url url desc explicit)))
1345 (when text
1346 (muse-insert-markup text))))
1348 (defun muse-publish-markup-link ()
1349 (let (desc explicit link)
1350 (setq explicit (save-match-data
1351 (if (string-match muse-explicit-link-regexp
1352 (match-string 0))
1353 t nil)))
1354 (setq desc (if explicit
1355 (or (match-string 2) (match-string 1))
1356 (match-string 0)))
1357 (setq link (if explicit
1358 (muse-handle-explicit-link (match-string 1))
1359 (muse-handle-implicit-link (match-string 0))))
1360 (when (and link
1361 (or explicit
1362 (not (or (eq (char-before (match-beginning 0)) ?\")
1363 (eq (char-after (match-end 0)) ?\")))))
1364 ;; if explicit link has no user-provided description, treat it
1365 ;; as if it were an implicit link
1366 (when (and explicit (not (match-string 2)))
1367 (setq explicit nil))
1368 (muse-publish-insert-url link desc explicit))))
1370 (defun muse-publish-markup-url ()
1371 (unless (or (eq (char-before (match-beginning 0)) ?\")
1372 (eq (char-after (match-end 0)) ?\"))
1373 (muse-publish-insert-url (match-string 0))))
1375 ;; Default publishing tags
1377 (defcustom muse-publish-contents-depth 2
1378 "The number of heading levels to include with <contents> tags."
1379 :type 'integer
1380 :group 'muse-publish)
1382 (defun muse-publish-contents-tag (beg end attrs)
1383 (set (make-local-variable 'muse-publish-generate-contents)
1384 (cons (copy-marker (point) t)
1385 (let ((depth (cdr (assoc "depth" attrs))))
1386 (or (and depth (string-to-number depth))
1387 muse-publish-contents-depth)))))
1389 (defun muse-publish-verse-tag (beg end)
1390 (save-excursion
1391 (save-restriction
1392 (narrow-to-region beg end)
1393 (goto-char (point-min))
1394 (while (eq ?\ (char-syntax (char-after)))
1395 (delete-char 1))
1396 (while (< (point) (point-max))
1397 (insert "> ")
1398 (forward-line))
1399 (if (eq ?\ (char-syntax (char-before)))
1400 (delete-char -1)))))
1402 (defun muse-publish-mark-read-only (beg end)
1403 "Add read-only properties to the given region."
1404 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1405 nil)
1407 (defun muse-publish-mark-link (&optional beg end)
1408 "Indicate that the given region is a Muse link, so that other
1409 markup elements respect it. If a region is not specified, use
1410 the 0th match data to determine it.
1412 This is usually applied to extended links."
1413 (unless beg (setq beg (match-beginning 0)))
1414 (unless end (setq end (match-end 0)))
1415 (add-text-properties beg end '(muse-link t))
1416 nil)
1418 (defun muse-publish-quote-tag (beg end)
1419 (save-excursion
1420 (save-restriction
1421 (narrow-to-region beg end)
1422 (muse-insert-markup (muse-markup-text 'begin-quote))
1423 (muse-publish-surround-text (muse-markup-text 'begin-quote-item)
1424 (muse-markup-text 'end-quote-item)
1425 (function (lambda (indent)
1426 (muse-forward-paragraph)
1427 (goto-char (match-end 0))
1428 (< (point) (point-max)))))
1429 (muse-insert-markup (muse-markup-text 'end-quote)))))
1431 (defun muse-publish-code-tag (beg end)
1432 (muse-publish-escape-specials beg end nil 'literal)
1433 (goto-char beg)
1434 (insert (muse-markup-text 'begin-literal))
1435 (goto-char end)
1436 (insert (muse-markup-text 'end-literal))
1437 (muse-publish-mark-read-only beg (point)))
1439 (defun muse-publish-example-tag (beg end)
1440 (muse-publish-escape-specials beg end nil 'example)
1441 (goto-char beg)
1442 (insert (muse-markup-text 'begin-example))
1443 (goto-char end)
1444 (insert (muse-markup-text 'end-example))
1445 (muse-publish-mark-read-only beg (point)))
1447 (defun muse-publish-verbatim-tag (beg end)
1448 (muse-publish-escape-specials beg end nil 'verbatim)
1449 (muse-publish-mark-read-only beg end))
1451 (defalias 'muse-publish-class-tag 'ignore)
1453 (defun muse-publish-lisp-tag (beg end)
1454 (save-excursion
1455 (let ((str (muse-eval-lisp
1456 (prog1
1457 (concat "(progn "
1458 (buffer-substring-no-properties beg end)
1459 ")")
1460 (delete-region beg end)))))
1461 (set-text-properties 0 (length str) nil str)
1462 (insert str))))
1464 (defun muse-publish-command-tag (beg end attrs)
1465 (while (looking-at "\\s-*$")
1466 (forward-line))
1467 (let ((interp (cdr (assoc "interp" attrs))))
1468 (if (null interp)
1469 (shell-command
1470 (prog1
1471 (buffer-substring-no-properties (point) end)
1472 (delete-region beg end)) t)
1473 (shell-command-on-region beg end interp t t))
1474 (muse-publish-mark-read-only beg (point))))
1476 (defun muse-publish-comment-tag (beg end)
1477 (if (null muse-publish-comments-p)
1478 (delete-region beg end)
1479 (goto-char end)
1480 (muse-insert-markup (muse-markup-text 'comment-end))
1481 (muse-publish-mark-read-only beg end)
1482 (goto-char beg)
1483 (muse-insert-markup (muse-markup-text 'comment-begin))))
1485 (defun muse-publish-examplify-buffer ()
1486 "Transform the current buffer as if it were an <example> region."
1487 (let ((end (progn (goto-char (point-max)) (point-marker))))
1488 (muse-publish-example-tag (point-min) end)))
1490 (defun muse-publish-versify-buffer ()
1491 "Transform the current buffer as if it were a <verse> region."
1492 (muse-publish-verse-tag (point-min) (point-max))
1493 (muse-publish-markup ""
1494 `((100 ,(concat "^[" muse-regexp-blank "]*> ") 0
1495 muse-publish-markup-verse))))
1497 (defun muse-publish-include-tag (beg end attrs)
1498 "Include the named file at the current location during publishing.
1500 <include file=\"...\" markup=\"...\">
1502 The `markup' attribute controls how this section is marked up.
1503 If it is omitted, publish the included text with the normal Muse
1504 rules.
1506 If \"nil\", do not mark up the included text at all.
1508 If \"example\", treat the included text as if it was surrounded
1509 by the <example> tag.
1511 If \"verse\", treat the included text as if it was surrounded
1512 by the <verse> tag, to preserve newlines.
1514 Otherwise, it should be the name of a function to call after
1515 inserting the file with the buffer narrowed to the section
1516 inserted.
1518 Note that no further marking-up will be performed on this
1519 region."
1520 (let ((filename (cdr (assoc "file" attrs)))
1521 (markup (cdr (assoc "markup" attrs)))
1522 (muse-publishing-directives muse-publishing-directives))
1523 (if filename
1524 (setq filename (expand-file-name
1525 filename
1526 (file-name-directory muse-publishing-current-file)))
1527 (error "No file attribute specified in <include> tag"))
1528 (if markup
1529 (let ((markup-function (read markup)))
1530 (cond ((eq markup-function 'example)
1531 (setq markup-function #'muse-publish-examplify-buffer))
1532 ((eq markup-function 'verse)
1533 (setq markup-function #'muse-publish-versify-buffer))
1534 ((and markup-function (not (functionp markup-function)))
1535 (error "Invalid markup function `%s'" markup))
1536 (t nil))
1537 (save-restriction
1538 (narrow-to-region beg end)
1539 (insert-file-contents filename)
1540 (when markup-function
1541 (funcall markup-function))
1542 (goto-char (point-max))))
1543 (insert-file-contents filename)
1544 (muse-publish-markup-region beg (point)))
1545 (muse-publish-mark-read-only beg (point))))
1547 (defun muse-publish-mark-up-tag (beg end attrs)
1548 "Run an Emacs Lisp function on the region delimted by this tag.
1550 <markup function=\"...\">
1552 The optional `function' attribute controls how this section is
1553 marked up. If used, it should be the name of a function to call
1554 with the buffer narrowed to the delimited region. Note that no
1555 further marking-up will be performed on this region.
1557 If `function' is ommitted, use the standard Muse markup function.
1558 This is useful for marking up content in headers and footers."
1559 (let ((function (cdr (assoc "function" attrs)))
1560 (muse-publishing-directives muse-publishing-directives))
1561 (if function
1562 (let ((markup-function (intern function)))
1563 (if (and markup-function (functionp markup-function))
1564 (save-restriction
1565 (narrow-to-region beg end)
1566 (funcall markup-function)
1567 (goto-char (point-max)))
1568 (error "Invalid markup function `%s'" function)))
1569 (muse-publish-markup-region beg end))
1570 (muse-publish-mark-read-only beg (point))))
1572 ;; Miscellaneous helper functions
1574 (defun muse-publish-strip-tags (string)
1575 "Remove all tags from the string."
1576 (while (string-match "<.*?>" string)
1577 (setq string (replace-match "" nil t string)))
1578 string)
1580 (defun muse-publish-markup-type (category default-func)
1581 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1582 (funcall (or rule default-func))))
1584 (defun muse-published-buffer-contents (buffer)
1585 (with-current-buffer buffer
1586 (goto-char (point-min))
1587 (let ((beg (and (search-forward "Emacs Muse begins here")
1588 (muse-line-end-position)))
1589 (end (and (search-forward "Emacs Muse ends here")
1590 (muse-line-beginning-position))))
1591 (buffer-substring-no-properties beg end))))
1593 (defun muse-published-contents (file)
1594 (when (file-readable-p file)
1595 (muse-with-temp-buffer
1596 (insert-file-contents file)
1597 (muse-published-buffer-contents (current-buffer)))))
1599 (defun muse-publish-transform-output
1600 (file temp-file output-path name gen-func &rest cleanup-exts)
1601 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1602 (setq file (muse-page-name file))
1603 (message "Generating %s output for %s..." name file)
1604 (if (not (funcall gen-func temp-file output-path))
1605 (message "Generating %s from %s...failed" name file)
1606 (message "Generating %s output for %s...done" name file)
1607 (muse-delete-file-if-exists temp-file)
1608 (dolist (ext cleanup-exts)
1609 (muse-delete-file-if-exists
1610 (expand-file-name (concat file ext)
1611 (file-name-directory output-path))))
1612 (message "Wrote %s" output-path)))
1614 (defun muse-publish-read-only (string)
1615 (let ((end (1- (length string))))
1616 (add-text-properties 0 end
1617 '(rear-nonsticky (read-only) read-only t)
1618 string)
1619 string))
1621 (provide 'muse-publish)
1623 ;;; muse-publish.el ends here