Fix issue with muse-list-edit-minor-mode and font-locking in other modes
[muse-el.git] / lisp / muse-publish.el
blob71db1597fa6b326f9f8077810ac0a13bca581917
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 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, 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 (cadr (insert-file-contents file-or-string))))
549 (insert file-or-string)
550 (setq end (point)))
551 (save-restriction
552 (narrow-to-region beg end)
553 (remove-text-properties (point-min) (point-max)
554 '(read-only nil rear-nonsticky nil))
555 (goto-char (point-min))
556 (let ((muse-inhibit-style-tags t)
557 (muse-publish-use-header-footer-tags t))
558 (muse-publish-markup (or title "")
559 '((100 muse-tag-regexp 0
560 muse-publish-markup-tag)))))))
562 (defun muse-style-run-hooks (keyword style &rest args)
563 (catch 'handled
564 (let ((cache nil))
565 (while (and style
566 (setq style (muse-style style)))
567 (let ((func (muse-style-element keyword style t)))
568 (when (and func
569 (not (member func cache)))
570 (setq cache (cons func cache))
571 (when (apply func args)
572 (throw 'handled t))))
573 (setq style (muse-style-element :base style))))))
575 (defun muse-publish-markup-region (beg end &optional title style)
576 "Apply the given STYLE's markup rules to the given region.
577 TITLE is used when indicating the publishing progress; it may be nil.
579 The point is guaranteed to be at END if the routine terminates
580 normally."
581 (unless title (setq title ""))
582 (unless style
583 (or (setq style muse-publishing-current-style)
584 (error "Cannot find any publishing styles to use")))
585 (save-restriction
586 (narrow-to-region beg end)
587 (let ((muse-publish-generate-contents nil))
588 (unless muse-publish-inhibit-style-hooks
589 (muse-style-run-hooks :before style))
590 (muse-publish-markup
591 title
592 (sort (copy-alist (append muse-publish-markup-regexps
593 (muse-style-elements-list :regexps style)))
594 (function
595 (lambda (l r)
596 (< (car l) (car r))))))
597 (unless muse-publish-inhibit-style-hooks
598 (muse-style-run-hooks :before-end style))
599 (muse-publish-escape-specials (point-min) (point-max) nil 'document))
600 (goto-char (point-max))))
602 (defun muse-publish-markup-buffer (title style)
603 "Apply the given STYLE's markup rules to the current buffer."
604 (setq style (muse-style style))
605 (let ((style-header (muse-style-element :header style))
606 (style-footer (muse-style-element :footer style))
607 (muse-publishing-current-style style)
608 (muse-publishing-directives
609 (list (cons "title" title)
610 (cons "author" (user-full-name))
611 (cons "date" (format-time-string
612 muse-publish-date-format
613 (if muse-publishing-current-file
614 (nth 5 (file-attributes
615 muse-publishing-current-file))
616 (current-time))))))
617 (muse-publishing-p t)
618 (inhibit-read-only t))
619 (run-hooks 'muse-update-values-hook)
620 (run-hooks 'muse-before-publish-hook)
621 (muse-publish-markup-region (point-min) (point-max) title style)
622 (goto-char (point-min))
623 (when style-header
624 (muse-insert-file-or-string style-header title))
625 (goto-char (point-max))
626 (when style-footer
627 (muse-insert-file-or-string style-footer title))
628 (muse-style-run-hooks :after style)
629 (run-hooks 'muse-after-publish-hook)))
631 (defun muse-publish-markup-string (string &optional style)
632 "Markup STRING using the given STYLE's markup rules."
633 (setq style (muse-style style))
634 (muse-with-temp-buffer
635 (insert string)
636 (let ((muse-publishing-current-style style)
637 (muse-publishing-p t))
638 (muse-publish-markup "*string*" (muse-style-element :rules style)))
639 (buffer-string)))
641 ;; Commands for publishing files
643 (defun muse-publish-get-style (&optional styles)
644 (unless styles (setq styles muse-publishing-styles))
645 (if (= 1 (length styles))
646 (car styles)
647 (when (catch 'different
648 (let ((first (car (car styles))))
649 (dolist (style (cdr styles))
650 (unless (equal first (car style))
651 (throw 'different t)))))
652 (setq styles (muse-collect-alist
653 styles
654 (funcall muse-completing-read-function
655 "Publish with style: " styles nil t))))
656 (if (or (= 1 (length styles))
657 (not (muse-get-keyword :path (car styles))))
658 (car styles)
659 (setq styles (mapcar (lambda (style)
660 (cons (muse-get-keyword :path style)
661 style))
662 styles))
663 (cdr (assoc (funcall muse-completing-read-function
664 "Publish to directory: " styles nil t)
665 styles)))))
667 (defsubst muse-publish-get-output-dir (style)
668 (let ((default-directory (or (muse-style-element :path style)
669 default-directory)))
670 (muse-read-directory-name "Publish to directory: " nil default-directory)))
672 (defsubst muse-publish-get-info ()
673 (let ((style (muse-publish-get-style)))
674 (list style (muse-publish-get-output-dir style)
675 current-prefix-arg)))
677 (defsubst muse-publish-output-name (&optional file style)
678 (setq style (muse-style style))
679 (concat (muse-style-element :prefix style)
680 (muse-page-name file)
681 (muse-style-element :suffix style)))
683 (defsubst muse-publish-output-file (file &optional output-dir style)
684 (setq style (muse-style style))
685 (if output-dir
686 (expand-file-name (muse-publish-output-name file style) output-dir)
687 (concat (file-name-directory file)
688 (muse-publish-output-name file style))))
690 (defsubst muse-publish-link-name (&optional file style)
691 (setq style (muse-style style))
692 (concat (muse-style-element :prefix style)
693 (muse-page-name file)
694 (or (muse-style-element :link-suffix style)
695 (muse-style-element :suffix style))))
697 (defsubst muse-publish-link-file (file &optional style)
698 (setq style (muse-style style))
699 (if (file-exists-p file)
700 file
701 (concat (file-name-directory file)
702 (muse-publish-link-name file style))))
704 (defsubst muse-publish-link-page (page)
705 (if (fboundp 'muse-project-link-page)
706 (muse-project-link-page page)
707 (muse-publish-link-file page)))
709 (defmacro muse-publish-ensure-block (beg &optional end)
710 "Ensure that block-level markup at BEG is published with at least one
711 preceding blank line. BEG must be an unquoted symbol that contains a
712 position or marker. BEG is modified to be the new position.
713 The point is left at the new value of BEG.
715 Additionally, make sure that BEG is placed on a blank line.
717 If END is given, make sure that it is placed on a blank line. In
718 order to achieve this, END must be an unquoted symbol that
719 contains a marker. This is the case with Muse tag functions."
720 `(progn
721 (goto-char ,beg)
722 (cond ((not (bolp)) (insert "\n\n"))
723 ((eq (point) (point-min)) nil)
724 ((prog2 (backward-char) (bolp) (forward-char)) nil)
725 (t (insert "\n")))
726 (unless (and (bolp) (eolp))
727 (insert "\n")
728 (backward-char))
729 (setq ,beg (point))
730 (when (markerp ,end)
731 (goto-char ,end)
732 (unless (and (bolp) (eolp))
733 (insert-before-markers "\n")))
734 (goto-char ,beg)))
736 ;;;###autoload
737 (defun muse-publish-region (beg end &optional title style)
738 "Apply the given STYLE's markup rules to the given region.
739 The result is placed in a new buffer that includes TITLE in its name."
740 (interactive "r")
741 (when (interactive-p)
742 (unless title (setq title (read-string "Title: ")))
743 (unless style (setq style (muse-publish-get-style))))
744 (let ((muse-publishing-current-style style)
745 (muse-publishing-p t)
746 (text (buffer-substring beg end))
747 (buf (generate-new-buffer (concat "*Muse: " title "*"))))
748 (with-current-buffer buf
749 (insert text)
750 (muse-publish-markup-buffer title style)
751 (goto-char (point-min))
752 (let ((inhibit-read-only t))
753 (remove-text-properties (point-min) (point-max)
754 '(rear-nonsticky nil read-only nil))))
755 (pop-to-buffer buf)))
757 ;;;###autoload
758 (defun muse-publish-file (file style &optional output-dir force)
759 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
760 If the argument FORCE is nil, each file is only published if it is
761 newer than the published version. If the argument FORCE is non-nil,
762 the file is published no matter what."
763 (interactive (cons (read-file-name "Publish file: ")
764 (muse-publish-get-info)))
765 (let ((style-name style))
766 (setq style (muse-style style))
767 (unless style
768 (error "There is no style '%s' defined" style-name)))
769 (let* ((output-path (muse-publish-output-file file output-dir style))
770 (output-suffix (muse-style-element :osuffix style))
771 (muse-publishing-current-file file)
772 (muse-publishing-current-output-path output-path)
773 (target (if output-suffix
774 (concat (muse-path-sans-extension output-path)
775 output-suffix)
776 output-path))
777 (threshhold (nth 7 (file-attributes file))))
778 (if (not threshhold)
779 (message "Please save %s before publishing" file)
780 (when (or force (file-newer-than-file-p file target))
781 (if (and muse-publish-report-threshhold
782 (> threshhold
783 muse-publish-report-threshhold))
784 (message "Publishing %s ..." file))
785 (muse-with-temp-buffer
786 (insert-file-contents file)
787 (muse-publish-markup-buffer (muse-page-name file) style)
788 (let ((backup-inhibited t))
789 (write-file output-path))
790 (muse-style-run-hooks :final style file output-path target))
791 t))))
793 ;;;###autoload
794 (defun muse-publish-this-file (style output-dir &optional force)
795 "Publish the currently-visited file.
796 Prompt for both the STYLE and OUTPUT-DIR if they are not
797 supplied."
798 (interactive (muse-publish-get-info))
799 (if buffer-file-name
800 (let ((muse-current-output-style (list :base (car style)
801 :path output-dir)))
802 (unless (muse-publish-file buffer-file-name style output-dir force)
803 (message (concat "The published version is up-to-date; use"
804 " C-u C-c C-T to force an update."))))
805 (message "This buffer is not associated with any file")))
807 (defun muse-batch-publish-files ()
808 "Publish Muse files in batch mode."
809 (let ((muse-batch-publishing-p t)
810 style output-dir)
811 (setq style (car command-line-args-left)
812 command-line-args-left (cdr command-line-args-left)
813 output-dir (car command-line-args-left)
814 output-dir
815 (if (string-match "\\`--output-dir=" output-dir)
816 (prog1
817 (substring output-dir (match-end 0))
818 (setq command-line-args-left (cdr command-line-args-left)))))
819 (setq auto-mode-alist
820 (delete (cons (concat "\\." muse-file-extension "\\'")
821 'muse-mode-choose-mode)
822 auto-mode-alist))
823 (dolist (file command-line-args-left)
824 (muse-publish-file file style output-dir t))))
826 ;; Default publishing rules
828 (defun muse-publish-section-close (depth)
829 "Seach forward for the closing tag of given DEPTH."
830 (let (not-end)
831 (save-excursion
832 (while (and (setq not-end (re-search-forward
833 (concat "^\\*\\{1," (number-to-string depth)
834 "\\}\\s-+")
835 nil t))
836 (get-text-property (match-beginning 0) 'read-only)))
837 (if not-end
838 (forward-line 0)
839 (goto-char (point-max)))
840 (cond ((not (eq (char-before) ?\n))
841 (insert "\n\n"))
842 ((not (eq (char-before (1- (point))) ?\n))
843 (insert "\n")))
844 (muse-insert-markup (muse-markup-text 'section-close depth))
845 (insert "\n"))))
847 (defun muse-publish-markup-directive (&optional name value)
848 (unless name (setq name (match-string 1)))
849 (unless value (setq value (match-string 2)))
850 (let ((elem (assoc name muse-publishing-directives)))
851 (if elem
852 (setcdr elem value)
853 (setq muse-publishing-directives
854 (cons (cons name value)
855 muse-publishing-directives))))
856 ;; Make sure we don't ever try to move the point forward (past the
857 ;; beginning of buffer) while we're still searching for directives.
858 (setq muse-publishing-last-position nil)
859 (delete-region (match-beginning 0) (match-end 0)))
861 (defsubst muse-publishing-directive (name)
862 (cdr (assoc name muse-publishing-directives)))
864 (defmacro muse-publish-get-and-delete-attr (attr attrs)
865 "Delete attribute ATTR from ATTRS only once, destructively.
867 This function returns the matching attribute value, if found."
868 (let ((last (make-symbol "last"))
869 (found (make-symbol "found"))
870 (vals (make-symbol "vals")))
871 `(let ((,vals ,attrs))
872 (if (string= (caar ,vals) ,attr)
873 (prog1 (cdar ,vals)
874 (setq ,attrs (cdr ,vals)))
875 (let ((,last ,vals)
876 (,found nil))
877 (while ,vals
878 (setq ,vals (cdr ,vals))
879 (when (string= (caar ,vals) ,attr)
880 (setq ,found (cdar ,vals))
881 (setcdr ,last (cdr ,vals))
882 (setq ,vals nil))
883 (setq ,last ,vals))
884 ,found)))))
886 (defun muse-publish-markup-anchor ()
887 (unless (get-text-property (match-end 1) 'muse-link)
888 (let ((text (muse-markup-text 'anchor (match-string 2))))
889 (unless (string= text "")
890 (save-match-data
891 (skip-chars-forward (concat muse-regexp-blank "\n"))
892 (muse-insert-markup text)))
893 (match-string 1))))
895 (defun muse-publish-markup-comment ()
896 (if (null muse-publish-comments-p)
898 (goto-char (match-end 0))
899 (muse-insert-markup (muse-markup-text 'comment-end))
900 (if (match-beginning 1)
901 (progn
902 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
903 (delete-region (match-beginning 0) (match-beginning 1)))
904 (delete-region (match-beginning 0) (match-end 0)))
905 (goto-char (match-beginning 0))
906 (muse-insert-markup (muse-markup-text 'comment-begin))))
908 (defun muse-publish-markup-tag ()
909 (let ((tag-info (muse-markup-tag-info (match-string 1))))
910 (when (and tag-info
911 (not (get-text-property (match-beginning 0) 'read-only)))
912 (let ((closed-tag (match-string 3))
913 (start (match-beginning 0))
914 (beg (point))
915 end attrs)
916 (when (nth 2 tag-info)
917 (let ((attrstr (match-string 2)))
918 (while (and attrstr
919 (string-match (concat "\\([^"
920 muse-regexp-blank
921 "=\n]+\\)\\(=\"\\"
922 "([^\"]+\\)\"\\)?")
923 attrstr))
924 (let ((attr (cons (downcase
925 (muse-match-string-no-properties 1 attrstr))
926 (muse-match-string-no-properties 3 attrstr))))
927 (setq attrstr (replace-match "" t t attrstr))
928 (if attrs
929 (nconc attrs (list attr))
930 (setq attrs (list attr)))))))
931 (if (and (cadr tag-info) (not closed-tag))
932 (if (muse-goto-tag-end (car tag-info) (nth 3 tag-info))
933 (delete-region (match-beginning 0) (point))
934 (setq tag-info nil)))
935 (when tag-info
936 (setq end (point-marker))
937 (delete-region start beg)
938 (goto-char start)
939 (let ((args (list start end)))
940 (if (nth 2 tag-info)
941 (nconc args (list attrs)))
942 (let ((muse-inhibit-style-tags nil))
943 ;; remove the inhibition
944 (apply (nth 4 tag-info) args)))))))
945 nil)
947 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
948 "Escape specials from BEG to END using style-specific :specials.
949 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
950 CONTEXT is used to figure out what kind of specials to escape.
952 The following contexts exist in Muse.
953 'underline _underlined text_
954 'literal =monospaced text= or <code> region (monospaced, escaped)
955 'emphasis *emphasized text*
956 'email email@example.com
957 'url http://example.com
958 'url-desc [[...][description of an explicit link]]
959 'image [[image.png]]
960 'example <example> region (monospaced, block context, escaped)
961 'verbatim <verbatim> region (escaped)
962 'document normal text"
963 (let ((specials (muse-style-element :specials nil t)))
964 (cond ((functionp specials)
965 (setq specials (funcall specials context)))
966 ((symbolp specials)
967 (setq specials (symbol-value specials))))
968 (if (functionp specials)
969 (funcall specials beg end ignore-read-only)
970 (save-excursion
971 (save-restriction
972 (narrow-to-region beg end)
973 (goto-char (point-min))
974 (while (< (point) (point-max))
975 (if (and (not ignore-read-only)
976 (get-text-property (point) 'read-only))
977 (goto-char (or (next-single-property-change (point) 'read-only)
978 (point-max)))
979 (let ((repl (or (assoc (char-after) specials)
980 (assoc (char-after)
981 muse-publish-markup-specials))))
982 (if (null repl)
983 (forward-char 1)
984 (delete-char 1)
985 (insert-before-markers (cdr repl)))))))))))
987 (defun muse-publish-markup-word ()
988 (let* ((beg (match-beginning 2))
989 (end (1- (match-end 2)))
990 (leader (buffer-substring-no-properties beg end))
991 open-tag close-tag mark-read-only loc context)
992 (cond
993 ((string= leader "_")
994 (setq context 'underline
995 open-tag (muse-markup-text 'begin-underline)
996 close-tag (muse-markup-text 'end-underline)))
997 ((string= leader "=")
998 (setq context 'literal
999 open-tag (muse-markup-text 'begin-literal)
1000 close-tag (muse-markup-text 'end-literal))
1001 (setq mark-read-only t))
1003 (let ((l (length leader)))
1004 (setq context 'emphasis)
1005 (cond
1006 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
1007 close-tag (muse-markup-text 'end-emph)))
1008 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
1009 close-tag (muse-markup-text 'end-more-emph)))
1010 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
1011 close-tag (muse-markup-text 'end-most-emph)))
1012 (t (setq context nil))))))
1013 (if (and context
1014 (not (get-text-property beg 'muse-link))
1015 (setq loc (search-forward leader nil t))
1016 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
1017 (not (eq (char-syntax (char-before (point))) ?\ ))
1018 (not (get-text-property (point) 'muse-link)))
1019 (progn
1020 (replace-match "")
1021 (delete-region beg end)
1022 (setq end (point-marker))
1023 (muse-insert-markup close-tag)
1024 (goto-char beg)
1025 (muse-insert-markup open-tag)
1026 (setq beg (point))
1027 (when mark-read-only
1028 (muse-publish-escape-specials beg end t context)
1029 (muse-publish-mark-read-only beg end)))
1030 (backward-char))
1031 nil))
1033 (defun muse-publish-markup-emdash ()
1034 (unless (get-text-property (match-beginning 0) 'muse-link)
1035 (let ((prespace (match-string 1))
1036 (postspace (match-string 2)))
1037 (delete-region (match-beginning 0) (match-end 0))
1038 (muse-insert-markup (muse-markup-text 'emdash prespace postspace))
1039 (when (eq (char-after) ?\<)
1040 (insert ?\n)))))
1042 (defun muse-publish-markup-enddots ()
1043 (unless (get-text-property (match-beginning 0) 'muse-link)
1044 (delete-region (match-beginning 0) (match-end 0))
1045 (muse-insert-markup (muse-markup-text 'enddots))))
1047 (defun muse-publish-markup-dots ()
1048 (unless (get-text-property (match-beginning 0) 'muse-link)
1049 (delete-region (match-beginning 0) (match-end 0))
1050 (muse-insert-markup (muse-markup-text 'dots))))
1052 (defun muse-publish-markup-rule ()
1053 (unless (get-text-property (match-beginning 0) 'muse-link)
1054 (delete-region (match-beginning 0) (match-end 0))
1055 (muse-insert-markup (muse-markup-text 'rule))))
1057 (defun muse-publish-markup-no-break-space ()
1058 (unless (get-text-property (match-beginning 0) 'muse-link)
1059 (delete-region (match-beginning 0) (match-end 0))
1060 (muse-insert-markup (muse-markup-text 'no-break-space))))
1062 (defun muse-publish-markup-heading ()
1063 (let* ((len (length (match-string 1)))
1064 (start (muse-markup-text
1065 (cond ((= len 1) 'section)
1066 ((= len 2) 'subsection)
1067 ((= len 3) 'subsubsection)
1068 (t 'section-other))
1069 len))
1070 (end (muse-markup-text
1071 (cond ((= len 1) 'section-end)
1072 ((= len 2) 'subsection-end)
1073 ((= len 3) 'subsubsection-end)
1074 (t 'section-other-end))
1075 len)))
1076 (delete-region (match-beginning 0) (match-end 0))
1077 (muse-insert-markup start)
1078 (end-of-line)
1079 (when end
1080 (muse-insert-markup end))
1081 (forward-line 1)
1082 (unless (eq (char-after) ?\n)
1083 (insert "\n"))
1084 (muse-publish-section-close len)))
1086 (defvar muse-publish-footnotes nil)
1088 (defun muse-publish-markup-footnote ()
1089 "Scan ahead and snarf up the footnote body"
1090 (cond
1091 ((get-text-property (match-beginning 0) 'muse-link)
1092 nil)
1093 ((= (muse-line-beginning-position) (match-beginning 0))
1096 (let ((footnote (save-match-data
1097 (string-to-number (match-string 1))))
1098 (oldtext (match-string 0))
1099 footnotemark)
1100 (delete-region (match-beginning 0) (match-end 0))
1101 (save-excursion
1102 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
1103 (let* ((start (match-beginning 0))
1104 (beg (goto-char (match-end 0)))
1105 (end (save-excursion
1106 (if (search-forward "\n\n" nil t)
1107 (copy-marker (match-beginning 0))
1108 (goto-char (point-max))
1109 (skip-chars-backward "\n")
1110 (point-marker)))))
1111 (while (re-search-forward
1112 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
1113 end t)
1114 (replace-match "\\1" t))
1115 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
1116 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
1117 (if (string= "" footnotemark-cmd)
1118 (setq footnotemark
1119 (concat (muse-markup-text 'footnote)
1120 (buffer-substring-no-properties beg end)
1121 (muse-markup-text 'footnote-end)))
1122 (setq footnotemark (format footnotemark-cmd footnote
1123 footnotemark-end-cmd))
1124 (unless muse-publish-footnotes
1125 (set (make-local-variable 'muse-publish-footnotes)
1126 (make-vector 256 nil)))
1127 (unless (aref muse-publish-footnotes footnote)
1128 (setq footnotemark
1129 (concat
1130 footnotemark
1131 (concat (format (muse-markup-text 'footnotetext)
1132 footnote)
1133 (buffer-substring-no-properties beg end)
1134 (muse-markup-text 'footnotetext-end))))
1135 (aset muse-publish-footnotes footnote footnotemark))))
1136 (goto-char end)
1137 (skip-chars-forward "\n")
1138 (delete-region start (point)))))
1139 (if footnotemark
1140 (muse-insert-markup footnotemark)
1141 (insert oldtext))))))
1143 (defun muse-publish-markup-fn-sep ()
1144 (delete-region (match-beginning 0) (match-end 0))
1145 (muse-insert-markup (muse-markup-text 'fn-sep)))
1147 (defun muse-insert-markup-end-list (&rest args)
1148 (let ((beg (point)))
1149 (apply 'insert args)
1150 (add-text-properties beg (point) '(end-list t))
1151 (muse-publish-mark-read-only beg (point))))
1153 (defun muse-publish-determine-dl-indent (continue indent-sym determine-sym)
1154 ;; If the caller doesn't know how much indentation to use, figure it
1155 ;; out ourselves. It is assumed that `muse-forward-list-item' has
1156 ;; been called just before this to set the match data.
1157 (when (and continue
1158 (symbol-value determine-sym))
1159 (save-match-data
1160 ;; snarf all leading whitespace
1161 (let ((indent (and (match-beginning 2)
1162 (buffer-substring (match-beginning 1)
1163 (match-beginning 2)))))
1164 (when (and indent
1165 (not (string= indent "")))
1166 (set indent-sym indent)
1167 (set determine-sym nil))))))
1169 (defun muse-publish-surround-dl (indent post-indent)
1170 (let* ((beg-item (muse-markup-text 'begin-dl-item))
1171 (end-item (muse-markup-text 'end-dl-item))
1172 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
1173 (end-ddt (muse-markup-text 'end-ddt))
1174 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
1175 (end-dde (muse-markup-text 'end-dde))
1176 (continue t)
1177 def-on-same-line beg)
1178 (while continue
1179 ;; envelope this as one term+definitions unit -- HTML does not
1180 ;; need this, but DocBook and Muse's custom XML format do
1181 (muse-insert-markup beg-item)
1182 (when (looking-at muse-dl-term-regexp)
1183 ;; find the term and wrap it with published markup
1184 (setq beg (point))
1185 (goto-char (match-end 1))
1186 (delete-region (point) (match-end 0))
1187 (muse-insert-markup-end-list end-ddt)
1188 ;; if definition is immediately after term, move to next line
1189 (unless (eq (char-after) ?\n)
1190 (insert ?\n))
1191 (save-excursion
1192 (goto-char beg)
1193 (delete-region (point) (match-beginning 1))
1194 (muse-insert-markup beg-ddt)))
1195 (setq beg (point)
1196 ;; move past current item
1197 continue (muse-forward-list-item 'dl-term indent))
1198 (save-restriction
1199 (narrow-to-region beg (point))
1200 (goto-char (point-min))
1201 ;; publish each definition that we find, defaulting to an
1202 ;; empty definition if none are found
1203 (muse-publish-surround-text beg-dde end-dde
1204 (lambda (indent)
1205 (muse-forward-list-item 'dl-entry indent))
1206 indent post-indent
1207 #'muse-publish-determine-dl-indent)
1208 (goto-char (point-max))
1209 (skip-chars-backward (concat muse-regexp-blank "\n"))
1210 (muse-insert-markup-end-list end-item)
1211 (when continue
1212 (goto-char (point-max)))))))
1214 (defun muse-publish-strip-list-indentation (list-item empty-line indent post-indent)
1215 (let ((list-nested nil)
1216 (indent-found nil))
1217 (while (< (point) (point-max))
1218 (when (and (looking-at list-item)
1219 (not (or (get-text-property
1220 (muse-list-item-critical-point) 'read-only)
1221 (get-text-property
1222 (muse-list-item-critical-point) 'muse-link))))
1223 ;; if we encounter a list item, allow no post-indent space
1224 (setq list-nested t))
1225 (when (and (not (looking-at empty-line))
1226 (looking-at (concat indent "\\("
1227 (or (and list-nested "")
1228 post-indent)
1229 "\\)")))
1230 ;; if list is not nested, remove indentation
1231 (unless indent-found
1232 (setq post-indent (match-string 1)
1233 indent-found t))
1234 (replace-match ""))
1235 (forward-line 1))))
1237 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent determine-indent-func list-item)
1238 (unless list-item
1239 (setq list-item (format muse-list-item-regexp
1240 (concat "[" muse-regexp-blank "]*"))))
1241 (let ((continue t)
1242 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
1243 (determine-indent (if determine-indent-func t nil))
1244 (new-indent indent)
1245 (first t)
1246 beg)
1247 (unless indent
1248 (setq indent (concat "[" muse-regexp-blank "]+")))
1249 (if post-indent
1250 (setq post-indent (concat " \\{0," (number-to-string post-indent)
1251 "\\}"))
1252 (setq post-indent ""))
1253 (while continue
1254 (if (or (not end-tag) (string= end-tag ""))
1255 ;; if no end of list item markup exists, treat the beginning
1256 ;; of list item markup as it if it were the end -- this
1257 ;; prevents multiple-level lists from being confused
1258 (muse-insert-markup-end-list beg-tag)
1259 (muse-insert-markup beg-tag))
1260 (setq beg (point)
1261 ;; move past current item; continue is non-nil if there
1262 ;; are more like items to be processed
1263 continue (if (and determine-indent-func first)
1264 (funcall move-func (concat indent post-indent))
1265 (funcall move-func indent)))
1266 (when determine-indent-func
1267 (funcall determine-indent-func continue 'new-indent 'determine-indent))
1268 (when continue
1269 ;; remove list markup if we encountered another item of the
1270 ;; same type
1271 (replace-match "" t t nil 1))
1272 (save-restriction
1273 ;; narrow to current item
1274 (narrow-to-region beg (point))
1275 ;; move to second line of text
1276 (goto-char (point-min))
1277 (while (and (< (point) (point-max))
1278 (looking-at empty-line))
1279 (forward-line 1))
1280 (forward-line 1)
1281 ;; strip list indentation
1282 (muse-publish-strip-list-indentation list-item empty-line
1283 indent post-indent)
1284 (skip-chars-backward (concat muse-regexp-blank "\n"))
1285 (muse-insert-markup-end-list end-tag)
1286 (when determine-indent-func
1287 (setq indent new-indent))
1288 (when first
1289 (setq first nil))
1290 (when continue
1291 (goto-char (point-max)))))))
1293 (defun muse-publish-markup-list ()
1294 "Markup a list entry.
1295 This function works by marking up items of the same list level
1296 and type, respecting the end-of-list property."
1297 (let* ((str (match-string 1))
1298 (type (muse-list-item-type str))
1299 (indent (buffer-substring (muse-line-beginning-position)
1300 (match-beginning 1)))
1301 (post-indent (length str)))
1302 (cond
1303 ((or (get-text-property (muse-list-item-critical-point) 'read-only)
1304 (get-text-property (muse-list-item-critical-point) 'muse-link))
1305 nil)
1306 ((eq type 'ul)
1307 (unless (eq (char-after (match-end 1)) ?-)
1308 (delete-region (match-beginning 0) (match-end 0))
1309 (let ((beg (point)))
1310 (muse-publish-ensure-block beg))
1311 (muse-insert-markup (muse-markup-text 'begin-uli))
1312 (save-excursion
1313 (muse-publish-surround-text
1314 (muse-markup-text 'begin-uli-item)
1315 (muse-markup-text 'end-uli-item)
1316 (lambda (indent)
1317 (muse-forward-list-item 'ul indent))
1318 indent post-indent)
1319 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1320 (forward-line 1)))
1321 ((eq type 'ol)
1322 (delete-region (match-beginning 0) (match-end 0))
1323 (let ((beg (point)))
1324 (muse-publish-ensure-block beg))
1325 (muse-insert-markup (muse-markup-text 'begin-oli))
1326 (save-excursion
1327 (muse-publish-surround-text
1328 (muse-markup-text 'begin-oli-item)
1329 (muse-markup-text 'end-oli-item)
1330 (lambda (indent)
1331 (muse-forward-list-item 'ol indent))
1332 indent post-indent)
1333 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1334 (forward-line 1))
1335 ((not (string= (match-string 2) ""))
1336 ;; must have an initial term
1337 (goto-char (match-beginning 0))
1338 (let ((beg (point)))
1339 (muse-publish-ensure-block beg))
1340 (muse-insert-markup (muse-markup-text 'begin-dl))
1341 (save-excursion
1342 (muse-publish-surround-dl indent post-indent)
1343 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1344 (forward-line 1))))
1345 nil)
1347 (defun muse-publish-markup-quote ()
1348 "Markup a quoted paragraph.
1349 The reason this function is so funky, is to prevent text properties
1350 like read-only from being inadvertently deleted."
1351 (let* ((ws (match-string 1))
1352 (centered (>= (string-width ws) 6))
1353 (begin-elem (if centered 'begin-center 'begin-quote-item))
1354 (end-elem (if centered 'end-center 'end-quote-item)))
1355 (replace-match "" t t nil 1)
1356 (unless centered
1357 (muse-insert-markup (muse-markup-text 'begin-quote)))
1358 (muse-publish-surround-text (muse-markup-text begin-elem)
1359 (muse-markup-text end-elem)
1360 (function (lambda (indent)
1361 (muse-forward-paragraph)
1362 nil)))
1363 (unless centered
1364 (muse-insert-markup (muse-markup-text 'end-quote)))))
1366 (defun muse-publish-markup-leading-space (markup-space multiple)
1367 (let (count)
1368 (when (and markup-space
1369 (>= (setq count (skip-chars-forward " ")) 0))
1370 (delete-region (muse-line-beginning-position) (point))
1371 (while (> count 0)
1372 (muse-insert-markup markup-space)
1373 (setq count (- count multiple))))))
1375 (defun muse-publish-markup-verse ()
1376 (let ((leader (match-string 0)))
1377 (goto-char (match-beginning 0))
1378 (muse-insert-markup (muse-markup-text 'begin-verse))
1379 (while (looking-at leader)
1380 (replace-match "")
1381 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1382 (let ((beg (point)))
1383 (end-of-line)
1384 (cond
1385 ((bolp)
1386 (let ((text (muse-markup-text 'empty-verse-line)))
1387 (when text (muse-insert-markup text))))
1388 ((save-excursion
1389 (save-match-data
1390 (forward-line 1)
1391 (or (looking-at (concat leader "["
1392 muse-regexp-blank
1393 "]*$"))
1394 (not (looking-at leader)))))
1395 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1396 (end-text (muse-markup-text 'end-last-stanza-line)))
1397 (when end-text (muse-insert-markup end-text))
1398 (goto-char beg)
1399 (when begin-text (muse-insert-markup begin-text))
1400 (end-of-line)))
1402 (let ((begin-text (muse-markup-text 'begin-verse-line))
1403 (end-text (muse-markup-text 'end-verse-line)))
1404 (when end-text (muse-insert-markup end-text))
1405 (goto-char beg)
1406 (when begin-text (muse-insert-markup begin-text))
1407 (end-of-line))))
1408 (forward-line 1))))
1409 (muse-insert-markup (muse-markup-text 'end-verse))
1410 (insert ?\n))
1412 (defun muse-publish-trim-table (table)
1413 "Remove completely blank columns from table, if at start or end of row."
1414 ;; remove first
1415 (catch 'found
1416 (dolist (row (cdr table))
1417 (let ((el (cadr row)))
1418 (when (and (stringp el) (not (string= el "")))
1419 (throw 'found t))))
1420 (dolist (row (cdr table))
1421 (setcdr row (cddr row)))
1422 (setcar table (1- (car table))))
1423 ;; remove last
1424 (catch 'found
1425 (dolist (row (cdr table))
1426 (let ((el (car (last row))))
1427 (when (and (stringp el) (not (string= el "")))
1428 (throw 'found t))))
1429 (dolist (row (cdr table))
1430 (setcdr (last row 2) nil))
1431 (setcar table (1- (car table))))
1432 table)
1434 (defun muse-publish-table-fields (beg end)
1435 "Parse given region as a table, returning a cons cell.
1436 The car is the length of the longest row.
1438 The cdr is a list of the fields of the table, with the first
1439 element indicating the type of the row:
1440 1: body, 2: header, 3: footer, hline: separator.
1442 The existing region will be removed, except for initial blank lines."
1443 (unless (muse-publishing-directive "disable-tables")
1444 (let ((longest 0)
1445 (left 0)
1446 (seen-hline nil)
1447 fields field-list)
1448 (save-restriction
1449 (narrow-to-region beg end)
1450 (goto-char (point-min))
1451 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1452 (forward-line 1))
1453 (setq beg (point))
1454 (while (= left 0)
1455 (cond
1456 ((looking-at muse-table-hline-regexp)
1457 (when field-list ; skip if at the beginning of table
1458 (if seen-hline
1459 (setq field-list (cons (cons 'hline nil) field-list))
1460 (dolist (field field-list)
1461 ;; the preceding fields are header lines
1462 (setcar field 2))
1463 (setq seen-hline t))))
1464 ((looking-at muse-table-line-regexp)
1465 (setq fields (cons (length (match-string 1))
1466 (mapcar #'muse-trim-whitespace
1467 (split-string (match-string 0)
1468 muse-table-field-regexp)))
1469 field-list (cons fields field-list)
1470 longest (max (length fields) longest))
1471 ;; strip initial bars, if they exist
1472 (let ((first (cadr fields)))
1473 (when (and first (string-match "\\`|+\\s-*" first))
1474 (setcar (cdr fields) (replace-match "" t t first))))))
1475 (setq left (forward-line 1))))
1476 (delete-region beg end)
1477 (if (= longest 0)
1478 (cons 0 nil)
1479 ;; if the last line was an hline, remove it
1480 (when (eq (caar field-list) 'hline)
1481 (setq field-list (cdr field-list)))
1482 (muse-publish-trim-table (cons (1- longest) (nreverse field-list)))))))
1484 (defun muse-publish-markup-table ()
1485 "Style does not support tables.\n")
1487 (defun muse-publish-table-el-table (variant)
1488 "Publish table.el-style tables in the format given by VARIANT."
1489 (when (condition-case nil
1490 (progn (require 'table)
1492 (error nil))
1493 (let ((muse-buf (current-buffer)))
1494 (save-restriction
1495 (narrow-to-region (match-beginning 0) (match-end 0))
1496 (goto-char (point-min))
1497 (forward-line 1)
1498 (search-forward "|" nil t)
1499 (with-temp-buffer
1500 (let ((temp-buf (current-buffer)))
1501 (with-current-buffer muse-buf
1502 (table-generate-source variant temp-buf))
1503 (with-current-buffer muse-buf
1504 (delete-region (point-min) (point-max))
1505 (insert-buffer-substring temp-buf)
1506 (muse-publish-mark-read-only (point-min) (point-max)))))))))
1508 (defun muse-publish-markup-table-el ()
1509 "Mark up table.el-style tables."
1510 (cond ((muse-style-derived-p 'html)
1511 (muse-publish-table-el-table 'html))
1512 ((muse-style-derived-p 'latex)
1513 (muse-publish-table-el-table 'latex))
1514 ((muse-style-derived-p 'docbook)
1515 (muse-publish-table-el-table 'cals))
1516 (t "Style does not support table.el tables.\n")))
1518 (defun muse-publish-escape-specials-in-string (string &optional context)
1519 "Escape specials in STRING using style-specific :specials.
1520 CONTEXT is used to figure out what kind of specials to escape.
1522 See the documentation of the `muse-publish-escape-specials'
1523 function for the list of available contexts."
1524 (unless string
1525 (setq string ""))
1526 (let ((specials (muse-style-element :specials nil t)))
1527 (cond ((functionp specials)
1528 (setq specials (funcall specials context)))
1529 ((symbolp specials)
1530 (setq specials (symbol-value specials))))
1531 (if (functionp specials)
1532 (funcall specials string)
1533 (apply (function concat)
1534 (mapcar
1535 (lambda (ch)
1536 (let ((repl (or (assoc ch specials)
1537 (assoc ch muse-publish-markup-specials))))
1538 (if (null repl)
1539 (char-to-string ch)
1540 (cdr repl))))
1541 (append string nil))))))
1543 (defun muse-publish-markup-email ()
1544 (let* ((beg (match-end 1))
1545 (addr (buffer-substring-no-properties beg (match-end 0))))
1546 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1547 (goto-char beg)
1548 (delete-region beg (match-end 0))
1549 (if (or (eq (char-before (match-beginning 0)) ?\")
1550 (eq (char-after (match-end 0)) ?\"))
1551 (insert addr)
1552 (insert (format (muse-markup-text 'email-addr) addr addr)))
1553 (muse-publish-mark-read-only beg (point))))
1555 (defun muse-publish-classify-url (target)
1556 "Transform anchors and get published name, if TARGET is a page.
1557 The return value is two linked cons cells. The car is the type
1558 of link, the cadr is the page name, and the cddr is the anchor."
1559 (save-match-data
1560 (cond ((or (null target) (string= target ""))
1561 nil)
1562 ((string-match "\\`[uU][rR][lL]:\\(.+\\)\\'" target)
1563 (cons 'url (cons (match-string 1 target) nil)))
1564 ((string-match muse-image-regexp target)
1565 (cons 'image (cons target nil)))
1566 ((string-match muse-url-regexp target)
1567 (cons 'url (cons target nil)))
1568 ((string-match muse-file-regexp target)
1569 (cons 'file (cons target nil)))
1570 ((string-match "#" target)
1571 (if (eq (aref target 0) ?\#)
1572 (cons 'anchor-ref (cons nil (substring target 1)))
1573 (cons 'link-and-anchor
1574 (cons (muse-publish-link-page
1575 (substring target 0 (match-beginning 0)))
1576 (substring target (match-end 0))))))
1578 (cons 'link (cons (muse-publish-link-page target) nil))))))
1580 (defun muse-publish-url-desc (desc explicit)
1581 (when desc
1582 (dolist (transform muse-publish-desc-transforms)
1583 (setq desc (save-match-data
1584 (when desc (funcall transform desc explicit)))))
1585 (setq desc (muse-link-unescape desc))
1586 (muse-publish-escape-specials-in-string desc 'url-desc)))
1588 (defun muse-publish-url (url &optional desc orig-url explicit)
1589 "Resolve a URL into its final <a href> form."
1590 (let (type anchor)
1591 (dolist (transform muse-publish-url-transforms)
1592 (setq url (save-match-data (when url (funcall transform url explicit)))))
1593 (if desc
1594 (setq desc (muse-publish-url-desc desc explicit))
1595 (if orig-url
1596 (setq orig-url (muse-publish-url-desc orig-url explicit))))
1597 (let ((target (muse-publish-classify-url url)))
1598 (setq type (car target)
1599 url (if (eq type 'image)
1600 (muse-publish-escape-specials-in-string (cadr target)
1601 'image)
1602 (muse-publish-escape-specials-in-string (cadr target) 'url))
1603 anchor (muse-publish-escape-specials-in-string
1604 (cddr target) 'url)))
1605 (cond ((eq type 'anchor-ref)
1606 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1607 ((and desc (string-match muse-image-regexp desc))
1608 (let ((ext (or (file-name-extension desc) "")))
1609 (setq desc (muse-path-sans-extension desc))
1610 (muse-markup-text 'image-link url desc ext)))
1611 ((string= url "")
1612 desc)
1613 ((eq type 'image)
1614 (let ((ext (or (file-name-extension url) "")))
1615 (setq url (muse-path-sans-extension url))
1616 (if desc
1617 (muse-markup-text 'image-with-desc url ext desc)
1618 (muse-markup-text 'image url ext))))
1619 ((eq type 'link-and-anchor)
1620 (muse-markup-text 'link-and-anchor url anchor
1621 (or desc orig-url)
1622 (muse-path-sans-extension url)))
1623 ((eq type 'link)
1624 (muse-markup-text 'link url (or desc orig-url)))
1626 (or (and (or desc
1627 (not (string= url orig-url)))
1628 (let ((text (muse-markup-text 'url-and-desc url
1629 (or desc orig-url))))
1630 (and (not (string= text ""))
1631 text)))
1632 (muse-markup-text 'url url (or desc orig-url)))))))
1634 (defun muse-publish-insert-url (url &optional desc orig-url explicit)
1635 "Resolve a URL into its final <a href> form."
1636 (delete-region (match-beginning 0) (match-end 0))
1637 (let ((text (muse-publish-url url desc orig-url explicit)))
1638 (when text
1639 (muse-insert-markup text))))
1641 (defun muse-publish-markup-link ()
1642 (let (desc explicit orig-link link)
1643 (setq explicit (save-match-data
1644 (if (string-match muse-explicit-link-regexp
1645 (match-string 0))
1646 t nil)))
1647 (setq orig-link (if explicit (match-string 1) (match-string 0)))
1648 (setq desc (when explicit (match-string 2)))
1649 (setq link (if explicit
1650 (muse-handle-explicit-link orig-link)
1651 (muse-handle-implicit-link orig-link)))
1652 (when (and link
1653 (or explicit
1654 (not (or (eq (char-before (match-beginning 0)) ?\")
1655 (eq (char-after (match-end 0)) ?\")))))
1656 ;; if explicit link has no user-provided description, treat it
1657 ;; as if it were an implicit link
1658 (when (and explicit (not desc))
1659 (setq explicit nil))
1660 (muse-publish-insert-url link desc orig-link explicit))))
1662 (defun muse-publish-markup-url ()
1663 (unless (or (eq (char-before (match-beginning 0)) ?\")
1664 (eq (char-after (match-end 0)) ?\"))
1665 (let ((url (match-string 0)))
1666 (muse-publish-insert-url url nil url))))
1668 ;; Default publishing tags
1670 (defcustom muse-publish-contents-depth 2
1671 "The number of heading levels to include with <contents> tags."
1672 :type 'integer
1673 :group 'muse-publish)
1675 (defun muse-publish-contents-tag (beg end attrs)
1676 (set (make-local-variable 'muse-publish-generate-contents)
1677 (cons (copy-marker (point) t)
1678 (let ((depth (cdr (assoc "depth" attrs))))
1679 (or (and depth (string-to-number depth))
1680 muse-publish-contents-depth)))))
1682 (defun muse-publish-verse-tag (beg end)
1683 (muse-publish-ensure-block beg end)
1684 (save-excursion
1685 (save-restriction
1686 (narrow-to-region beg end)
1687 (goto-char (point-min))
1688 (while (eq ?\ (char-syntax (char-after)))
1689 (delete-char 1))
1690 (while (< (point) (point-max))
1691 (insert "> ")
1692 (forward-line))
1693 (if (eq ?\ (char-syntax (char-before)))
1694 (delete-char -1)))))
1696 (defun muse-publish-mark-read-only (beg end)
1697 "Add read-only properties to the given region."
1698 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1699 nil)
1701 (defun muse-publish-mark-link (&optional beg end)
1702 "Indicate that the given region is a Muse link, so that other
1703 markup elements respect it. If a region is not specified, use
1704 the 0th match data to determine it.
1706 This is usually applied to explicit links."
1707 (unless beg (setq beg (match-beginning 0)))
1708 (unless end (setq end (match-end 0)))
1709 (add-text-properties beg end '(muse-link t))
1710 nil)
1712 (defun muse-publish-quote-tag (beg end)
1713 (muse-publish-ensure-block beg)
1714 (save-excursion
1715 (save-restriction
1716 (narrow-to-region beg end)
1717 (let ((quote-regexp "^\\(<\\(/?\\)quote>\\)"))
1718 (muse-insert-markup (muse-markup-text 'begin-quote))
1719 (while (progn
1720 (unless (looking-at (concat "[" muse-regexp-blank "\n]*"
1721 "<quote>"))
1722 (muse-publish-surround-text
1723 (muse-markup-text 'begin-quote-item)
1724 (muse-markup-text 'end-quote-item)
1725 (function
1726 (lambda (indent)
1727 (muse-forward-paragraph)
1728 (goto-char (match-end 0))
1729 (and (< (point) (point-max))
1730 (not (looking-at quote-regexp)))))
1731 nil nil nil
1732 quote-regexp))
1733 (if (>= (point) (point-max))
1735 (and (search-forward "<quote>" nil t)
1736 (muse-goto-tag-end "quote" t)
1737 (progn (forward-line 1) t)
1738 (< (point) (point-max))))))
1739 (goto-char (point-max))
1740 (muse-insert-markup (muse-markup-text 'end-quote))))))
1742 (defun muse-publish-code-tag (beg end)
1743 (muse-publish-escape-specials beg end nil 'literal)
1744 (goto-char beg)
1745 (insert (muse-markup-text 'begin-literal))
1746 (goto-char end)
1747 (insert (muse-markup-text 'end-literal))
1748 (muse-publish-mark-read-only beg (point)))
1750 (defun muse-publish-cite-tag (beg end attrs)
1751 (let* ((type (muse-publish-get-and-delete-attr "type" attrs))
1752 (muse-publishing-directives muse-publishing-directives)
1753 (citetag
1754 (cond ((string-equal type "author")
1755 'begin-cite-author)
1756 ((string-equal type "year")
1757 'begin-cite-year)
1759 'begin-cite))))
1760 (goto-char beg)
1761 (insert (muse-markup-text citetag (muse-publishing-directive "bibsource")))
1762 (goto-char end)
1763 (insert (muse-markup-text 'end-cite))
1764 (muse-publish-mark-read-only beg (point))))
1766 (defun muse-publish-src-tag (beg end attrs)
1767 (muse-publish-example-tag beg end))
1769 (defun muse-publish-example-tag (beg end)
1770 (muse-publish-ensure-block beg end)
1771 (muse-publish-escape-specials beg end nil 'example)
1772 (goto-char beg)
1773 (insert (muse-markup-text 'begin-example))
1774 (goto-char end)
1775 (insert (muse-markup-text 'end-example))
1776 (muse-publish-mark-read-only beg (point)))
1778 (defun muse-publish-literal-tag (beg end attrs)
1779 "Ensure that the text between BEG and END is not interpreted later on.
1781 ATTRS is an alist of attributes.
1783 If it contains a \"style\" element, delete the region if the
1784 current style is neither derived from nor equal to this style.
1786 If it contains both a \"style\" element and an \"exact\" element
1787 with the value \"t\", delete the region only if the current style
1788 is exactly this style."
1789 (let* ((style (cdr (assoc "style" attrs)))
1790 (exact (cdr (assoc "exact" attrs)))
1791 (exactp (and (stringp exact) (string= exact "t"))))
1792 (if (or (not style)
1793 (and exactp (equal (muse-style style)
1794 muse-publishing-current-style))
1795 (and (not exactp) (muse-style-derived-p style)))
1796 (muse-publish-mark-read-only beg end)
1797 (delete-region beg end)
1798 (when (and (bolp) (eolp) (not (eobp)))
1799 (delete-char 1)))))
1801 (defun muse-publish-verbatim-tag (beg end)
1802 (muse-publish-escape-specials beg end nil 'verbatim)
1803 (muse-publish-mark-read-only beg end))
1805 (defalias 'muse-publish-class-tag 'ignore)
1807 (defun muse-publish-call-tag-on-buffer (tag &optional attrs)
1808 "Transform the current buffer as if it were surrounded by the tag TAG.
1809 If attributes ATTRS are given, pass them to the tag function."
1810 (let ((tag-info (muse-markup-tag-info tag)))
1811 (when tag-info
1812 (let* ((end (progn (goto-char (point-max)) (point-marker)))
1813 (args (list (point-min) end))
1814 (muse-inhibit-style-tags nil))
1815 (when (nth 2 tag-info)
1816 (nconc args (list attrs)))
1817 (apply (nth 4 tag-info) args)))))
1819 (defun muse-publish-examplify-buffer (&optional attrs)
1820 "Transform the current buffer as if it were an <example> region."
1821 (muse-publish-call-tag-on-buffer "example" attrs))
1823 (defun muse-publish-srcify-buffer (&optional attrs)
1824 "Transform the current buffer as if it were a <src> region."
1825 (muse-publish-call-tag-on-buffer "src" attrs))
1827 (defun muse-publish-versify-buffer (&optional attrs)
1828 "Transform the current buffer as if it were a <verse> region."
1829 (muse-publish-call-tag-on-buffer "verse" attrs)
1830 (muse-publish-markup ""
1831 `((100 ,(concat "^[" muse-regexp-blank "]*> ") 0
1832 muse-publish-markup-verse)))
1833 (goto-char (point-min)))
1835 (defmacro muse-publish-markup-attribute (beg end attrs reinterp &rest body)
1836 "Evaluate BODY within the bounds of BEG and END.
1837 ATTRS is an alist. Only the \"markup\" element of ATTRS is acted
1840 If it is omitted, publish the region with the normal Muse rules.
1841 If RE-INTERP is specified, this is done immediately in a new
1842 publishing process. Currently, RE-INTERP is specified only by
1843 the <include> tag.
1845 If \"nil\", do not mark up the region at all, but prevent it from
1846 being further interpreted by Muse.
1848 If \"example\", treat the region as if it was surrounded by the
1849 <example> tag.
1851 If \"src\", treat the region as if it was surrounded by the
1852 <src> tag.
1854 If \"verse\", treat the region as if it was surrounded by the
1855 <verse> tag, to preserve newlines.
1857 Otherwise, it should be the name of a function to call in the
1858 narrowed region after evaluating BODY. The function should
1859 take the ATTRS parameter.
1861 BEG is modified to be the start of the published markup."
1862 (let ((markup (make-symbol "markup"))
1863 (markup-function (make-symbol "markup-function")))
1864 `(let ((,markup (muse-publish-get-and-delete-attr "markup" ,attrs)))
1865 (save-restriction
1866 (narrow-to-region ,beg ,end)
1867 (goto-char (point-min))
1868 ,@body
1869 (if (not ,markup)
1870 (when ,reinterp
1871 (muse-publish-markup-region (point-min) (point-max))
1872 (muse-publish-mark-read-only (point-min) (point-max))
1873 (goto-char (point-max)))
1874 (let ((,markup-function (read ,markup)))
1875 (cond ((eq ,markup-function 'example)
1876 (setq ,markup-function #'muse-publish-examplify-buffer))
1877 ((eq ,markup-function 'src)
1878 (setq ,markup-function #'muse-publish-srcify-buffer))
1879 ((eq ,markup-function 'verse)
1880 (setq ,markup-function #'muse-publish-versify-buffer))
1881 ((and ,markup-function (not (functionp ,markup-function)))
1882 (error "Invalid markup function `%s'" ,markup))
1883 (t nil))
1884 (if ,markup-function
1885 (funcall ,markup-function ,attrs)
1886 (muse-publish-mark-read-only (point-min) (point-max))
1887 (goto-char (point-max)))))))))
1889 (put 'muse-publish-markup-attribute 'lisp-indent-function 4)
1890 (put 'muse-publish-markup-attribute 'edebug-form-spec
1891 '(form form form form body))
1893 (defun muse-publish-lisp-tag (beg end attrs)
1894 (muse-publish-markup-attribute beg end attrs nil
1895 (save-excursion
1896 (save-restriction
1897 (let ((str (muse-eval-lisp
1898 (prog1
1899 (concat "(progn "
1900 (buffer-substring-no-properties (point-min)
1901 (point-max))
1902 ")")
1903 (delete-region (point-min) (point-max))
1904 (widen)))))
1905 (set-text-properties 0 (length str) nil str)
1906 (insert str))))))
1908 (defun muse-publish-command-tag (beg end attrs)
1909 (muse-publish-markup-attribute beg end attrs nil
1910 (while (looking-at "\\s-*$")
1911 (forward-line))
1912 (let ((interp (muse-publish-get-and-delete-attr "interp" attrs)))
1913 (if interp
1914 (shell-command-on-region (point) (point-max) interp t t)
1915 (shell-command
1916 (prog1
1917 (buffer-substring-no-properties (point) (point-max))
1918 (delete-region (point-min) (point-max)))
1919 t)))
1920 ;; make sure there is a newline at end
1921 (goto-char (point-max))
1922 (forward-line 0)
1923 (unless (looking-at "\\s-*$")
1924 (goto-char (point-max))
1925 (insert ?\n))
1926 (goto-char (point-min))))
1928 (defun muse-publish-perl-tag (beg end attrs)
1929 (muse-publish-command-tag beg end
1930 (cons (cons "interp" (executable-find "perl"))
1931 attrs)))
1933 (defun muse-publish-python-tag (beg end attrs)
1934 (muse-publish-command-tag beg end
1935 (cons (cons "interp" (executable-find "python"))
1936 attrs)))
1938 (defun muse-publish-ruby-tag (beg end attrs)
1939 (muse-publish-command-tag beg end
1940 (cons (cons "interp" (executable-find "ruby"))
1941 attrs)))
1943 (defun muse-publish-comment-tag (beg end)
1944 (if (null muse-publish-comments-p)
1945 (delete-region beg end)
1946 (goto-char end)
1947 (muse-insert-markup (muse-markup-text 'comment-end))
1948 (muse-publish-mark-read-only beg end)
1949 (goto-char beg)
1950 (muse-insert-markup (muse-markup-text 'comment-begin))))
1952 (defun muse-publish-include-tag (beg end attrs)
1953 "Include the named file at the current location during publishing.
1955 <include file=\"...\" markup=\"...\">
1957 The `markup' attribute controls how this file is marked up after
1958 being inserted. See `muse-publish-markup-attribute' for an
1959 explanation of how it works."
1960 (let ((filename (muse-publish-get-and-delete-attr "file" attrs))
1961 (muse-publishing-directives muse-publishing-directives))
1962 (if filename
1963 (setq filename (expand-file-name
1964 filename
1965 (file-name-directory muse-publishing-current-file)))
1966 (error "No file attribute specified in <include> tag"))
1967 (muse-publish-markup-attribute beg end attrs t
1968 (insert-file-contents filename))))
1970 (defun muse-publish-mark-up-tag (beg end attrs)
1971 "Run an Emacs Lisp function on the region delimted by this tag.
1973 <markup function=\"...\" style=\"...\" exact=\"...\">
1975 The optional \"function\" attribute controls how this section is
1976 marked up. If used, it should be the name of a function to call
1977 with the buffer narrowed to the delimited region. Note that no
1978 further marking-up will be performed on this region.
1980 If \"function\" is omitted, use the standard Muse markup function.
1981 This is useful for marking up content in headers and footers.
1983 The optional \"style\" attribute causes the region to be deleted
1984 if the current style is neither derived from nor equal to this
1985 style.
1987 If both a \"style\" attribute and an \"exact\" attribute are
1988 provided, and \"exact\" is \"t\", delete the region only if the
1989 current style is exactly this style."
1990 (let* ((style (cdr (assoc "style" attrs)))
1991 (exact (cdr (assoc "exact" attrs)))
1992 (exactp (and (stringp exact) (string= exact "t"))))
1993 (if (or (not style)
1994 (and exactp (equal (muse-style style)
1995 muse-publishing-current-style))
1996 (and (not exactp) (muse-style-derived-p style)))
1997 (let* ((function (cdr (assoc "function" attrs)))
1998 (muse-publish-use-header-footer-tags nil)
1999 (muse-publishing-directives muse-publishing-directives)
2000 (markup-function (and function (intern function))))
2001 (if (and markup-function (functionp markup-function))
2002 (save-restriction
2003 (narrow-to-region beg end)
2004 (funcall markup-function)
2005 (goto-char (point-max)))
2006 (let ((muse-publish-inhibit-style-hooks t))
2007 (muse-publish-markup-region beg end)))
2008 (muse-publish-mark-read-only beg (point)))
2009 (delete-region beg end))))
2011 ;; Miscellaneous helper functions
2013 (defun muse-publish-strip-URL (string &rest ignored)
2014 "If the text \"URL:\" exists at the beginning of STRING, remove it.
2015 The text is removed regardless of whether and part of it is uppercase."
2016 (save-match-data
2017 (if (string-match "\\`[uU][rR][lL]:\\(.+\\)\\'" string)
2018 (match-string 1 string)
2019 string)))
2021 (defun muse-publish-strip-tags (string)
2022 "Remove all tags from the string."
2023 (while (string-match "<.*?>" string)
2024 (setq string (replace-match "" nil t string)))
2025 string)
2027 (defun muse-publish-markup-type (category default-func)
2028 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
2029 (funcall (or rule default-func))))
2031 (defun muse-published-buffer-contents (buffer)
2032 (with-current-buffer buffer
2033 (goto-char (point-min))
2034 (let ((beg (and (search-forward "Emacs Muse begins here")
2035 (muse-line-end-position)))
2036 (end (and (search-forward "Emacs Muse ends here")
2037 (muse-line-beginning-position))))
2038 (buffer-substring-no-properties beg end))))
2040 (defun muse-published-contents (file)
2041 (when (file-readable-p file)
2042 (muse-with-temp-buffer
2043 (insert-file-contents file)
2044 (muse-published-buffer-contents (current-buffer)))))
2046 (defun muse-publish-transform-output
2047 (file temp-file output-path name gen-func &rest cleanup-exts)
2048 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
2049 (setq file (muse-page-name file))
2050 (message "Generating %s output for %s..." name file)
2051 (if (not (funcall gen-func temp-file output-path))
2052 (message "Generating %s from %s...failed" name file)
2053 (message "Generating %s output for %s...done" name file)
2054 (muse-delete-file-if-exists temp-file)
2055 (dolist (ext cleanup-exts)
2056 (muse-delete-file-if-exists
2057 (expand-file-name (concat file ext)
2058 (file-name-directory output-path))))
2059 (message "Wrote %s" output-path)))
2061 (defun muse-publish-read-only (string)
2062 (let ((end (1- (length string))))
2063 (add-text-properties 0 end
2064 '(rear-nonsticky (read-only) read-only t)
2065 string)
2066 string))
2068 ;;; muse-publish.el ends here