9e8f6edc3afb6ed56bf702af8d1f5f87b5b75fc3
[muse-el.git] / lisp / muse-publish.el
blob9e8f6edc3afb6ed56bf702af8d1f5f87b5b75fc3
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 ((text (buffer-substring beg end))
799 (buf (generate-new-buffer (concat "*Muse: " title "*"))))
800 (with-current-buffer buf
801 (insert text)
802 (muse-publish-markup-buffer title style)
803 (goto-char (point-min))
804 (let ((inhibit-read-only t))
805 (remove-text-properties (point-min) (point-max)
806 '(rear-nonsticky nil read-only nil))))
807 (pop-to-buffer buf)))
809 ;;;###autoload
810 (defun muse-publish-file (file style &optional output-dir force)
811 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
812 If the argument FORCE is nil, each file is only published if it is
813 newer than the published version. If the argument FORCE is non-nil,
814 the file is published no matter what."
815 (interactive (cons (read-file-name "Publish file: ")
816 (muse-publish-get-info)))
817 (let ((style-name style))
818 (setq style (muse-style style))
819 (unless style
820 (error "There is no style '%s' defined" style-name)))
821 (let* ((output-path (muse-publish-output-file file output-dir style))
822 (output-suffix (muse-style-element :osuffix style))
823 (muse-publishing-current-file file)
824 (muse-publishing-current-output-path output-path)
825 (target (if output-suffix
826 (concat (muse-path-sans-extension output-path)
827 output-suffix)
828 output-path))
829 (threshhold (nth 7 (file-attributes file))))
830 (if (not threshhold)
831 (message "Please save %s before publishing" file)
832 (when (or force (file-newer-than-file-p file target))
833 (if (and muse-publish-report-threshhold
834 (> threshhold
835 muse-publish-report-threshhold))
836 (message "Publishing %s ..." file))
837 (muse-with-temp-buffer
838 (muse-insert-file-contents file)
839 (run-hooks 'muse-before-publish-hook)
840 (when muse-publish-enable-local-variables
841 (hack-local-variables))
842 (let ((muse-inhibit-before-publish-hook t))
843 (muse-publish-markup-buffer (muse-page-name file) style))
844 (when (muse-write-file output-path)
845 (muse-style-run-hooks :final style file output-path target)))
846 t))))
848 ;;;###autoload
849 (defun muse-publish-this-file (style output-dir &optional force)
850 "Publish the currently-visited file.
851 Prompt for both the STYLE and OUTPUT-DIR if they are not
852 supplied."
853 (interactive (muse-publish-get-info))
854 (setq style (muse-style style))
855 (if buffer-file-name
856 (let ((muse-current-output-style (list :base (car style)
857 :path output-dir)))
858 (unless (muse-publish-file buffer-file-name style output-dir force)
859 (message (concat "The published version is up-to-date; use"
860 " C-u C-c C-T to force an update."))))
861 (message "This buffer is not associated with any file")))
863 (defun muse-batch-publish-files ()
864 "Publish Muse files in batch mode."
865 (let ((muse-batch-publishing-p t)
866 (font-lock-verbose nil)
867 muse-current-output-style
868 style output-dir)
869 ;; don't activate VC when publishing files
870 (setq vc-handled-backends nil)
871 (setq style (car command-line-args-left)
872 command-line-args-left (cdr command-line-args-left)
873 output-dir (car command-line-args-left)
874 output-dir
875 (if (string-match "\\`--output-dir=" output-dir)
876 (prog1
877 (substring output-dir (match-end 0))
878 (setq command-line-args-left (cdr command-line-args-left))))
879 muse-current-output-style (list :base style :path output-dir))
880 (setq auto-mode-alist
881 (delete (cons (concat "\\." muse-file-extension "\\'")
882 'muse-mode-choose-mode)
883 auto-mode-alist))
884 (dolist (file command-line-args-left)
885 (muse-publish-file file style output-dir t))))
887 ;; Default publishing rules
889 (defun muse-publish-section-close (depth)
890 "Seach forward for the closing tag of given DEPTH."
891 (let (not-end)
892 (save-excursion
893 (while (and (setq not-end (re-search-forward
894 (concat "^\\*\\{1," (number-to-string depth)
895 "\\}\\s-+")
896 nil t))
897 (get-text-property (match-beginning 0) 'read-only)))
898 (if not-end
899 (forward-line 0)
900 (goto-char (point-max)))
901 (cond ((not (eq (char-before) ?\n))
902 (insert "\n\n"))
903 ((not (eq (char-before (1- (point))) ?\n))
904 (insert "\n")))
905 (muse-insert-markup (muse-markup-text 'section-close depth))
906 (insert "\n"))))
908 (defun muse-publish-markup-directive (&optional name value)
909 (unless name (setq name (match-string 1)))
910 (unless value (setq value (match-string 2)))
911 (let ((elem (assoc name muse-publishing-directives)))
912 (if elem
913 (setcdr elem value)
914 (setq muse-publishing-directives
915 (cons (cons name value)
916 muse-publishing-directives))))
917 ;; Make sure we don't ever try to move the point forward (past the
918 ;; beginning of buffer) while we're still searching for directives.
919 (setq muse-publishing-last-position nil)
920 (delete-region (match-beginning 0) (match-end 0)))
922 (defsubst muse-publishing-directive (name)
923 (cdr (assoc name muse-publishing-directives)))
925 (defmacro muse-publish-get-and-delete-attr (attr attrs)
926 "Delete attribute ATTR from ATTRS only once, destructively.
928 This function returns the matching attribute value, if found."
929 (let ((last (make-symbol "last"))
930 (found (make-symbol "found"))
931 (vals (make-symbol "vals")))
932 `(let ((,vals ,attrs))
933 (if (string= (caar ,vals) ,attr)
934 (prog1 (cdar ,vals)
935 (setq ,attrs (cdr ,vals)))
936 (let ((,last ,vals)
937 (,found nil))
938 (while ,vals
939 (setq ,vals (cdr ,vals))
940 (when (string= (caar ,vals) ,attr)
941 (setq ,found (cdar ,vals))
942 (setcdr ,last (cdr ,vals))
943 (setq ,vals nil))
944 (setq ,last ,vals))
945 ,found)))))
947 (defun muse-publish-markup-anchor ()
948 (unless (get-text-property (match-end 1) 'muse-link)
949 (let ((text (muse-markup-text 'anchor (match-string 2))))
950 (unless (string= text "")
951 (save-match-data
952 (skip-chars-forward (concat muse-regexp-blank "\n"))
953 (muse-insert-markup text)))
954 (match-string 1))))
956 (defun muse-publish-markup-comment ()
957 (if (null muse-publish-comments-p)
959 (goto-char (match-end 0))
960 (muse-insert-markup (muse-markup-text 'comment-end))
961 (if (match-beginning 1)
962 (progn
963 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
964 (delete-region (match-beginning 0) (match-beginning 1)))
965 (delete-region (match-beginning 0) (match-end 0)))
966 (goto-char (match-beginning 0))
967 (muse-insert-markup (muse-markup-text 'comment-begin))))
969 (defun muse-publish-markup-tag ()
970 (let ((tag-info (muse-markup-tag-info (match-string 1))))
971 (when (and tag-info
972 (not (get-text-property (match-beginning 0) 'read-only))
973 (nth 4 tag-info)
974 (or muse-publish-enable-dangerous-tags
975 (not (get (nth 4 tag-info) 'muse-dangerous-tag))))
976 (let ((closed-tag (match-string 3))
977 (start (match-beginning 0))
978 (beg (point))
979 end attrs)
980 (when (nth 2 tag-info)
981 (let ((attrstr (match-string 2)))
982 (while (and attrstr
983 (string-match (concat "\\([^"
984 muse-regexp-blank
985 "=\n]+\\)\\(=\"\\"
986 "([^\"]+\\)\"\\)?")
987 attrstr))
988 (let ((attr (cons (downcase
989 (muse-match-string-no-properties 1 attrstr))
990 (muse-match-string-no-properties 3 attrstr))))
991 (setq attrstr (replace-match "" t t attrstr))
992 (if attrs
993 (nconc attrs (list attr))
994 (setq attrs (list attr)))))))
995 (if (and (cadr tag-info) (not closed-tag))
996 (if (muse-goto-tag-end (car tag-info) (nth 3 tag-info))
997 (delete-region (match-beginning 0) (point))
998 (setq tag-info nil)))
999 (when tag-info
1000 (setq end (point-marker))
1001 (delete-region start beg)
1002 (goto-char start)
1003 (let ((args (list start end)))
1004 (if (nth 2 tag-info)
1005 (nconc args (list attrs)))
1006 (let ((muse-inhibit-style-tags nil))
1007 ;; remove the inhibition
1008 (apply (nth 4 tag-info) args)))
1009 (set-marker end nil)))))
1010 nil)
1012 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
1013 "Escape specials from BEG to END using style-specific :specials.
1014 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
1015 CONTEXT is used to figure out what kind of specials to escape.
1017 The following contexts exist in Muse.
1018 'underline _underlined text_
1019 'literal =monospaced text= or <code> region (monospaced, escaped)
1020 'emphasis *emphasized text*
1021 'email email@example.com
1022 'url http://example.com
1023 'url-desc [[...][description of an explicit link]]
1024 'image [[image.png]]
1025 'example <example> region (monospaced, block context, escaped)
1026 'verbatim <verbatim> region (escaped)
1027 'footnote footnote text
1028 'document normal text"
1029 (let ((specials (muse-style-element :specials nil t)))
1030 (cond ((functionp specials)
1031 (setq specials (funcall specials context)))
1032 ((symbolp specials)
1033 (setq specials (symbol-value specials))))
1034 (if (functionp specials)
1035 (funcall specials beg end ignore-read-only)
1036 (save-excursion
1037 (save-restriction
1038 (narrow-to-region beg end)
1039 (goto-char (point-min))
1040 (while (< (point) (point-max))
1041 (if (and (not ignore-read-only)
1042 (get-text-property (point) 'read-only))
1043 (goto-char (or (next-single-property-change (point) 'read-only)
1044 (point-max)))
1045 (let ((repl (or (assoc (char-after) specials)
1046 (assoc (char-after)
1047 muse-publish-markup-specials))))
1048 (if (null repl)
1049 (forward-char 1)
1050 (delete-char 1)
1051 (insert-before-markers (cdr repl)))))))))))
1053 (defun muse-publish-markup-word ()
1054 (let* ((beg (match-beginning 2))
1055 (end (1- (match-end 2)))
1056 (leader (buffer-substring-no-properties beg end))
1057 open-tag close-tag mark-read-only loc context)
1058 (cond
1059 ((string= leader "_")
1060 (setq context 'underline
1061 open-tag (muse-markup-text 'begin-underline)
1062 close-tag (muse-markup-text 'end-underline)))
1063 ((string= leader "=")
1064 (setq context 'literal
1065 open-tag (muse-markup-text 'begin-literal)
1066 close-tag (muse-markup-text 'end-literal))
1067 (setq mark-read-only t))
1069 (let ((l (length leader)))
1070 (setq context 'emphasis)
1071 (cond
1072 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
1073 close-tag (muse-markup-text 'end-emph)))
1074 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
1075 close-tag (muse-markup-text 'end-more-emph)))
1076 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
1077 close-tag (muse-markup-text 'end-most-emph)))
1078 (t (setq context nil))))))
1079 (if (and context
1080 (not (get-text-property beg 'muse-link))
1081 (setq loc (search-forward leader nil t))
1082 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
1083 (not (eq (char-syntax (char-before (point))) ?\ ))
1084 (not (get-text-property (point) 'muse-link)))
1085 (progn
1086 (replace-match "")
1087 (delete-region beg end)
1088 (setq end (point-marker))
1089 (muse-insert-markup close-tag)
1090 (goto-char beg)
1091 (muse-insert-markup open-tag)
1092 (setq beg (point))
1093 (when mark-read-only
1094 (muse-publish-escape-specials beg end t context)
1095 (muse-publish-mark-read-only beg end))
1096 (set-marker end nil))
1097 (backward-char))
1098 nil))
1100 (defun muse-publish-markup-emdash ()
1101 (unless (get-text-property (match-beginning 0) 'muse-link)
1102 (let ((prespace (match-string 1))
1103 (postspace (match-string 2)))
1104 (delete-region (match-beginning 0) (match-end 0))
1105 (muse-insert-markup (muse-markup-text 'emdash prespace postspace))
1106 (when (eq (char-after) ?\<)
1107 (insert ?\n)))))
1109 (defun muse-publish-markup-enddots ()
1110 (unless (get-text-property (match-beginning 0) 'muse-link)
1111 (delete-region (match-beginning 0) (match-end 0))
1112 (muse-insert-markup (muse-markup-text 'enddots))))
1114 (defun muse-publish-markup-dots ()
1115 (unless (get-text-property (match-beginning 0) 'muse-link)
1116 (delete-region (match-beginning 0) (match-end 0))
1117 (muse-insert-markup (muse-markup-text 'dots))))
1119 (defun muse-publish-markup-rule ()
1120 (unless (get-text-property (match-beginning 0) 'muse-link)
1121 (delete-region (match-beginning 0) (match-end 0))
1122 (muse-insert-markup (muse-markup-text 'rule))))
1124 (defun muse-publish-markup-no-break-space ()
1125 (unless (get-text-property (match-beginning 0) 'muse-link)
1126 (delete-region (match-beginning 0) (match-end 0))
1127 (muse-insert-markup (muse-markup-text 'no-break-space))))
1129 (defun muse-publish-markup-heading ()
1130 (let* ((len (length (match-string 1)))
1131 (start (muse-markup-text
1132 (cond ((= len 1) 'section)
1133 ((= len 2) 'subsection)
1134 ((= len 3) 'subsubsection)
1135 (t 'section-other))
1136 len))
1137 (end (muse-markup-text
1138 (cond ((= len 1) 'section-end)
1139 ((= len 2) 'subsection-end)
1140 ((= len 3) 'subsubsection-end)
1141 (t 'section-other-end))
1142 len)))
1143 (delete-region (match-beginning 0) (match-end 0))
1144 (muse-insert-markup start)
1145 (end-of-line)
1146 (when end
1147 (muse-insert-markup end))
1148 (forward-line 1)
1149 (unless (eq (char-after) ?\n)
1150 (insert "\n"))
1151 (muse-publish-section-close len)))
1153 (defvar muse-publish-footnotes nil)
1155 (defun muse-publish-markup-footnote ()
1156 "Scan ahead and snarf up the footnote body."
1157 (cond
1158 ((get-text-property (match-beginning 0) 'muse-link)
1159 nil)
1160 ((= (muse-line-beginning-position) (match-beginning 0))
1163 (let ((footnote (save-match-data
1164 (string-to-number (match-string 1))))
1165 (oldtext (match-string 0))
1166 footnotemark)
1167 (delete-region (match-beginning 0) (match-end 0))
1168 (save-excursion
1169 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
1170 (let* ((start (match-beginning 0))
1171 (beg (goto-char (match-end 0)))
1172 (end (save-excursion
1173 (if (search-forward "\n\n" nil t)
1174 (copy-marker (match-beginning 0))
1175 (goto-char (point-max))
1176 (skip-chars-backward "\n")
1177 (point-marker)))))
1178 (while (re-search-forward
1179 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
1180 end t)
1181 (replace-match "\\1" t))
1182 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
1183 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
1184 (if (string= "" footnotemark-cmd)
1185 (setq footnotemark
1186 (concat (muse-markup-text 'footnote)
1187 (muse-publish-escape-specials-in-string
1188 (buffer-substring-no-properties beg end)
1189 'footnote)
1190 (muse-markup-text 'footnote-end)))
1191 (setq footnotemark (format footnotemark-cmd footnote
1192 footnotemark-end-cmd))
1193 (unless muse-publish-footnotes
1194 (set (make-local-variable 'muse-publish-footnotes)
1195 (make-vector 256 nil)))
1196 (unless (aref muse-publish-footnotes footnote)
1197 (setq footnotemark
1198 (concat
1199 footnotemark
1200 (concat (format (muse-markup-text 'footnotetext)
1201 footnote)
1202 (buffer-substring-no-properties beg end)
1203 (muse-markup-text 'footnotetext-end))))
1204 (aset muse-publish-footnotes footnote footnotemark))))
1205 (goto-char end)
1206 (skip-chars-forward "\n")
1207 (delete-region start (point))
1208 (set-marker end nil))))
1209 (if footnotemark
1210 (muse-insert-markup footnotemark)
1211 (insert oldtext))))))
1213 (defun muse-publish-markup-fn-sep ()
1214 (delete-region (match-beginning 0) (match-end 0))
1215 (muse-insert-markup (muse-markup-text 'fn-sep)))
1217 (defun muse-insert-markup-end-list (&rest args)
1218 (let ((beg (point)))
1219 (apply 'insert args)
1220 (add-text-properties beg (point) '(muse-end-list t))
1221 (muse-publish-mark-read-only beg (point))))
1223 (defun muse-publish-determine-dl-indent (continue indent-sym determine-sym)
1224 ;; If the caller doesn't know how much indentation to use, figure it
1225 ;; out ourselves. It is assumed that `muse-forward-list-item' has
1226 ;; been called just before this to set the match data.
1227 (when (and continue
1228 (symbol-value determine-sym))
1229 (save-match-data
1230 ;; snarf all leading whitespace
1231 (let ((indent (and (match-beginning 2)
1232 (buffer-substring (match-beginning 1)
1233 (match-beginning 2)))))
1234 (when (and indent
1235 (not (string= indent "")))
1236 (set indent-sym indent)
1237 (set determine-sym nil))))))
1239 (defun muse-publish-surround-dl (indent post-indent)
1240 (let* ((beg-item (muse-markup-text 'begin-dl-item))
1241 (end-item (muse-markup-text 'end-dl-item))
1242 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
1243 (end-ddt (muse-markup-text 'end-ddt))
1244 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
1245 (end-dde (muse-markup-text 'end-dde))
1246 (continue t)
1247 (no-terms t)
1248 beg)
1249 (while continue
1250 ;; envelope this as one term+definitions unit -- HTML does not
1251 ;; need this, but DocBook and Muse's custom XML format do
1252 (muse-insert-markup beg-item)
1253 (when (looking-at muse-dl-term-regexp)
1254 ;; find the term and wrap it with published markup
1255 (setq beg (point)
1256 no-terms nil)
1257 (goto-char (match-end 1))
1258 (delete-region (point) (match-end 0))
1259 (muse-insert-markup-end-list end-ddt)
1260 ;; if definition is immediately after term, move to next line
1261 (unless (eq (char-after) ?\n)
1262 (insert ?\n))
1263 (save-excursion
1264 (goto-char beg)
1265 (delete-region (point) (match-beginning 1))
1266 (muse-insert-markup beg-ddt)))
1267 ;; handle pathological edge case where there is no term -- I
1268 ;; would prefer to just disallow this, but people seem to want
1269 ;; this behavior
1270 (when (and no-terms
1271 (looking-at (concat "[" muse-regexp-blank "]*::"
1272 "[" muse-regexp-blank "]*")))
1273 (delete-region (point) (match-end 0))
1274 ;; but only do this once
1275 (setq no-terms nil))
1276 (setq beg (point)
1277 ;; move past current item
1278 continue (muse-forward-list-item 'dl-term indent))
1279 (save-restriction
1280 (narrow-to-region beg (point))
1281 (goto-char (point-min))
1282 ;; publish each definition that we find, defaulting to an
1283 ;; empty definition if none are found
1284 (muse-publish-surround-text beg-dde end-dde
1285 (lambda (indent)
1286 (muse-forward-list-item 'dl-entry indent))
1287 indent post-indent
1288 #'muse-publish-determine-dl-indent)
1289 (goto-char (point-max))
1290 (skip-chars-backward (concat muse-regexp-blank "\n"))
1291 (muse-insert-markup-end-list end-item)
1292 (when continue
1293 (goto-char (point-max)))))))
1295 (defun muse-publish-strip-list-indentation (list-item empty-line indent post-indent)
1296 (let ((list-nested nil)
1297 (indent-found nil))
1298 (while (< (point) (point-max))
1299 (when (and (looking-at list-item)
1300 (not (or (get-text-property
1301 (muse-list-item-critical-point) 'read-only)
1302 (get-text-property
1303 (muse-list-item-critical-point) 'muse-link))))
1304 ;; if we encounter a list item, allow no post-indent space
1305 (setq list-nested t))
1306 (when (and (not (looking-at empty-line))
1307 (looking-at (concat indent "\\("
1308 (or (and list-nested "")
1309 post-indent)
1310 "\\)")))
1311 ;; if list is not nested, remove indentation
1312 (unless indent-found
1313 (setq post-indent (match-string 1)
1314 indent-found t))
1315 (replace-match ""))
1316 (forward-line 1))))
1318 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent determine-indent-func list-item)
1319 (unless list-item
1320 (setq list-item (format muse-list-item-regexp
1321 (concat "[" muse-regexp-blank "]*"))))
1322 (let ((continue t)
1323 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
1324 (determine-indent (if determine-indent-func t nil))
1325 (new-indent indent)
1326 (first t)
1327 beg)
1328 (unless indent
1329 (setq indent (concat "[" muse-regexp-blank "]+")))
1330 (if post-indent
1331 (setq post-indent (concat " \\{0," (number-to-string post-indent)
1332 "\\}"))
1333 (setq post-indent ""))
1334 (while continue
1335 (if (or (not end-tag) (string= end-tag ""))
1336 ;; if no end of list item markup exists, treat the beginning
1337 ;; of list item markup as it if it were the end -- this
1338 ;; prevents multiple-level lists from being confused
1339 (muse-insert-markup-end-list beg-tag)
1340 (muse-insert-markup beg-tag))
1341 (setq beg (point)
1342 ;; move past current item; continue is non-nil if there
1343 ;; are more like items to be processed
1344 continue (if (and determine-indent-func first)
1345 (funcall move-func (concat indent post-indent))
1346 (funcall move-func indent)))
1347 (when determine-indent-func
1348 (funcall determine-indent-func continue 'new-indent 'determine-indent))
1349 (when continue
1350 ;; remove list markup if we encountered another item of the
1351 ;; same type
1352 (replace-match "" t t nil 1))
1353 (save-restriction
1354 ;; narrow to current item
1355 (narrow-to-region beg (point))
1356 (goto-char (point-min))
1357 (if (looking-at empty-line)
1358 ;; if initial line is blank, move to first non-blank line
1359 (while (progn (forward-line 1)
1360 (and (< (point) (point-max))
1361 (looking-at empty-line))))
1362 ;; otherwise, move to second line of text
1363 (forward-line 1))
1364 ;; strip list indentation
1365 (muse-publish-strip-list-indentation list-item empty-line
1366 indent post-indent)
1367 (skip-chars-backward (concat muse-regexp-blank "\n"))
1368 (muse-insert-markup-end-list end-tag)
1369 (when determine-indent-func
1370 (setq indent new-indent))
1371 (when first
1372 (setq first nil))
1373 (when continue
1374 (goto-char (point-max)))))))
1376 (defun muse-publish-ensure-blank-line ()
1377 "Make sure that a blank line exists on the line before point."
1378 (let ((pt (point-marker)))
1379 (beginning-of-line)
1380 (cond ((eq (point) (point-min)) nil)
1381 ((prog2 (backward-char) (bolp) (forward-char)) nil)
1382 (t (insert-before-markers "\n")))
1383 (goto-char pt)
1384 (set-marker pt nil)))
1386 (defun muse-publish-markup-list ()
1387 "Markup a list entry.
1388 This function works by marking up items of the same list level
1389 and type, respecting the end-of-list property."
1390 (let* ((str (match-string 1))
1391 (type (muse-list-item-type str))
1392 (indent (buffer-substring (muse-line-beginning-position)
1393 (match-beginning 1)))
1394 (post-indent (length str)))
1395 (cond
1396 ((or (get-text-property (muse-list-item-critical-point) 'read-only)
1397 (get-text-property (muse-list-item-critical-point) 'muse-link))
1398 nil)
1399 ((eq type 'ul)
1400 (unless (eq (char-after (match-end 1)) ?-)
1401 (delete-region (match-beginning 0) (match-end 0))
1402 (muse-publish-ensure-blank-line)
1403 (muse-insert-markup (muse-markup-text 'begin-uli))
1404 (save-excursion
1405 (muse-publish-surround-text
1406 (muse-markup-text 'begin-uli-item)
1407 (muse-markup-text 'end-uli-item)
1408 (lambda (indent)
1409 (muse-forward-list-item 'ul indent))
1410 indent post-indent)
1411 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1412 (forward-line 1)))
1413 ((eq type 'ol)
1414 (delete-region (match-beginning 0) (match-end 0))
1415 (muse-publish-ensure-blank-line)
1416 (muse-insert-markup (muse-markup-text 'begin-oli))
1417 (save-excursion
1418 (muse-publish-surround-text
1419 (muse-markup-text 'begin-oli-item)
1420 (muse-markup-text 'end-oli-item)
1421 (lambda (indent)
1422 (muse-forward-list-item 'ol indent))
1423 indent post-indent)
1424 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1425 (forward-line 1))
1427 (goto-char (match-beginning 0))
1428 (muse-publish-ensure-blank-line)
1429 (muse-insert-markup (muse-markup-text 'begin-dl))
1430 (save-excursion
1431 (muse-publish-surround-dl indent post-indent)
1432 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1433 (forward-line 1))))
1434 nil)
1436 (defun muse-publish-markup-quote ()
1437 "Markup a quoted paragraph.
1438 The reason this function is so funky, is to prevent text properties
1439 like read-only from being inadvertently deleted."
1440 (let* ((ws (match-string 1))
1441 (centered (>= (string-width ws) 6))
1442 (begin-elem (if centered 'begin-center 'begin-quote-item))
1443 (end-elem (if centered 'end-center 'end-quote-item)))
1444 (replace-match "" t t nil 1)
1445 (unless centered
1446 (muse-insert-markup (muse-markup-text 'begin-quote)))
1447 (muse-publish-surround-text (muse-markup-text begin-elem)
1448 (muse-markup-text end-elem)
1449 (function (lambda (indent)
1450 (muse-forward-paragraph)
1451 nil)))
1452 (unless centered
1453 (muse-insert-markup (muse-markup-text 'end-quote)))))
1455 (defun muse-publish-markup-leading-space (markup-space multiple)
1456 (let (count)
1457 (when (and markup-space
1458 (>= (setq count (skip-chars-forward " ")) 0))
1459 (delete-region (muse-line-beginning-position) (point))
1460 (while (> count 0)
1461 (muse-insert-markup markup-space)
1462 (setq count (- count multiple))))))
1464 (defun muse-publish-markup-verse ()
1465 (let ((leader (match-string 0)))
1466 (goto-char (match-beginning 0))
1467 (muse-insert-markup (muse-markup-text 'begin-verse))
1468 (while (looking-at leader)
1469 (replace-match "")
1470 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1471 (let ((beg (point)))
1472 (end-of-line)
1473 (cond
1474 ((bolp)
1475 (let ((text (muse-markup-text 'empty-verse-line)))
1476 (when text (muse-insert-markup text))))
1477 ((save-excursion
1478 (save-match-data
1479 (forward-line 1)
1480 (or (looking-at (concat leader "["
1481 muse-regexp-blank
1482 "]*$"))
1483 (not (looking-at leader)))))
1484 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1485 (end-text (muse-markup-text 'end-last-stanza-line)))
1486 (when end-text (muse-insert-markup end-text))
1487 (goto-char beg)
1488 (when begin-text (muse-insert-markup begin-text))
1489 (end-of-line)))
1491 (let ((begin-text (muse-markup-text 'begin-verse-line))
1492 (end-text (muse-markup-text 'end-verse-line)))
1493 (when end-text (muse-insert-markup end-text))
1494 (goto-char beg)
1495 (when begin-text (muse-insert-markup begin-text))
1496 (end-of-line))))
1497 (forward-line 1))))
1498 (muse-insert-markup (muse-markup-text 'end-verse))
1499 (insert ?\n))
1501 (defun muse-publish-trim-table (table)
1502 "Remove completely blank columns from table, if at start or end of row."
1503 ;; remove first
1504 (catch 'found
1505 (dolist (row (cdr table))
1506 (let ((el (cadr row)))
1507 (when (and (stringp el) (not (string= el "")))
1508 (throw 'found t))))
1509 (dolist (row (cdr table))
1510 (setcdr row (cddr row)))
1511 (setcar table (1- (car table))))
1512 ;; remove last
1513 (catch 'found
1514 (dolist (row (cdr table))
1515 (let ((el (car (last row))))
1516 (when (and (stringp el) (not (string= el "")))
1517 (throw 'found t))))
1518 (dolist (row (cdr table))
1519 (setcdr (last row 2) nil))
1520 (setcar table (1- (car table))))
1521 table)
1523 (defun muse-publish-table-fields (beg end)
1524 "Parse given region as a table, returning a cons cell.
1525 The car is the length of the longest row.
1527 The cdr is a list of the fields of the table, with the first
1528 element indicating the type of the row:
1529 1: body, 2: header, 3: footer, hline: separator.
1531 The existing region will be removed, except for initial blank lines."
1532 (unless (muse-publishing-directive "disable-tables")
1533 (let ((longest 0)
1534 (left 0)
1535 (seen-hline nil)
1536 fields field-list)
1537 (save-restriction
1538 (narrow-to-region beg end)
1539 (goto-char (point-min))
1540 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1541 (forward-line 1))
1542 (setq beg (point))
1543 (while (= left 0)
1544 (cond
1545 ((looking-at muse-table-hline-regexp)
1546 (when field-list ; skip if at the beginning of table
1547 (if seen-hline
1548 (setq field-list (cons (cons 'hline nil) field-list))
1549 (dolist (field field-list)
1550 ;; the preceding fields are header lines
1551 (setcar field 2))
1552 (setq seen-hline t))))
1553 ((looking-at muse-table-line-regexp)
1554 (setq fields (cons (length (match-string 1))
1555 (mapcar #'muse-trim-whitespace
1556 (split-string (match-string 0)
1557 muse-table-field-regexp)))
1558 field-list (cons fields field-list)
1559 longest (max (length fields) longest))
1560 ;; strip initial bars, if they exist
1561 (let ((first (cadr fields)))
1562 (when (and first (string-match "\\`|+\\s-*" first))
1563 (setcar (cdr fields) (replace-match "" t t first))))))
1564 (setq left (forward-line 1))))
1565 (delete-region beg end)
1566 (if (= longest 0)
1567 (cons 0 nil)
1568 ;; if the last line was an hline, remove it
1569 (when (eq (caar field-list) 'hline)
1570 (setq field-list (cdr field-list)))
1571 (muse-publish-trim-table (cons (1- longest) (nreverse field-list)))))))
1573 (defun muse-publish-markup-table ()
1574 "Style does not support tables.\n")
1576 (defun muse-publish-table-el-table (variant)
1577 "Publish table.el-style tables in the format given by VARIANT."
1578 (when (condition-case nil
1579 (progn (require 'table)
1581 (error nil))
1582 (let ((muse-buf (current-buffer)))
1583 (save-restriction
1584 (narrow-to-region (match-beginning 0) (match-end 0))
1585 (goto-char (point-min))
1586 (forward-line 1)
1587 (when (search-forward "|" nil t)
1588 (with-temp-buffer
1589 (let ((temp-buf (current-buffer)))
1590 (with-current-buffer muse-buf
1591 (table-generate-source variant temp-buf))
1592 (with-current-buffer muse-buf
1593 (delete-region (point-min) (point-max))
1594 (insert-buffer-substring temp-buf)
1595 (muse-publish-mark-read-only (point-min) (point-max))))))))))
1597 (defun muse-publish-markup-table-el ()
1598 "Mark up table.el-style tables."
1599 (cond ((muse-style-derived-p 'html)
1600 (muse-publish-table-el-table 'html))
1601 ((muse-style-derived-p 'latex)
1602 (muse-publish-table-el-table 'latex))
1603 ((muse-style-derived-p 'docbook)
1604 (muse-publish-table-el-table 'cals))
1605 (t "Style does not support table.el tables.\n")))
1607 (defun muse-publish-escape-specials-in-string (string &optional context)
1608 "Escape specials in STRING using style-specific :specials.
1609 CONTEXT is used to figure out what kind of specials to escape.
1611 See the documentation of the `muse-publish-escape-specials'
1612 function for the list of available contexts."
1613 (unless string
1614 (setq string ""))
1615 (let ((specials (muse-style-element :specials nil t)))
1616 (cond ((functionp specials)
1617 (setq specials (funcall specials context)))
1618 ((symbolp specials)
1619 (setq specials (symbol-value specials))))
1620 (if (functionp specials)
1621 (funcall specials string)
1622 (apply (function concat)
1623 (mapcar
1624 (lambda (ch)
1625 (let ((repl (or (assoc ch specials)
1626 (assoc ch muse-publish-markup-specials))))
1627 (if (null repl)
1628 (char-to-string ch)
1629 (cdr repl))))
1630 (append string nil))))))
1632 (defun muse-publish-markup-email ()
1633 (let* ((beg (match-end 1))
1634 (addr (buffer-substring-no-properties beg (match-end 0))))
1635 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1636 (goto-char beg)
1637 (delete-region beg (match-end 0))
1638 (if (or (eq (char-before (match-beginning 0)) ?\")
1639 (eq (char-after (match-end 0)) ?\"))
1640 (insert addr)
1641 (insert (format (muse-markup-text 'email-addr) addr addr)))
1642 (muse-publish-mark-read-only beg (point))))
1644 (defun muse-publish-classify-url (target)
1645 "Transform anchors and get published name, if TARGET is a page.
1646 The return value is two linked cons cells. The car is the type
1647 of link, the cadr is the page name, and the cddr is the anchor."
1648 (save-match-data
1649 (cond ((or (null target) (string= target ""))
1650 nil)
1651 ((string-match "\\`[uU][rR][lL]:\\(.+\\)\\'" target)
1652 (cons 'url (cons (match-string 1 target) nil)))
1653 ((string-match muse-image-regexp target)
1654 (cons 'image (cons target nil)))
1655 ((string-match muse-url-regexp target)
1656 (cons 'url (cons target nil)))
1657 ((string-match muse-file-regexp target)
1658 (cons 'file (cons target nil)))
1659 ((string-match "#" target)
1660 (if (eq (aref target 0) ?\#)
1661 (cons 'anchor-ref (cons nil (substring target 1)))
1662 (cons 'link-and-anchor
1663 ;; match-data is changed by
1664 ;; `muse-publish-link-page' or descendants.
1665 (cons (save-match-data
1666 (muse-publish-link-page
1667 (substring target 0 (match-beginning 0))))
1668 (substring target (match-end 0))))))
1670 (cons 'link (cons (muse-publish-link-page target) nil))))))
1672 (defun muse-publish-url-desc (desc explicit)
1673 (when desc
1674 (dolist (transform muse-publish-desc-transforms)
1675 (setq desc (save-match-data
1676 (when desc (funcall transform desc explicit)))))
1677 (setq desc (muse-link-unescape desc))
1678 (muse-publish-escape-specials-in-string desc 'url-desc)))
1680 (defun muse-publish-url (url &optional desc orig-url explicit)
1681 "Resolve a URL into its final <a href> form."
1682 (let ((unesc-url url)
1683 (unesc-orig-url orig-url)
1684 (unesc-desc desc)
1685 type anchor)
1686 ;; Transform URL
1687 (dolist (transform muse-publish-url-transforms)
1688 (setq url (save-match-data (when url (funcall transform url explicit)))))
1689 ;; Classify URL
1690 (let ((target (muse-publish-classify-url url)))
1691 (setq type (car target)
1692 url (if (eq type 'image)
1693 (muse-publish-escape-specials-in-string (cadr target)
1694 'image)
1695 (muse-publish-escape-specials-in-string (cadr target) 'url))
1696 anchor (muse-publish-escape-specials-in-string
1697 (cddr target) 'url)))
1698 ;; Transform description
1699 (if desc
1700 (setq desc (muse-publish-url-desc desc explicit))
1701 (when orig-url
1702 (setq orig-url (muse-publish-url-desc orig-url explicit))))
1703 ;; Act on URL classification
1704 (cond ((eq type 'anchor-ref)
1705 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1706 ((and unesc-desc (string-match muse-image-regexp unesc-desc))
1707 (let ((ext (or (file-name-extension desc) "")))
1708 (setq desc (muse-publish-escape-specials-in-string unesc-desc
1709 'image))
1710 (setq desc (muse-path-sans-extension desc))
1711 (muse-markup-text 'image-link url desc ext)))
1712 ((string= url "")
1713 desc)
1714 ((eq type 'image)
1715 (let ((ext (or (file-name-extension url) "")))
1716 (setq url (muse-path-sans-extension url))
1717 (if desc
1718 (muse-markup-text 'image-with-desc url ext desc)
1719 (muse-markup-text 'image url ext))))
1720 ((eq type 'link-and-anchor)
1721 (muse-markup-text 'link-and-anchor url anchor
1722 (or desc orig-url)
1723 (muse-path-sans-extension url)))
1724 ((eq type 'link)
1725 (muse-markup-text 'link url (or desc orig-url)))
1727 (or (and (or desc
1728 ;; compare the not-escaped versions of url and
1729 ;; orig-url
1730 (not (string= unesc-url unesc-orig-url)))
1731 (let ((text (muse-markup-text 'url-and-desc url
1732 (or desc orig-url))))
1733 (and (not (string= text ""))
1734 text)))
1735 (muse-markup-text 'url url (or desc orig-url)))))))
1737 (defun muse-publish-insert-url (url &optional desc orig-url explicit)
1738 "Resolve a URL into its final <a href> form."
1739 (delete-region (match-beginning 0) (match-end 0))
1740 (let ((text (muse-publish-url url desc orig-url explicit)))
1741 (when text
1742 (muse-insert-markup text))))
1744 (defun muse-publish-markup-link ()
1745 (let (desc explicit orig-link link)
1746 (setq explicit (save-match-data
1747 (if (string-match muse-explicit-link-regexp
1748 (match-string 0))
1749 t nil)))
1750 (setq orig-link (if explicit (match-string 1) (match-string 0)))
1751 (setq desc (when explicit (match-string 2)))
1752 (setq link (if explicit
1753 (muse-handle-explicit-link orig-link)
1754 (muse-handle-implicit-link orig-link)))
1755 (when (and link
1756 (or explicit
1757 (not (or (eq (char-before (match-beginning 0)) ?\")
1758 (eq (char-after (match-end 0)) ?\")))))
1759 ;; if explicit link has no user-provided description, treat it
1760 ;; as if it were an implicit link
1761 (when (and explicit (not desc))
1762 (setq explicit nil))
1763 (muse-publish-insert-url link desc orig-link explicit))))
1765 (defun muse-publish-markup-url ()
1766 (unless (or (eq (char-before (match-beginning 0)) ?\")
1767 (eq (char-after (match-end 0)) ?\"))
1768 (let ((url (match-string 0)))
1769 (muse-publish-insert-url url nil url))))
1771 ;; Default publishing tags
1773 (defcustom muse-publish-contents-depth 2
1774 "The number of heading levels to include with <contents> tags."
1775 :type 'integer
1776 :group 'muse-publish)
1778 (defun muse-publish-contents-tag (beg end attrs)
1779 (set (make-local-variable 'muse-publish-generate-contents)
1780 (cons (copy-marker (point) t)
1781 (let ((depth (cdr (assoc "depth" attrs))))
1782 (or (and depth (string-to-number depth))
1783 muse-publish-contents-depth)))))
1785 (defun muse-publish-verse-tag (beg end)
1786 (muse-publish-ensure-block beg end)
1787 (save-excursion
1788 (save-restriction
1789 (narrow-to-region beg end)
1790 (goto-char (point-min))
1791 (delete-char 1)
1792 (while (< (point) (point-max))
1793 (insert "> ")
1794 (forward-line))
1795 (if (eq ?\ (char-syntax (char-before)))
1796 (delete-char -1)))))
1798 (defun muse-publish-mark-read-only (beg end)
1799 "Add read-only properties to the given region."
1800 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1801 nil)
1803 (defun muse-publish-mark-link (&optional beg end)
1804 "Indicate that the given region is a Muse link, so that other
1805 markup elements respect it. If a region is not specified, use
1806 the 0th match data to determine it.
1808 This is usually applied to explicit links."
1809 (unless beg (setq beg (match-beginning 0)))
1810 (unless end (setq end (match-end 0)))
1811 (add-text-properties beg end '(muse-link t))
1812 nil)
1814 (defun muse-publish-quote-tag (beg end)
1815 (muse-publish-ensure-block beg)
1816 (save-excursion
1817 (save-restriction
1818 (narrow-to-region beg end)
1819 (let ((quote-regexp "^\\(<\\(/?\\)quote>\\)"))
1820 (muse-insert-markup (muse-markup-text 'begin-quote))
1821 (while (progn
1822 (unless (looking-at (concat "[" muse-regexp-blank "\n]*"
1823 "<quote>"))
1824 (muse-publish-surround-text
1825 (muse-markup-text 'begin-quote-item)
1826 (muse-markup-text 'end-quote-item)
1827 (function
1828 (lambda (indent)
1829 (muse-forward-paragraph)
1830 (goto-char (match-end 0))
1831 (and (< (point) (point-max))
1832 (not (looking-at quote-regexp)))))
1833 nil nil nil
1834 quote-regexp))
1835 (if (>= (point) (point-max))
1837 (and (search-forward "<quote>" nil t)
1838 (muse-goto-tag-end "quote" t)
1839 (progn (forward-line 1) t)
1840 (< (point) (point-max))))))
1841 (goto-char (point-max))
1842 (muse-insert-markup (muse-markup-text 'end-quote))))))
1844 (defun muse-publish-code-tag (beg end)
1845 (muse-publish-escape-specials beg end nil 'literal)
1846 (goto-char beg)
1847 (insert (muse-markup-text 'begin-literal))
1848 (goto-char end)
1849 (insert (muse-markup-text 'end-literal))
1850 (muse-publish-mark-read-only beg (point)))
1852 (defun muse-publish-cite-tag (beg end attrs)
1853 (let* ((type (muse-publish-get-and-delete-attr "type" attrs))
1854 (citetag (cond ((string-equal type "author")
1855 'begin-cite-author)
1856 ((string-equal type "year")
1857 'begin-cite-year)
1859 'begin-cite))))
1860 (goto-char beg)
1861 (insert (muse-markup-text citetag (muse-publishing-directive "bibsource")))
1862 (goto-char end)
1863 (insert (muse-markup-text 'end-cite))
1864 (muse-publish-mark-read-only beg (point))))
1866 (defun muse-publish-src-tag (beg end attrs)
1867 (muse-publish-example-tag beg end))
1869 (defun muse-publish-example-tag (beg end)
1870 (muse-publish-ensure-block beg end)
1871 (muse-publish-escape-specials beg end nil 'example)
1872 (goto-char beg)
1873 (insert (muse-markup-text 'begin-example))
1874 (goto-char end)
1875 (insert (muse-markup-text 'end-example))
1876 (muse-publish-mark-read-only beg (point)))
1878 (defun muse-publish-literal-tag (beg end attrs)
1879 "Ensure that the text between BEG and END is not interpreted later on.
1881 ATTRS is an alist of attributes.
1883 If it contains a \"style\" element, delete the region if the
1884 current style is neither derived from nor equal to this style.
1886 If it contains both a \"style\" element and an \"exact\" element
1887 with the value \"t\", delete the region only if the current style
1888 is exactly this style."
1889 (let* ((style (cdr (assoc "style" attrs)))
1890 (exact (cdr (assoc "exact" attrs)))
1891 (exactp (and (stringp exact) (string= exact "t"))))
1892 (if (or (not style)
1893 (and exactp (equal (muse-style style)
1894 muse-publishing-current-style))
1895 (and (not exactp) (muse-style-derived-p style)))
1896 (muse-publish-mark-read-only beg end)
1897 (delete-region beg end)
1898 (when (and (bolp) (eolp) (not (eobp)))
1899 (delete-char 1)))))
1901 (put 'muse-publish-literal-tag 'muse-dangerous-tag t)
1903 (defun muse-publish-verbatim-tag (beg end)
1904 (muse-publish-escape-specials beg end nil 'verbatim)
1905 (muse-publish-mark-read-only beg end))
1907 (defun muse-publish-br-tag (beg end)
1908 "Insert a line break."
1909 (delete-region beg end)
1910 (muse-insert-markup (muse-markup-text 'line-break)))
1912 (defalias 'muse-publish-class-tag 'ignore)
1913 (defalias 'muse-publish-div-tag 'ignore)
1915 (defun muse-publish-call-tag-on-buffer (tag &optional attrs)
1916 "Transform the current buffer as if it were surrounded by the tag TAG.
1917 If attributes ATTRS are given, pass them to the tag function."
1918 (let ((tag-info (muse-markup-tag-info tag)))
1919 (when tag-info
1920 (let* ((end (progn (goto-char (point-max)) (point-marker)))
1921 (args (list (point-min) end))
1922 (muse-inhibit-style-tags nil))
1923 (when (nth 2 tag-info)
1924 (nconc args (list attrs)))
1925 (apply (nth 4 tag-info) args)
1926 (set-marker end nil)))))
1928 (defun muse-publish-examplify-buffer (&optional attrs)
1929 "Transform the current buffer as if it were an <example> region."
1930 (muse-publish-call-tag-on-buffer "example" attrs))
1932 (defun muse-publish-srcify-buffer (&optional attrs)
1933 "Transform the current buffer as if it were a <src> region."
1934 (muse-publish-call-tag-on-buffer "src" attrs))
1936 (defun muse-publish-versify-buffer (&optional attrs)
1937 "Transform the current buffer as if it were a <verse> region."
1938 (muse-publish-call-tag-on-buffer "verse" attrs)
1939 (muse-publish-markup ""
1940 `((100 ,(concat "^[" muse-regexp-blank "]*> ") 0
1941 muse-publish-markup-verse)))
1942 (goto-char (point-min)))
1944 (defmacro muse-publish-markup-attribute (beg end attrs reinterp &rest body)
1945 "Evaluate BODY within the bounds of BEG and END.
1946 ATTRS is an alist. Only the \"markup\" element of ATTRS is acted
1949 If it is omitted, publish the region with the normal Muse rules.
1950 If RE-INTERP is specified, this is done immediately in a new
1951 publishing process. Currently, RE-INTERP is specified only by
1952 the <include> tag.
1954 If \"nil\", do not mark up the region at all, but prevent it from
1955 being further interpreted by Muse.
1957 If \"example\", treat the region as if it was surrounded by the
1958 <example> tag.
1960 If \"src\", treat the region as if it was surrounded by the
1961 <src> tag.
1963 If \"verse\", treat the region as if it was surrounded by the
1964 <verse> tag, to preserve newlines.
1966 Otherwise, it should be the name of a function to call in the
1967 narrowed region after evaluating BODY. The function should
1968 take the ATTRS parameter.
1970 BEG is modified to be the start of the published markup."
1971 (let ((attrs-sym (make-symbol "attrs"))
1972 (markup (make-symbol "markup"))
1973 (markup-function (make-symbol "markup-function")))
1974 `(let* ((,attrs-sym ,attrs)
1975 (,markup (muse-publish-get-and-delete-attr "markup" ,attrs-sym)))
1976 (save-restriction
1977 (narrow-to-region ,beg ,end)
1978 (goto-char (point-min))
1979 ,@body
1980 (if (not ,markup)
1981 (when ,reinterp
1982 (muse-publish-markup-region (point-min) (point-max))
1983 (muse-publish-mark-read-only (point-min) (point-max))
1984 (goto-char (point-max)))
1985 (let ((,markup-function (read ,markup)))
1986 (cond ((eq ,markup-function 'example)
1987 (setq ,markup-function #'muse-publish-examplify-buffer))
1988 ((eq ,markup-function 'src)
1989 (setq ,markup-function #'muse-publish-srcify-buffer))
1990 ((eq ,markup-function 'verse)
1991 (setq ,markup-function #'muse-publish-versify-buffer))
1992 ((and ,markup-function (not (functionp ,markup-function)))
1993 (error "Invalid markup function `%s'" ,markup))
1994 (t nil))
1995 (if ,markup-function
1996 (funcall ,markup-function ,attrs-sym)
1997 (muse-publish-mark-read-only (point-min) (point-max))
1998 (goto-char (point-max)))))))))
2000 (put 'muse-publish-markup-attribute 'lisp-indent-function 4)
2001 (put 'muse-publish-markup-attribute 'edebug-form-spec
2002 '(sexp sexp sexp sexp body))
2004 (defun muse-publish-lisp-tag (beg end attrs)
2005 (muse-publish-markup-attribute beg end attrs nil
2006 (save-excursion
2007 (save-restriction
2008 (let ((str (muse-eval-lisp
2009 (prog1
2010 (concat "(progn "
2011 (buffer-substring-no-properties (point-min)
2012 (point-max))
2013 ")")
2014 (delete-region (point-min) (point-max))
2015 (widen)))))
2016 (set-text-properties 0 (length str) nil str)
2017 (insert str))))))
2019 (put 'muse-publish-lisp-tag 'muse-dangerous-tag t)
2021 (defun muse-publish-command-tag (beg end attrs)
2022 (muse-publish-markup-attribute beg end attrs nil
2023 (while (looking-at "\\s-*$")
2024 (forward-line))
2025 (let ((interp (muse-publish-get-and-delete-attr "interp" attrs)))
2026 (if interp
2027 (shell-command-on-region (point) (point-max) interp t t)
2028 (shell-command
2029 (prog1
2030 (buffer-substring-no-properties (point) (point-max))
2031 (delete-region (point-min) (point-max)))
2032 t)))
2033 ;; make sure there is a newline at end
2034 (goto-char (point-max))
2035 (forward-line 0)
2036 (unless (looking-at "\\s-*$")
2037 (goto-char (point-max))
2038 (insert ?\n))
2039 (goto-char (point-min))))
2041 (put 'muse-publish-command-tag 'muse-dangerous-tag t)
2043 (defun muse-publish-perl-tag (beg end attrs)
2044 (muse-publish-command-tag beg end
2045 (cons (cons "interp" (executable-find "perl"))
2046 attrs)))
2048 (put 'muse-publish-perl-tag 'muse-dangerous-tag t)
2050 (defun muse-publish-php-tag (beg end attrs)
2051 (muse-publish-command-tag beg end
2052 (cons (cons "interp" (executable-find "php"))
2053 attrs)))
2055 (put 'muse-publish-php-tag 'muse-dangerous-tag t)
2057 (defun muse-publish-python-tag (beg end attrs)
2058 (muse-publish-command-tag beg end
2059 (cons (cons "interp" (executable-find "python"))
2060 attrs)))
2062 (put 'muse-publish-python-tag 'muse-dangerous-tag t)
2064 (defun muse-publish-ruby-tag (beg end attrs)
2065 (muse-publish-command-tag beg end
2066 (cons (cons "interp" (executable-find "ruby"))
2067 attrs)))
2069 (put 'muse-publish-ruby-tag 'muse-dangerous-tag t)
2071 (defun muse-publish-comment-tag (beg end)
2072 (if (null muse-publish-comments-p)
2073 (delete-region beg end)
2074 (goto-char end)
2075 (muse-insert-markup (muse-markup-text 'comment-end))
2076 (muse-publish-mark-read-only beg end)
2077 (goto-char beg)
2078 (muse-insert-markup (muse-markup-text 'comment-begin))))
2080 (defun muse-publish-include-tag (beg end attrs)
2081 "Include the named file at the current location during publishing.
2083 <include file=\"...\" markup=\"...\">
2085 The `markup' attribute controls how this file is marked up after
2086 being inserted. See `muse-publish-markup-attribute' for an
2087 explanation of how it works."
2088 (let ((filename (muse-publish-get-and-delete-attr "file" attrs))
2089 (muse-publishing-directives (copy-alist muse-publishing-directives)))
2090 (if filename
2091 (setq filename (expand-file-name
2092 filename
2093 (file-name-directory muse-publishing-current-file)))
2094 (error "No file attribute specified in <include> tag"))
2095 (muse-publish-markup-attribute beg end attrs t
2096 (muse-insert-file-contents filename))))
2098 (put 'muse-publish-include-tag 'muse-dangerous-tag t)
2100 (defun muse-publish-mark-up-tag (beg end attrs)
2101 "Run an Emacs Lisp function on the region delimted by this tag.
2103 <markup function=\"...\" style=\"...\" exact=\"...\">
2105 The optional \"function\" attribute controls how this section is
2106 marked up. If used, it should be the name of a function to call
2107 with the buffer narrowed to the delimited region. Note that no
2108 further marking-up will be performed on this region.
2110 If \"function\" is omitted, use the standard Muse markup function.
2111 This is useful for marking up content in headers and footers.
2113 The optional \"style\" attribute causes the region to be deleted
2114 if the current style is neither derived from nor equal to this
2115 style.
2117 If both a \"style\" attribute and an \"exact\" attribute are
2118 provided, and \"exact\" is \"t\", delete the region only if the
2119 current style is exactly this style."
2120 (let* ((style (cdr (assoc "style" attrs)))
2121 (exact (cdr (assoc "exact" attrs)))
2122 (exactp (and (stringp exact) (string= exact "t"))))
2123 (if (or (not style)
2124 (and exactp (equal (muse-style style)
2125 muse-publishing-current-style))
2126 (and (not exactp) (muse-style-derived-p style)))
2127 (let* ((function (cdr (assoc "function" attrs)))
2128 (muse-publish-use-header-footer-tags nil)
2129 (markup-function (and function (intern-soft function))))
2130 (if (and markup-function (functionp markup-function))
2131 (save-restriction
2132 (narrow-to-region beg end)
2133 (funcall markup-function)
2134 (goto-char (point-max)))
2135 (let ((muse-publish-inhibit-style-hooks t))
2136 (muse-publish-markup-region beg end)))
2137 (muse-publish-mark-read-only beg (point)))
2138 (delete-region beg end))))
2140 (put 'muse-publish-mark-up-tag 'muse-dangerous-tag t)
2142 ;; Miscellaneous helper functions
2144 (defun muse-publish-strip-URL (string &rest ignored)
2145 "If the text \"URL:\" exists at the beginning of STRING, remove it.
2146 The text is removed regardless of whether and part of it is uppercase."
2147 (save-match-data
2148 (if (string-match "\\`[uU][rR][lL]:\\(.+\\)\\'" string)
2149 (match-string 1 string)
2150 string)))
2152 (defun muse-publish-markup-type (category default-func)
2153 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
2154 (funcall (or rule default-func))))
2156 (defun muse-published-buffer-contents (buffer)
2157 (with-current-buffer buffer
2158 (goto-char (point-min))
2159 (let ((beg (and (search-forward "Emacs Muse begins here")
2160 (muse-line-end-position)))
2161 (end (and (search-forward "Emacs Muse ends here")
2162 (muse-line-beginning-position))))
2163 (buffer-substring-no-properties beg end))))
2165 (defun muse-published-contents (file)
2166 (when (file-readable-p file)
2167 (muse-with-temp-buffer
2168 (muse-insert-file-contents file)
2169 (muse-published-buffer-contents (current-buffer)))))
2171 (defun muse-publish-transform-output
2172 (file temp-file output-path name gen-func &rest cleanup-exts)
2173 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
2174 (setq file (muse-page-name file))
2175 (message "Generating %s output for %s..." name file)
2176 (if (not (funcall gen-func temp-file output-path))
2177 (message "Generating %s from %s...failed" name file)
2178 (message "Generating %s output for %s...done" name file)
2179 (muse-delete-file-if-exists temp-file)
2180 (dolist (ext cleanup-exts)
2181 (muse-delete-file-if-exists
2182 (expand-file-name (concat file ext)
2183 (file-name-directory output-path))))
2184 (message "Wrote %s" output-path)))
2186 (defun muse-publish-read-only (string)
2187 (let ((end (1- (length string))))
2188 (add-text-properties 0 end
2189 '(rear-nonsticky (read-only) read-only t)
2190 string)
2191 string))
2193 ;;; muse-publish.el ends here