Release Emacs Muse 3.03!
[muse-el.git] / lisp / muse-publish.el
blob8d0819bcdd3f9dd75307834e876c1f3bf2b37afd
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 "A list of tag specifications, for specially marking up text.
281 XML-style tags are the best way to add custom markup to Muse.
282 This is easily accomplished by customizing this list of markup tags.
284 For each entry, the name of the tag is given, whether it expects
285 a closing tag and/or an optional set of attributes, whether it is
286 nestable, and a function that performs whatever action is desired
287 within the delimited region.
289 The tags themselves are deleted during publishing, before the
290 function is called. The function is called with three arguments,
291 the beginning and end of the region surrounded by the tags. If
292 properties are allowed, they are passed as a third argument in
293 the form of an alist. The `end' argument to the function is
294 always a marker.
296 Point is always at the beginning of the region within the tags, when
297 the function is called. Wherever point is when the function finishes
298 is where tag markup will resume.
300 These tag rules are processed once at the beginning of markup, and
301 once at the end, to catch any tags which may have been inserted
302 in-between."
303 :type '(repeat (list (string :tag "Markup tag")
304 (boolean :tag "Expect closing tag" :value t)
305 (boolean :tag "Parse attributes" :value nil)
306 (boolean :tag "Nestable" :value nil)
307 function))
308 :group 'muse-publish)
310 (defcustom muse-publish-markup-specials nil
311 "A table of characters which must be represented specially."
312 :type '(alist :key-type character :value-type string)
313 :group 'muse-publish)
315 (defvar muse-publishing-p nil
316 "Set to t while a page is being published.")
317 (defvar muse-batch-publishing-p nil
318 "Set to t while a page is being batch published.")
319 (defvar muse-publishing-styles nil
320 "The publishing styles that Muse recognizes.
321 This is automatically generated when loading publishing styles.")
322 (defvar muse-publishing-current-file nil
323 "The file that is currently being published.")
324 (defvar muse-publishing-current-output-path nil
325 "The path where the current file will be published to.")
326 (defvar muse-publishing-current-style nil
327 "The style of the file that is currently being published.")
328 (defvar muse-publishing-directives nil
329 "An alist of publishing directives from the top of a file.")
330 (defvar muse-publish-generate-contents nil
331 "Non-nil if a table of contents should be generated.
332 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
333 tell where the <contents> was seen, and to what depth the
334 contents were requested.")
335 (defvar muse-publishing-last-position nil
336 "Last position of the point when publishing.
337 This is used to make sure that publishing doesn't get stalled.")
339 ;; Functions for handling style information
341 (defsubst muse-style (&optional style)
342 "Resolve the given STYLE into a Muse style, if it is a string."
343 (if (null style)
344 muse-publishing-current-style
345 (if (stringp style)
346 (assoc style muse-publishing-styles)
347 (muse-assert (consp style))
348 style)))
350 (defun muse-define-style (name &rest elements)
351 (let ((entry (assoc name muse-publishing-styles)))
352 (if entry
353 (setcdr entry elements)
354 (setq muse-publishing-styles
355 (cons (append (list name) elements)
356 muse-publishing-styles)))))
358 (defun muse-derive-style (new-name base-name &rest elements)
359 (apply 'muse-define-style new-name
360 (append elements (list :base base-name))))
362 (defsubst muse-get-keyword (keyword list &optional direct)
363 (let ((value (cadr (memq keyword list))))
364 (if (and (not direct) (symbolp value))
365 (symbol-value value)
366 value)))
368 (defun muse-style-elements-list (elem &optional style)
369 "Return a list all references to ELEM in STYLE, including base styles.
370 If STYLE is not specified, use current style."
371 (let (base elements)
372 (while style
373 (setq style (muse-style style))
374 (setq elements (append elements
375 (muse-get-keyword elem style)))
376 (setq style (muse-get-keyword :base style)))
377 elements))
379 (defun muse-style-element (elem &optional style direct)
380 "Search for ELEM in STYLE, including base styles.
381 If STYLE is not specified, use current style."
382 (setq style (muse-style style))
383 (let ((value (muse-get-keyword elem style direct)))
384 (if value
385 value
386 (let ((base (muse-get-keyword :base style)))
387 (if base
388 (muse-style-element elem base direct))))))
390 (defun muse-style-derived-p-1 (base style)
391 "Internal function used by `muse-style-derived-p'."
392 (if (and (stringp style)
393 (string= style base))
395 (setq style (muse-style style))
396 (let ((value (muse-get-keyword :base style)))
397 (when value
398 (muse-style-derived-p base value)))))
400 (defun muse-style-derived-p (base &optional style)
401 "Return non-nil if STYLE is equal to or derived from BASE,
402 non-nil otherwise.
404 BASE should be a string."
405 (unless style
406 (setq style (muse-style)))
407 (when (and (consp style)
408 (stringp (car style)))
409 (setq style (car style)))
410 (muse-style-derived-p-1 base style))
412 (defun muse-find-markup-element (keyword ident style)
413 (let ((def (assq ident (muse-style-element keyword style))))
414 (if def
415 (cdr def)
416 (let ((base (muse-style-element :base style)))
417 (if base
418 (muse-find-markup-element keyword ident base))))))
420 (defun muse-markup-text (ident &rest args)
421 "Insert ARGS into the text markup associated with IDENT.
422 If the markup text has sections like %N%, this will be replaced
423 with the N-1th argument in ARGS. After that, `format' is applied
424 to the text with ARGS as parameters."
425 (let ((text (muse-find-markup-element :strings ident (muse-style))))
426 (if (and text args)
427 (progn
428 (let (start repl-text)
429 (while (setq start (string-match "%\\([1-9][0-9]*\\)%" text start))
430 ;; escape '%' in the argument text, since we will be
431 ;; using format on it
432 (setq repl-text (muse-replace-regexp-in-string
433 "%" "%%"
434 (nth (1- (string-to-number
435 (match-string 1 text))) args)
436 t t)
437 start (+ start (length repl-text))
438 text (replace-match repl-text t t text))))
439 (apply 'format text args))
440 (or text ""))))
442 (defun muse-insert-markup (&rest args)
443 (let ((beg (point)))
444 (apply 'insert args)
445 (muse-publish-mark-read-only beg (point))))
447 (defun muse-find-markup-tag (keyword tagname style)
448 (let ((def (assoc tagname (muse-style-element keyword style))))
449 (or def
450 (let ((base (muse-style-element :base style)))
451 (if base
452 (muse-find-markup-tag keyword tagname base))))))
454 (defsubst muse-markup-tag-info (tagname &rest args)
455 (let ((tag-info (muse-find-markup-tag :tags tagname (muse-style))))
456 (or tag-info
457 (assoc tagname muse-publish-markup-tags))))
459 (defsubst muse-markup-function (category)
460 (let ((func (muse-find-markup-element :functions category (muse-style))))
461 (or func
462 (cdr (assq category muse-publish-markup-functions)))))
464 ;; Publishing routines
466 (defun muse-publish-markup (name rules)
467 (let* ((case-fold-search nil)
468 (inhibit-read-only t)
469 (limit (* (length rules) (point-max)))
470 (verbose (and muse-publish-report-threshhold
471 (> (point-max) muse-publish-report-threshhold)))
472 (base 0))
473 (while rules
474 (goto-char (point-min))
475 (let ((regexp (nth 1 (car rules)))
476 (group (nth 2 (car rules)))
477 (repl (nth 3 (car rules)))
478 pos)
479 (setq muse-publishing-last-position nil)
480 (if (symbolp regexp)
481 (setq regexp (symbol-value regexp)))
482 (if (and verbose (not muse-batch-publishing-p))
483 (message "Publishing %s...%d%%" name
484 (* (/ (float (+ (point) base)) limit) 100)))
485 (while (and regexp (setq pos (re-search-forward regexp nil t)))
486 (if (and verbose (not muse-batch-publishing-p))
487 (message "Publishing %s...%d%%" name
488 (* (/ (float (+ (point) base)) limit) 100)))
489 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
490 (match-beginning group)
491 (get-text-property (match-beginning group) 'read-only))
492 (let* (func
493 (text (cond
494 ((and (symbolp repl)
495 (setq func (muse-markup-function repl)))
496 (funcall func))
497 ((functionp repl)
498 (funcall repl))
499 ((symbolp repl)
500 (symbol-value repl))
501 (t repl))))
502 (if (stringp text)
503 (replace-match text t))))
504 (if (and muse-publishing-last-position
505 (= pos muse-publishing-last-position))
506 (if (eobp)
507 (setq regexp nil)
508 (forward-char 1)))
509 (setq muse-publishing-last-position pos)))
510 (setq rules (cdr rules)
511 base (+ base (point-max))))
512 (if (and verbose (not muse-batch-publishing-p))
513 (message "Publishing %s...done" name))))
515 (defcustom muse-publish-markup-header-footer-tags
516 '(("lisp" t t nil muse-publish-lisp-tag)
517 ("markup" t t nil muse-publish-mark-up-tag))
518 "Tags used when publishing headers and footers.
519 See `muse-publish-markup-tags' for details."
520 :type '(repeat (list (string :tag "Markup tag")
521 (boolean :tag "Expect closing tag" :value t)
522 (boolean :tag "Parse attributes" :value nil)
523 (boolean :tag "Nestable" :value nil)
524 function))
525 :group 'muse-publish)
527 (defun muse-insert-file-or-string (file-or-string &optional title)
528 (let ((beg (point)) end)
529 (if (and (not (string-equal file-or-string ""))
530 (not (string-match "\n" file-or-string))
531 (file-readable-p file-or-string))
532 (setq end (+ beg (cadr (insert-file-contents file-or-string))))
533 (insert file-or-string)
534 (setq end (point)))
535 (save-restriction
536 (narrow-to-region beg end)
537 (remove-text-properties (point-min) (point-max)
538 '(read-only nil rear-nonsticky nil))
539 (goto-char (point-min))
540 (let ((muse-inhibit-style-tags t))
541 (muse-publish-markup (or title "")
542 '((100 muse-tag-regexp 0
543 muse-publish-markup-tag)))))))
545 (defun muse-style-run-hooks (keyword style &rest args)
546 (catch 'handled
547 (let ((cache nil))
548 (while (and style
549 (setq style (muse-style style)))
550 (let ((func (muse-style-element keyword style t)))
551 (when (and func
552 (not (member func cache)))
553 (setq cache (cons func cache))
554 (when (apply func args)
555 (throw 'handled t))))
556 (setq style (muse-style-element :base style))))))
558 (defvar muse-publish-inhibit-style-hooks nil
559 "If non-nil, do not call the :before or :before-end hooks when publishing.")
561 (defun muse-publish-markup-region (beg end &optional title style)
562 "Apply the given STYLE's markup rules to the given region.
563 TITLE is used when indicating the publishing progress; it may be nil.
565 The point is guaranteed to be at END if the routine terminates
566 normally."
567 (unless title (setq title ""))
568 (unless style
569 (or (setq style muse-publishing-current-style)
570 (error "Cannot find any publishing styles to use")))
571 (save-restriction
572 (narrow-to-region beg end)
573 (let ((muse-publish-generate-contents nil))
574 (unless muse-publish-inhibit-style-hooks
575 (muse-style-run-hooks :before style))
576 (muse-publish-markup
577 title
578 (sort (copy-alist (append muse-publish-markup-regexps
579 (muse-style-elements-list :regexps style)))
580 (function
581 (lambda (l r)
582 (< (car l) (car r))))))
583 (unless muse-publish-inhibit-style-hooks
584 (muse-style-run-hooks :before-end style))
585 (muse-publish-escape-specials (point-min) (point-max) nil 'document))
586 (goto-char (point-max))))
588 (defun muse-publish-markup-buffer (title style)
589 "Apply the given STYLE's markup rules to the current buffer."
590 (setq style (muse-style style))
591 (let ((style-header (muse-style-element :header style))
592 (style-footer (muse-style-element :footer style))
593 (muse-publishing-current-style style)
594 (muse-publishing-directives
595 (list (cons "title" title)
596 (cons "author" (user-full-name))
597 (cons "date" (format-time-string
598 muse-publish-date-format
599 (if muse-publishing-current-file
600 (nth 5 (file-attributes
601 muse-publishing-current-file))
602 (current-time))))))
603 (muse-publishing-p t)
604 (inhibit-read-only t))
605 (run-hooks 'muse-update-values-hook)
606 (run-hooks 'muse-before-publish-hook)
607 (muse-publish-markup-region (point-min) (point-max) title style)
608 (goto-char (point-min))
609 (when style-header
610 (muse-insert-file-or-string style-header title))
611 (goto-char (point-max))
612 (when style-footer
613 (muse-insert-file-or-string style-footer title))
614 (muse-style-run-hooks :after style)
615 (run-hooks 'muse-after-publish-hook)))
617 (defun muse-publish-markup-string (string &optional style)
618 "Markup STRING using the given STYLE's markup rules."
619 (setq style (muse-style style))
620 (muse-with-temp-buffer
621 (insert string)
622 (let ((muse-publishing-current-style style)
623 (muse-publishing-p t))
624 (muse-publish-markup "*string*" (muse-style-element :rules style)))
625 (buffer-string)))
627 ;; Commands for publishing files
629 (defun muse-publish-get-style (&optional styles)
630 (unless styles (setq styles muse-publishing-styles))
631 (if (= 1 (length styles))
632 (car styles)
633 (when (catch 'different
634 (let ((first (car (car styles))))
635 (dolist (style (cdr styles))
636 (unless (equal first (car style))
637 (throw 'different t)))))
638 (setq styles (muse-collect-alist
639 styles
640 (completing-read "Publish with style: " styles nil t))))
641 (if (or (= 1 (length styles))
642 (not (muse-get-keyword :path (car styles))))
643 (car styles)
644 (setq styles (mapcar (lambda (style)
645 (cons (muse-get-keyword :path style)
646 style))
647 styles))
648 (cdr (assoc (completing-read "Publish to directory: " styles nil t)
649 styles)))))
651 (defsubst muse-publish-get-output-dir (style)
652 (let ((default-directory (or (muse-style-element :path style)
653 default-directory)))
654 (muse-read-directory-name "Publish to directory: " nil default-directory)))
656 (defsubst muse-publish-get-info ()
657 (let ((style (muse-publish-get-style)))
658 (list style (muse-publish-get-output-dir style)
659 current-prefix-arg)))
661 (defsubst muse-publish-output-name (&optional file style)
662 (setq style (muse-style style))
663 (concat (muse-style-element :prefix style)
664 (muse-page-name file)
665 (muse-style-element :suffix style)))
667 (defsubst muse-publish-output-file (file &optional output-dir style)
668 (setq style (muse-style style))
669 (if output-dir
670 (expand-file-name (muse-publish-output-name file style) output-dir)
671 (concat (file-name-directory file)
672 (muse-publish-output-name file style))))
674 (defsubst muse-publish-link-name (&optional file style)
675 (setq style (muse-style style))
676 (concat (muse-style-element :prefix style)
677 (muse-page-name file)
678 (or (muse-style-element :link-suffix style)
679 (muse-style-element :suffix style))))
681 (defsubst muse-publish-link-file (file &optional style)
682 (setq style (muse-style style))
683 (if (file-exists-p file)
684 file
685 (concat (file-name-directory file)
686 (muse-publish-link-name file style))))
688 (defsubst muse-publish-link-page (page)
689 (if (fboundp 'muse-project-link-page)
690 (muse-project-link-page page)
691 (muse-publish-link-file page)))
693 (defmacro muse-publish-ensure-block (beg)
694 "Ensure that block-level markup at BEG is published with at least one
695 preceding blank line. BEG is modified to be the new position.
696 The point is left at the new value of BEG."
697 `(progn
698 (goto-char ,beg)
699 (cond ((not (bolp)) (insert "\n\n"))
700 ((eq (point) (point-min)) nil)
701 ((prog2 (backward-char) (bolp) (forward-char)) nil)
702 (t (insert "\n")))
703 (setq ,beg (point))))
705 ;;;###autoload
706 (defun muse-publish-region (beg end &optional title style)
707 "Apply the given STYLE's markup rules to the given region.
708 The result is placed in a new buffer that includes TITLE in its name."
709 (interactive "r")
710 (when (interactive-p)
711 (unless title (setq title (read-string "Title: ")))
712 (unless style (setq style (muse-publish-get-style))))
713 (let ((muse-publishing-current-style style)
714 (muse-publishing-p t)
715 (text (buffer-substring beg end))
716 (buf (generate-new-buffer (concat "*Muse: " title "*"))))
717 (with-current-buffer buf
718 (insert text)
719 (muse-publish-markup-buffer title style)
720 (goto-char (point-min))
721 (let ((inhibit-read-only t))
722 (remove-text-properties (point-min) (point-max)
723 '(rear-nonsticky nil read-only nil))))
724 (pop-to-buffer buf)))
726 ;;;###autoload
727 (defun muse-publish-file (file style &optional output-dir force)
728 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
729 If the argument FORCE is nil, each file is only published if it is
730 newer than the published version. If the argument FORCE is non-nil,
731 the file is published no matter what."
732 (interactive (cons (read-file-name "Publish file: ")
733 (muse-publish-get-info)))
734 (let ((style-name style))
735 (setq style (muse-style style))
736 (unless style
737 (error "There is no style '%s' defined" style-name)))
738 (let* ((output-path (muse-publish-output-file file output-dir style))
739 (output-suffix (muse-style-element :osuffix style))
740 (muse-publishing-current-file file)
741 (muse-publishing-current-output-path output-path)
742 (target (if output-suffix
743 (concat (muse-path-sans-extension output-path)
744 output-suffix)
745 output-path))
746 (threshhold (nth 7 (file-attributes file))))
747 (if (not threshhold)
748 (message "Please save %s before publishing" file)
749 (when (or force (file-newer-than-file-p file target))
750 (if (and muse-publish-report-threshhold
751 (> threshhold
752 muse-publish-report-threshhold))
753 (message "Publishing %s ..." file))
754 (muse-with-temp-buffer
755 (insert-file-contents file)
756 (muse-publish-markup-buffer (muse-page-name file) style)
757 (let ((backup-inhibited t))
758 (write-file output-path))
759 (muse-style-run-hooks :final style file output-path target))
760 t))))
762 ;;;###autoload
763 (defun muse-publish-this-file (style output-dir &optional force)
764 "Publish the currently-visited file.
765 Prompt for both the STYLE and OUTPUT-DIR if they are not
766 supplied."
767 (interactive (muse-publish-get-info))
768 (if buffer-file-name
769 (let ((muse-current-output-style (list :base (car style)
770 :path output-dir)))
771 (unless (muse-publish-file buffer-file-name style output-dir force)
772 (message (concat "The published version is up-to-date; use"
773 " C-u C-c C-T to force an update."))))
774 (message "This buffer is not associated with any file")))
776 (defun muse-batch-publish-files ()
777 "Publish Muse files in batch mode."
778 (let ((muse-batch-publishing-p t)
779 style output-dir)
780 (setq style (car command-line-args-left)
781 command-line-args-left (cdr command-line-args-left)
782 output-dir (car command-line-args-left)
783 output-dir
784 (if (string-match "\\`--output-dir=" output-dir)
785 (prog1
786 (substring output-dir (match-end 0))
787 (setq command-line-args-left (cdr command-line-args-left)))))
788 (setq auto-mode-alist
789 (delete (cons (concat "\\." muse-file-extension "\\'")
790 'muse-mode-choose-mode)
791 auto-mode-alist))
792 (dolist (file command-line-args-left)
793 (muse-publish-file file style output-dir t))))
795 ;; Default publishing rules
797 (defun muse-publish-section-close (depth)
798 "Seach forward for the closing tag of given DEPTH."
799 (let (not-end)
800 (save-excursion
801 (while (and (setq not-end (re-search-forward
802 (concat "^\\*\\{1," (number-to-string depth)
803 "\\}\\s-+")
804 nil t))
805 (get-text-property (match-beginning 0) 'read-only)))
806 (if not-end
807 (forward-line 0)
808 (goto-char (point-max)))
809 (cond ((not (eq (char-before) ?\n))
810 (insert "\n\n"))
811 ((not (eq (char-before (1- (point))) ?\n))
812 (insert "\n")))
813 (muse-insert-markup (muse-markup-text 'section-close depth))
814 (insert "\n"))))
816 (defun muse-publish-markup-directive (&optional name value)
817 (unless name (setq name (match-string 1)))
818 (unless value (setq value (match-string 2)))
819 (let ((elem (assoc name muse-publishing-directives)))
820 (if elem
821 (setcdr elem value)
822 (setq muse-publishing-directives
823 (cons (cons name value)
824 muse-publishing-directives))))
825 ;; Make sure we don't ever try to move the point forward (past the
826 ;; beginning of buffer) while we're still searching for directives.
827 (setq muse-publishing-last-position nil)
828 (delete-region (match-beginning 0) (match-end 0)))
830 (defsubst muse-publishing-directive (name)
831 (cdr (assoc name muse-publishing-directives)))
833 (defun muse-publish-markup-anchor ()
834 (unless (get-text-property (match-end 1) 'muse-link)
835 (let ((text (muse-markup-text 'anchor (match-string 2))))
836 (unless (string= text "")
837 (save-match-data
838 (skip-chars-forward (concat muse-regexp-blank "\n"))
839 (muse-insert-markup text)))
840 (match-string 1))))
842 (defun muse-publish-markup-comment ()
843 (if (null muse-publish-comments-p)
845 (goto-char (match-end 0))
846 (muse-insert-markup (muse-markup-text 'comment-end))
847 (if (match-beginning 1)
848 (progn
849 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
850 (delete-region (match-beginning 0) (match-beginning 1)))
851 (delete-region (match-beginning 0) (match-end 0)))
852 (goto-char (match-beginning 0))
853 (muse-insert-markup (muse-markup-text 'comment-begin))))
855 (defvar muse-inhibit-style-tags nil
856 "If non-nil, do not search for style-specific tags.
857 This is used when publishing headers and footers.")
859 (defun muse-publish-markup-tag ()
860 (let ((tag-info (if muse-inhibit-style-tags
861 (assoc (match-string 1) muse-publish-markup-tags)
862 (muse-markup-tag-info (match-string 1)))))
863 (when (and tag-info
864 (not (get-text-property (match-beginning 0) 'read-only)))
865 (let ((closed-tag (match-string 3))
866 (start (match-beginning 0))
867 (beg (point))
868 end attrs)
869 (when (nth 2 tag-info)
870 (let ((attrstr (match-string 2)))
871 (while (and attrstr
872 (string-match (concat "\\([^"
873 muse-regexp-blank
874 "=\n]+\\)\\(=\"\\"
875 "([^\"]+\\)\"\\)?")
876 attrstr))
877 (let ((attr (cons (downcase
878 (muse-match-string-no-properties 1 attrstr))
879 (muse-match-string-no-properties 3 attrstr))))
880 (setq attrstr (replace-match "" t t attrstr))
881 (if attrs
882 (nconc attrs (list attr))
883 (setq attrs (list attr)))))))
884 (if (and (cadr tag-info) (not closed-tag))
885 (if (muse-goto-tag-end (car tag-info) (nth 3 tag-info))
886 (delete-region (match-beginning 0) (point))
887 (setq tag-info nil)))
888 (when tag-info
889 (setq end (point-marker))
890 (delete-region start beg)
891 (goto-char start)
892 (let ((args (list start end)))
893 (if (nth 2 tag-info)
894 (nconc args (list attrs)))
895 (let ((muse-inhibit-style-tags nil))
896 ;; remove the inhibition
897 (apply (nth 4 tag-info) args)))))))
898 nil)
900 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
901 "Escape specials from BEG to END using style-specific :specials.
902 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
903 CONTEXT is used to figure out what kind of specials to escape.
905 The following contexts exist in Muse.
906 'underline _underlined text_
907 'literal =monospaced text= or <code> region (monospaced, escaped)
908 'emphasis *emphasized text*
909 'email email@example.com
910 'url http://example.com
911 'url-desc [[...][description of an explicit link]]
912 'image [[image.png]]
913 'example <example> region (monospaced, block context, escaped)
914 'verbatim <verbatim> region (escaped)
915 'document normal text"
916 (let ((specials (muse-style-element :specials nil t)))
917 (cond ((functionp specials)
918 (setq specials (funcall specials context)))
919 ((symbolp specials)
920 (setq specials (symbol-value specials))))
921 (if (functionp specials)
922 (funcall specials beg end ignore-read-only)
923 (save-excursion
924 (save-restriction
925 (narrow-to-region beg end)
926 (goto-char (point-min))
927 (while (< (point) (point-max))
928 (if (and (not ignore-read-only)
929 (get-text-property (point) 'read-only))
930 (goto-char (or (next-single-property-change (point) 'read-only)
931 (point-max)))
932 (let ((repl (or (assoc (char-after) specials)
933 (assoc (char-after)
934 muse-publish-markup-specials))))
935 (if (null repl)
936 (forward-char 1)
937 (delete-char 1)
938 (insert-before-markers (cdr repl)))))))))))
940 (defun muse-publish-markup-word ()
941 (let* ((beg (match-beginning 2))
942 (end (1- (match-end 2)))
943 (leader (buffer-substring-no-properties beg end))
944 open-tag close-tag mark-read-only loc context)
945 (cond
946 ((string= leader "_")
947 (setq context 'underline
948 open-tag (muse-markup-text 'begin-underline)
949 close-tag (muse-markup-text 'end-underline)))
950 ((string= leader "=")
951 (setq context 'literal
952 open-tag (muse-markup-text 'begin-literal)
953 close-tag (muse-markup-text 'end-literal))
954 (setq mark-read-only t))
956 (let ((l (length leader)))
957 (setq context 'emphasis)
958 (cond
959 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
960 close-tag (muse-markup-text 'end-emph)))
961 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
962 close-tag (muse-markup-text 'end-more-emph)))
963 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
964 close-tag (muse-markup-text 'end-most-emph)))
965 (t (setq context nil))))))
966 (if (and context
967 (not (get-text-property beg 'muse-link))
968 (setq loc (search-forward leader nil t))
969 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
970 (not (eq (char-syntax (char-before (point))) ?\ ))
971 (not (get-text-property (point) 'muse-link)))
972 (progn
973 (replace-match "")
974 (delete-region beg end)
975 (setq end (point-marker))
976 (muse-insert-markup close-tag)
977 (goto-char beg)
978 (muse-insert-markup open-tag)
979 (setq beg (point))
980 (when mark-read-only
981 (muse-publish-escape-specials beg end t context)
982 (muse-publish-mark-read-only beg end)))
983 (backward-char))
984 nil))
986 (defun muse-publish-markup-emdash ()
987 (unless (get-text-property (match-beginning 0) 'muse-link)
988 (let ((prespace (match-string 1))
989 (postspace (match-string 2)))
990 (delete-region (match-beginning 0) (match-end 0))
991 (muse-insert-markup (muse-markup-text 'emdash prespace postspace))
992 (when (eq (char-after) ?\<)
993 (insert ?\n)))))
995 (defun muse-publish-markup-enddots ()
996 (unless (get-text-property (match-beginning 0) 'muse-link)
997 (delete-region (match-beginning 0) (match-end 0))
998 (muse-insert-markup (muse-markup-text 'enddots))))
1000 (defun muse-publish-markup-dots ()
1001 (unless (get-text-property (match-beginning 0) 'muse-link)
1002 (delete-region (match-beginning 0) (match-end 0))
1003 (muse-insert-markup (muse-markup-text 'dots))))
1005 (defun muse-publish-markup-rule ()
1006 (unless (get-text-property (match-beginning 0) 'muse-link)
1007 (delete-region (match-beginning 0) (match-end 0))
1008 (muse-insert-markup (muse-markup-text 'rule))))
1010 (defun muse-publish-markup-no-break-space ()
1011 (unless (get-text-property (match-beginning 0) 'muse-link)
1012 (delete-region (match-beginning 0) (match-end 0))
1013 (muse-insert-markup (muse-markup-text 'no-break-space))))
1015 (defun muse-publish-markup-heading ()
1016 (let* ((len (length (match-string 1)))
1017 (start (muse-markup-text
1018 (cond ((= len 1) 'section)
1019 ((= len 2) 'subsection)
1020 ((= len 3) 'subsubsection)
1021 (t 'section-other))
1022 len))
1023 (end (muse-markup-text
1024 (cond ((= len 1) 'section-end)
1025 ((= len 2) 'subsection-end)
1026 ((= len 3) 'subsubsection-end)
1027 (t 'section-other-end))
1028 len)))
1029 (delete-region (match-beginning 0) (match-end 0))
1030 (muse-insert-markup start)
1031 (end-of-line)
1032 (when end
1033 (muse-insert-markup end))
1034 (forward-line 1)
1035 (unless (eq (char-after) ?\n)
1036 (insert "\n"))
1037 (muse-publish-section-close len)))
1039 (defvar muse-publish-footnotes nil)
1041 (defun muse-publish-markup-footnote ()
1042 "Scan ahead and snarf up the footnote body"
1043 (cond
1044 ((get-text-property (match-beginning 0) 'muse-link)
1045 nil)
1046 ((= (muse-line-beginning-position) (match-beginning 0))
1049 (let ((footnote (save-match-data
1050 (string-to-number (match-string 1))))
1051 (oldtext (match-string 0))
1052 footnotemark)
1053 (delete-region (match-beginning 0) (match-end 0))
1054 (save-excursion
1055 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
1056 (let* ((start (match-beginning 0))
1057 (beg (goto-char (match-end 0)))
1058 (end (save-excursion
1059 (if (search-forward "\n\n" nil t)
1060 (copy-marker (match-beginning 0))
1061 (goto-char (point-max))
1062 (skip-chars-backward "\n")
1063 (point-marker)))))
1064 (while (re-search-forward
1065 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
1066 end t)
1067 (replace-match "\\1" t))
1068 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
1069 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
1070 (if (string= "" footnotemark-cmd)
1071 (setq footnotemark
1072 (concat (muse-markup-text 'footnote)
1073 (buffer-substring-no-properties beg end)
1074 (muse-markup-text 'footnote-end)))
1075 (setq footnotemark (format footnotemark-cmd footnote
1076 footnotemark-end-cmd))
1077 (unless muse-publish-footnotes
1078 (set (make-local-variable 'muse-publish-footnotes)
1079 (make-vector 256 nil)))
1080 (unless (aref muse-publish-footnotes footnote)
1081 (setq footnotemark
1082 (concat
1083 footnotemark
1084 (concat (format (muse-markup-text 'footnotetext)
1085 footnote)
1086 (buffer-substring-no-properties beg end)
1087 (muse-markup-text 'footnotetext-end))))
1088 (aset muse-publish-footnotes footnote footnotemark))))
1089 (goto-char end)
1090 (skip-chars-forward "\n")
1091 (delete-region start (point)))))
1092 (if footnotemark
1093 (muse-insert-markup footnotemark)
1094 (insert oldtext))))))
1096 (defun muse-publish-markup-fn-sep ()
1097 (delete-region (match-beginning 0) (match-end 0))
1098 (muse-insert-markup (muse-markup-text 'fn-sep)))
1100 (defun muse-insert-markup-end-list (&rest args)
1101 (let ((beg (point)))
1102 (apply 'insert args)
1103 (add-text-properties beg (point) '(end-list t))
1104 (muse-publish-mark-read-only beg (point))))
1106 (defun muse-publish-determine-dl-indent (continue indent-sym determine-sym)
1107 ;; If the caller doesn't know how much indentation to use, figure it
1108 ;; out ourselves. It is assumed that `muse-forward-list-item' has
1109 ;; been called just before this to set the match data.
1110 (when (and continue
1111 (symbol-value determine-sym))
1112 (save-match-data
1113 ;; snarf all leading whitespace
1114 (let ((indent (and (match-beginning 2)
1115 (buffer-substring (match-beginning 1)
1116 (match-beginning 2)))))
1117 (when (and indent
1118 (not (string= indent "")))
1119 (set indent-sym indent)
1120 (set determine-sym nil))))))
1122 (defun muse-publish-surround-dl (indent post-indent)
1123 (let* ((beg-item (muse-markup-text 'begin-dl-item))
1124 (end-item (muse-markup-text 'end-dl-item))
1125 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
1126 (end-ddt (muse-markup-text 'end-ddt))
1127 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
1128 (end-dde (muse-markup-text 'end-dde))
1129 (continue t)
1130 def-on-same-line beg)
1131 (while continue
1132 ;; envelope this as one term+definitions unit -- HTML does not
1133 ;; need this, but DocBook and Muse's custom XML format do
1134 (muse-insert-markup beg-item)
1135 (when (looking-at muse-dl-term-regexp)
1136 ;; find the term and wrap it with published markup
1137 (setq beg (point))
1138 (goto-char (match-end 1))
1139 (delete-region (point) (match-end 0))
1140 (muse-insert-markup-end-list end-ddt)
1141 ;; if definition is immediately after term, move to next line
1142 (unless (eq (char-after) ?\n)
1143 (insert ?\n))
1144 (save-excursion
1145 (goto-char beg)
1146 (delete-region (point) (match-beginning 1))
1147 (muse-insert-markup beg-ddt)))
1148 (setq beg (point)
1149 ;; move past current item
1150 continue (muse-forward-list-item 'dl-term indent))
1151 (save-restriction
1152 (narrow-to-region beg (point))
1153 (goto-char (point-min))
1154 ;; publish each definition that we find, defaulting to an
1155 ;; empty definition if none are found
1156 (muse-publish-surround-text beg-dde end-dde
1157 (lambda (indent)
1158 (muse-forward-list-item 'dl-entry indent))
1159 indent post-indent
1160 #'muse-publish-determine-dl-indent)
1161 (goto-char (point-max))
1162 (skip-chars-backward (concat muse-regexp-blank "\n"))
1163 (muse-insert-markup-end-list end-item)
1164 (when continue
1165 (goto-char (point-max)))))))
1167 (defun muse-publish-strip-list-indentation (list-item empty-line indent post-indent)
1168 (let ((list-nested nil)
1169 (indent-found nil))
1170 (while (< (point) (point-max))
1171 (when (and (looking-at list-item)
1172 (not (or (get-text-property
1173 (muse-list-item-critical-point) 'read-only)
1174 (get-text-property
1175 (muse-list-item-critical-point) 'muse-link))))
1176 ;; if we encounter a list item, allow no post-indent space
1177 (setq list-nested t))
1178 (when (and (not (looking-at empty-line))
1179 (looking-at (concat indent "\\("
1180 (or (and list-nested "")
1181 post-indent)
1182 "\\)")))
1183 ;; if list is not nested, remove indentation
1184 (unless indent-found
1185 (setq post-indent (match-string 1)
1186 indent-found t))
1187 (replace-match ""))
1188 (forward-line 1))))
1190 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent determine-indent-func list-item)
1191 (unless list-item
1192 (setq list-item (format muse-list-item-regexp
1193 (concat "[" muse-regexp-blank "]*"))))
1194 (let ((continue t)
1195 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
1196 (determine-indent (if determine-indent-func t nil))
1197 (new-indent indent)
1198 (first t)
1199 beg)
1200 (unless indent
1201 (setq indent (concat "[" muse-regexp-blank "]+")))
1202 (if post-indent
1203 (setq post-indent (concat " \\{0," (number-to-string post-indent)
1204 "\\}"))
1205 (setq post-indent ""))
1206 (while continue
1207 (if (or (not end-tag) (string= end-tag ""))
1208 ;; if no end of list item markup exists, treat the beginning
1209 ;; of list item markup as it if it were the end -- this
1210 ;; prevents multiple-level lists from being confused
1211 (muse-insert-markup-end-list beg-tag)
1212 (muse-insert-markup beg-tag))
1213 (setq beg (point)
1214 ;; move past current item; continue is non-nil if there
1215 ;; are more like items to be processed
1216 continue (if (and determine-indent-func first)
1217 (funcall move-func (concat indent post-indent))
1218 (funcall move-func indent)))
1219 (when determine-indent-func
1220 (funcall determine-indent-func continue 'new-indent 'determine-indent))
1221 (when continue
1222 ;; remove list markup if we encountered another item of the
1223 ;; same type
1224 (replace-match "" t t nil 1))
1225 (save-restriction
1226 (narrow-to-region beg (point))
1227 ;; narrow to current item
1228 (goto-char (point-min))
1229 (forward-line 1)
1230 (muse-publish-strip-list-indentation list-item empty-line
1231 indent post-indent)
1232 (skip-chars-backward (concat muse-regexp-blank "\n"))
1233 (muse-insert-markup-end-list end-tag)
1234 (when determine-indent-func
1235 (setq indent new-indent))
1236 (when first
1237 (setq first nil))
1238 (when continue
1239 (goto-char (point-max)))))))
1241 (defun muse-publish-markup-list ()
1242 "Markup a list entry.
1243 This function works by marking up items of the same list level
1244 and type, respecting the end-of-list property."
1245 (let* ((str (match-string 1))
1246 (type (muse-list-item-type str))
1247 (indent (buffer-substring (muse-line-beginning-position)
1248 (match-beginning 1)))
1249 (post-indent (length str)))
1250 (cond
1251 ((or (get-text-property (muse-list-item-critical-point) 'read-only)
1252 (get-text-property (muse-list-item-critical-point) 'muse-link))
1253 nil)
1254 ((eq type 'ul)
1255 (unless (eq (char-after (match-end 1)) ?-)
1256 (delete-region (match-beginning 0) (match-end 0))
1257 (let ((beg (point)))
1258 (muse-publish-ensure-block beg))
1259 (muse-insert-markup (muse-markup-text 'begin-uli))
1260 (save-excursion
1261 (muse-publish-surround-text
1262 (muse-markup-text 'begin-uli-item)
1263 (muse-markup-text 'end-uli-item)
1264 (lambda (indent)
1265 (muse-forward-list-item 'ul indent))
1266 indent post-indent)
1267 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1268 (forward-line 1)))
1269 ((eq type 'ol)
1270 (delete-region (match-beginning 0) (match-end 0))
1271 (let ((beg (point)))
1272 (muse-publish-ensure-block beg))
1273 (muse-insert-markup (muse-markup-text 'begin-oli))
1274 (save-excursion
1275 (muse-publish-surround-text
1276 (muse-markup-text 'begin-oli-item)
1277 (muse-markup-text 'end-oli-item)
1278 (lambda (indent)
1279 (muse-forward-list-item 'ol indent))
1280 indent post-indent)
1281 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1282 (forward-line 1))
1283 ((not (string= (match-string 2) ""))
1284 ;; must have an initial term
1285 (goto-char (match-beginning 0))
1286 (let ((beg (point)))
1287 (muse-publish-ensure-block beg))
1288 (muse-insert-markup (muse-markup-text 'begin-dl))
1289 (save-excursion
1290 (muse-publish-surround-dl indent post-indent)
1291 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1292 (forward-line 1))))
1293 nil)
1295 (defun muse-publish-markup-quote ()
1296 "Markup a quoted paragraph.
1297 The reason this function is so funky, is to prevent text properties
1298 like read-only from being inadvertently deleted."
1299 (let* ((ws (match-string 1))
1300 (centered (>= (string-width ws) 6))
1301 (begin-elem (if centered 'begin-center 'begin-quote-item))
1302 (end-elem (if centered 'end-center 'end-quote-item)))
1303 (replace-match "" t t nil 1)
1304 (unless centered
1305 (muse-insert-markup (muse-markup-text 'begin-quote)))
1306 (muse-publish-surround-text (muse-markup-text begin-elem)
1307 (muse-markup-text end-elem)
1308 (function (lambda (indent)
1309 (muse-forward-paragraph)
1310 nil)))
1311 (unless centered
1312 (muse-insert-markup (muse-markup-text 'end-quote)))))
1314 (defun muse-publish-markup-leading-space (markup-space multiple)
1315 (let (count)
1316 (when (and markup-space
1317 (>= (setq count (skip-chars-forward " ")) 0))
1318 (delete-region (muse-line-beginning-position) (point))
1319 (while (> count 0)
1320 (muse-insert-markup markup-space)
1321 (setq count (- count multiple))))))
1323 (defun muse-publish-markup-verse ()
1324 (let ((leader (match-string 0)))
1325 (goto-char (match-beginning 0))
1326 (muse-insert-markup (muse-markup-text 'begin-verse))
1327 (while (looking-at leader)
1328 (replace-match "")
1329 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1330 (let ((beg (point)))
1331 (end-of-line)
1332 (cond
1333 ((bolp)
1334 (let ((text (muse-markup-text 'empty-verse-line)))
1335 (when text (muse-insert-markup text))))
1336 ((save-excursion
1337 (save-match-data
1338 (forward-line 1)
1339 (or (looking-at (concat leader "["
1340 muse-regexp-blank
1341 "]*$"))
1342 (not (looking-at leader)))))
1343 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1344 (end-text (muse-markup-text 'end-last-stanza-line)))
1345 (when end-text (muse-insert-markup end-text))
1346 (goto-char beg)
1347 (when begin-text (muse-insert-markup begin-text))
1348 (end-of-line)))
1350 (let ((begin-text (muse-markup-text 'begin-verse-line))
1351 (end-text (muse-markup-text 'end-verse-line)))
1352 (when end-text (muse-insert-markup end-text))
1353 (goto-char beg)
1354 (when begin-text (muse-insert-markup begin-text))
1355 (end-of-line))))
1356 (forward-line 1))))
1357 (muse-insert-markup (muse-markup-text 'end-verse))
1358 (insert ?\n))
1360 (defun muse-publish-trim-table (table)
1361 "Remove completely blank columns from table, if at start or end of row."
1362 ;; remove first
1363 (catch 'found
1364 (dolist (row (cdr table))
1365 (let ((el (cadr row)))
1366 (when (and (stringp el) (not (string= el "")))
1367 (throw 'found t))))
1368 (dolist (row (cdr table))
1369 (setcdr row (cddr row)))
1370 (setcar table (1- (car table))))
1371 ;; remove last
1372 (catch 'found
1373 (dolist (row (cdr table))
1374 (let ((el (car (last row))))
1375 (when (and (stringp el) (not (string= el "")))
1376 (throw 'found t))))
1377 (dolist (row (cdr table))
1378 (setcdr (last row 2) nil))
1379 (setcar table (1- (car table))))
1380 table)
1382 (defun muse-publish-table-fields (beg end)
1383 "Parse given region as a table, returning a cons cell.
1384 The car is the length of the longest row.
1386 The cdr is a list of the fields of the table, with the first
1387 element indicating the type of the row:
1388 1: body, 2: header, 3: footer, hline: separator.
1390 The existing region will be removed, except for initial blank lines."
1391 (unless (muse-publishing-directive "disable-tables")
1392 (let ((longest 0)
1393 (left 0)
1394 (seen-hline nil)
1395 fields field-list)
1396 (save-restriction
1397 (narrow-to-region beg end)
1398 (goto-char (point-min))
1399 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1400 (forward-line 1))
1401 (setq beg (point))
1402 (while (= left 0)
1403 (cond
1404 ((looking-at muse-table-hline-regexp)
1405 (when field-list ; skip if at the beginning of table
1406 (if seen-hline
1407 (setq field-list (cons (cons 'hline nil) field-list))
1408 (dolist (field field-list)
1409 ;; the preceding fields are header lines
1410 (setcar field 2))
1411 (setq seen-hline t))))
1412 ((looking-at muse-table-line-regexp)
1413 (setq fields (cons (length (match-string 1))
1414 (mapcar #'muse-trim-whitespace
1415 (split-string (match-string 0)
1416 muse-table-field-regexp)))
1417 field-list (cons fields field-list)
1418 longest (max (length fields) longest))
1419 ;; strip initial bars, if they exist
1420 (let ((first (cadr fields)))
1421 (when (and first (string-match "\\`|+\\s-*" first))
1422 (setcar (cdr fields) (replace-match "" t t first))))))
1423 (setq left (forward-line 1))))
1424 (delete-region beg end)
1425 (if (= longest 0)
1426 (cons 0 nil)
1427 ;; if the last line was an hline, remove it
1428 (when (eq (caar field-list) 'hline)
1429 (setq field-list (cdr field-list)))
1430 (muse-publish-trim-table (cons (1- longest) (nreverse field-list)))))))
1432 (defun muse-publish-markup-table ()
1433 "Style does not support tables.\n")
1435 (defun muse-publish-table-el-table (variant)
1436 "Publish table.el-style tables in the format given by VARIANT."
1437 (when (condition-case nil
1438 (progn (require 'table)
1440 (error nil))
1441 (let ((muse-buf (current-buffer)))
1442 (save-restriction
1443 (narrow-to-region (match-beginning 0) (match-end 0))
1444 (goto-char (point-min))
1445 (forward-line 1)
1446 (search-forward "|" nil t)
1447 (with-temp-buffer
1448 (let ((temp-buf (current-buffer)))
1449 (with-current-buffer muse-buf
1450 (table-generate-source variant temp-buf))
1451 (with-current-buffer muse-buf
1452 (delete-region (point-min) (point-max))
1453 (insert-buffer-substring temp-buf)
1454 (muse-publish-mark-read-only (point-min) (point-max)))))))))
1456 (defun muse-publish-markup-table-el ()
1457 "Mark up table.el-style tables."
1458 (cond ((muse-style-derived-p 'html)
1459 (muse-publish-table-el-table 'html))
1460 ((muse-style-derived-p 'latex)
1461 (muse-publish-table-el-table 'latex))
1462 ((muse-style-derived-p 'docbook)
1463 (muse-publish-table-el-table 'cals))
1464 (t "Style does not support table.el tables.\n")))
1466 (defun muse-publish-escape-specials-in-string (string &optional context)
1467 "Escape specials in STRING using style-specific :specials.
1468 CONTEXT is used to figure out what kind of specials to escape.
1470 See the documentation of the `muse-publish-escape-specials'
1471 function for the list of available contexts."
1472 (unless string
1473 (setq string ""))
1474 (let ((specials (muse-style-element :specials nil t)))
1475 (cond ((functionp specials)
1476 (setq specials (funcall specials context)))
1477 ((symbolp specials)
1478 (setq specials (symbol-value specials))))
1479 (if (functionp specials)
1480 (funcall specials string)
1481 (apply (function concat)
1482 (mapcar
1483 (lambda (ch)
1484 (let ((repl (or (assoc ch specials)
1485 (assoc ch muse-publish-markup-specials))))
1486 (if (null repl)
1487 (char-to-string ch)
1488 (cdr repl))))
1489 (append string nil))))))
1491 (defun muse-publish-markup-email ()
1492 (let* ((beg (match-end 1))
1493 (addr (buffer-substring-no-properties beg (match-end 0))))
1494 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1495 (goto-char beg)
1496 (delete-region beg (match-end 0))
1497 (if (or (eq (char-before (match-beginning 0)) ?\")
1498 (eq (char-after (match-end 0)) ?\"))
1499 (insert addr)
1500 (insert (format (muse-markup-text 'email-addr) addr addr)))
1501 (muse-publish-mark-read-only beg (point))))
1503 (defun muse-publish-classify-url (target)
1504 "Transform anchors and get published name, if TARGET is a page.
1505 The return value is two linked cons cells. The car is the type
1506 of link, the cadr is the page name, and the cddr is the anchor."
1507 (save-match-data
1508 (cond ((or (null target) (string= target ""))
1509 nil)
1510 ((string-match "\\`[uU][rR][lL]:\\(.+\\)\\'" target)
1511 (cons 'url (cons (match-string 1 target) nil)))
1512 ((string-match muse-image-regexp target)
1513 (cons 'image (cons target nil)))
1514 ((string-match muse-url-regexp target)
1515 (cons 'url (cons target nil)))
1516 ((string-match muse-file-regexp target)
1517 (cons 'file (cons target nil)))
1518 ((string-match "#" target)
1519 (if (eq (aref target 0) ?\#)
1520 (cons 'anchor-ref (cons nil (substring target 1)))
1521 (cons 'link-and-anchor
1522 (cons (muse-publish-link-page
1523 (substring target 0 (match-beginning 0)))
1524 (substring target (match-end 0))))))
1526 (cons 'link (cons (muse-publish-link-page target) nil))))))
1528 (defun muse-publish-url-desc (desc explicit)
1529 (when desc
1530 (dolist (transform muse-publish-desc-transforms)
1531 (setq desc (save-match-data
1532 (when desc (funcall transform desc explicit)))))
1533 (setq desc (muse-link-unescape desc))
1534 (muse-publish-escape-specials-in-string desc 'url-desc)))
1536 (defun muse-publish-url (url &optional desc orig-url explicit)
1537 "Resolve a URL into its final <a href> form."
1538 (let (type anchor)
1539 (dolist (transform muse-publish-url-transforms)
1540 (setq url (save-match-data (when url (funcall transform url explicit)))))
1541 (if desc
1542 (setq desc (muse-publish-url-desc desc explicit))
1543 (if orig-url
1544 (setq orig-url (muse-publish-url-desc orig-url explicit))))
1545 (let ((target (muse-publish-classify-url url)))
1546 (setq type (car target)
1547 url (if (eq type 'image)
1548 (muse-publish-escape-specials-in-string (cadr target)
1549 'image)
1550 (muse-publish-escape-specials-in-string (cadr target) 'url))
1551 anchor (muse-publish-escape-specials-in-string
1552 (cddr target) 'url)))
1553 (cond ((eq type 'anchor-ref)
1554 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1555 ((and desc (string-match muse-image-regexp desc))
1556 (let ((ext (or (file-name-extension desc) "")))
1557 (setq desc (muse-path-sans-extension desc))
1558 (muse-markup-text 'image-link url desc ext)))
1559 ((string= url "")
1560 desc)
1561 ((eq type 'image)
1562 (let ((ext (or (file-name-extension url) "")))
1563 (setq url (muse-path-sans-extension url))
1564 (if desc
1565 (muse-markup-text 'image-with-desc url ext desc)
1566 (muse-markup-text 'image url ext))))
1567 ((eq type 'link-and-anchor)
1568 (muse-markup-text 'link-and-anchor url anchor
1569 (or desc orig-url)
1570 (muse-path-sans-extension url)))
1571 ((eq type 'link)
1572 (muse-markup-text 'link url (or desc orig-url)))
1574 (or (and (or desc
1575 (not (string= url orig-url)))
1576 (let ((text (muse-markup-text 'url-and-desc url
1577 (or desc orig-url))))
1578 (and (not (string= text ""))
1579 text)))
1580 (muse-markup-text 'url url (or desc orig-url)))))))
1582 (defun muse-publish-insert-url (url &optional desc orig-url explicit)
1583 "Resolve a URL into its final <a href> form."
1584 (delete-region (match-beginning 0) (match-end 0))
1585 (let ((text (muse-publish-url url desc orig-url explicit)))
1586 (when text
1587 (muse-insert-markup text))))
1589 (defun muse-publish-markup-link ()
1590 (let (desc explicit orig-link link)
1591 (setq explicit (save-match-data
1592 (if (string-match muse-explicit-link-regexp
1593 (match-string 0))
1594 t nil)))
1595 (setq orig-link (if explicit (match-string 1) (match-string 0)))
1596 (setq desc (when explicit (match-string 2)))
1597 (setq link (if explicit
1598 (muse-handle-explicit-link orig-link)
1599 (muse-handle-implicit-link orig-link)))
1600 (when (and link
1601 (or explicit
1602 (not (or (eq (char-before (match-beginning 0)) ?\")
1603 (eq (char-after (match-end 0)) ?\")))))
1604 ;; if explicit link has no user-provided description, treat it
1605 ;; as if it were an implicit link
1606 (when (and explicit (not desc))
1607 (setq explicit nil))
1608 (muse-publish-insert-url link desc orig-link explicit))))
1610 (defun muse-publish-markup-url ()
1611 (unless (or (eq (char-before (match-beginning 0)) ?\")
1612 (eq (char-after (match-end 0)) ?\"))
1613 (let ((url (match-string 0)))
1614 (muse-publish-insert-url url nil url))))
1616 ;; Default publishing tags
1618 (defcustom muse-publish-contents-depth 2
1619 "The number of heading levels to include with <contents> tags."
1620 :type 'integer
1621 :group 'muse-publish)
1623 (defun muse-publish-contents-tag (beg end attrs)
1624 (set (make-local-variable 'muse-publish-generate-contents)
1625 (cons (copy-marker (point) t)
1626 (let ((depth (cdr (assoc "depth" attrs))))
1627 (or (and depth (string-to-number depth))
1628 muse-publish-contents-depth)))))
1630 (defun muse-publish-verse-tag (beg end)
1631 (muse-publish-ensure-block beg)
1632 (save-excursion
1633 (save-restriction
1634 (narrow-to-region beg end)
1635 (goto-char (point-min))
1636 (while (eq ?\ (char-syntax (char-after)))
1637 (delete-char 1))
1638 (while (< (point) (point-max))
1639 (insert "> ")
1640 (forward-line))
1641 (if (eq ?\ (char-syntax (char-before)))
1642 (delete-char -1)))))
1644 (defun muse-publish-mark-read-only (beg end)
1645 "Add read-only properties to the given region."
1646 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1647 nil)
1649 (defun muse-publish-mark-link (&optional beg end)
1650 "Indicate that the given region is a Muse link, so that other
1651 markup elements respect it. If a region is not specified, use
1652 the 0th match data to determine it.
1654 This is usually applied to explicit links."
1655 (unless beg (setq beg (match-beginning 0)))
1656 (unless end (setq end (match-end 0)))
1657 (add-text-properties beg end '(muse-link t))
1658 nil)
1660 (defun muse-publish-quote-tag (beg end)
1661 (muse-publish-ensure-block beg)
1662 (save-excursion
1663 (save-restriction
1664 (narrow-to-region beg end)
1665 (let ((quote-regexp "^\\(<\\(/?\\)quote>\\)"))
1666 (muse-insert-markup (muse-markup-text 'begin-quote))
1667 (while (progn
1668 (unless (looking-at (concat "[" muse-regexp-blank "\n]*"
1669 "<quote>"))
1670 (muse-publish-surround-text
1671 (muse-markup-text 'begin-quote-item)
1672 (muse-markup-text 'end-quote-item)
1673 (function
1674 (lambda (indent)
1675 (muse-forward-paragraph)
1676 (goto-char (match-end 0))
1677 (and (< (point) (point-max))
1678 (not (looking-at quote-regexp)))))
1679 nil nil nil
1680 quote-regexp))
1681 (if (>= (point) (point-max))
1683 (and (search-forward "<quote>" nil t)
1684 (muse-goto-tag-end "quote" t)
1685 (progn (forward-line 1) t)
1686 (< (point) (point-max))))))
1687 (goto-char (point-max))
1688 (muse-insert-markup (muse-markup-text 'end-quote))))))
1690 (defun muse-publish-code-tag (beg end)
1691 (muse-publish-escape-specials beg end nil 'literal)
1692 (goto-char beg)
1693 (insert (muse-markup-text 'begin-literal))
1694 (goto-char end)
1695 (insert (muse-markup-text 'end-literal))
1696 (muse-publish-mark-read-only beg (point)))
1698 (defun muse-publish-src-tag (beg end attrs)
1699 (muse-publish-example-tag beg end))
1701 (defun muse-publish-example-tag (beg end)
1702 (muse-publish-ensure-block beg)
1703 (muse-publish-escape-specials beg end nil 'example)
1704 (goto-char beg)
1705 (insert (muse-markup-text 'begin-example))
1706 (goto-char end)
1707 (insert (muse-markup-text 'end-example))
1708 (muse-publish-mark-read-only beg (point)))
1710 (defun muse-publish-literal-tag (beg end attrs)
1711 "Ensure that the text between BEG and END is not interpreted later on.
1713 ATTRS is an alist of attributes.
1715 If it contains a \"style\" element, delete the region if the
1716 current style is neither derived from nor equal to this style.
1718 If it contains both a \"style\" element and an \"exact\" element
1719 with the value \"t\", delete the region only if the current style
1720 is exactly this style."
1721 (let* ((style (cdr (assoc "style" attrs)))
1722 (exact (cdr (assoc "exact" attrs)))
1723 (exactp (and (stringp exact) (string= exact "t"))))
1724 (if (or (not style)
1725 (and exactp (equal (muse-style style)
1726 muse-publishing-current-style))
1727 (and (not exactp) (muse-style-derived-p style)))
1728 (muse-publish-mark-read-only beg end)
1729 (delete-region beg end)
1730 (when (and (bolp) (eolp) (not (eobp)))
1731 (delete-char 1)))))
1733 (defun muse-publish-verbatim-tag (beg end)
1734 (muse-publish-escape-specials beg end nil 'verbatim)
1735 (muse-publish-mark-read-only beg end))
1737 (defalias 'muse-publish-class-tag 'ignore)
1739 (defun muse-publish-call-tag-on-buffer (tag &optional attrs)
1740 "Transform the current buffer as if it were surrounded by the tag TAG.
1741 If attributes ATTRS are given, pass them to the tag function."
1742 (let ((tag-info (if muse-inhibit-style-tags
1743 (assoc tag muse-publish-markup-tags)
1744 (muse-markup-tag-info tag))))
1745 (when tag-info
1746 (let* ((end (progn (goto-char (point-max)) (point-marker)))
1747 (args (list (point-min) end))
1748 (muse-inhibit-style-tags nil))
1749 (when (nth 2 tag-info)
1750 (nconc args (list attrs)))
1751 (apply (nth 4 tag-info) args)))))
1753 (defun muse-publish-examplify-buffer (&optional attrs)
1754 "Transform the current buffer as if it were an <example> region."
1755 (muse-publish-call-tag-on-buffer "example" attrs))
1757 (defun muse-publish-srcify-buffer (&optional attrs)
1758 "Transform the current buffer as if it were a <src> region."
1759 (muse-publish-call-tag-on-buffer "src" attrs))
1761 (defun muse-publish-versify-buffer (&optional attrs)
1762 "Transform the current buffer as if it were a <verse> region."
1763 (muse-publish-call-tag-on-buffer "verse" attrs)
1764 (muse-publish-markup ""
1765 `((100 ,(concat "^[" muse-regexp-blank "]*> ") 0
1766 muse-publish-markup-verse)))
1767 (goto-char (point-min)))
1769 (defmacro muse-publish-get-and-delete-attr (attr attrs)
1770 "Delete attribute ATTR from ATTRS only once, destructively.
1772 This function returns the matching attribute value, if found."
1773 (let ((last (make-symbol "last"))
1774 (found (make-symbol "found"))
1775 (vals (make-symbol "vals")))
1776 `(let ((,vals ,attrs))
1777 (if (string= (caar ,vals) ,attr)
1778 (prog1 (cdar ,vals)
1779 (setq ,attrs (cdr ,vals)))
1780 (let ((,last ,vals)
1781 (,found nil))
1782 (while ,vals
1783 (setq ,vals (cdr ,vals))
1784 (when (string= (caar ,vals) ,attr)
1785 (setq ,found (cdar ,vals))
1786 (setcdr ,last (cdr ,vals))
1787 (setq ,vals nil))
1788 (setq ,last ,vals))
1789 ,found)))))
1791 (defmacro muse-publish-markup-attribute (beg end attrs reinterp &rest body)
1792 "Evaluate BODY within the bounds of BEG and END.
1793 ATTRS is an alist. Only the \"markup\" element of ATTRS is acted
1796 If it is omitted, publish the region with the normal Muse rules.
1797 If RE-INTERP is specified, this is done immediately in a new
1798 publishing process. Currently, RE-INTERP is specified only by
1799 the <include> tag.
1801 If \"nil\", do not mark up the region at all, but prevent it from
1802 being further interpreted by Muse.
1804 If \"example\", treat the region as if it was surrounded by the
1805 <example> tag.
1807 If \"src\", treat the region as if it was surrounded by the
1808 <src> tag.
1810 If \"verse\", treat the region as if it was surrounded by the
1811 <verse> tag, to preserve newlines.
1813 Otherwise, it should be the name of a function to call in the
1814 narrowed region after evaluating BODY. The function should
1815 take the ATTRS parameter.
1817 BEG is modified to be the start of the published markup."
1818 (let ((markup (make-symbol "markup"))
1819 (markup-function (make-symbol "markup-function")))
1820 `(let ((,markup (muse-publish-get-and-delete-attr "markup" ,attrs)))
1821 (save-restriction
1822 (narrow-to-region ,beg ,end)
1823 (goto-char (point-min))
1824 ,@body
1825 (if (not ,markup)
1826 (when ,reinterp
1827 (muse-publish-markup-region (point-min) (point-max))
1828 (muse-publish-mark-read-only (point-min) (point-max))
1829 (goto-char (point-max)))
1830 (let ((,markup-function (read ,markup)))
1831 (cond ((eq ,markup-function 'example)
1832 (setq ,markup-function #'muse-publish-examplify-buffer))
1833 ((eq ,markup-function 'src)
1834 (setq ,markup-function #'muse-publish-srcify-buffer))
1835 ((eq ,markup-function 'verse)
1836 (setq ,markup-function #'muse-publish-versify-buffer))
1837 ((and ,markup-function (not (functionp ,markup-function)))
1838 (error "Invalid markup function `%s'" ,markup))
1839 (t nil))
1840 (if ,markup-function
1841 (funcall ,markup-function ,attrs)
1842 (muse-publish-mark-read-only (point-min) (point-max))
1843 (goto-char (point-max)))))))))
1845 (put 'muse-publish-markup-attribute 'lisp-indent-function 4)
1846 (put 'muse-publish-markup-attribute 'edebug-form-spec
1847 '(form form form form body))
1849 (defun muse-publish-lisp-tag (beg end attrs)
1850 (muse-publish-markup-attribute beg end attrs nil
1851 (save-excursion
1852 (let ((str (muse-eval-lisp
1853 (prog1
1854 (concat "(progn "
1855 (buffer-substring-no-properties (point-min)
1856 (point-max))
1857 ")")
1858 (delete-region beg end)))))
1859 (set-text-properties 0 (length str) nil str)
1860 (insert str)))))
1862 (defun muse-publish-command-tag (beg end attrs)
1863 (muse-publish-markup-attribute beg end attrs nil
1864 (while (looking-at "\\s-*$")
1865 (forward-line))
1866 (let ((interp (muse-publish-get-and-delete-attr "interp" attrs)))
1867 (if interp
1868 (shell-command-on-region (point) (point-max) interp t t)
1869 (shell-command
1870 (prog1
1871 (buffer-substring-no-properties (point) (point-max))
1872 (delete-region (point-min) (point-max)))
1873 t)))
1874 ;; make sure there is a newline at end
1875 (goto-char (point-max))
1876 (forward-line 0)
1877 (unless (looking-at "\\s-*$")
1878 (goto-char (point-max))
1879 (insert ?\n))
1880 (goto-char (point-min))))
1882 (defun muse-publish-perl-tag (beg end attrs)
1883 (muse-publish-command-tag beg end
1884 (cons (cons "interp" (executable-find "perl"))
1885 attrs)))
1887 (defun muse-publish-python-tag (beg end attrs)
1888 (muse-publish-command-tag beg end
1889 (cons (cons "interp" (executable-find "python"))
1890 attrs)))
1892 (defun muse-publish-ruby-tag (beg end attrs)
1893 (muse-publish-command-tag beg end
1894 (cons (cons "interp" (executable-find "ruby"))
1895 attrs)))
1897 (defun muse-publish-comment-tag (beg end)
1898 (if (null muse-publish-comments-p)
1899 (delete-region beg end)
1900 (goto-char end)
1901 (muse-insert-markup (muse-markup-text 'comment-end))
1902 (muse-publish-mark-read-only beg end)
1903 (goto-char beg)
1904 (muse-insert-markup (muse-markup-text 'comment-begin))))
1906 (defun muse-publish-include-tag (beg end attrs)
1907 "Include the named file at the current location during publishing.
1909 <include file=\"...\" markup=\"...\">
1911 The `markup' attribute controls how this file is marked up after
1912 being inserted. See `muse-publish-markup-attribute' for an
1913 explanation of how it works."
1914 (let ((filename (muse-publish-get-and-delete-attr "file" attrs))
1915 (muse-publishing-directives muse-publishing-directives))
1916 (if filename
1917 (setq filename (expand-file-name
1918 filename
1919 (file-name-directory muse-publishing-current-file)))
1920 (error "No file attribute specified in <include> tag"))
1921 (muse-publish-markup-attribute beg end attrs t
1922 (insert-file-contents filename))))
1924 (defun muse-publish-mark-up-tag (beg end attrs)
1925 "Run an Emacs Lisp function on the region delimted by this tag.
1927 <markup function=\"...\" style=\"...\" exact=\"...\">
1929 The optional \"function\" attribute controls how this section is
1930 marked up. If used, it should be the name of a function to call
1931 with the buffer narrowed to the delimited region. Note that no
1932 further marking-up will be performed on this region.
1934 If \"function\" is omitted, use the standard Muse markup function.
1935 This is useful for marking up content in headers and footers.
1937 The optional \"style\" attribute causes the region to be deleted
1938 if the current style is neither derived from nor equal to this
1939 style.
1941 If both a \"style\" attribute and an \"exact\" attribute are
1942 provided, and \"exact\" is \"t\", delete the region only if the
1943 current style is exactly this style."
1944 (let* ((style (cdr (assoc "style" attrs)))
1945 (exact (cdr (assoc "exact" attrs)))
1946 (exactp (and (stringp exact) (string= exact "t"))))
1947 (if (or (not style)
1948 (and exactp (equal (muse-style style)
1949 muse-publishing-current-style))
1950 (and (not exactp) (muse-style-derived-p style)))
1951 (let* ((function (cdr (assoc "function" attrs)))
1952 (muse-publishing-directives muse-publishing-directives)
1953 (markup-function (and function (intern function))))
1954 (if (and markup-function (functionp markup-function))
1955 (save-restriction
1956 (narrow-to-region beg end)
1957 (funcall markup-function)
1958 (goto-char (point-max)))
1959 (let ((muse-publish-inhibit-style-hooks t))
1960 (muse-publish-markup-region beg end)))
1961 (muse-publish-mark-read-only beg (point)))
1962 (delete-region beg end))))
1964 ;; Miscellaneous helper functions
1966 (defun muse-publish-strip-URL (string &rest ignored)
1967 "If the text \"URL:\" exists at the beginning of STRING, remove it.
1968 The text is removed regardless of whether and part of it is uppercase."
1969 (save-match-data
1970 (if (string-match "\\`[uU][rR][lL]:\\(.+\\)\\'" string)
1971 (match-string 1 string)
1972 string)))
1974 (defun muse-publish-strip-tags (string)
1975 "Remove all tags from the string."
1976 (while (string-match "<.*?>" string)
1977 (setq string (replace-match "" nil t string)))
1978 string)
1980 (defun muse-publish-markup-type (category default-func)
1981 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1982 (funcall (or rule default-func))))
1984 (defun muse-published-buffer-contents (buffer)
1985 (with-current-buffer buffer
1986 (goto-char (point-min))
1987 (let ((beg (and (search-forward "Emacs Muse begins here")
1988 (muse-line-end-position)))
1989 (end (and (search-forward "Emacs Muse ends here")
1990 (muse-line-beginning-position))))
1991 (buffer-substring-no-properties beg end))))
1993 (defun muse-published-contents (file)
1994 (when (file-readable-p file)
1995 (muse-with-temp-buffer
1996 (insert-file-contents file)
1997 (muse-published-buffer-contents (current-buffer)))))
1999 (defun muse-publish-transform-output
2000 (file temp-file output-path name gen-func &rest cleanup-exts)
2001 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
2002 (setq file (muse-page-name file))
2003 (message "Generating %s output for %s..." name file)
2004 (if (not (funcall gen-func temp-file output-path))
2005 (message "Generating %s from %s...failed" name file)
2006 (message "Generating %s output for %s...done" name file)
2007 (muse-delete-file-if-exists temp-file)
2008 (dolist (ext cleanup-exts)
2009 (muse-delete-file-if-exists
2010 (expand-file-name (concat file ext)
2011 (file-name-directory output-path))))
2012 (message "Wrote %s" output-path)))
2014 (defun muse-publish-read-only (string)
2015 (let ((end (1- (length string))))
2016 (add-text-properties 0 end
2017 '(rear-nonsticky (read-only) read-only t)
2018 string)
2019 string))
2021 ;;; muse-publish.el ends here