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