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