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