Emacs21 fixes
[muse-el.git] / lisp / muse-publish.el
blobcabce6f9653061d2c2ced5b45e8a79d4e9587f03
1 ;;; muse-publish.el --- base publishing implementation
3 ;; Copyright (C) 2004, 2005, 2006, 2007 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 3, 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, as well as some code for
34 ;; the "style" element of the <literal> tag.
36 ;;; Code:
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
40 ;; Muse Publishing
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 (provide 'muse-publish)
46 (require 'muse)
47 (require 'muse-regexps)
49 (defgroup muse-publish nil
50 "Options controlling the general behavior of Muse publishing."
51 :group 'muse)
53 (defcustom muse-before-publish-hook nil
54 "A hook run in the buffer to be published, before it is done."
55 :type 'hook
56 :group 'muse-publish)
58 (defcustom muse-after-publish-hook nil
59 "A hook run in the buffer to be published, after it is done."
60 :type 'hook
61 :group 'muse-publish)
63 (defcustom muse-publish-url-transforms
64 '(muse-resolve-url)
65 "A list of functions used to prepare URLs for publication.
66 Each is passed the URL. The transformed URL should be returned."
67 :type 'hook
68 :options '(muse-resolve-url)
69 :group 'muse-publish)
71 (defcustom muse-publish-desc-transforms
72 '(muse-publish-strip-URL)
73 "A list of functions used to prepare URL desciptions for publication.
74 Each is passed the description. The modified description should
75 be returned."
76 :type 'hook
77 :options '(muse-publish-strip-URL)
78 :group 'muse-publish)
80 (defcustom muse-publish-date-format "%B %e, %Y"
81 "Format string for the date, used by `muse-publish-markup-buffer'.
82 See `format-time-string' for details on the format options."
83 :type 'string
84 :group 'muse-publish)
86 (defcustom muse-publish-comments-p nil
87 "If nil, remove comments before publishing.
88 If non-nil, publish comments using the markup of the current style."
89 :type 'boolean
90 :group 'muse-publish)
92 (defcustom muse-publish-report-threshhold 100000
93 "If a file is this size or larger, report publishing progress."
94 :type 'integer
95 :group 'muse-publish)
97 (defcustom muse-publish-markup-regexps
98 `(;; Remove leading and trailing whitespace from the file
99 (1000 "\\(\\`\n+\\|\n+\\'\\)" 0 "")
101 ;; Remove trailing whitespace from all lines
102 (1100 ,(concat "[" muse-regexp-blank "]+$") 0 "")
104 ;; Handle any leading #directives
105 (1200 "\\`#\\([a-zA-Z-]+\\)\\s-+\\(.+\\)\n+" 0 directive)
107 ;; commented lines
108 (1250 ,(concat "^;\\(?:[" muse-regexp-blank "]+\\(.+\\)\\|$\\|'\\)")
109 0 comment)
111 ;; markup tags
112 (1300 muse-tag-regexp 0 tag)
114 ;; prevent emphasis characters in explicit links from being marked
115 (1400 muse-explicit-link-regexp 0 muse-publish-mark-link)
117 ;; emphasized or literal text
118 (1600 ,(concat "\\(^\\|[-[" muse-regexp-blank
119 "<('`\"\n]\\)\\(=[^=" muse-regexp-blank
120 "\n]\\|_[^_" muse-regexp-blank
121 "\n]\\|\\*+[^*" muse-regexp-blank
122 "\n]\\)")
123 2 word)
125 ;; headings, outline-mode style
126 (1700 "^\\(\\*+\\)\\s-+" 0 heading)
128 ;; ellipses
129 (1800 "\\.\\.\\.\\." 0 enddots)
130 (1850 "\\.\\.\\." 0 dots)
132 ;; horizontal rule, or section separator
133 (1900 "^----+" 0 rule)
135 ;; non-breaking space
136 (1950 "~~" 0 no-break-space)
138 ;; beginning of footnotes section
139 (2000 "^Footnotes:?\\s-*" 0 fn-sep)
140 ;; footnote definition/reference (def if at beginning of line)
141 (2100 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote)
143 ;; unnumbered List items begin with a -. numbered list items
144 ;; begin with number and a period. definition lists have a
145 ;; leading term separated from the body with ::. centered
146 ;; paragraphs begin with at least six columns of whitespace; any
147 ;; other whitespace at the beginning indicates a blockquote. The
148 ;; reason all of these rules are handled here, is so that
149 ;; blockquote detection doesn't interfere with indented list
150 ;; members.
151 (2200 ,(format muse-list-item-regexp (concat "[" muse-regexp-blank "]*"))
152 0 list)
154 ;; support table.el style tables
155 (2300 ,(concat "^" muse-table-el-border-regexp "\n"
156 "\\(\\(" muse-table-line-regexp "\n\\)+"
157 "\\(" muse-table-el-border-regexp "\\)"
158 "\\(\n\\|\\'\\)\\)+")
159 0 table-el)
161 ;; simple table markup is supported, nothing fancy. use | to
162 ;; separate cells, || to separate header cells, and ||| for footer
163 ;; cells
164 (2350 ,(concat "\\(\\([" muse-regexp-blank "]*\n\\)?"
165 "\\(\\(?:" muse-table-line-regexp "\\|"
166 muse-table-hline-regexp "\\)\\(?:\n\\|\\'\\)\\)\\)+")
167 0 table)
169 ;; blockquote and centered text
170 (2400 ,(concat "^\\([" muse-regexp-blank "]+\\).+") 0 quote)
172 ;; the emdash ("--")
173 (2500 ,(concat "\\(^\\|[" muse-regexp-blank "]*\\)--\\($\\|["
174 muse-regexp-blank "]*\\)")
175 0 emdash)
177 ;; "verse" text is indicated the same way as a quoted e-mail
178 ;; response: "> text", where text may contain initial whitespace
179 ;; (see below).
180 (2600 ,(concat "^[" muse-regexp-blank "]*> ") 0 verse)
182 ;; define anchor points
183 (2700 "^\\(\\W*\\)#\\(\\S-+\\)\\s-*" 0 anchor)
185 ;; replace links in the buffer (links to other pages)
186 (2900 muse-explicit-link-regexp 0 link)
188 ;; bare URLs
189 (3000 muse-url-regexp 0 url)
191 ;; bare email addresses
192 (3500
193 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email)
195 "List of markup rules for publishing a page with Muse.
196 The rules given in this variable are invoked first, followed by
197 whatever rules are specified by the current style.
199 Each member of the list is either a function, or a list of the form:
201 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
203 REGEXP is a regular expression, or symbol whose value is a regular
204 expression, which is searched for using `re-search-forward'.
205 TEXT-BEGIN-GROUP is the matching group within that regexp which
206 denotes the beginning of the actual text to be marked up.
207 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
208 If it is not a string, but a function, it will be called to determine
209 what the replacement text should be (it must return a string). If it
210 is a symbol, the value of that symbol should be a string.
212 The replacements are done in order, one rule at a time. Writing
213 the regular expressions can be a tricky business. Note that case
214 is never ignored. `case-fold-search' is always bound to nil
215 while processing the markup rules."
216 :type '(repeat (choice
217 (list :tag "Markup rule"
218 integer
219 (choice regexp symbol)
220 integer
221 (choice string function symbol))
222 function))
223 :group 'muse-publish)
225 (defcustom muse-publish-markup-functions
226 '((directive . muse-publish-markup-directive)
227 (comment . muse-publish-markup-comment)
228 (anchor . muse-publish-markup-anchor)
229 (tag . muse-publish-markup-tag)
230 (word . muse-publish-markup-word)
231 (emdash . muse-publish-markup-emdash)
232 (enddots . muse-publish-markup-enddots)
233 (dots . muse-publish-markup-dots)
234 (rule . muse-publish-markup-rule)
235 (no-break-space . muse-publish-markup-no-break-space)
236 (heading . muse-publish-markup-heading)
237 (footnote . muse-publish-markup-footnote)
238 (fn-sep . muse-publish-markup-fn-sep)
239 (list . muse-publish-markup-list)
240 (quote . muse-publish-markup-quote)
241 (verse . muse-publish-markup-verse)
242 (table . muse-publish-markup-table)
243 (table-el . muse-publish-markup-table-el)
244 (email . muse-publish-markup-email)
245 (link . muse-publish-markup-link)
246 (url . muse-publish-markup-url))
247 "An alist of style types to custom functions for that kind of text.
249 Each member of the list is of the form:
251 (SYMBOL FUNCTION)
253 SYMBOL describes the type of text to associate with this rule.
254 `muse-publish-markup-regexps' maps regexps to these symbols.
256 FUNCTION is the function to use to mark up this kind of rule if
257 no suitable function is found through the :functions tag of the
258 current style."
259 :type '(alist :key-type symbol :value-type function)
260 :group 'muse-publish)
262 (defcustom muse-publish-markup-tags
263 '(("contents" nil t nil muse-publish-contents-tag)
264 ("verse" t nil nil muse-publish-verse-tag)
265 ("example" t nil nil muse-publish-example-tag)
266 ("src" t t nil muse-publish-src-tag)
267 ("code" t nil nil muse-publish-code-tag)
268 ("quote" t nil t muse-publish-quote-tag)
269 ("literal" t t nil muse-publish-literal-tag)
270 ("verbatim" t nil nil muse-publish-verbatim-tag)
271 ("lisp" t t nil muse-publish-lisp-tag)
272 ("class" t t nil muse-publish-class-tag)
273 ("command" t t nil muse-publish-command-tag)
274 ("perl" t t nil muse-publish-perl-tag)
275 ("python" t t nil muse-publish-python-tag)
276 ("ruby" t t nil muse-publish-ruby-tag)
277 ("comment" t nil nil muse-publish-comment-tag)
278 ("include" nil t nil muse-publish-include-tag)
279 ("markup" t t nil muse-publish-mark-up-tag)
280 ("cite" t t nil muse-publish-cite-tag))
281 "A list of tag specifications, for specially marking up text.
282 XML-style tags are the best way to add custom markup to Muse.
283 This is easily accomplished by customizing this list of markup tags.
285 For each entry, the name of the tag is given, whether it expects
286 a closing tag and/or an optional set of attributes, whether it is
287 nestable, and a function that performs whatever action is desired
288 within the delimited region.
290 The tags themselves are deleted during publishing, before the
291 function is called. The function is called with three arguments,
292 the beginning and end of the region surrounded by the tags. If
293 properties are allowed, they are passed as a third argument in
294 the form of an alist. The `end' argument to the function is
295 always a marker.
297 Point is always at the beginning of the region within the tags, when
298 the function is called. Wherever point is when the function finishes
299 is where tag markup will resume.
301 These tag rules are processed once at the beginning of markup, and
302 once at the end, to catch any tags which may have been inserted
303 in-between."
304 :type '(repeat (list (string :tag "Markup tag")
305 (boolean :tag "Expect closing tag" :value t)
306 (boolean :tag "Parse attributes" :value nil)
307 (boolean :tag "Nestable" :value nil)
308 function))
309 :group 'muse-publish)
311 (defcustom muse-publish-markup-header-footer-tags
312 '(("lisp" t t nil muse-publish-lisp-tag)
313 ("markup" t t nil muse-publish-mark-up-tag))
314 "Tags used when publishing headers and footers.
315 See `muse-publish-markup-tags' for details."
316 :type '(repeat (list (string :tag "Markup tag")
317 (boolean :tag "Expect closing tag" :value t)
318 (boolean :tag "Parse attributes" :value nil)
319 (boolean :tag "Nestable" :value nil)
320 function))
321 :group 'muse-publish)
323 (defcustom muse-publish-markup-specials nil
324 "A table of characters which must be represented specially."
325 :type '(alist :key-type character :value-type string)
326 :group 'muse-publish)
328 (defvar muse-publishing-p nil
329 "Set to t while a page is being published.")
330 (defvar muse-batch-publishing-p nil
331 "Set to t while a page is being batch published.")
332 (defvar muse-publishing-styles nil
333 "The publishing styles that Muse recognizes.
334 This is automatically generated when loading publishing styles.")
335 (defvar muse-publishing-current-file nil
336 "The file that is currently being published.")
337 (defvar muse-publishing-current-output-path nil
338 "The path where the current file will be published to.")
339 (defvar muse-publishing-current-style nil
340 "The style of the file that is currently being published.")
341 (defvar muse-publishing-directives nil
342 "An alist of publishing directives from the top of a file.")
343 (defvar muse-publish-generate-contents nil
344 "Non-nil if a table of contents should be generated.
345 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
346 tell where the <contents> was seen, and to what depth the
347 contents were requested.")
348 (defvar muse-publishing-last-position nil
349 "Last position of the point when publishing.
350 This is used to make sure that publishing doesn't get stalled.")
352 (defvar muse-publish-inhibit-style-hooks nil
353 "If non-nil, do not call the :before or :before-end hooks when publishing.")
355 (defvar muse-publish-use-header-footer-tags nil
356 "If non-nil, use `muse-publish-markup-header-footer-tags' for looking up
357 tags. Otherwise, use `muse-publish-markup-tags'.")
359 (defvar muse-inhibit-style-tags nil
360 "If non-nil, do not search for style-specific tags.
361 This is used when publishing headers and footers.")
363 ;; Functions for handling style information
365 (defsubst muse-style (&optional style)
366 "Resolve the given STYLE into a Muse style, if it is a string."
367 (if (null style)
368 muse-publishing-current-style
369 (if (stringp style)
370 (assoc style muse-publishing-styles)
371 (muse-assert (consp style))
372 style)))
374 (defun muse-define-style (name &rest elements)
375 (let ((entry (assoc name muse-publishing-styles)))
376 (if entry
377 (setcdr entry elements)
378 (setq muse-publishing-styles
379 (cons (append (list name) elements)
380 muse-publishing-styles)))))
382 (defun muse-derive-style (new-name base-name &rest elements)
383 (apply 'muse-define-style new-name
384 (append elements (list :base base-name))))
386 (defsubst muse-get-keyword (keyword list &optional direct)
387 (let ((value (cadr (memq keyword list))))
388 (if (and (not direct) (symbolp value))
389 (symbol-value value)
390 value)))
392 (defun muse-style-elements-list (elem &optional style)
393 "Return a list all references to ELEM in STYLE, including base styles.
394 If STYLE is not specified, use current style."
395 (let (base elements)
396 (while style
397 (setq style (muse-style style))
398 (setq elements (append elements
399 (muse-get-keyword elem style)))
400 (setq style (muse-get-keyword :base style)))
401 elements))
403 (defun muse-style-element (elem &optional style direct)
404 "Search for ELEM in STYLE, including base styles.
405 If STYLE is not specified, use current style."
406 (setq style (muse-style style))
407 (let ((value (muse-get-keyword elem style direct)))
408 (if value
409 value
410 (let ((base (muse-get-keyword :base style)))
411 (if base
412 (muse-style-element elem base direct))))))
414 (defun muse-style-derived-p-1 (base style)
415 "Internal function used by `muse-style-derived-p'."
416 (if (and (stringp style)
417 (string= style base))
419 (setq style (muse-style style))
420 (let ((value (muse-get-keyword :base style)))
421 (when value
422 (muse-style-derived-p base value)))))
424 (defun muse-style-derived-p (base &optional style)
425 "Return non-nil if STYLE is equal to or derived from BASE,
426 non-nil otherwise.
428 BASE should be a string."
429 (unless style
430 (setq style (muse-style)))
431 (when (and (consp style)
432 (stringp (car style)))
433 (setq style (car style)))
434 (muse-style-derived-p-1 base style))
436 (defun muse-find-markup-element (keyword ident style)
437 (let ((def (assq ident (muse-style-element keyword style))))
438 (if def
439 (cdr def)
440 (let ((base (muse-style-element :base style)))
441 (if base
442 (muse-find-markup-element keyword ident base))))))
444 (defun muse-markup-text (ident &rest args)
445 "Insert ARGS into the text markup associated with IDENT.
446 If the markup text has sections like %N%, this will be replaced
447 with the N-1th argument in ARGS. After that, `format' is applied
448 to the text with ARGS as parameters."
449 (let ((text (muse-find-markup-element :strings ident (muse-style))))
450 (if (and text args)
451 (progn
452 (let (start repl-text)
453 (while (setq start (string-match "%\\([1-9][0-9]*\\)%" text start))
454 ;; escape '%' in the argument text, since we will be
455 ;; using format on it
456 (setq repl-text (muse-replace-regexp-in-string
457 "%" "%%"
458 (nth (1- (string-to-number
459 (match-string 1 text))) args)
460 t t)
461 start (+ start (length repl-text))
462 text (replace-match repl-text t t text))))
463 (apply 'format text args))
464 (or text ""))))
466 (defun muse-insert-markup (&rest args)
467 (let ((beg (point)))
468 (apply 'insert args)
469 (muse-publish-mark-read-only beg (point))))
471 (defun muse-find-markup-tag (keyword tagname style)
472 (let ((def (assoc tagname (muse-style-element keyword style))))
473 (or def
474 (let ((base (muse-style-element :base style)))
475 (if base
476 (muse-find-markup-tag keyword tagname base))))))
478 (defun muse-markup-tag-info (tagname &rest args)
479 (let ((tag-info (and (not muse-inhibit-style-tags)
480 (muse-find-markup-tag :tags tagname (muse-style)))))
481 (or tag-info
482 (assoc tagname
483 (if muse-publish-use-header-footer-tags
484 muse-publish-markup-header-footer-tags
485 muse-publish-markup-tags)))))
487 (defsubst muse-markup-function (category)
488 (let ((func (muse-find-markup-element :functions category (muse-style))))
489 (or func
490 (cdr (assq category muse-publish-markup-functions)))))
492 ;; Publishing routines
494 (defun muse-publish-markup (name rules)
495 (let* ((case-fold-search nil)
496 (inhibit-read-only t)
497 (limit (* (length rules) (point-max)))
498 (verbose (and muse-publish-report-threshhold
499 (> (point-max) muse-publish-report-threshhold)))
500 (base 0))
501 (while rules
502 (goto-char (point-min))
503 (let ((regexp (nth 1 (car rules)))
504 (group (nth 2 (car rules)))
505 (repl (nth 3 (car rules)))
506 pos)
507 (setq muse-publishing-last-position nil)
508 (if (symbolp regexp)
509 (setq regexp (symbol-value regexp)))
510 (if (and verbose (not muse-batch-publishing-p))
511 (message "Publishing %s...%d%%" name
512 (* (/ (float (+ (point) base)) limit) 100)))
513 (while (and regexp (setq pos (re-search-forward regexp nil t)))
514 (if (and verbose (not muse-batch-publishing-p))
515 (message "Publishing %s...%d%%" name
516 (* (/ (float (+ (point) base)) limit) 100)))
517 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
518 (match-beginning group)
519 (get-text-property (match-beginning group) 'read-only))
520 (let* (func
521 (text (cond
522 ((and (symbolp repl)
523 (setq func (muse-markup-function repl)))
524 (funcall func))
525 ((functionp repl)
526 (funcall repl))
527 ((symbolp repl)
528 (symbol-value repl))
529 (t repl))))
530 (if (stringp text)
531 (replace-match text t))))
532 (if (and muse-publishing-last-position
533 (= pos muse-publishing-last-position))
534 (if (eobp)
535 (setq regexp nil)
536 (forward-char 1)))
537 (setq muse-publishing-last-position pos)))
538 (setq rules (cdr rules)
539 base (+ base (point-max))))
540 (if (and verbose (not muse-batch-publishing-p))
541 (message "Publishing %s...done" name))))
543 (defun muse-insert-file-or-string (file-or-string &optional title)
544 (let ((beg (point)) end)
545 (if (and (not (string-equal file-or-string ""))
546 (not (string-match "\n" file-or-string))
547 (file-readable-p file-or-string))
548 (setq end (+ beg
549 (cadr (muse-insert-file-contents file-or-string))))
550 (insert file-or-string)
551 (setq end (point)))
552 (save-restriction
553 (narrow-to-region beg end)
554 (remove-text-properties (point-min) (point-max)
555 '(read-only nil rear-nonsticky nil))
556 (goto-char (point-min))
557 (let ((muse-inhibit-style-tags t)
558 (muse-publish-use-header-footer-tags t))
559 (muse-publish-markup (or title "")
560 '((100 muse-tag-regexp 0
561 muse-publish-markup-tag)))))))
563 (defun muse-style-run-hooks (keyword style &rest args)
564 (catch 'handled
565 (let ((cache nil))
566 (while (and style
567 (setq style (muse-style style)))
568 (let ((func (muse-style-element keyword style t)))
569 (when (and func
570 (not (member func cache)))
571 (setq cache (cons func cache))
572 (when (apply func args)
573 (throw 'handled t))))
574 (setq style (muse-style-element :base style))))))
576 (defun muse-publish-markup-region (beg end &optional title style)
577 "Apply the given STYLE's markup rules to the given region.
578 TITLE is used when indicating the publishing progress; it may be nil.
580 The point is guaranteed to be at END if the routine terminates
581 normally."
582 (unless title (setq title ""))
583 (unless style
584 (or (setq style muse-publishing-current-style)
585 (error "Cannot find any publishing styles to use")))
586 (save-restriction
587 (narrow-to-region beg end)
588 (let ((muse-publish-generate-contents nil))
589 (unless muse-publish-inhibit-style-hooks
590 (muse-style-run-hooks :before style))
591 (muse-publish-markup
592 title
593 (sort (copy-alist (append muse-publish-markup-regexps
594 (muse-style-elements-list :regexps style)))
595 (function
596 (lambda (l r)
597 (< (car l) (car r))))))
598 (unless muse-publish-inhibit-style-hooks
599 (muse-style-run-hooks :before-end style))
600 (muse-publish-escape-specials (point-min) (point-max) nil 'document))
601 (goto-char (point-max))))
603 (defun muse-publish-markup-buffer (title style)
604 "Apply the given STYLE's markup rules to the current buffer."
605 (setq style (muse-style style))
606 (let ((style-header (muse-style-element :header style))
607 (style-footer (muse-style-element :footer style))
608 (muse-publishing-current-style style)
609 (muse-publishing-directives
610 (list (cons "title" title)
611 (cons "author" (user-full-name))
612 (cons "date" (format-time-string
613 muse-publish-date-format
614 (if muse-publishing-current-file
615 (nth 5 (file-attributes
616 muse-publishing-current-file))
617 (current-time))))))
618 (muse-publishing-p t)
619 (inhibit-read-only t))
620 (run-hooks 'muse-update-values-hook)
621 (run-hooks 'muse-before-publish-hook)
622 (muse-publish-markup-region (point-min) (point-max) title style)
623 (goto-char (point-min))
624 (when style-header
625 (muse-insert-file-or-string style-header title))
626 (goto-char (point-max))
627 (when style-footer
628 (muse-insert-file-or-string style-footer title))
629 (muse-style-run-hooks :after style)
630 (run-hooks 'muse-after-publish-hook)))
632 (defun muse-publish-markup-string (string &optional style)
633 "Markup STRING using the given STYLE's markup rules."
634 (setq style (muse-style style))
635 (muse-with-temp-buffer
636 (insert string)
637 (let ((muse-publishing-current-style style)
638 (muse-publishing-p t))
639 (muse-publish-markup "*string*" (muse-style-element :rules style)))
640 (buffer-string)))
642 ;; Commands for publishing files
644 (defun muse-publish-get-style (&optional styles)
645 (unless styles (setq styles muse-publishing-styles))
646 (if (= 1 (length styles))
647 (car styles)
648 (when (catch 'different
649 (let ((first (car (car styles))))
650 (dolist (style (cdr styles))
651 (unless (equal first (car style))
652 (throw 'different t)))))
653 (setq styles (muse-collect-alist
654 styles
655 (funcall muse-completing-read-function
656 "Publish with style: " styles nil t))))
657 (if (or (= 1 (length styles))
658 (not (muse-get-keyword :path (car styles))))
659 (car styles)
660 (setq styles (mapcar (lambda (style)
661 (cons (muse-get-keyword :path style)
662 style))
663 styles))
664 (cdr (assoc (funcall muse-completing-read-function
665 "Publish to directory: " styles nil t)
666 styles)))))
668 (defsubst muse-publish-get-output-dir (style)
669 (let ((default-directory (or (muse-style-element :path style)
670 default-directory)))
671 (muse-read-directory-name "Publish to directory: " nil default-directory)))
673 (defsubst muse-publish-get-info ()
674 (let ((style (muse-publish-get-style)))
675 (list style (muse-publish-get-output-dir style)
676 current-prefix-arg)))
678 (defsubst muse-publish-output-name (&optional file style)
679 (setq style (muse-style style))
680 (concat (muse-style-element :prefix style)
681 (muse-page-name file)
682 (muse-style-element :suffix style)))
684 (defsubst muse-publish-output-file (file &optional output-dir style)
685 (setq style (muse-style style))
686 (if output-dir
687 (expand-file-name (muse-publish-output-name file style) output-dir)
688 (concat (file-name-directory file)
689 (muse-publish-output-name file style))))
691 (defsubst muse-publish-link-name (&optional file style)
692 "Take FILE and add :prefix and either :link-suffix or :suffix from STYLE.
693 We assume that FILE is a Muse file.
695 We call `muse-page-name' on FILE to remove the directory part of
696 FILE and any extensions that are in `muse-ignored-extensions'."
697 (setq style (muse-style style))
698 (concat (muse-style-element :prefix style)
699 (muse-page-name file)
700 (or (muse-style-element :link-suffix style)
701 (muse-style-element :suffix style))))
703 (defsubst muse-publish-link-file (file &optional style)
704 "Turn FILE into a URL.
706 If FILE exists on the system as-is, return it without
707 modification. In the case of wanting to link to Muse files when
708 `muse-file-extension' is nil, you should load muse-project.el.
710 Otherwise, assume that it is a Muse file and call
711 `muse-publish-link-name' to add :prefix, :link-suffix, :suffix,
712 and removing ignored file extensions, but preserving the
713 directory part of FILE."
714 (setq style (muse-style style))
715 (if (file-exists-p file)
716 file
717 (concat (file-name-directory file)
718 (muse-publish-link-name file style))))
720 (defsubst muse-publish-link-page (page)
721 "Turn PAGE into a URL.
723 This is called by `muse-publish-classify-url' to figure out what
724 a link to another file or Muse page should look like.
726 If muse-project.el is loaded, call `muse-project-link-page' for this.
727 Otherwise, call `muse-publish-link-file'."
728 (if (fboundp 'muse-project-link-page)
729 (muse-project-link-page page)
730 (muse-publish-link-file page)))
732 (defmacro muse-publish-ensure-block (beg &optional end)
733 "Ensure that block-level markup at BEG is published with at least one
734 preceding blank line. BEG must be an unquoted symbol that contains a
735 position or marker. BEG is modified to be the new position.
736 The point is left at the new value of BEG.
738 Additionally, make sure that BEG is placed on a blank line.
740 If END is given, make sure that it is placed on a blank line. In
741 order to achieve this, END must be an unquoted symbol that
742 contains a marker. This is the case with Muse tag functions."
743 `(progn
744 (goto-char ,beg)
745 (cond ((not (bolp)) (insert "\n\n"))
746 ((eq (point) (point-min)) nil)
747 ((prog2 (backward-char) (bolp) (forward-char)) nil)
748 (t (insert "\n")))
749 (unless (and (bolp) (eolp))
750 (insert "\n")
751 (backward-char))
752 (setq ,beg (point))
753 (when (markerp ,end)
754 (goto-char ,end)
755 (unless (and (bolp) (eolp))
756 (insert-before-markers "\n")))
757 (goto-char ,beg)))
759 ;;;###autoload
760 (defun muse-publish-region (beg end &optional title style)
761 "Apply the given STYLE's markup rules to the given region.
762 The result is placed in a new buffer that includes TITLE in its name."
763 (interactive "r")
764 (when (interactive-p)
765 (unless title (setq title (read-string "Title: ")))
766 (unless style (setq style (muse-publish-get-style))))
767 (let ((muse-publishing-current-style style)
768 (muse-publishing-p t)
769 (text (buffer-substring beg end))
770 (buf (generate-new-buffer (concat "*Muse: " title "*"))))
771 (with-current-buffer buf
772 (insert text)
773 (muse-publish-markup-buffer title style)
774 (goto-char (point-min))
775 (let ((inhibit-read-only t))
776 (remove-text-properties (point-min) (point-max)
777 '(rear-nonsticky nil read-only nil))))
778 (pop-to-buffer buf)))
780 ;;;###autoload
781 (defun muse-publish-file (file style &optional output-dir force)
782 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
783 If the argument FORCE is nil, each file is only published if it is
784 newer than the published version. If the argument FORCE is non-nil,
785 the file is published no matter what."
786 (interactive (cons (read-file-name "Publish file: ")
787 (muse-publish-get-info)))
788 (let ((style-name style))
789 (setq style (muse-style style))
790 (unless style
791 (error "There is no style '%s' defined" style-name)))
792 (let* ((output-path (muse-publish-output-file file output-dir style))
793 (output-suffix (muse-style-element :osuffix style))
794 (muse-publishing-current-file file)
795 (muse-publishing-current-output-path output-path)
796 (target (if output-suffix
797 (concat (muse-path-sans-extension output-path)
798 output-suffix)
799 output-path))
800 (threshhold (nth 7 (file-attributes file))))
801 (if (not threshhold)
802 (message "Please save %s before publishing" file)
803 (when (or force (file-newer-than-file-p file target))
804 (if (and muse-publish-report-threshhold
805 (> threshhold
806 muse-publish-report-threshhold))
807 (message "Publishing %s ..." file))
808 (muse-with-temp-buffer
809 (muse-insert-file-contents file)
810 (muse-publish-markup-buffer (muse-page-name file) style)
811 (when (muse-write-file output-path)
812 (muse-style-run-hooks :final style file output-path target)))
813 t))))
815 ;;;###autoload
816 (defun muse-publish-this-file (style output-dir &optional force)
817 "Publish the currently-visited file.
818 Prompt for both the STYLE and OUTPUT-DIR if they are not
819 supplied."
820 (interactive (muse-publish-get-info))
821 (if buffer-file-name
822 (let ((muse-current-output-style (list :base (car style)
823 :path output-dir)))
824 (unless (muse-publish-file buffer-file-name style output-dir force)
825 (message (concat "The published version is up-to-date; use"
826 " C-u C-c C-T to force an update."))))
827 (message "This buffer is not associated with any file")))
829 (defun muse-batch-publish-files ()
830 "Publish Muse files in batch mode."
831 (let ((muse-batch-publishing-p t)
832 muse-current-output-style
833 style output-dir)
834 ;; don't activate VC when publishing files
835 (setq vc-handled-backends nil)
836 (setq style (car command-line-args-left)
837 command-line-args-left (cdr command-line-args-left)
838 output-dir (car command-line-args-left)
839 output-dir
840 (if (string-match "\\`--output-dir=" output-dir)
841 (prog1
842 (substring output-dir (match-end 0))
843 (setq command-line-args-left (cdr command-line-args-left))))
844 muse-current-output-style (list :base style :path output-dir))
845 (setq auto-mode-alist
846 (delete (cons (concat "\\." muse-file-extension "\\'")
847 'muse-mode-choose-mode)
848 auto-mode-alist))
849 (dolist (file command-line-args-left)
850 (muse-publish-file file style output-dir t))))
852 ;; Default publishing rules
854 (defun muse-publish-section-close (depth)
855 "Seach forward for the closing tag of given DEPTH."
856 (let (not-end)
857 (save-excursion
858 (while (and (setq not-end (re-search-forward
859 (concat "^\\*\\{1," (number-to-string depth)
860 "\\}\\s-+")
861 nil t))
862 (get-text-property (match-beginning 0) 'read-only)))
863 (if not-end
864 (forward-line 0)
865 (goto-char (point-max)))
866 (cond ((not (eq (char-before) ?\n))
867 (insert "\n\n"))
868 ((not (eq (char-before (1- (point))) ?\n))
869 (insert "\n")))
870 (muse-insert-markup (muse-markup-text 'section-close depth))
871 (insert "\n"))))
873 (defun muse-publish-markup-directive (&optional name value)
874 (unless name (setq name (match-string 1)))
875 (unless value (setq value (match-string 2)))
876 (let ((elem (assoc name muse-publishing-directives)))
877 (if elem
878 (setcdr elem value)
879 (setq muse-publishing-directives
880 (cons (cons name value)
881 muse-publishing-directives))))
882 ;; Make sure we don't ever try to move the point forward (past the
883 ;; beginning of buffer) while we're still searching for directives.
884 (setq muse-publishing-last-position nil)
885 (delete-region (match-beginning 0) (match-end 0)))
887 (defsubst muse-publishing-directive (name)
888 (cdr (assoc name muse-publishing-directives)))
890 (defmacro muse-publish-get-and-delete-attr (attr attrs)
891 "Delete attribute ATTR from ATTRS only once, destructively.
893 This function returns the matching attribute value, if found."
894 (let ((last (make-symbol "last"))
895 (found (make-symbol "found"))
896 (vals (make-symbol "vals")))
897 `(let ((,vals ,attrs))
898 (if (string= (caar ,vals) ,attr)
899 (prog1 (cdar ,vals)
900 (setq ,attrs (cdr ,vals)))
901 (let ((,last ,vals)
902 (,found nil))
903 (while ,vals
904 (setq ,vals (cdr ,vals))
905 (when (string= (caar ,vals) ,attr)
906 (setq ,found (cdar ,vals))
907 (setcdr ,last (cdr ,vals))
908 (setq ,vals nil))
909 (setq ,last ,vals))
910 ,found)))))
912 (defun muse-publish-markup-anchor ()
913 (unless (get-text-property (match-end 1) 'muse-link)
914 (let ((text (muse-markup-text 'anchor (match-string 2))))
915 (unless (string= text "")
916 (save-match-data
917 (skip-chars-forward (concat muse-regexp-blank "\n"))
918 (muse-insert-markup text)))
919 (match-string 1))))
921 (defun muse-publish-markup-comment ()
922 (if (null muse-publish-comments-p)
924 (goto-char (match-end 0))
925 (muse-insert-markup (muse-markup-text 'comment-end))
926 (if (match-beginning 1)
927 (progn
928 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
929 (delete-region (match-beginning 0) (match-beginning 1)))
930 (delete-region (match-beginning 0) (match-end 0)))
931 (goto-char (match-beginning 0))
932 (muse-insert-markup (muse-markup-text 'comment-begin))))
934 (defun muse-publish-markup-tag ()
935 (let ((tag-info (muse-markup-tag-info (match-string 1))))
936 (when (and tag-info
937 (not (get-text-property (match-beginning 0) 'read-only)))
938 (let ((closed-tag (match-string 3))
939 (start (match-beginning 0))
940 (beg (point))
941 end attrs)
942 (when (nth 2 tag-info)
943 (let ((attrstr (match-string 2)))
944 (while (and attrstr
945 (string-match (concat "\\([^"
946 muse-regexp-blank
947 "=\n]+\\)\\(=\"\\"
948 "([^\"]+\\)\"\\)?")
949 attrstr))
950 (let ((attr (cons (downcase
951 (muse-match-string-no-properties 1 attrstr))
952 (muse-match-string-no-properties 3 attrstr))))
953 (setq attrstr (replace-match "" t t attrstr))
954 (if attrs
955 (nconc attrs (list attr))
956 (setq attrs (list attr)))))))
957 (if (and (cadr tag-info) (not closed-tag))
958 (if (muse-goto-tag-end (car tag-info) (nth 3 tag-info))
959 (delete-region (match-beginning 0) (point))
960 (setq tag-info nil)))
961 (when tag-info
962 (setq end (point-marker))
963 (delete-region start beg)
964 (goto-char start)
965 (let ((args (list start end)))
966 (if (nth 2 tag-info)
967 (nconc args (list attrs)))
968 (let ((muse-inhibit-style-tags nil))
969 ;; remove the inhibition
970 (apply (nth 4 tag-info) args)))
971 (set-marker end nil)))))
972 nil)
974 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
975 "Escape specials from BEG to END using style-specific :specials.
976 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
977 CONTEXT is used to figure out what kind of specials to escape.
979 The following contexts exist in Muse.
980 'underline _underlined text_
981 'literal =monospaced text= or <code> region (monospaced, escaped)
982 'emphasis *emphasized text*
983 'email email@example.com
984 'url http://example.com
985 'url-desc [[...][description of an explicit link]]
986 'image [[image.png]]
987 'example <example> region (monospaced, block context, escaped)
988 'verbatim <verbatim> region (escaped)
989 'document normal text"
990 (let ((specials (muse-style-element :specials nil t)))
991 (cond ((functionp specials)
992 (setq specials (funcall specials context)))
993 ((symbolp specials)
994 (setq specials (symbol-value specials))))
995 (if (functionp specials)
996 (funcall specials beg end ignore-read-only)
997 (save-excursion
998 (save-restriction
999 (narrow-to-region beg end)
1000 (goto-char (point-min))
1001 (while (< (point) (point-max))
1002 (if (and (not ignore-read-only)
1003 (get-text-property (point) 'read-only))
1004 (goto-char (or (next-single-property-change (point) 'read-only)
1005 (point-max)))
1006 (let ((repl (or (assoc (char-after) specials)
1007 (assoc (char-after)
1008 muse-publish-markup-specials))))
1009 (if (null repl)
1010 (forward-char 1)
1011 (delete-char 1)
1012 (insert-before-markers (cdr repl)))))))))))
1014 (defun muse-publish-markup-word ()
1015 (let* ((beg (match-beginning 2))
1016 (end (1- (match-end 2)))
1017 (leader (buffer-substring-no-properties beg end))
1018 open-tag close-tag mark-read-only loc context)
1019 (cond
1020 ((string= leader "_")
1021 (setq context 'underline
1022 open-tag (muse-markup-text 'begin-underline)
1023 close-tag (muse-markup-text 'end-underline)))
1024 ((string= leader "=")
1025 (setq context 'literal
1026 open-tag (muse-markup-text 'begin-literal)
1027 close-tag (muse-markup-text 'end-literal))
1028 (setq mark-read-only t))
1030 (let ((l (length leader)))
1031 (setq context 'emphasis)
1032 (cond
1033 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
1034 close-tag (muse-markup-text 'end-emph)))
1035 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
1036 close-tag (muse-markup-text 'end-more-emph)))
1037 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
1038 close-tag (muse-markup-text 'end-most-emph)))
1039 (t (setq context nil))))))
1040 (if (and context
1041 (not (get-text-property beg 'muse-link))
1042 (setq loc (search-forward leader nil t))
1043 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
1044 (not (eq (char-syntax (char-before (point))) ?\ ))
1045 (not (get-text-property (point) 'muse-link)))
1046 (progn
1047 (replace-match "")
1048 (delete-region beg end)
1049 (setq end (point-marker))
1050 (muse-insert-markup close-tag)
1051 (goto-char beg)
1052 (muse-insert-markup open-tag)
1053 (setq beg (point))
1054 (when mark-read-only
1055 (muse-publish-escape-specials beg end t context)
1056 (muse-publish-mark-read-only beg end))
1057 (set-marker end nil))
1058 (backward-char))
1059 nil))
1061 (defun muse-publish-markup-emdash ()
1062 (unless (get-text-property (match-beginning 0) 'muse-link)
1063 (let ((prespace (match-string 1))
1064 (postspace (match-string 2)))
1065 (delete-region (match-beginning 0) (match-end 0))
1066 (muse-insert-markup (muse-markup-text 'emdash prespace postspace))
1067 (when (eq (char-after) ?\<)
1068 (insert ?\n)))))
1070 (defun muse-publish-markup-enddots ()
1071 (unless (get-text-property (match-beginning 0) 'muse-link)
1072 (delete-region (match-beginning 0) (match-end 0))
1073 (muse-insert-markup (muse-markup-text 'enddots))))
1075 (defun muse-publish-markup-dots ()
1076 (unless (get-text-property (match-beginning 0) 'muse-link)
1077 (delete-region (match-beginning 0) (match-end 0))
1078 (muse-insert-markup (muse-markup-text 'dots))))
1080 (defun muse-publish-markup-rule ()
1081 (unless (get-text-property (match-beginning 0) 'muse-link)
1082 (delete-region (match-beginning 0) (match-end 0))
1083 (muse-insert-markup (muse-markup-text 'rule))))
1085 (defun muse-publish-markup-no-break-space ()
1086 (unless (get-text-property (match-beginning 0) 'muse-link)
1087 (delete-region (match-beginning 0) (match-end 0))
1088 (muse-insert-markup (muse-markup-text 'no-break-space))))
1090 (defun muse-publish-markup-heading ()
1091 (let* ((len (length (match-string 1)))
1092 (start (muse-markup-text
1093 (cond ((= len 1) 'section)
1094 ((= len 2) 'subsection)
1095 ((= len 3) 'subsubsection)
1096 (t 'section-other))
1097 len))
1098 (end (muse-markup-text
1099 (cond ((= len 1) 'section-end)
1100 ((= len 2) 'subsection-end)
1101 ((= len 3) 'subsubsection-end)
1102 (t 'section-other-end))
1103 len)))
1104 (delete-region (match-beginning 0) (match-end 0))
1105 (muse-insert-markup start)
1106 (muse-publish-escape-specials (point) (muse-line-end-position) nil
1107 'document)
1108 (end-of-line)
1109 (when end
1110 (muse-insert-markup end))
1111 (forward-line 1)
1112 (unless (eq (char-after) ?\n)
1113 (insert "\n"))
1114 (muse-publish-section-close len)))
1116 (defvar muse-publish-footnotes nil)
1118 (defun muse-publish-markup-footnote ()
1119 "Scan ahead and snarf up the footnote body"
1120 (cond
1121 ((get-text-property (match-beginning 0) 'muse-link)
1122 nil)
1123 ((= (muse-line-beginning-position) (match-beginning 0))
1126 (let ((footnote (save-match-data
1127 (string-to-number (match-string 1))))
1128 (oldtext (match-string 0))
1129 footnotemark)
1130 (delete-region (match-beginning 0) (match-end 0))
1131 (save-excursion
1132 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
1133 (let* ((start (match-beginning 0))
1134 (beg (goto-char (match-end 0)))
1135 (end (save-excursion
1136 (if (search-forward "\n\n" nil t)
1137 (copy-marker (match-beginning 0))
1138 (goto-char (point-max))
1139 (skip-chars-backward "\n")
1140 (point-marker)))))
1141 (while (re-search-forward
1142 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
1143 end t)
1144 (replace-match "\\1" t))
1145 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
1146 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
1147 (if (string= "" footnotemark-cmd)
1148 (setq footnotemark
1149 (concat (muse-markup-text 'footnote)
1150 (buffer-substring-no-properties beg end)
1151 (muse-markup-text 'footnote-end)))
1152 (setq footnotemark (format footnotemark-cmd footnote
1153 footnotemark-end-cmd))
1154 (unless muse-publish-footnotes
1155 (set (make-local-variable 'muse-publish-footnotes)
1156 (make-vector 256 nil)))
1157 (unless (aref muse-publish-footnotes footnote)
1158 (setq footnotemark
1159 (concat
1160 footnotemark
1161 (concat (format (muse-markup-text 'footnotetext)
1162 footnote)
1163 (buffer-substring-no-properties beg end)
1164 (muse-markup-text 'footnotetext-end))))
1165 (aset muse-publish-footnotes footnote footnotemark))))
1166 (goto-char end)
1167 (skip-chars-forward "\n")
1168 (delete-region start (point))
1169 (set-marker end nil))))
1170 (if footnotemark
1171 (muse-insert-markup footnotemark)
1172 (insert oldtext))))))
1174 (defun muse-publish-markup-fn-sep ()
1175 (delete-region (match-beginning 0) (match-end 0))
1176 (muse-insert-markup (muse-markup-text 'fn-sep)))
1178 (defun muse-insert-markup-end-list (&rest args)
1179 (let ((beg (point)))
1180 (apply 'insert args)
1181 (add-text-properties beg (point) '(end-list t))
1182 (muse-publish-mark-read-only beg (point))))
1184 (defun muse-publish-determine-dl-indent (continue indent-sym determine-sym)
1185 ;; If the caller doesn't know how much indentation to use, figure it
1186 ;; out ourselves. It is assumed that `muse-forward-list-item' has
1187 ;; been called just before this to set the match data.
1188 (when (and continue
1189 (symbol-value determine-sym))
1190 (save-match-data
1191 ;; snarf all leading whitespace
1192 (let ((indent (and (match-beginning 2)
1193 (buffer-substring (match-beginning 1)
1194 (match-beginning 2)))))
1195 (when (and indent
1196 (not (string= indent "")))
1197 (set indent-sym indent)
1198 (set determine-sym nil))))))
1200 (defun muse-publish-surround-dl (indent post-indent)
1201 (let* ((beg-item (muse-markup-text 'begin-dl-item))
1202 (end-item (muse-markup-text 'end-dl-item))
1203 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
1204 (end-ddt (muse-markup-text 'end-ddt))
1205 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
1206 (end-dde (muse-markup-text 'end-dde))
1207 (continue t)
1208 (no-terms t)
1209 beg)
1210 (while continue
1211 ;; envelope this as one term+definitions unit -- HTML does not
1212 ;; need this, but DocBook and Muse's custom XML format do
1213 (muse-insert-markup beg-item)
1214 (when (looking-at muse-dl-term-regexp)
1215 ;; find the term and wrap it with published markup
1216 (setq beg (point)
1217 no-terms nil)
1218 (goto-char (match-end 1))
1219 (delete-region (point) (match-end 0))
1220 (muse-insert-markup-end-list end-ddt)
1221 ;; if definition is immediately after term, move to next line
1222 (unless (eq (char-after) ?\n)
1223 (insert ?\n))
1224 (save-excursion
1225 (goto-char beg)
1226 (delete-region (point) (match-beginning 1))
1227 (muse-insert-markup beg-ddt)))
1228 ;; handle pathological edge case where there is no term -- I
1229 ;; would prefer to just disallow this, but people seem to want
1230 ;; this behavior
1231 (when (and no-terms
1232 (looking-at (concat "[" muse-regexp-blank "]*::"
1233 "[" muse-regexp-blank "]*")))
1234 (delete-region (point) (match-end 0))
1235 ;; but only do this once
1236 (setq no-terms nil))
1237 (setq beg (point)
1238 ;; move past current item
1239 continue (muse-forward-list-item 'dl-term indent))
1240 (save-restriction
1241 (narrow-to-region beg (point))
1242 (goto-char (point-min))
1243 ;; publish each definition that we find, defaulting to an
1244 ;; empty definition if none are found
1245 (muse-publish-surround-text beg-dde end-dde
1246 (lambda (indent)
1247 (muse-forward-list-item 'dl-entry indent))
1248 indent post-indent
1249 #'muse-publish-determine-dl-indent)
1250 (goto-char (point-max))
1251 (skip-chars-backward (concat muse-regexp-blank "\n"))
1252 (muse-insert-markup-end-list end-item)
1253 (when continue
1254 (goto-char (point-max)))))))
1256 (defun muse-publish-strip-list-indentation (list-item empty-line indent post-indent)
1257 (let ((list-nested nil)
1258 (indent-found nil))
1259 (while (< (point) (point-max))
1260 (when (and (looking-at list-item)
1261 (not (or (get-text-property
1262 (muse-list-item-critical-point) 'read-only)
1263 (get-text-property
1264 (muse-list-item-critical-point) 'muse-link))))
1265 ;; if we encounter a list item, allow no post-indent space
1266 (setq list-nested t))
1267 (when (and (not (looking-at empty-line))
1268 (looking-at (concat indent "\\("
1269 (or (and list-nested "")
1270 post-indent)
1271 "\\)")))
1272 ;; if list is not nested, remove indentation
1273 (unless indent-found
1274 (setq post-indent (match-string 1)
1275 indent-found t))
1276 (replace-match ""))
1277 (forward-line 1))))
1279 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent determine-indent-func list-item)
1280 (unless list-item
1281 (setq list-item (format muse-list-item-regexp
1282 (concat "[" muse-regexp-blank "]*"))))
1283 (let ((continue t)
1284 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
1285 (determine-indent (if determine-indent-func t nil))
1286 (new-indent indent)
1287 (first t)
1288 beg)
1289 (unless indent
1290 (setq indent (concat "[" muse-regexp-blank "]+")))
1291 (if post-indent
1292 (setq post-indent (concat " \\{0," (number-to-string post-indent)
1293 "\\}"))
1294 (setq post-indent ""))
1295 (while continue
1296 (if (or (not end-tag) (string= end-tag ""))
1297 ;; if no end of list item markup exists, treat the beginning
1298 ;; of list item markup as it if it were the end -- this
1299 ;; prevents multiple-level lists from being confused
1300 (muse-insert-markup-end-list beg-tag)
1301 (muse-insert-markup beg-tag))
1302 (setq beg (point)
1303 ;; move past current item; continue is non-nil if there
1304 ;; are more like items to be processed
1305 continue (if (and determine-indent-func first)
1306 (funcall move-func (concat indent post-indent))
1307 (funcall move-func indent)))
1308 (when determine-indent-func
1309 (funcall determine-indent-func continue 'new-indent 'determine-indent))
1310 (when continue
1311 ;; remove list markup if we encountered another item of the
1312 ;; same type
1313 (replace-match "" t t nil 1))
1314 (save-restriction
1315 ;; narrow to current item
1316 (narrow-to-region beg (point))
1317 ;; move to second line of text
1318 (goto-char (point-min))
1319 (while (and (< (point) (point-max))
1320 (looking-at empty-line))
1321 (forward-line 1))
1322 (forward-line 1)
1323 ;; strip list indentation
1324 (muse-publish-strip-list-indentation list-item empty-line
1325 indent post-indent)
1326 (skip-chars-backward (concat muse-regexp-blank "\n"))
1327 (muse-insert-markup-end-list end-tag)
1328 (when determine-indent-func
1329 (setq indent new-indent))
1330 (when first
1331 (setq first nil))
1332 (when continue
1333 (goto-char (point-max)))))))
1335 (defun muse-publish-ensure-blank-line ()
1336 "Make sure that a blank line exists on the line before point."
1337 (let ((pt (point-marker)))
1338 (beginning-of-line)
1339 (cond ((eq (point) (point-min)) nil)
1340 ((prog2 (backward-char) (bolp) (forward-char)) nil)
1341 (t (insert-before-markers "\n")))
1342 (goto-char pt)
1343 (set-marker pt nil)))
1345 (defun muse-publish-markup-list ()
1346 "Markup a list entry.
1347 This function works by marking up items of the same list level
1348 and type, respecting the end-of-list property."
1349 (let* ((str (match-string 1))
1350 (type (muse-list-item-type str))
1351 (indent (buffer-substring (muse-line-beginning-position)
1352 (match-beginning 1)))
1353 (post-indent (length str)))
1354 (cond
1355 ((or (get-text-property (muse-list-item-critical-point) 'read-only)
1356 (get-text-property (muse-list-item-critical-point) 'muse-link))
1357 nil)
1358 ((eq type 'ul)
1359 (unless (eq (char-after (match-end 1)) ?-)
1360 (delete-region (match-beginning 0) (match-end 0))
1361 (muse-publish-ensure-blank-line)
1362 (muse-insert-markup (muse-markup-text 'begin-uli))
1363 (save-excursion
1364 (muse-publish-surround-text
1365 (muse-markup-text 'begin-uli-item)
1366 (muse-markup-text 'end-uli-item)
1367 (lambda (indent)
1368 (muse-forward-list-item 'ul indent))
1369 indent post-indent)
1370 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1371 (forward-line 1)))
1372 ((eq type 'ol)
1373 (delete-region (match-beginning 0) (match-end 0))
1374 (muse-publish-ensure-blank-line)
1375 (muse-insert-markup (muse-markup-text 'begin-oli))
1376 (save-excursion
1377 (muse-publish-surround-text
1378 (muse-markup-text 'begin-oli-item)
1379 (muse-markup-text 'end-oli-item)
1380 (lambda (indent)
1381 (muse-forward-list-item 'ol indent))
1382 indent post-indent)
1383 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1384 (forward-line 1))
1386 (goto-char (match-beginning 0))
1387 (muse-publish-ensure-blank-line)
1388 (muse-insert-markup (muse-markup-text 'begin-dl))
1389 (save-excursion
1390 (muse-publish-surround-dl indent post-indent)
1391 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1392 (forward-line 1))))
1393 nil)
1395 (defun muse-publish-markup-quote ()
1396 "Markup a quoted paragraph.
1397 The reason this function is so funky, is to prevent text properties
1398 like read-only from being inadvertently deleted."
1399 (let* ((ws (match-string 1))
1400 (centered (>= (string-width ws) 6))
1401 (begin-elem (if centered 'begin-center 'begin-quote-item))
1402 (end-elem (if centered 'end-center 'end-quote-item)))
1403 (replace-match "" t t nil 1)
1404 (unless centered
1405 (muse-insert-markup (muse-markup-text 'begin-quote)))
1406 (muse-publish-surround-text (muse-markup-text begin-elem)
1407 (muse-markup-text end-elem)
1408 (function (lambda (indent)
1409 (muse-forward-paragraph)
1410 nil)))
1411 (unless centered
1412 (muse-insert-markup (muse-markup-text 'end-quote)))))
1414 (defun muse-publish-markup-leading-space (markup-space multiple)
1415 (let (count)
1416 (when (and markup-space
1417 (>= (setq count (skip-chars-forward " ")) 0))
1418 (delete-region (muse-line-beginning-position) (point))
1419 (while (> count 0)
1420 (muse-insert-markup markup-space)
1421 (setq count (- count multiple))))))
1423 (defun muse-publish-markup-verse ()
1424 (let ((leader (match-string 0)))
1425 (goto-char (match-beginning 0))
1426 (muse-insert-markup (muse-markup-text 'begin-verse))
1427 (while (looking-at leader)
1428 (replace-match "")
1429 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1430 (let ((beg (point)))
1431 (end-of-line)
1432 (cond
1433 ((bolp)
1434 (let ((text (muse-markup-text 'empty-verse-line)))
1435 (when text (muse-insert-markup text))))
1436 ((save-excursion
1437 (save-match-data
1438 (forward-line 1)
1439 (or (looking-at (concat leader "["
1440 muse-regexp-blank
1441 "]*$"))
1442 (not (looking-at leader)))))
1443 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1444 (end-text (muse-markup-text 'end-last-stanza-line)))
1445 (when end-text (muse-insert-markup end-text))
1446 (goto-char beg)
1447 (when begin-text (muse-insert-markup begin-text))
1448 (end-of-line)))
1450 (let ((begin-text (muse-markup-text 'begin-verse-line))
1451 (end-text (muse-markup-text 'end-verse-line)))
1452 (when end-text (muse-insert-markup end-text))
1453 (goto-char beg)
1454 (when begin-text (muse-insert-markup begin-text))
1455 (end-of-line))))
1456 (forward-line 1))))
1457 (muse-insert-markup (muse-markup-text 'end-verse))
1458 (insert ?\n))
1460 (defun muse-publish-trim-table (table)
1461 "Remove completely blank columns from table, if at start or end of row."
1462 ;; remove first
1463 (catch 'found
1464 (dolist (row (cdr table))
1465 (let ((el (cadr row)))
1466 (when (and (stringp el) (not (string= el "")))
1467 (throw 'found t))))
1468 (dolist (row (cdr table))
1469 (setcdr row (cddr row)))
1470 (setcar table (1- (car table))))
1471 ;; remove last
1472 (catch 'found
1473 (dolist (row (cdr table))
1474 (let ((el (car (last row))))
1475 (when (and (stringp el) (not (string= el "")))
1476 (throw 'found t))))
1477 (dolist (row (cdr table))
1478 (setcdr (last row 2) nil))
1479 (setcar table (1- (car table))))
1480 table)
1482 (defun muse-publish-table-fields (beg end)
1483 "Parse given region as a table, returning a cons cell.
1484 The car is the length of the longest row.
1486 The cdr is a list of the fields of the table, with the first
1487 element indicating the type of the row:
1488 1: body, 2: header, 3: footer, hline: separator.
1490 The existing region will be removed, except for initial blank lines."
1491 (unless (muse-publishing-directive "disable-tables")
1492 (let ((longest 0)
1493 (left 0)
1494 (seen-hline nil)
1495 fields field-list)
1496 (save-restriction
1497 (narrow-to-region beg end)
1498 (goto-char (point-min))
1499 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1500 (forward-line 1))
1501 (setq beg (point))
1502 (while (= left 0)
1503 (cond
1504 ((looking-at muse-table-hline-regexp)
1505 (when field-list ; skip if at the beginning of table
1506 (if seen-hline
1507 (setq field-list (cons (cons 'hline nil) field-list))
1508 (dolist (field field-list)
1509 ;; the preceding fields are header lines
1510 (setcar field 2))
1511 (setq seen-hline t))))
1512 ((looking-at muse-table-line-regexp)
1513 (setq fields (cons (length (match-string 1))
1514 (mapcar #'muse-trim-whitespace
1515 (split-string (match-string 0)
1516 muse-table-field-regexp)))
1517 field-list (cons fields field-list)
1518 longest (max (length fields) longest))
1519 ;; strip initial bars, if they exist
1520 (let ((first (cadr fields)))
1521 (when (and first (string-match "\\`|+\\s-*" first))
1522 (setcar (cdr fields) (replace-match "" t t first))))))
1523 (setq left (forward-line 1))))
1524 (delete-region beg end)
1525 (if (= longest 0)
1526 (cons 0 nil)
1527 ;; if the last line was an hline, remove it
1528 (when (eq (caar field-list) 'hline)
1529 (setq field-list (cdr field-list)))
1530 (muse-publish-trim-table (cons (1- longest) (nreverse field-list)))))))
1532 (defun muse-publish-markup-table ()
1533 "Style does not support tables.\n")
1535 (defun muse-publish-table-el-table (variant)
1536 "Publish table.el-style tables in the format given by VARIANT."
1537 (when (condition-case nil
1538 (progn (require 'table)
1540 (error nil))
1541 (let ((muse-buf (current-buffer)))
1542 (save-restriction
1543 (narrow-to-region (match-beginning 0) (match-end 0))
1544 (goto-char (point-min))
1545 (forward-line 1)
1546 (search-forward "|" nil t)
1547 (with-temp-buffer
1548 (let ((temp-buf (current-buffer)))
1549 (with-current-buffer muse-buf
1550 (table-generate-source variant temp-buf))
1551 (with-current-buffer muse-buf
1552 (delete-region (point-min) (point-max))
1553 (insert-buffer-substring temp-buf)
1554 (muse-publish-mark-read-only (point-min) (point-max)))))))))
1556 (defun muse-publish-markup-table-el ()
1557 "Mark up table.el-style tables."
1558 (cond ((muse-style-derived-p 'html)
1559 (muse-publish-table-el-table 'html))
1560 ((muse-style-derived-p 'latex)
1561 (muse-publish-table-el-table 'latex))
1562 ((muse-style-derived-p 'docbook)
1563 (muse-publish-table-el-table 'cals))
1564 (t "Style does not support table.el tables.\n")))
1566 (defun muse-publish-escape-specials-in-string (string &optional context)
1567 "Escape specials in STRING using style-specific :specials.
1568 CONTEXT is used to figure out what kind of specials to escape.
1570 See the documentation of the `muse-publish-escape-specials'
1571 function for the list of available contexts."
1572 (unless string
1573 (setq string ""))
1574 (let ((specials (muse-style-element :specials nil t)))
1575 (cond ((functionp specials)
1576 (setq specials (funcall specials context)))
1577 ((symbolp specials)
1578 (setq specials (symbol-value specials))))
1579 (if (functionp specials)
1580 (funcall specials string)
1581 (apply (function concat)
1582 (mapcar
1583 (lambda (ch)
1584 (let ((repl (or (assoc ch specials)
1585 (assoc ch muse-publish-markup-specials))))
1586 (if (null repl)
1587 (char-to-string ch)
1588 (cdr repl))))
1589 (append string nil))))))
1591 (defun muse-publish-markup-email ()
1592 (let* ((beg (match-end 1))
1593 (addr (buffer-substring-no-properties beg (match-end 0))))
1594 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1595 (goto-char beg)
1596 (delete-region beg (match-end 0))
1597 (if (or (eq (char-before (match-beginning 0)) ?\")
1598 (eq (char-after (match-end 0)) ?\"))
1599 (insert addr)
1600 (insert (format (muse-markup-text 'email-addr) addr addr)))
1601 (muse-publish-mark-read-only beg (point))))
1603 (defun muse-publish-classify-url (target)
1604 "Transform anchors and get published name, if TARGET is a page.
1605 The return value is two linked cons cells. The car is the type
1606 of link, the cadr is the page name, and the cddr is the anchor."
1607 (save-match-data
1608 (cond ((or (null target) (string= target ""))
1609 nil)
1610 ((string-match "\\`[uU][rR][lL]:\\(.+\\)\\'" target)
1611 (cons 'url (cons (match-string 1 target) nil)))
1612 ((string-match muse-image-regexp target)
1613 (cons 'image (cons target nil)))
1614 ((string-match muse-url-regexp target)
1615 (cons 'url (cons target nil)))
1616 ((string-match muse-file-regexp target)
1617 (cons 'file (cons target nil)))
1618 ((string-match "#" target)
1619 (if (eq (aref target 0) ?\#)
1620 (cons 'anchor-ref (cons nil (substring target 1)))
1621 (cons 'link-and-anchor
1622 (cons (muse-publish-link-page
1623 (substring target 0 (match-beginning 0)))
1624 (substring target (match-end 0))))))
1626 (cons 'link (cons (muse-publish-link-page target) nil))))))
1628 (defun muse-publish-url-desc (desc explicit)
1629 (when desc
1630 (dolist (transform muse-publish-desc-transforms)
1631 (setq desc (save-match-data
1632 (when desc (funcall transform desc explicit)))))
1633 (setq desc (muse-link-unescape desc))
1634 (muse-publish-escape-specials-in-string desc 'url-desc)))
1636 (defun muse-publish-url (url &optional desc orig-url explicit)
1637 "Resolve a URL into its final <a href> form."
1638 (let (type anchor)
1639 (dolist (transform muse-publish-url-transforms)
1640 (setq url (save-match-data (when url (funcall transform url explicit)))))
1641 (if desc
1642 (setq desc (muse-publish-url-desc desc explicit))
1643 (if orig-url
1644 (setq orig-url (muse-publish-url-desc orig-url explicit))))
1645 (let ((target (muse-publish-classify-url url)))
1646 (setq type (car target)
1647 url (if (eq type 'image)
1648 (muse-publish-escape-specials-in-string (cadr target)
1649 'image)
1650 (muse-publish-escape-specials-in-string (cadr target) 'url))
1651 anchor (muse-publish-escape-specials-in-string
1652 (cddr target) 'url)))
1653 (cond ((eq type 'anchor-ref)
1654 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1655 ((and desc (string-match muse-image-regexp desc))
1656 (let ((ext (or (file-name-extension desc) "")))
1657 (setq desc (muse-path-sans-extension desc))
1658 (muse-markup-text 'image-link url desc ext)))
1659 ((string= url "")
1660 desc)
1661 ((eq type 'image)
1662 (let ((ext (or (file-name-extension url) "")))
1663 (setq url (muse-path-sans-extension url))
1664 (if desc
1665 (muse-markup-text 'image-with-desc url ext desc)
1666 (muse-markup-text 'image url ext))))
1667 ((eq type 'link-and-anchor)
1668 (muse-markup-text 'link-and-anchor url anchor
1669 (or desc orig-url)
1670 (muse-path-sans-extension url)))
1671 ((eq type 'link)
1672 (muse-markup-text 'link url (or desc orig-url)))
1674 (or (and (or desc
1675 (not (string= url orig-url)))
1676 (let ((text (muse-markup-text 'url-and-desc url
1677 (or desc orig-url))))
1678 (and (not (string= text ""))
1679 text)))
1680 (muse-markup-text 'url url (or desc orig-url)))))))
1682 (defun muse-publish-insert-url (url &optional desc orig-url explicit)
1683 "Resolve a URL into its final <a href> form."
1684 (delete-region (match-beginning 0) (match-end 0))
1685 (let ((text (muse-publish-url url desc orig-url explicit)))
1686 (when text
1687 (muse-insert-markup text))))
1689 (defun muse-publish-markup-link ()
1690 (let (desc explicit orig-link link)
1691 (setq explicit (save-match-data
1692 (if (string-match muse-explicit-link-regexp
1693 (match-string 0))
1694 t nil)))
1695 (setq orig-link (if explicit (match-string 1) (match-string 0)))
1696 (setq desc (when explicit (match-string 2)))
1697 (setq link (if explicit
1698 (muse-handle-explicit-link orig-link)
1699 (muse-handle-implicit-link orig-link)))
1700 (when (and link
1701 (or explicit
1702 (not (or (eq (char-before (match-beginning 0)) ?\")
1703 (eq (char-after (match-end 0)) ?\")))))
1704 ;; if explicit link has no user-provided description, treat it
1705 ;; as if it were an implicit link
1706 (when (and explicit (not desc))
1707 (setq explicit nil))
1708 (muse-publish-insert-url link desc orig-link explicit))))
1710 (defun muse-publish-markup-url ()
1711 (unless (or (eq (char-before (match-beginning 0)) ?\")
1712 (eq (char-after (match-end 0)) ?\"))
1713 (let ((url (match-string 0)))
1714 (muse-publish-insert-url url nil url))))
1716 ;; Default publishing tags
1718 (defcustom muse-publish-contents-depth 2
1719 "The number of heading levels to include with <contents> tags."
1720 :type 'integer
1721 :group 'muse-publish)
1723 (defun muse-publish-contents-tag (beg end attrs)
1724 (set (make-local-variable 'muse-publish-generate-contents)
1725 (cons (copy-marker (point) t)
1726 (let ((depth (cdr (assoc "depth" attrs))))
1727 (or (and depth (string-to-number depth))
1728 muse-publish-contents-depth)))))
1730 (defun muse-publish-verse-tag (beg end)
1731 (muse-publish-ensure-block beg end)
1732 (save-excursion
1733 (save-restriction
1734 (narrow-to-region beg end)
1735 (goto-char (point-min))
1736 (delete-char 1)
1737 (while (< (point) (point-max))
1738 (insert "> ")
1739 (forward-line))
1740 (if (eq ?\ (char-syntax (char-before)))
1741 (delete-char -1)))))
1743 (defun muse-publish-mark-read-only (beg end)
1744 "Add read-only properties to the given region."
1745 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1746 nil)
1748 (defun muse-publish-mark-link (&optional beg end)
1749 "Indicate that the given region is a Muse link, so that other
1750 markup elements respect it. If a region is not specified, use
1751 the 0th match data to determine it.
1753 This is usually applied to explicit links."
1754 (unless beg (setq beg (match-beginning 0)))
1755 (unless end (setq end (match-end 0)))
1756 (add-text-properties beg end '(muse-link t))
1757 nil)
1759 (defun muse-publish-quote-tag (beg end)
1760 (muse-publish-ensure-block beg)
1761 (save-excursion
1762 (save-restriction
1763 (narrow-to-region beg end)
1764 (let ((quote-regexp "^\\(<\\(/?\\)quote>\\)"))
1765 (muse-insert-markup (muse-markup-text 'begin-quote))
1766 (while (progn
1767 (unless (looking-at (concat "[" muse-regexp-blank "\n]*"
1768 "<quote>"))
1769 (muse-publish-surround-text
1770 (muse-markup-text 'begin-quote-item)
1771 (muse-markup-text 'end-quote-item)
1772 (function
1773 (lambda (indent)
1774 (muse-forward-paragraph)
1775 (goto-char (match-end 0))
1776 (and (< (point) (point-max))
1777 (not (looking-at quote-regexp)))))
1778 nil nil nil
1779 quote-regexp))
1780 (if (>= (point) (point-max))
1782 (and (search-forward "<quote>" nil t)
1783 (muse-goto-tag-end "quote" t)
1784 (progn (forward-line 1) t)
1785 (< (point) (point-max))))))
1786 (goto-char (point-max))
1787 (muse-insert-markup (muse-markup-text 'end-quote))))))
1789 (defun muse-publish-code-tag (beg end)
1790 (muse-publish-escape-specials beg end nil 'literal)
1791 (goto-char beg)
1792 (insert (muse-markup-text 'begin-literal))
1793 (goto-char end)
1794 (insert (muse-markup-text 'end-literal))
1795 (muse-publish-mark-read-only beg (point)))
1797 (defun muse-publish-cite-tag (beg end attrs)
1798 (let* ((type (muse-publish-get-and-delete-attr "type" attrs))
1799 (citetag (cond ((string-equal type "author")
1800 'begin-cite-author)
1801 ((string-equal type "year")
1802 'begin-cite-year)
1804 'begin-cite))))
1805 (goto-char beg)
1806 (insert (muse-markup-text citetag (muse-publishing-directive "bibsource")))
1807 (goto-char end)
1808 (insert (muse-markup-text 'end-cite))
1809 (muse-publish-mark-read-only beg (point))))
1811 (defun muse-publish-src-tag (beg end attrs)
1812 (muse-publish-example-tag beg end))
1814 (defun muse-publish-example-tag (beg end)
1815 (muse-publish-ensure-block beg end)
1816 (muse-publish-escape-specials beg end nil 'example)
1817 (goto-char beg)
1818 (insert (muse-markup-text 'begin-example))
1819 (goto-char end)
1820 (insert (muse-markup-text 'end-example))
1821 (muse-publish-mark-read-only beg (point)))
1823 (defun muse-publish-literal-tag (beg end attrs)
1824 "Ensure that the text between BEG and END is not interpreted later on.
1826 ATTRS is an alist of attributes.
1828 If it contains a \"style\" element, delete the region if the
1829 current style is neither derived from nor equal to this style.
1831 If it contains both a \"style\" element and an \"exact\" element
1832 with the value \"t\", delete the region only if the current style
1833 is exactly this style."
1834 (let* ((style (cdr (assoc "style" attrs)))
1835 (exact (cdr (assoc "exact" attrs)))
1836 (exactp (and (stringp exact) (string= exact "t"))))
1837 (if (or (not style)
1838 (and exactp (equal (muse-style style)
1839 muse-publishing-current-style))
1840 (and (not exactp) (muse-style-derived-p style)))
1841 (muse-publish-mark-read-only beg end)
1842 (delete-region beg end)
1843 (when (and (bolp) (eolp) (not (eobp)))
1844 (delete-char 1)))))
1846 (defun muse-publish-verbatim-tag (beg end)
1847 (muse-publish-escape-specials beg end nil 'verbatim)
1848 (muse-publish-mark-read-only beg end))
1850 (defalias 'muse-publish-class-tag 'ignore)
1852 (defun muse-publish-call-tag-on-buffer (tag &optional attrs)
1853 "Transform the current buffer as if it were surrounded by the tag TAG.
1854 If attributes ATTRS are given, pass them to the tag function."
1855 (let ((tag-info (muse-markup-tag-info tag)))
1856 (when tag-info
1857 (let* ((end (progn (goto-char (point-max)) (point-marker)))
1858 (args (list (point-min) end))
1859 (muse-inhibit-style-tags nil))
1860 (when (nth 2 tag-info)
1861 (nconc args (list attrs)))
1862 (apply (nth 4 tag-info) args)
1863 (set-marker end nil)))))
1865 (defun muse-publish-examplify-buffer (&optional attrs)
1866 "Transform the current buffer as if it were an <example> region."
1867 (muse-publish-call-tag-on-buffer "example" attrs))
1869 (defun muse-publish-srcify-buffer (&optional attrs)
1870 "Transform the current buffer as if it were a <src> region."
1871 (muse-publish-call-tag-on-buffer "src" attrs))
1873 (defun muse-publish-versify-buffer (&optional attrs)
1874 "Transform the current buffer as if it were a <verse> region."
1875 (muse-publish-call-tag-on-buffer "verse" attrs)
1876 (muse-publish-markup ""
1877 `((100 ,(concat "^[" muse-regexp-blank "]*> ") 0
1878 muse-publish-markup-verse)))
1879 (goto-char (point-min)))
1881 (defmacro muse-publish-markup-attribute (beg end attrs reinterp &rest body)
1882 "Evaluate BODY within the bounds of BEG and END.
1883 ATTRS is an alist. Only the \"markup\" element of ATTRS is acted
1886 If it is omitted, publish the region with the normal Muse rules.
1887 If RE-INTERP is specified, this is done immediately in a new
1888 publishing process. Currently, RE-INTERP is specified only by
1889 the <include> tag.
1891 If \"nil\", do not mark up the region at all, but prevent it from
1892 being further interpreted by Muse.
1894 If \"example\", treat the region as if it was surrounded by the
1895 <example> tag.
1897 If \"src\", treat the region as if it was surrounded by the
1898 <src> tag.
1900 If \"verse\", treat the region as if it was surrounded by the
1901 <verse> tag, to preserve newlines.
1903 Otherwise, it should be the name of a function to call in the
1904 narrowed region after evaluating BODY. The function should
1905 take the ATTRS parameter.
1907 BEG is modified to be the start of the published markup."
1908 (let ((attrs-sym (make-symbol "attrs"))
1909 (markup (make-symbol "markup"))
1910 (markup-function (make-symbol "markup-function")))
1911 `(let* ((,attrs-sym ,attrs)
1912 (,markup (muse-publish-get-and-delete-attr "markup" ,attrs-sym)))
1913 (save-restriction
1914 (narrow-to-region ,beg ,end)
1915 (goto-char (point-min))
1916 ,@body
1917 (if (not ,markup)
1918 (when ,reinterp
1919 (muse-publish-markup-region (point-min) (point-max))
1920 (muse-publish-mark-read-only (point-min) (point-max))
1921 (goto-char (point-max)))
1922 (let ((,markup-function (read ,markup)))
1923 (cond ((eq ,markup-function 'example)
1924 (setq ,markup-function #'muse-publish-examplify-buffer))
1925 ((eq ,markup-function 'src)
1926 (setq ,markup-function #'muse-publish-srcify-buffer))
1927 ((eq ,markup-function 'verse)
1928 (setq ,markup-function #'muse-publish-versify-buffer))
1929 ((and ,markup-function (not (functionp ,markup-function)))
1930 (error "Invalid markup function `%s'" ,markup))
1931 (t nil))
1932 (if ,markup-function
1933 (funcall ,markup-function ,attrs-sym)
1934 (muse-publish-mark-read-only (point-min) (point-max))
1935 (goto-char (point-max)))))))))
1937 (put 'muse-publish-markup-attribute 'lisp-indent-function 4)
1938 (put 'muse-publish-markup-attribute 'edebug-form-spec
1939 '(sexp sexp sexp sexp body))
1941 (defun muse-publish-lisp-tag (beg end attrs)
1942 (muse-publish-markup-attribute beg end attrs nil
1943 (save-excursion
1944 (save-restriction
1945 (let ((str (muse-eval-lisp
1946 (prog1
1947 (concat "(progn "
1948 (buffer-substring-no-properties (point-min)
1949 (point-max))
1950 ")")
1951 (delete-region (point-min) (point-max))
1952 (widen)))))
1953 (set-text-properties 0 (length str) nil str)
1954 (insert str))))))
1956 (defun muse-publish-command-tag (beg end attrs)
1957 (muse-publish-markup-attribute beg end attrs nil
1958 (while (looking-at "\\s-*$")
1959 (forward-line))
1960 (let ((interp (muse-publish-get-and-delete-attr "interp" attrs)))
1961 (if interp
1962 (shell-command-on-region (point) (point-max) interp t t)
1963 (shell-command
1964 (prog1
1965 (buffer-substring-no-properties (point) (point-max))
1966 (delete-region (point-min) (point-max)))
1967 t)))
1968 ;; make sure there is a newline at end
1969 (goto-char (point-max))
1970 (forward-line 0)
1971 (unless (looking-at "\\s-*$")
1972 (goto-char (point-max))
1973 (insert ?\n))
1974 (goto-char (point-min))))
1976 (defun muse-publish-perl-tag (beg end attrs)
1977 (muse-publish-command-tag beg end
1978 (cons (cons "interp" (executable-find "perl"))
1979 attrs)))
1981 (defun muse-publish-python-tag (beg end attrs)
1982 (muse-publish-command-tag beg end
1983 (cons (cons "interp" (executable-find "python"))
1984 attrs)))
1986 (defun muse-publish-ruby-tag (beg end attrs)
1987 (muse-publish-command-tag beg end
1988 (cons (cons "interp" (executable-find "ruby"))
1989 attrs)))
1991 (defun muse-publish-comment-tag (beg end)
1992 (if (null muse-publish-comments-p)
1993 (delete-region beg end)
1994 (goto-char end)
1995 (muse-insert-markup (muse-markup-text 'comment-end))
1996 (muse-publish-mark-read-only beg end)
1997 (goto-char beg)
1998 (muse-insert-markup (muse-markup-text 'comment-begin))))
2000 (defun muse-publish-include-tag (beg end attrs)
2001 "Include the named file at the current location during publishing.
2003 <include file=\"...\" markup=\"...\">
2005 The `markup' attribute controls how this file is marked up after
2006 being inserted. See `muse-publish-markup-attribute' for an
2007 explanation of how it works."
2008 (let ((filename (muse-publish-get-and-delete-attr "file" attrs))
2009 (muse-publishing-directives (copy-alist muse-publishing-directives)))
2010 (if filename
2011 (setq filename (expand-file-name
2012 filename
2013 (file-name-directory muse-publishing-current-file)))
2014 (error "No file attribute specified in <include> tag"))
2015 (muse-publish-markup-attribute beg end attrs t
2016 (muse-insert-file-contents filename))))
2018 (defun muse-publish-mark-up-tag (beg end attrs)
2019 "Run an Emacs Lisp function on the region delimted by this tag.
2021 <markup function=\"...\" style=\"...\" exact=\"...\">
2023 The optional \"function\" attribute controls how this section is
2024 marked up. If used, it should be the name of a function to call
2025 with the buffer narrowed to the delimited region. Note that no
2026 further marking-up will be performed on this region.
2028 If \"function\" is omitted, use the standard Muse markup function.
2029 This is useful for marking up content in headers and footers.
2031 The optional \"style\" attribute causes the region to be deleted
2032 if the current style is neither derived from nor equal to this
2033 style.
2035 If both a \"style\" attribute and an \"exact\" attribute are
2036 provided, and \"exact\" is \"t\", delete the region only if the
2037 current style is exactly this style."
2038 (let* ((style (cdr (assoc "style" attrs)))
2039 (exact (cdr (assoc "exact" attrs)))
2040 (exactp (and (stringp exact) (string= exact "t"))))
2041 (if (or (not style)
2042 (and exactp (equal (muse-style style)
2043 muse-publishing-current-style))
2044 (and (not exactp) (muse-style-derived-p style)))
2045 (let* ((function (cdr (assoc "function" attrs)))
2046 (muse-publish-use-header-footer-tags nil)
2047 (markup-function (and function (intern function))))
2048 (if (and markup-function (functionp markup-function))
2049 (save-restriction
2050 (narrow-to-region beg end)
2051 (funcall markup-function)
2052 (goto-char (point-max)))
2053 (let ((muse-publish-inhibit-style-hooks t))
2054 (muse-publish-markup-region beg end)))
2055 (muse-publish-mark-read-only beg (point)))
2056 (delete-region beg end))))
2058 ;; Miscellaneous helper functions
2060 (defun muse-publish-strip-URL (string &rest ignored)
2061 "If the text \"URL:\" exists at the beginning of STRING, remove it.
2062 The text is removed regardless of whether and part of it is uppercase."
2063 (save-match-data
2064 (if (string-match "\\`[uU][rR][lL]:\\(.+\\)\\'" string)
2065 (match-string 1 string)
2066 string)))
2068 (defun muse-publish-markup-type (category default-func)
2069 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
2070 (funcall (or rule default-func))))
2072 (defun muse-published-buffer-contents (buffer)
2073 (with-current-buffer buffer
2074 (goto-char (point-min))
2075 (let ((beg (and (search-forward "Emacs Muse begins here")
2076 (muse-line-end-position)))
2077 (end (and (search-forward "Emacs Muse ends here")
2078 (muse-line-beginning-position))))
2079 (buffer-substring-no-properties beg end))))
2081 (defun muse-published-contents (file)
2082 (when (file-readable-p file)
2083 (muse-with-temp-buffer
2084 (muse-insert-file-contents file)
2085 (muse-published-buffer-contents (current-buffer)))))
2087 (defun muse-publish-transform-output
2088 (file temp-file output-path name gen-func &rest cleanup-exts)
2089 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
2090 (setq file (muse-page-name file))
2091 (message "Generating %s output for %s..." name file)
2092 (if (not (funcall gen-func temp-file output-path))
2093 (message "Generating %s from %s...failed" name file)
2094 (message "Generating %s output for %s...done" name file)
2095 (muse-delete-file-if-exists temp-file)
2096 (dolist (ext cleanup-exts)
2097 (muse-delete-file-if-exists
2098 (expand-file-name (concat file ext)
2099 (file-name-directory output-path))))
2100 (message "Wrote %s" output-path)))
2102 (defun muse-publish-read-only (string)
2103 (let ((end (1- (length string))))
2104 (add-text-properties 0 end
2105 '(rear-nonsticky (read-only) read-only t)
2106 string)
2107 string))
2109 ;;; muse-publish.el ends here