Update copyright years
[muse-el.git] / lisp / muse-publish.el
blob9c78e6a06a2b51867f98c4dee0858bc278020c18
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 (last (match-beginning 0)))
1251 (cond
1252 ((or (get-text-property (muse-list-item-critical-point) 'read-only)
1253 (get-text-property (muse-list-item-critical-point) 'muse-link))
1254 nil)
1255 ((eq type 'ul)
1256 (unless (eq (char-after (match-end 1)) ?-)
1257 (delete-region (match-beginning 0) (match-end 0))
1258 (let ((beg (point)))
1259 (muse-publish-ensure-block beg))
1260 (muse-insert-markup (muse-markup-text 'begin-uli))
1261 (save-excursion
1262 (muse-publish-surround-text
1263 (muse-markup-text 'begin-uli-item)
1264 (muse-markup-text 'end-uli-item)
1265 (lambda (indent)
1266 (muse-forward-list-item 'ul indent))
1267 indent post-indent)
1268 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1269 (forward-line 1)))
1270 ((eq type 'ol)
1271 (delete-region (match-beginning 0) (match-end 0))
1272 (let ((beg (point)))
1273 (muse-publish-ensure-block beg))
1274 (muse-insert-markup (muse-markup-text 'begin-oli))
1275 (save-excursion
1276 (muse-publish-surround-text
1277 (muse-markup-text 'begin-oli-item)
1278 (muse-markup-text 'end-oli-item)
1279 (lambda (indent)
1280 (muse-forward-list-item 'ol indent))
1281 indent post-indent)
1282 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1283 (forward-line 1))
1284 ((not (string= (match-string 2) ""))
1285 ;; must have an initial term
1286 (goto-char (match-beginning 0))
1287 (let ((beg (point)))
1288 (muse-publish-ensure-block beg))
1289 (muse-insert-markup (muse-markup-text 'begin-dl))
1290 (save-excursion
1291 (muse-publish-surround-dl indent post-indent)
1292 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1293 (forward-line 1))))
1294 nil)
1296 (defun muse-publish-markup-quote ()
1297 "Markup a quoted paragraph.
1298 The reason this function is so funky, is to prevent text properties
1299 like read-only from being inadvertently deleted."
1300 (let* ((ws (match-string 1))
1301 (centered (>= (string-width ws) 6))
1302 (begin-elem (if centered 'begin-center 'begin-quote-item))
1303 (end-elem (if centered 'end-center 'end-quote-item)))
1304 (replace-match "" t t nil 1)
1305 (unless centered
1306 (muse-insert-markup (muse-markup-text 'begin-quote)))
1307 (muse-publish-surround-text (muse-markup-text begin-elem)
1308 (muse-markup-text end-elem)
1309 (function (lambda (indent)
1310 (muse-forward-paragraph)
1311 nil)))
1312 (unless centered
1313 (muse-insert-markup (muse-markup-text 'end-quote)))))
1315 (defun muse-publish-markup-leading-space (markup-space multiple)
1316 (let (count)
1317 (when (and markup-space
1318 (>= (setq count (skip-chars-forward " ")) 0))
1319 (delete-region (muse-line-beginning-position) (point))
1320 (while (> count 0)
1321 (muse-insert-markup markup-space)
1322 (setq count (- count multiple))))))
1324 (defun muse-publish-markup-verse ()
1325 (let ((leader (match-string 0)))
1326 (goto-char (match-beginning 0))
1327 (muse-insert-markup (muse-markup-text 'begin-verse))
1328 (while (looking-at leader)
1329 (replace-match "")
1330 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1331 (let ((beg (point)))
1332 (end-of-line)
1333 (cond
1334 ((bolp)
1335 (let ((text (muse-markup-text 'empty-verse-line)))
1336 (when text (muse-insert-markup text))))
1337 ((save-excursion
1338 (save-match-data
1339 (forward-line 1)
1340 (or (looking-at (concat leader "["
1341 muse-regexp-blank
1342 "]*$"))
1343 (not (looking-at leader)))))
1344 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1345 (end-text (muse-markup-text 'end-last-stanza-line)))
1346 (when end-text (muse-insert-markup end-text))
1347 (goto-char beg)
1348 (when begin-text (muse-insert-markup begin-text))
1349 (end-of-line)))
1351 (let ((begin-text (muse-markup-text 'begin-verse-line))
1352 (end-text (muse-markup-text 'end-verse-line)))
1353 (when end-text (muse-insert-markup end-text))
1354 (goto-char beg)
1355 (when begin-text (muse-insert-markup begin-text))
1356 (end-of-line))))
1357 (forward-line 1))))
1358 (muse-insert-markup (muse-markup-text 'end-verse))
1359 (insert ?\n))
1361 (defun muse-publish-trim-table (table)
1362 "Remove completely blank columns from table, if at start or end of row."
1363 ;; remove first
1364 (catch 'found
1365 (dolist (row (cdr table))
1366 (let ((el (cadr row)))
1367 (when (and (stringp el) (not (string= el "")))
1368 (throw 'found t))))
1369 (dolist (row (cdr table))
1370 (setcdr row (cddr row)))
1371 (setcar table (1- (car table))))
1372 ;; remove last
1373 (catch 'found
1374 (dolist (row (cdr table))
1375 (let ((el (car (last row))))
1376 (when (and (stringp el) (not (string= el "")))
1377 (throw 'found t))))
1378 (dolist (row (cdr table))
1379 (setcdr (last row 2) nil))
1380 (setcar table (1- (car table))))
1381 table)
1383 (defun muse-publish-table-fields (beg end)
1384 "Parse given region as a table, returning a cons cell.
1385 The car is the length of the longest row.
1387 The cdr is a list of the fields of the table, with the first
1388 element indicating the type of the row:
1389 1: body, 2: header, 3: footer, hline: separator.
1391 The existing region will be removed, except for initial blank lines."
1392 (unless (muse-publishing-directive "disable-tables")
1393 (let ((longest 0)
1394 (left 0)
1395 (seen-hline nil)
1396 fields field-list)
1397 (save-restriction
1398 (narrow-to-region beg end)
1399 (goto-char (point-min))
1400 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1401 (forward-line 1))
1402 (setq beg (point))
1403 (while (= left 0)
1404 (cond
1405 ((looking-at muse-table-hline-regexp)
1406 (when field-list ; skip if at the beginning of table
1407 (if seen-hline
1408 (setq field-list (cons (cons 'hline nil) field-list))
1409 (dolist (field field-list)
1410 ;; the preceding fields are header lines
1411 (setcar field 2))
1412 (setq seen-hline t))))
1413 ((looking-at muse-table-line-regexp)
1414 (setq fields (cons (length (match-string 1))
1415 (mapcar #'muse-trim-whitespace
1416 (split-string (match-string 0)
1417 muse-table-field-regexp)))
1418 field-list (cons fields field-list)
1419 longest (max (length fields) longest))))
1420 (setq left (forward-line 1))))
1421 (delete-region beg end)
1422 (if (= longest 0)
1423 (cons 0 nil)
1424 ;; if the last line was an hline, remove it
1425 (when (eq (caar field-list) 'hline)
1426 (setq field-list (cdr field-list)))
1427 (muse-publish-trim-table (cons (1- longest) (nreverse field-list)))))))
1429 (defun muse-publish-markup-table ()
1430 "Style does not support tables.")
1432 (defun muse-publish-table-el-table (variant)
1433 "Publish table.el-style tables in the format given by VARIANT."
1434 (when (condition-case nil
1435 (progn (require 'table)
1437 (error nil))
1438 (let ((muse-buf (current-buffer)))
1439 (save-restriction
1440 (narrow-to-region (match-beginning 0) (match-end 0))
1441 (goto-char (point-min))
1442 (forward-line 1)
1443 (search-forward "|" nil t)
1444 (with-temp-buffer
1445 (let ((temp-buf (current-buffer)))
1446 (with-current-buffer muse-buf
1447 (table-generate-source variant temp-buf))
1448 (with-current-buffer muse-buf
1449 (delete-region (point-min) (point-max))
1450 (insert-buffer-substring temp-buf)
1451 (muse-publish-mark-read-only (point-min) (point-max)))))))))
1453 (defun muse-publish-markup-table-el ()
1454 "Mark up table.el-style tables."
1455 (cond ((muse-style-derived-p 'html)
1456 (muse-publish-table-el-table 'html))
1457 ((muse-style-derived-p 'latex)
1458 (muse-publish-table-el-table 'latex))
1459 ((muse-style-derived-p 'docbook)
1460 (muse-publish-table-el-table 'cals))
1461 (t "Style does not support table.el tables.")))
1463 (defun muse-publish-escape-specials-in-string (string &optional context)
1464 "Escape specials in STRING using style-specific :specials.
1465 CONTEXT is used to figure out what kind of specials to escape.
1467 See the documentation of the `muse-publish-escape-specials'
1468 function for the list of available contexts."
1469 (unless string
1470 (setq string ""))
1471 (let ((specials (muse-style-element :specials nil t)))
1472 (cond ((functionp specials)
1473 (setq specials (funcall specials context)))
1474 ((symbolp specials)
1475 (setq specials (symbol-value specials))))
1476 (if (functionp specials)
1477 (funcall specials string)
1478 (apply (function concat)
1479 (mapcar
1480 (lambda (ch)
1481 (let ((repl (or (assoc ch specials)
1482 (assoc ch muse-publish-markup-specials))))
1483 (if (null repl)
1484 (char-to-string ch)
1485 (cdr repl))))
1486 (append string nil))))))
1488 (defun muse-publish-markup-email ()
1489 (let* ((beg (match-end 1))
1490 (addr (buffer-substring-no-properties beg (match-end 0))))
1491 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1492 (goto-char beg)
1493 (delete-region beg (match-end 0))
1494 (if (or (eq (char-before (match-beginning 0)) ?\")
1495 (eq (char-after (match-end 0)) ?\"))
1496 (insert addr)
1497 (insert (format (muse-markup-text 'email-addr) addr addr)))
1498 (muse-publish-mark-read-only beg (point))))
1500 (defun muse-publish-classify-url (target)
1501 "Transform anchors and get published name, if TARGET is a page.
1502 The return value is two linked cons cells. The car is the type
1503 of link, the cadr is the page name, and the cddr is the anchor."
1504 (save-match-data
1505 (cond ((or (null target) (string= target ""))
1506 nil)
1507 ((string-match "\\`[uU][rR][lL]:\\(.+\\)\\'" target)
1508 (cons 'url (cons (match-string 1 target) nil)))
1509 ((string-match muse-image-regexp target)
1510 (cons 'image (cons target nil)))
1511 ((string-match muse-url-regexp target)
1512 (cons 'url (cons target nil)))
1513 ((string-match muse-file-regexp target)
1514 (cons 'file (cons target nil)))
1515 ((string-match "#" target)
1516 (if (eq (aref target 0) ?\#)
1517 (cons 'anchor-ref (cons nil (substring target 1)))
1518 (cons 'link-and-anchor
1519 (cons (muse-publish-link-page
1520 (substring target 0 (match-beginning 0)))
1521 (substring target (match-end 0))))))
1523 (cons 'link (cons (muse-publish-link-page target) nil))))))
1525 (defun muse-publish-url-desc (desc explicit)
1526 (when desc
1527 (dolist (transform muse-publish-desc-transforms)
1528 (setq desc (save-match-data
1529 (when desc (funcall transform desc explicit)))))
1530 (setq desc (muse-link-unescape desc))
1531 (muse-publish-escape-specials-in-string desc 'url-desc)))
1533 (defun muse-publish-url (url &optional desc orig-url explicit)
1534 "Resolve a URL into its final <a href> form."
1535 (let (type anchor)
1536 (dolist (transform muse-publish-url-transforms)
1537 (setq url (save-match-data (when url (funcall transform url explicit)))))
1538 (if desc
1539 (setq desc (muse-publish-url-desc desc explicit))
1540 (if orig-url
1541 (setq orig-url (muse-publish-url-desc orig-url explicit))))
1542 (let ((target (muse-publish-classify-url url)))
1543 (setq type (car target)
1544 url (if (eq type 'image)
1545 (muse-publish-escape-specials-in-string (cadr target)
1546 'image)
1547 (muse-publish-escape-specials-in-string (cadr target) 'url))
1548 anchor (muse-publish-escape-specials-in-string
1549 (cddr target) 'url)))
1550 (cond ((eq type 'anchor-ref)
1551 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1552 ((and desc (string-match muse-image-regexp desc))
1553 (let ((ext (or (file-name-extension desc) "")))
1554 (setq desc (muse-path-sans-extension desc))
1555 (muse-markup-text 'image-link url desc ext)))
1556 ((string= url "")
1557 desc)
1558 ((eq type 'image)
1559 (let ((ext (or (file-name-extension url) "")))
1560 (setq url (muse-path-sans-extension url))
1561 (if desc
1562 (muse-markup-text 'image-with-desc url ext desc)
1563 (muse-markup-text 'image url ext))))
1564 ((eq type 'link-and-anchor)
1565 (muse-markup-text 'link-and-anchor url anchor
1566 (or desc orig-url)
1567 (muse-path-sans-extension url)))
1568 ((eq type 'link)
1569 (muse-markup-text 'link url (or desc orig-url)))
1571 (or (and (or desc
1572 (not (string= url orig-url)))
1573 (let ((text (muse-markup-text 'url-and-desc url
1574 (or desc orig-url))))
1575 (and (not (string= text ""))
1576 text)))
1577 (muse-markup-text 'url url (or desc orig-url)))))))
1579 (defun muse-publish-insert-url (url &optional desc orig-url explicit)
1580 "Resolve a URL into its final <a href> form."
1581 (delete-region (match-beginning 0) (match-end 0))
1582 (let ((text (muse-publish-url url desc orig-url explicit)))
1583 (when text
1584 (muse-insert-markup text))))
1586 (defun muse-publish-markup-link ()
1587 (let (desc explicit orig-link link)
1588 (setq explicit (save-match-data
1589 (if (string-match muse-explicit-link-regexp
1590 (match-string 0))
1591 t nil)))
1592 (setq orig-link (if explicit (match-string 1) (match-string 0)))
1593 (setq desc (when explicit (match-string 2)))
1594 (setq link (if explicit
1595 (muse-handle-explicit-link orig-link)
1596 (muse-handle-implicit-link orig-link)))
1597 (when (and link
1598 (or explicit
1599 (not (or (eq (char-before (match-beginning 0)) ?\")
1600 (eq (char-after (match-end 0)) ?\")))))
1601 ;; if explicit link has no user-provided description, treat it
1602 ;; as if it were an implicit link
1603 (when (and explicit (not desc))
1604 (setq explicit nil))
1605 (muse-publish-insert-url link desc orig-link explicit))))
1607 (defun muse-publish-markup-url ()
1608 (unless (or (eq (char-before (match-beginning 0)) ?\")
1609 (eq (char-after (match-end 0)) ?\"))
1610 (let ((url (match-string 0)))
1611 (muse-publish-insert-url url nil url))))
1613 ;; Default publishing tags
1615 (defcustom muse-publish-contents-depth 2
1616 "The number of heading levels to include with <contents> tags."
1617 :type 'integer
1618 :group 'muse-publish)
1620 (defun muse-publish-contents-tag (beg end attrs)
1621 (set (make-local-variable 'muse-publish-generate-contents)
1622 (cons (copy-marker (point) t)
1623 (let ((depth (cdr (assoc "depth" attrs))))
1624 (or (and depth (string-to-number depth))
1625 muse-publish-contents-depth)))))
1627 (defun muse-publish-verse-tag (beg end)
1628 (muse-publish-ensure-block beg)
1629 (save-excursion
1630 (save-restriction
1631 (narrow-to-region beg end)
1632 (goto-char (point-min))
1633 (while (eq ?\ (char-syntax (char-after)))
1634 (delete-char 1))
1635 (while (< (point) (point-max))
1636 (insert "> ")
1637 (forward-line))
1638 (if (eq ?\ (char-syntax (char-before)))
1639 (delete-char -1)))))
1641 (defun muse-publish-mark-read-only (beg end)
1642 "Add read-only properties to the given region."
1643 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1644 nil)
1646 (defun muse-publish-mark-link (&optional beg end)
1647 "Indicate that the given region is a Muse link, so that other
1648 markup elements respect it. If a region is not specified, use
1649 the 0th match data to determine it.
1651 This is usually applied to explicit links."
1652 (unless beg (setq beg (match-beginning 0)))
1653 (unless end (setq end (match-end 0)))
1654 (add-text-properties beg end '(muse-link t))
1655 nil)
1657 (defun muse-publish-quote-tag (beg end)
1658 (muse-publish-ensure-block beg)
1659 (save-excursion
1660 (save-restriction
1661 (narrow-to-region beg end)
1662 (let ((quote-regexp "^\\(<\\(/?\\)quote>\\)"))
1663 (muse-insert-markup (muse-markup-text 'begin-quote))
1664 (while (progn
1665 (unless (looking-at (concat "[" muse-regexp-blank "\n]*"
1666 "<quote>"))
1667 (muse-publish-surround-text
1668 (muse-markup-text 'begin-quote-item)
1669 (muse-markup-text 'end-quote-item)
1670 (function
1671 (lambda (indent)
1672 (muse-forward-paragraph)
1673 (goto-char (match-end 0))
1674 (and (< (point) (point-max))
1675 (not (looking-at quote-regexp)))))
1676 nil nil nil
1677 quote-regexp))
1678 (if (>= (point) (point-max))
1680 (and (search-forward "<quote>" nil t)
1681 (muse-goto-tag-end "quote" t)
1682 (progn (forward-line 1) t)
1683 (< (point) (point-max))))))
1684 (goto-char (point-max))
1685 (muse-insert-markup (muse-markup-text 'end-quote))))))
1687 (defun muse-publish-code-tag (beg end)
1688 (muse-publish-escape-specials beg end nil 'literal)
1689 (goto-char beg)
1690 (insert (muse-markup-text 'begin-literal))
1691 (goto-char end)
1692 (insert (muse-markup-text 'end-literal))
1693 (muse-publish-mark-read-only beg (point)))
1695 (defun muse-publish-src-tag (beg end attrs)
1696 (muse-publish-example-tag beg end))
1698 (defun muse-publish-example-tag (beg end)
1699 (muse-publish-ensure-block beg)
1700 (muse-publish-escape-specials beg end nil 'example)
1701 (goto-char beg)
1702 (insert (muse-markup-text 'begin-example))
1703 (goto-char end)
1704 (insert (muse-markup-text 'end-example))
1705 (muse-publish-mark-read-only beg (point)))
1707 (defun muse-publish-literal-tag (beg end attrs)
1708 "Ensure that the text between BEG and END is not interpreted later on.
1710 ATTRS is an alist of attributes.
1712 If it contains a \"style\" element, delete the region if the
1713 current style is neither derived from nor equal to this style.
1715 If it contains both a \"style\" element and an \"exact\" element
1716 with the value \"t\", delete the region only if the current style
1717 is exactly this style."
1718 (let* ((style (cdr (assoc "style" attrs)))
1719 (exact (cdr (assoc "exact" attrs)))
1720 (exactp (and (stringp exact) (string= exact "t"))))
1721 (if (or (not style)
1722 (and exactp (equal (muse-style style)
1723 muse-publishing-current-style))
1724 (and (not exactp) (muse-style-derived-p style)))
1725 (muse-publish-mark-read-only beg end)
1726 (delete-region beg end))))
1728 (defun muse-publish-verbatim-tag (beg end)
1729 (muse-publish-escape-specials beg end nil 'verbatim)
1730 (muse-publish-mark-read-only beg end))
1732 (defalias 'muse-publish-class-tag 'ignore)
1734 (defun muse-publish-call-tag-on-buffer (tag &optional attrs)
1735 "Transform the current buffer as if it were surrounded by the tag TAG.
1736 If attributes ATTRS are given, pass them to the tag function."
1737 (let ((tag-info (if muse-inhibit-style-tags
1738 (assoc tag muse-publish-markup-tags)
1739 (muse-markup-tag-info tag))))
1740 (when tag-info
1741 (let* ((end (progn (goto-char (point-max)) (point-marker)))
1742 (args (list (point-min) end))
1743 (muse-inhibit-style-tags nil))
1744 (when (nth 2 tag-info)
1745 (nconc args (list attrs)))
1746 (apply (nth 4 tag-info) args)))))
1748 (defun muse-publish-examplify-buffer (&optional attrs)
1749 "Transform the current buffer as if it were an <example> region."
1750 (muse-publish-call-tag-on-buffer "example" attrs))
1752 (defun muse-publish-srcify-buffer (&optional attrs)
1753 "Transform the current buffer as if it were a <src> region."
1754 (muse-publish-call-tag-on-buffer "src" attrs))
1756 (defun muse-publish-versify-buffer (&optional attrs)
1757 "Transform the current buffer as if it were a <verse> region."
1758 (muse-publish-call-tag-on-buffer "verse" attrs)
1759 (muse-publish-markup ""
1760 `((100 ,(concat "^[" muse-regexp-blank "]*> ") 0
1761 muse-publish-markup-verse)))
1762 (goto-char (point-min)))
1764 (defmacro muse-publish-get-and-delete-attr (attr attrs)
1765 "Delete attribute ATTR from ATTRS only once, destructively.
1767 This function returns the matching attribute value, if found."
1768 (let ((last (make-symbol "last"))
1769 (found (make-symbol "found"))
1770 (vals (make-symbol "vals")))
1771 `(let ((,vals ,attrs))
1772 (if (string= (caar ,vals) ,attr)
1773 (prog1 (cdar ,vals)
1774 (setq ,attrs (cdr ,vals)))
1775 (let ((,last ,vals)
1776 (,found nil))
1777 (while ,vals
1778 (setq ,vals (cdr ,vals))
1779 (when (string= (caar ,vals) ,attr)
1780 (setq ,found (cdar ,vals))
1781 (setcdr ,last (cdr ,vals))
1782 (setq ,vals nil))
1783 (setq ,last ,vals))
1784 ,found)))))
1786 (defmacro muse-publish-markup-attribute (beg end attrs reinterp &rest body)
1787 "Evaluate BODY within the bounds of BEG and END.
1788 ATTRS is an alist. Only the \"markup\" element of ATTRS is acted
1791 If it is omitted, publish the region with the normal Muse rules.
1792 If RE-INTERP is specified, this is done immediately in a new
1793 publishing process. Currently, RE-INTERP is specified only by
1794 the <include> tag.
1796 If \"nil\", do not mark up the region at all, but prevent it from
1797 being further interpreted by Muse.
1799 If \"example\", treat the region as if it was surrounded by the
1800 <example> tag.
1802 If \"src\", treat the region as if it was surrounded by the
1803 <src> tag.
1805 If \"verse\", treat the region as if it was surrounded by the
1806 <verse> tag, to preserve newlines.
1808 Otherwise, it should be the name of a function to call in the
1809 narrowed region after evaluating BODY. The function should
1810 take the ATTRS parameter.
1812 BEG is modified to be the start of the published markup."
1813 (let ((markup (make-symbol "markup"))
1814 (markup-function (make-symbol "markup-function")))
1815 `(let ((,markup (muse-publish-get-and-delete-attr "markup" ,attrs)))
1816 (save-restriction
1817 (narrow-to-region ,beg ,end)
1818 (goto-char (point-min))
1819 ,@body
1820 (if (not ,markup)
1821 (when ,reinterp
1822 (muse-publish-markup-region (point-min) (point-max))
1823 (muse-publish-mark-read-only (point-min) (point-max))
1824 (goto-char (point-max)))
1825 (let ((,markup-function (read ,markup)))
1826 (cond ((eq ,markup-function 'example)
1827 (setq ,markup-function #'muse-publish-examplify-buffer))
1828 ((eq ,markup-function 'src)
1829 (setq ,markup-function #'muse-publish-srcify-buffer))
1830 ((eq ,markup-function 'verse)
1831 (setq ,markup-function #'muse-publish-versify-buffer))
1832 ((and ,markup-function (not (functionp ,markup-function)))
1833 (error "Invalid markup function `%s'" ,markup))
1834 (t nil))
1835 (if ,markup-function
1836 (funcall ,markup-function ,attrs)
1837 (muse-publish-mark-read-only (point-min) (point-max))
1838 (goto-char (point-max)))))))))
1840 (put 'muse-publish-markup-attribute 'lisp-indent-function 4)
1841 (put 'muse-publish-markup-attribute 'edebug-form-spec
1842 '(form form form form body))
1844 (defun muse-publish-lisp-tag (beg end attrs)
1845 (muse-publish-markup-attribute beg end attrs nil
1846 (save-excursion
1847 (let ((str (muse-eval-lisp
1848 (prog1
1849 (concat "(progn "
1850 (buffer-substring-no-properties (point-min)
1851 (point-max))
1852 ")")
1853 (delete-region beg end)))))
1854 (set-text-properties 0 (length str) nil str)
1855 (insert str)))))
1857 (defun muse-publish-command-tag (beg end attrs)
1858 (muse-publish-markup-attribute beg end attrs nil
1859 (while (looking-at "\\s-*$")
1860 (forward-line))
1861 (let ((interp (muse-publish-get-and-delete-attr "interp" attrs)))
1862 (if interp
1863 (shell-command-on-region (point) (point-max) interp t t)
1864 (shell-command
1865 (prog1
1866 (buffer-substring-no-properties (point) (point-max))
1867 (delete-region (point-min) (point-max)))
1868 t)))
1869 ;; make sure there is a newline at end
1870 (goto-char (point-max))
1871 (forward-line 0)
1872 (unless (looking-at "\\s-*$")
1873 (goto-char (point-max))
1874 (insert ?\n))
1875 (goto-char (point-min))))
1877 (defun muse-publish-perl-tag (beg end attrs)
1878 (muse-publish-command-tag beg end
1879 (cons (cons "interp" (executable-find "perl"))
1880 attrs)))
1882 (defun muse-publish-python-tag (beg end attrs)
1883 (muse-publish-command-tag beg end
1884 (cons (cons "interp" (executable-find "python"))
1885 attrs)))
1887 (defun muse-publish-ruby-tag (beg end attrs)
1888 (muse-publish-command-tag beg end
1889 (cons (cons "interp" (executable-find "ruby"))
1890 attrs)))
1892 (defun muse-publish-comment-tag (beg end)
1893 (if (null muse-publish-comments-p)
1894 (delete-region beg end)
1895 (goto-char end)
1896 (muse-insert-markup (muse-markup-text 'comment-end))
1897 (muse-publish-mark-read-only beg end)
1898 (goto-char beg)
1899 (muse-insert-markup (muse-markup-text 'comment-begin))))
1901 (defun muse-publish-include-tag (beg end attrs)
1902 "Include the named file at the current location during publishing.
1904 <include file=\"...\" markup=\"...\">
1906 The `markup' attribute controls how this file is marked up after
1907 being inserted. See `muse-publish-markup-attribute' for an
1908 explanation of how it works."
1909 (let ((filename (muse-publish-get-and-delete-attr "file" attrs))
1910 (muse-publishing-directives muse-publishing-directives))
1911 (if filename
1912 (setq filename (expand-file-name
1913 filename
1914 (file-name-directory muse-publishing-current-file)))
1915 (error "No file attribute specified in <include> tag"))
1916 (muse-publish-markup-attribute beg end attrs t
1917 (insert-file-contents filename))))
1919 (defun muse-publish-mark-up-tag (beg end attrs)
1920 "Run an Emacs Lisp function on the region delimted by this tag.
1922 <markup function=\"...\" style=\"...\" exact=\"...\">
1924 The optional \"function\" attribute controls how this section is
1925 marked up. If used, it should be the name of a function to call
1926 with the buffer narrowed to the delimited region. Note that no
1927 further marking-up will be performed on this region.
1929 If \"function\" is omitted, use the standard Muse markup function.
1930 This is useful for marking up content in headers and footers.
1932 The optional \"style\" attribute causes the region to be deleted
1933 if the current style is neither derived from nor equal to this
1934 style.
1936 If both a \"style\" attribute and an \"exact\" attribute are
1937 provided, and \"exact\" is \"t\", delete the region only if the
1938 current style is exactly this style."
1939 (let* ((style (cdr (assoc "style" attrs)))
1940 (exact (cdr (assoc "exact" attrs)))
1941 (exactp (and (stringp exact) (string= exact "t"))))
1942 (if (or (not style)
1943 (and exactp (equal (muse-style style)
1944 muse-publishing-current-style))
1945 (and (not exactp) (muse-style-derived-p style)))
1946 (let* ((function (cdr (assoc "function" attrs)))
1947 (muse-publishing-directives muse-publishing-directives)
1948 (markup-function (and function (intern function))))
1949 (if (and markup-function (functionp markup-function))
1950 (save-restriction
1951 (narrow-to-region beg end)
1952 (funcall markup-function)
1953 (goto-char (point-max)))
1954 (let ((muse-publish-inhibit-style-hooks t))
1955 (muse-publish-markup-region beg end)))
1956 (muse-publish-mark-read-only beg (point)))
1957 (delete-region beg end))))
1959 ;; Miscellaneous helper functions
1961 (defun muse-publish-strip-URL (string &rest ignored)
1962 "If the text \"URL:\" exists at the beginning of STRING, remove it.
1963 The text is removed regardless of whether and part of it is uppercase."
1964 (save-match-data
1965 (if (string-match "\\`[uU][rR][lL]:\\(.+\\)\\'" string)
1966 (match-string 1 string)
1967 string)))
1969 (defun muse-publish-strip-tags (string)
1970 "Remove all tags from the string."
1971 (while (string-match "<.*?>" string)
1972 (setq string (replace-match "" nil t string)))
1973 string)
1975 (defun muse-publish-markup-type (category default-func)
1976 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1977 (funcall (or rule default-func))))
1979 (defun muse-published-buffer-contents (buffer)
1980 (with-current-buffer buffer
1981 (goto-char (point-min))
1982 (let ((beg (and (search-forward "Emacs Muse begins here")
1983 (muse-line-end-position)))
1984 (end (and (search-forward "Emacs Muse ends here")
1985 (muse-line-beginning-position))))
1986 (buffer-substring-no-properties beg end))))
1988 (defun muse-published-contents (file)
1989 (when (file-readable-p file)
1990 (muse-with-temp-buffer
1991 (insert-file-contents file)
1992 (muse-published-buffer-contents (current-buffer)))))
1994 (defun muse-publish-transform-output
1995 (file temp-file output-path name gen-func &rest cleanup-exts)
1996 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1997 (setq file (muse-page-name file))
1998 (message "Generating %s output for %s..." name file)
1999 (if (not (funcall gen-func temp-file output-path))
2000 (message "Generating %s from %s...failed" name file)
2001 (message "Generating %s output for %s...done" name file)
2002 (muse-delete-file-if-exists temp-file)
2003 (dolist (ext cleanup-exts)
2004 (muse-delete-file-if-exists
2005 (expand-file-name (concat file ext)
2006 (file-name-directory output-path))))
2007 (message "Wrote %s" output-path)))
2009 (defun muse-publish-read-only (string)
2010 (let ((end (1- (length string))))
2011 (add-text-properties 0 end
2012 '(rear-nonsticky (read-only) read-only t)
2013 string)
2014 string))
2016 ;;; muse-publish.el ends here