publish: Permit "---" to be marked up the same as "--"
[muse-el.git] / lisp / muse-publish.el
blobe52933c7109b342d826b4e79a777a0375afff3eb
1 ;;; muse-publish.el --- base publishing implementation
3 ;; Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
7 ;; Emacs Muse is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published
9 ;; by the Free Software Foundation; either version 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-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 ("lisp" t t nil muse-publish-lisp-tag)
274 ("class" t t nil muse-publish-class-tag)
275 ("command" t t nil muse-publish-command-tag)
276 ("perl" t t nil muse-publish-perl-tag)
277 ("php" t t nil muse-publish-php-tag)
278 ("python" t t nil muse-publish-python-tag)
279 ("ruby" t t nil muse-publish-ruby-tag)
280 ("comment" t nil nil muse-publish-comment-tag)
281 ("include" nil t nil muse-publish-include-tag)
282 ("markup" t t nil muse-publish-mark-up-tag)
283 ("cite" t t nil muse-publish-cite-tag))
284 "A list of tag specifications, for specially marking up text.
285 XML-style tags are the best way to add custom markup to Muse.
286 This is easily accomplished by customizing this list of markup tags.
288 For each entry, the name of the tag is given, whether it expects
289 a closing tag and/or an optional set of attributes, whether it is
290 nestable, and a function that performs whatever action is desired
291 within the delimited region.
293 The tags themselves are deleted during publishing, before the
294 function is called. The function is called with three arguments,
295 the beginning and end of the region surrounded by the tags. If
296 properties are allowed, they are passed as a third argument in
297 the form of an alist. The `end' argument to the function is
298 always a marker.
300 Point is always at the beginning of the region within the tags, when
301 the function is called. Wherever point is when the function finishes
302 is where tag markup will resume.
304 These tag rules are processed once at the beginning of markup, and
305 once at the end, to catch any tags which may have been inserted
306 in-between."
307 :type '(repeat (list (string :tag "Markup tag")
308 (boolean :tag "Expect closing tag" :value t)
309 (boolean :tag "Parse attributes" :value nil)
310 (boolean :tag "Nestable" :value nil)
311 function))
312 :group 'muse-publish)
314 (defcustom muse-publish-markup-header-footer-tags
315 '(("lisp" t t nil muse-publish-lisp-tag)
316 ("markup" t t nil muse-publish-mark-up-tag))
317 "Tags used when publishing headers and footers.
318 See `muse-publish-markup-tags' for details."
319 :type '(repeat (list (string :tag "Markup tag")
320 (boolean :tag "Expect closing tag" :value t)
321 (boolean :tag "Parse attributes" :value nil)
322 (boolean :tag "Nestable" :value nil)
323 function))
324 :group 'muse-publish)
326 (defcustom muse-publish-markup-specials nil
327 "A table of characters which must be represented specially."
328 :type '(alist :key-type character :value-type string)
329 :group 'muse-publish)
331 (defvar muse-publishing-p nil
332 "Set to t while a page is being published.")
333 (defvar muse-batch-publishing-p nil
334 "Set to t while a page is being batch published.")
335 (defvar muse-publishing-styles nil
336 "The publishing styles that Muse recognizes.
337 This is automatically generated when loading publishing styles.")
338 (defvar muse-publishing-current-file nil
339 "The file that is currently being published.")
340 (defvar muse-publishing-current-output-path nil
341 "The path where the current file will be published to.")
342 (defvar muse-publishing-current-style nil
343 "The style of the file that is currently being published.")
344 (defvar muse-publishing-directives nil
345 "An alist of publishing directives from the top of a file.")
346 (defvar muse-publish-generate-contents nil
347 "Non-nil if a table of contents should be generated.
348 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
349 tell where the <contents> was seen, and to what depth the
350 contents were requested.")
351 (defvar muse-publishing-last-position nil
352 "Last position of the point when publishing.
353 This is used to make sure that publishing doesn't get stalled.")
355 (defvar muse-publish-inhibit-style-hooks nil
356 "If non-nil, do not call the :before or :before-end hooks when publishing.")
358 (defvar muse-publish-use-header-footer-tags nil
359 "If non-nil, use `muse-publish-markup-header-footer-tags' for looking up
360 tags. Otherwise, use `muse-publish-markup-tags'.")
362 (defvar muse-inhibit-style-tags nil
363 "If non-nil, do not search for style-specific tags.
364 This is used when publishing headers and footers.")
366 ;; Functions for handling style information
368 (defsubst muse-style (&optional style)
369 "Resolve the given STYLE into a Muse style, if it is a string."
370 (if (null style)
371 muse-publishing-current-style
372 (if (stringp style)
373 (assoc style muse-publishing-styles)
374 (muse-assert (consp style))
375 style)))
377 (defun muse-define-style (name &rest elements)
378 (let ((entry (assoc name muse-publishing-styles)))
379 (if entry
380 (setcdr entry elements)
381 (setq muse-publishing-styles
382 (cons (append (list name) elements)
383 muse-publishing-styles)))))
385 (defun muse-derive-style (new-name base-name &rest elements)
386 (apply 'muse-define-style new-name
387 (append elements (list :base base-name))))
389 (defsubst muse-get-keyword (keyword list &optional direct)
390 (let ((value (cadr (memq keyword list))))
391 (if (and (not direct) (symbolp value))
392 (symbol-value value)
393 value)))
395 (defun muse-style-elements-list (elem &optional style)
396 "Return a list all references to ELEM in STYLE, including base styles.
397 If STYLE is not specified, use current style."
398 (let (base elements)
399 (while style
400 (setq style (muse-style style))
401 (setq elements (append elements
402 (muse-get-keyword elem style)))
403 (setq style (muse-get-keyword :base style)))
404 elements))
406 (defun muse-style-element (elem &optional style direct)
407 "Search for ELEM in STYLE, including base styles.
408 If STYLE is not specified, use current style."
409 (setq style (muse-style style))
410 (let ((value (muse-get-keyword elem style direct)))
411 (if value
412 value
413 (let ((base (muse-get-keyword :base style)))
414 (if base
415 (muse-style-element elem base direct))))))
417 (defun muse-style-derived-p-1 (base style)
418 "Internal function used by `muse-style-derived-p'."
419 (if (and (stringp style)
420 (string= style base))
422 (setq style (muse-style style))
423 (let ((value (muse-get-keyword :base style)))
424 (when value
425 (muse-style-derived-p base value)))))
427 (defun muse-style-derived-p (base &optional style)
428 "Return non-nil if STYLE is equal to or derived from BASE,
429 non-nil otherwise.
431 BASE should be a string."
432 (unless style
433 (setq style (muse-style)))
434 (when (and (consp style)
435 (stringp (car style)))
436 (setq style (car style)))
437 (muse-style-derived-p-1 base style))
439 (defun muse-find-markup-element (keyword ident style)
440 (let ((def (assq ident (muse-style-element keyword style))))
441 (if def
442 (cdr def)
443 (let ((base (muse-style-element :base style)))
444 (if base
445 (muse-find-markup-element keyword ident base))))))
447 (defun muse-markup-text (ident &rest args)
448 "Insert ARGS into the text markup associated with IDENT.
449 If the markup text has sections like %N%, this will be replaced
450 with the N-1th argument in ARGS. After that, `format' is applied
451 to the text with ARGS as parameters."
452 (let ((text (muse-find-markup-element :strings ident (muse-style))))
453 (if (and text args)
454 (progn
455 (let (start repl-text)
456 (while (setq start (string-match "%\\([1-9][0-9]*\\)%" text start))
457 ;; escape '%' in the argument text, since we will be
458 ;; using format on it
459 (setq repl-text (muse-replace-regexp-in-string
460 "%" "%%"
461 (nth (1- (string-to-number
462 (match-string 1 text))) args)
463 t t)
464 start (+ start (length repl-text))
465 text (replace-match repl-text t t text))))
466 (apply 'format text args))
467 (or text ""))))
469 (defun muse-insert-markup (&rest args)
470 (let ((beg (point)))
471 (apply 'insert args)
472 (muse-publish-mark-read-only beg (point))))
474 (defun muse-find-markup-tag (keyword tagname style)
475 (let ((def (assoc tagname (muse-style-element keyword style))))
476 (or def
477 (let ((base (muse-style-element :base style)))
478 (if base
479 (muse-find-markup-tag keyword tagname base))))))
481 (defun muse-markup-tag-info (tagname &rest args)
482 (let ((tag-info (and (not muse-inhibit-style-tags)
483 (muse-find-markup-tag :tags tagname (muse-style)))))
484 (or tag-info
485 (assoc tagname
486 (if muse-publish-use-header-footer-tags
487 muse-publish-markup-header-footer-tags
488 muse-publish-markup-tags)))))
490 (defsubst muse-markup-function (category)
491 (let ((func (muse-find-markup-element :functions category (muse-style))))
492 (or func
493 (cdr (assq category muse-publish-markup-functions)))))
495 ;; Publishing routines
497 (defun muse-publish-markup (name rules)
498 (let* ((case-fold-search nil)
499 (inhibit-read-only t)
500 (limit (* (length rules) (point-max)))
501 (verbose (and muse-publish-report-threshhold
502 (> (point-max) muse-publish-report-threshhold)))
503 (base 0))
504 (while rules
505 (goto-char (point-min))
506 (let ((regexp (nth 1 (car rules)))
507 (group (nth 2 (car rules)))
508 (repl (nth 3 (car rules)))
509 pos)
510 (setq muse-publishing-last-position nil)
511 (if (symbolp regexp)
512 (setq regexp (symbol-value regexp)))
513 (if (and verbose (not muse-batch-publishing-p))
514 (message "Publishing %s...%d%%" name
515 (* (/ (float (+ (point) base)) limit) 100)))
516 (while (and regexp (setq pos (re-search-forward regexp nil t)))
517 (if (and verbose (not muse-batch-publishing-p))
518 (message "Publishing %s...%d%%" name
519 (* (/ (float (+ (point) base)) limit) 100)))
520 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
521 (match-beginning group)
522 (get-text-property (match-beginning group) 'read-only))
523 (let* (func
524 (text (cond
525 ((and (symbolp repl)
526 (setq func (muse-markup-function repl)))
527 (funcall func))
528 ((functionp repl)
529 (funcall repl))
530 ((symbolp repl)
531 (symbol-value repl))
532 (t repl))))
533 (if (stringp text)
534 (replace-match text t))))
535 (if (and muse-publishing-last-position
536 (= pos muse-publishing-last-position))
537 (if (eobp)
538 (setq regexp nil)
539 (forward-char 1)))
540 (setq muse-publishing-last-position pos)))
541 (setq rules (cdr rules)
542 base (+ base (point-max))))
543 (if (and verbose (not muse-batch-publishing-p))
544 (message "Publishing %s...done" name))))
546 (defun muse-insert-file-or-string (file-or-string &optional title)
547 (let ((beg (point)) end)
548 (if (and (not (string-equal file-or-string ""))
549 (not (string-match "\n" file-or-string))
550 (file-readable-p file-or-string))
551 (setq end (+ beg
552 (cadr (muse-insert-file-contents file-or-string))))
553 (insert file-or-string)
554 (setq end (point)))
555 (save-restriction
556 (narrow-to-region beg end)
557 (remove-text-properties (point-min) (point-max)
558 '(read-only nil rear-nonsticky nil))
559 (goto-char (point-min))
560 (let ((muse-inhibit-style-tags t)
561 (muse-publish-use-header-footer-tags t))
562 (muse-publish-markup (or title "")
563 '((100 muse-tag-regexp 0
564 muse-publish-markup-tag)))))))
566 (defun muse-style-run-hooks (keyword style &rest args)
567 (catch 'handled
568 (let ((cache nil))
569 (while (and style
570 (setq style (muse-style style)))
571 (let ((func (muse-style-element keyword style t)))
572 (when (and func
573 (not (member func cache)))
574 (setq cache (cons func cache))
575 (when (apply func args)
576 (throw 'handled t))))
577 (setq style (muse-style-element :base style))))))
579 (defun muse-publish-markup-region (beg end &optional title style)
580 "Apply the given STYLE's markup rules to the given region.
581 TITLE is used when indicating the publishing progress; it may be nil.
583 The point is guaranteed to be at END if the routine terminates
584 normally."
585 (unless title (setq title ""))
586 (unless style
587 (or (setq style muse-publishing-current-style)
588 (error "Cannot find any publishing styles to use")))
589 (save-restriction
590 (narrow-to-region beg end)
591 (let ((muse-publish-generate-contents nil))
592 (unless muse-publish-inhibit-style-hooks
593 (muse-style-run-hooks :before style))
594 (muse-publish-markup
595 title
596 (sort (copy-alist (append muse-publish-markup-regexps
597 (muse-style-elements-list :regexps style)))
598 (function
599 (lambda (l r)
600 (< (car l) (car r))))))
601 (unless muse-publish-inhibit-style-hooks
602 (muse-style-run-hooks :before-end style))
603 (muse-publish-escape-specials (point-min) (point-max) nil 'document))
604 (goto-char (point-max))))
606 (defun muse-publish-markup-buffer (title style)
607 "Apply the given STYLE's markup rules to the current buffer."
608 (setq style (muse-style style))
609 (let ((style-header (muse-style-element :header style))
610 (style-footer (muse-style-element :footer style))
611 (muse-publishing-current-style style)
612 (muse-publishing-directives
613 (list (cons "title" title)
614 (cons "author" (user-full-name))
615 (cons "date" (format-time-string
616 muse-publish-date-format
617 (if muse-publishing-current-file
618 (nth 5 (file-attributes
619 muse-publishing-current-file))
620 (current-time))))))
621 (muse-publishing-p t)
622 (inhibit-read-only t))
623 (run-hooks 'muse-update-values-hook)
624 (run-hooks 'muse-before-publish-hook)
625 (muse-publish-markup-region (point-min) (point-max) title style)
626 (goto-char (point-min))
627 (when style-header
628 (muse-insert-file-or-string style-header title))
629 (goto-char (point-max))
630 (when style-footer
631 (muse-insert-file-or-string style-footer title))
632 (muse-style-run-hooks :after style)
633 (run-hooks 'muse-after-publish-hook)))
635 (defun muse-publish-markup-string (string &optional style)
636 "Markup STRING using the given STYLE's markup rules."
637 (setq style (muse-style style))
638 (muse-with-temp-buffer
639 (insert string)
640 (let ((muse-publishing-current-style style)
641 (muse-publishing-p t))
642 (muse-publish-markup "*string*" (muse-style-element :rules style)))
643 (buffer-string)))
645 ;; Commands for publishing files
647 (defun muse-publish-get-style (&optional styles)
648 (unless styles (setq styles muse-publishing-styles))
649 (if (= 1 (length styles))
650 (car styles)
651 (when (catch 'different
652 (let ((first (car (car styles))))
653 (dolist (style (cdr styles))
654 (unless (equal first (car style))
655 (throw 'different t)))))
656 (setq styles (muse-collect-alist
657 styles
658 (funcall muse-completing-read-function
659 "Publish with style: " styles nil t))))
660 (if (or (= 1 (length styles))
661 (not (muse-get-keyword :path (car styles))))
662 (car styles)
663 (setq styles (mapcar (lambda (style)
664 (cons (muse-get-keyword :path style)
665 style))
666 styles))
667 (cdr (assoc (funcall muse-completing-read-function
668 "Publish to directory: " styles nil t)
669 styles)))))
671 (defsubst muse-publish-get-output-dir (style)
672 (let ((default-directory (or (muse-style-element :path style)
673 default-directory)))
674 (muse-read-directory-name "Publish to directory: " nil default-directory)))
676 (defsubst muse-publish-get-info ()
677 (let ((style (muse-publish-get-style)))
678 (list style (muse-publish-get-output-dir style)
679 current-prefix-arg)))
681 (defsubst muse-publish-output-name (&optional file style)
682 (setq style (muse-style style))
683 (concat (muse-style-element :prefix style)
684 (muse-page-name file)
685 (muse-style-element :suffix style)))
687 (defsubst muse-publish-output-file (file &optional output-dir style)
688 (setq style (muse-style style))
689 (if output-dir
690 (expand-file-name (muse-publish-output-name file style) output-dir)
691 (concat (file-name-directory file)
692 (muse-publish-output-name file style))))
694 (defsubst muse-publish-link-name (&optional file style)
695 "Take FILE and add :prefix and either :link-suffix or :suffix from STYLE.
696 We assume that FILE is a Muse file.
698 We call `muse-page-name' on FILE to remove the directory part of
699 FILE and any extensions that are in `muse-ignored-extensions'."
700 (setq style (muse-style style))
701 (concat (muse-style-element :prefix style)
702 (muse-page-name file)
703 (or (muse-style-element :link-suffix style)
704 (muse-style-element :suffix style))))
706 (defsubst muse-publish-link-file (file &optional style)
707 "Turn FILE into a URL.
709 If FILE exists on the system as-is, return it without
710 modification. In the case of wanting to link to Muse files when
711 `muse-file-extension' is nil, you should load muse-project.el.
713 Otherwise, assume that it is a Muse file and call
714 `muse-publish-link-name' to add :prefix, :link-suffix, :suffix,
715 and removing ignored file extensions, but preserving the
716 directory part of FILE."
717 (setq style (muse-style style))
718 (if (file-exists-p file)
719 file
720 (concat (file-name-directory file)
721 (muse-publish-link-name file style))))
723 (defsubst muse-publish-link-page (page)
724 "Turn PAGE into a URL.
726 This is called by `muse-publish-classify-url' to figure out what
727 a link to another file or Muse page should look like.
729 If muse-project.el is loaded, call `muse-project-link-page' for this.
730 Otherwise, call `muse-publish-link-file'."
731 (if (fboundp 'muse-project-link-page)
732 (muse-project-link-page page)
733 (muse-publish-link-file page)))
735 (defmacro muse-publish-ensure-block (beg &optional end)
736 "Ensure that block-level markup at BEG is published with at least one
737 preceding blank line. BEG must be an unquoted symbol that contains a
738 position or marker. BEG is modified to be the new position.
739 The point is left at the new value of BEG.
741 Additionally, make sure that BEG is placed on a blank line.
743 If END is given, make sure that it is placed on a blank line. In
744 order to achieve this, END must be an unquoted symbol that
745 contains a marker. This is the case with Muse tag functions."
746 `(progn
747 (goto-char ,beg)
748 (cond ((not (bolp)) (insert "\n\n"))
749 ((eq (point) (point-min)) nil)
750 ((prog2 (backward-char) (bolp) (forward-char)) nil)
751 (t (insert "\n")))
752 (unless (and (bolp) (eolp))
753 (insert "\n")
754 (backward-char))
755 (setq ,beg (point))
756 (when (markerp ,end)
757 (goto-char ,end)
758 (unless (and (bolp) (eolp))
759 (insert-before-markers "\n")))
760 (goto-char ,beg)))
762 ;;;###autoload
763 (defun muse-publish-region (beg end &optional title style)
764 "Apply the given STYLE's markup rules to the given region.
765 The result is placed in a new buffer that includes TITLE in its name."
766 (interactive "r")
767 (when (interactive-p)
768 (unless title (setq title (read-string "Title: ")))
769 (unless style (setq style (muse-publish-get-style))))
770 (let ((muse-publishing-current-style style)
771 (muse-publishing-p t)
772 (text (buffer-substring beg end))
773 (buf (generate-new-buffer (concat "*Muse: " title "*"))))
774 (with-current-buffer buf
775 (insert text)
776 (muse-publish-markup-buffer title style)
777 (goto-char (point-min))
778 (let ((inhibit-read-only t))
779 (remove-text-properties (point-min) (point-max)
780 '(rear-nonsticky nil read-only nil))))
781 (pop-to-buffer buf)))
783 ;;;###autoload
784 (defun muse-publish-file (file style &optional output-dir force)
785 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
786 If the argument FORCE is nil, each file is only published if it is
787 newer than the published version. If the argument FORCE is non-nil,
788 the file is published no matter what."
789 (interactive (cons (read-file-name "Publish file: ")
790 (muse-publish-get-info)))
791 (let ((style-name style))
792 (setq style (muse-style style))
793 (unless style
794 (error "There is no style '%s' defined" style-name)))
795 (let* ((output-path (muse-publish-output-file file output-dir style))
796 (output-suffix (muse-style-element :osuffix style))
797 (muse-publishing-current-file file)
798 (muse-publishing-current-output-path output-path)
799 (target (if output-suffix
800 (concat (muse-path-sans-extension output-path)
801 output-suffix)
802 output-path))
803 (threshhold (nth 7 (file-attributes file))))
804 (if (not threshhold)
805 (message "Please save %s before publishing" file)
806 (when (or force (file-newer-than-file-p file target))
807 (if (and muse-publish-report-threshhold
808 (> threshhold
809 muse-publish-report-threshhold))
810 (message "Publishing %s ..." file))
811 (muse-with-temp-buffer
812 (muse-insert-file-contents file)
813 (muse-publish-markup-buffer (muse-page-name file) style)
814 (when (muse-write-file output-path)
815 (muse-style-run-hooks :final style file output-path target)))
816 t))))
818 ;;;###autoload
819 (defun muse-publish-this-file (style output-dir &optional force)
820 "Publish the currently-visited file.
821 Prompt for both the STYLE and OUTPUT-DIR if they are not
822 supplied."
823 (interactive (muse-publish-get-info))
824 (if buffer-file-name
825 (let ((muse-current-output-style (list :base (car style)
826 :path output-dir)))
827 (unless (muse-publish-file buffer-file-name style output-dir force)
828 (message (concat "The published version is up-to-date; use"
829 " C-u C-c C-T to force an update."))))
830 (message "This buffer is not associated with any file")))
832 (defun muse-batch-publish-files ()
833 "Publish Muse files in batch mode."
834 (let ((muse-batch-publishing-p t)
835 muse-current-output-style
836 style output-dir)
837 ;; don't activate VC when publishing files
838 (setq vc-handled-backends nil)
839 (setq style (car command-line-args-left)
840 command-line-args-left (cdr command-line-args-left)
841 output-dir (car command-line-args-left)
842 output-dir
843 (if (string-match "\\`--output-dir=" output-dir)
844 (prog1
845 (substring output-dir (match-end 0))
846 (setq command-line-args-left (cdr command-line-args-left))))
847 muse-current-output-style (list :base style :path output-dir))
848 (setq auto-mode-alist
849 (delete (cons (concat "\\." muse-file-extension "\\'")
850 'muse-mode-choose-mode)
851 auto-mode-alist))
852 (dolist (file command-line-args-left)
853 (muse-publish-file file style output-dir t))))
855 ;; Default publishing rules
857 (defun muse-publish-section-close (depth)
858 "Seach forward for the closing tag of given DEPTH."
859 (let (not-end)
860 (save-excursion
861 (while (and (setq not-end (re-search-forward
862 (concat "^\\*\\{1," (number-to-string depth)
863 "\\}\\s-+")
864 nil t))
865 (get-text-property (match-beginning 0) 'read-only)))
866 (if not-end
867 (forward-line 0)
868 (goto-char (point-max)))
869 (cond ((not (eq (char-before) ?\n))
870 (insert "\n\n"))
871 ((not (eq (char-before (1- (point))) ?\n))
872 (insert "\n")))
873 (muse-insert-markup (muse-markup-text 'section-close depth))
874 (insert "\n"))))
876 (defun muse-publish-markup-directive (&optional name value)
877 (unless name (setq name (match-string 1)))
878 (unless value (setq value (match-string 2)))
879 (let ((elem (assoc name muse-publishing-directives)))
880 (if elem
881 (setcdr elem value)
882 (setq muse-publishing-directives
883 (cons (cons name value)
884 muse-publishing-directives))))
885 ;; Make sure we don't ever try to move the point forward (past the
886 ;; beginning of buffer) while we're still searching for directives.
887 (setq muse-publishing-last-position nil)
888 (delete-region (match-beginning 0) (match-end 0)))
890 (defsubst muse-publishing-directive (name)
891 (cdr (assoc name muse-publishing-directives)))
893 (defmacro muse-publish-get-and-delete-attr (attr attrs)
894 "Delete attribute ATTR from ATTRS only once, destructively.
896 This function returns the matching attribute value, if found."
897 (let ((last (make-symbol "last"))
898 (found (make-symbol "found"))
899 (vals (make-symbol "vals")))
900 `(let ((,vals ,attrs))
901 (if (string= (caar ,vals) ,attr)
902 (prog1 (cdar ,vals)
903 (setq ,attrs (cdr ,vals)))
904 (let ((,last ,vals)
905 (,found nil))
906 (while ,vals
907 (setq ,vals (cdr ,vals))
908 (when (string= (caar ,vals) ,attr)
909 (setq ,found (cdar ,vals))
910 (setcdr ,last (cdr ,vals))
911 (setq ,vals nil))
912 (setq ,last ,vals))
913 ,found)))))
915 (defun muse-publish-markup-anchor ()
916 (unless (get-text-property (match-end 1) 'muse-link)
917 (let ((text (muse-markup-text 'anchor (match-string 2))))
918 (unless (string= text "")
919 (save-match-data
920 (skip-chars-forward (concat muse-regexp-blank "\n"))
921 (muse-insert-markup text)))
922 (match-string 1))))
924 (defun muse-publish-markup-comment ()
925 (if (null muse-publish-comments-p)
927 (goto-char (match-end 0))
928 (muse-insert-markup (muse-markup-text 'comment-end))
929 (if (match-beginning 1)
930 (progn
931 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
932 (delete-region (match-beginning 0) (match-beginning 1)))
933 (delete-region (match-beginning 0) (match-end 0)))
934 (goto-char (match-beginning 0))
935 (muse-insert-markup (muse-markup-text 'comment-begin))))
937 (defun muse-publish-markup-tag ()
938 (let ((tag-info (muse-markup-tag-info (match-string 1))))
939 (when (and tag-info
940 (not (get-text-property (match-beginning 0) 'read-only)))
941 (let ((closed-tag (match-string 3))
942 (start (match-beginning 0))
943 (beg (point))
944 end attrs)
945 (when (nth 2 tag-info)
946 (let ((attrstr (match-string 2)))
947 (while (and attrstr
948 (string-match (concat "\\([^"
949 muse-regexp-blank
950 "=\n]+\\)\\(=\"\\"
951 "([^\"]+\\)\"\\)?")
952 attrstr))
953 (let ((attr (cons (downcase
954 (muse-match-string-no-properties 1 attrstr))
955 (muse-match-string-no-properties 3 attrstr))))
956 (setq attrstr (replace-match "" t t attrstr))
957 (if attrs
958 (nconc attrs (list attr))
959 (setq attrs (list attr)))))))
960 (if (and (cadr tag-info) (not closed-tag))
961 (if (muse-goto-tag-end (car tag-info) (nth 3 tag-info))
962 (delete-region (match-beginning 0) (point))
963 (setq tag-info nil)))
964 (when tag-info
965 (setq end (point-marker))
966 (delete-region start beg)
967 (goto-char start)
968 (let ((args (list start end)))
969 (if (nth 2 tag-info)
970 (nconc args (list attrs)))
971 (let ((muse-inhibit-style-tags nil))
972 ;; remove the inhibition
973 (apply (nth 4 tag-info) args)))
974 (set-marker end nil)))))
975 nil)
977 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
978 "Escape specials from BEG to END using style-specific :specials.
979 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
980 CONTEXT is used to figure out what kind of specials to escape.
982 The following contexts exist in Muse.
983 'underline _underlined text_
984 'literal =monospaced text= or <code> region (monospaced, escaped)
985 'emphasis *emphasized text*
986 'email email@example.com
987 'url http://example.com
988 'url-desc [[...][description of an explicit link]]
989 'image [[image.png]]
990 'example <example> region (monospaced, block context, escaped)
991 'verbatim <verbatim> region (escaped)
992 'footnote footnote text
993 'document normal text"
994 (let ((specials (muse-style-element :specials nil t)))
995 (cond ((functionp specials)
996 (setq specials (funcall specials context)))
997 ((symbolp specials)
998 (setq specials (symbol-value specials))))
999 (if (functionp specials)
1000 (funcall specials beg end ignore-read-only)
1001 (save-excursion
1002 (save-restriction
1003 (narrow-to-region beg end)
1004 (goto-char (point-min))
1005 (while (< (point) (point-max))
1006 (if (and (not ignore-read-only)
1007 (get-text-property (point) 'read-only))
1008 (goto-char (or (next-single-property-change (point) 'read-only)
1009 (point-max)))
1010 (let ((repl (or (assoc (char-after) specials)
1011 (assoc (char-after)
1012 muse-publish-markup-specials))))
1013 (if (null repl)
1014 (forward-char 1)
1015 (delete-char 1)
1016 (insert-before-markers (cdr repl)))))))))))
1018 (defun muse-publish-markup-word ()
1019 (let* ((beg (match-beginning 2))
1020 (end (1- (match-end 2)))
1021 (leader (buffer-substring-no-properties beg end))
1022 open-tag close-tag mark-read-only loc context)
1023 (cond
1024 ((string= leader "_")
1025 (setq context 'underline
1026 open-tag (muse-markup-text 'begin-underline)
1027 close-tag (muse-markup-text 'end-underline)))
1028 ((string= leader "=")
1029 (setq context 'literal
1030 open-tag (muse-markup-text 'begin-literal)
1031 close-tag (muse-markup-text 'end-literal))
1032 (setq mark-read-only t))
1034 (let ((l (length leader)))
1035 (setq context 'emphasis)
1036 (cond
1037 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
1038 close-tag (muse-markup-text 'end-emph)))
1039 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
1040 close-tag (muse-markup-text 'end-more-emph)))
1041 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
1042 close-tag (muse-markup-text 'end-most-emph)))
1043 (t (setq context nil))))))
1044 (if (and context
1045 (not (get-text-property beg 'muse-link))
1046 (setq loc (search-forward leader nil t))
1047 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
1048 (not (eq (char-syntax (char-before (point))) ?\ ))
1049 (not (get-text-property (point) 'muse-link)))
1050 (progn
1051 (replace-match "")
1052 (delete-region beg end)
1053 (setq end (point-marker))
1054 (muse-insert-markup close-tag)
1055 (goto-char beg)
1056 (muse-insert-markup open-tag)
1057 (setq beg (point))
1058 (when mark-read-only
1059 (muse-publish-escape-specials beg end t context)
1060 (muse-publish-mark-read-only beg end))
1061 (set-marker end nil))
1062 (backward-char))
1063 nil))
1065 (defun muse-publish-markup-emdash ()
1066 (unless (get-text-property (match-beginning 0) 'muse-link)
1067 (let ((prespace (match-string 1))
1068 (postspace (match-string 2)))
1069 (delete-region (match-beginning 0) (match-end 0))
1070 (muse-insert-markup (muse-markup-text 'emdash prespace postspace))
1071 (when (eq (char-after) ?\<)
1072 (insert ?\n)))))
1074 (defun muse-publish-markup-enddots ()
1075 (unless (get-text-property (match-beginning 0) 'muse-link)
1076 (delete-region (match-beginning 0) (match-end 0))
1077 (muse-insert-markup (muse-markup-text 'enddots))))
1079 (defun muse-publish-markup-dots ()
1080 (unless (get-text-property (match-beginning 0) 'muse-link)
1081 (delete-region (match-beginning 0) (match-end 0))
1082 (muse-insert-markup (muse-markup-text 'dots))))
1084 (defun muse-publish-markup-rule ()
1085 (unless (get-text-property (match-beginning 0) 'muse-link)
1086 (delete-region (match-beginning 0) (match-end 0))
1087 (muse-insert-markup (muse-markup-text 'rule))))
1089 (defun muse-publish-markup-no-break-space ()
1090 (unless (get-text-property (match-beginning 0) 'muse-link)
1091 (delete-region (match-beginning 0) (match-end 0))
1092 (muse-insert-markup (muse-markup-text 'no-break-space))))
1094 (defun muse-publish-markup-heading ()
1095 (let* ((len (length (match-string 1)))
1096 (start (muse-markup-text
1097 (cond ((= len 1) 'section)
1098 ((= len 2) 'subsection)
1099 ((= len 3) 'subsubsection)
1100 (t 'section-other))
1101 len))
1102 (end (muse-markup-text
1103 (cond ((= len 1) 'section-end)
1104 ((= len 2) 'subsection-end)
1105 ((= len 3) 'subsubsection-end)
1106 (t 'section-other-end))
1107 len)))
1108 (delete-region (match-beginning 0) (match-end 0))
1109 (muse-insert-markup start)
1110 (end-of-line)
1111 (when end
1112 (muse-insert-markup end))
1113 (forward-line 1)
1114 (unless (eq (char-after) ?\n)
1115 (insert "\n"))
1116 (muse-publish-section-close len)))
1118 (defvar muse-publish-footnotes nil)
1120 (defun muse-publish-markup-footnote ()
1121 "Scan ahead and snarf up the footnote body."
1122 (cond
1123 ((get-text-property (match-beginning 0) 'muse-link)
1124 nil)
1125 ((= (muse-line-beginning-position) (match-beginning 0))
1128 (let ((footnote (save-match-data
1129 (string-to-number (match-string 1))))
1130 (oldtext (match-string 0))
1131 footnotemark)
1132 (delete-region (match-beginning 0) (match-end 0))
1133 (save-excursion
1134 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
1135 (let* ((start (match-beginning 0))
1136 (beg (goto-char (match-end 0)))
1137 (end (save-excursion
1138 (if (search-forward "\n\n" nil t)
1139 (copy-marker (match-beginning 0))
1140 (goto-char (point-max))
1141 (skip-chars-backward "\n")
1142 (point-marker)))))
1143 (while (re-search-forward
1144 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
1145 end t)
1146 (replace-match "\\1" t))
1147 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
1148 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
1149 (if (string= "" footnotemark-cmd)
1150 (setq footnotemark
1151 (concat (muse-markup-text 'footnote)
1152 (muse-publish-escape-specials-in-string
1153 (buffer-substring-no-properties beg end)
1154 'footnote)
1155 (muse-markup-text 'footnote-end)))
1156 (setq footnotemark (format footnotemark-cmd footnote
1157 footnotemark-end-cmd))
1158 (unless muse-publish-footnotes
1159 (set (make-local-variable 'muse-publish-footnotes)
1160 (make-vector 256 nil)))
1161 (unless (aref muse-publish-footnotes footnote)
1162 (setq footnotemark
1163 (concat
1164 footnotemark
1165 (concat (format (muse-markup-text 'footnotetext)
1166 footnote)
1167 (buffer-substring-no-properties beg end)
1168 (muse-markup-text 'footnotetext-end))))
1169 (aset muse-publish-footnotes footnote footnotemark))))
1170 (goto-char end)
1171 (skip-chars-forward "\n")
1172 (delete-region start (point))
1173 (set-marker end nil))))
1174 (if footnotemark
1175 (muse-insert-markup footnotemark)
1176 (insert oldtext))))))
1178 (defun muse-publish-markup-fn-sep ()
1179 (delete-region (match-beginning 0) (match-end 0))
1180 (muse-insert-markup (muse-markup-text 'fn-sep)))
1182 (defun muse-insert-markup-end-list (&rest args)
1183 (let ((beg (point)))
1184 (apply 'insert args)
1185 (add-text-properties beg (point) '(end-list t))
1186 (muse-publish-mark-read-only beg (point))))
1188 (defun muse-publish-determine-dl-indent (continue indent-sym determine-sym)
1189 ;; If the caller doesn't know how much indentation to use, figure it
1190 ;; out ourselves. It is assumed that `muse-forward-list-item' has
1191 ;; been called just before this to set the match data.
1192 (when (and continue
1193 (symbol-value determine-sym))
1194 (save-match-data
1195 ;; snarf all leading whitespace
1196 (let ((indent (and (match-beginning 2)
1197 (buffer-substring (match-beginning 1)
1198 (match-beginning 2)))))
1199 (when (and indent
1200 (not (string= indent "")))
1201 (set indent-sym indent)
1202 (set determine-sym nil))))))
1204 (defun muse-publish-surround-dl (indent post-indent)
1205 (let* ((beg-item (muse-markup-text 'begin-dl-item))
1206 (end-item (muse-markup-text 'end-dl-item))
1207 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
1208 (end-ddt (muse-markup-text 'end-ddt))
1209 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
1210 (end-dde (muse-markup-text 'end-dde))
1211 (continue t)
1212 (no-terms t)
1213 beg)
1214 (while continue
1215 ;; envelope this as one term+definitions unit -- HTML does not
1216 ;; need this, but DocBook and Muse's custom XML format do
1217 (muse-insert-markup beg-item)
1218 (when (looking-at muse-dl-term-regexp)
1219 ;; find the term and wrap it with published markup
1220 (setq beg (point)
1221 no-terms nil)
1222 (goto-char (match-end 1))
1223 (delete-region (point) (match-end 0))
1224 (muse-insert-markup-end-list end-ddt)
1225 ;; if definition is immediately after term, move to next line
1226 (unless (eq (char-after) ?\n)
1227 (insert ?\n))
1228 (save-excursion
1229 (goto-char beg)
1230 (delete-region (point) (match-beginning 1))
1231 (muse-insert-markup beg-ddt)))
1232 ;; handle pathological edge case where there is no term -- I
1233 ;; would prefer to just disallow this, but people seem to want
1234 ;; this behavior
1235 (when (and no-terms
1236 (looking-at (concat "[" muse-regexp-blank "]*::"
1237 "[" muse-regexp-blank "]*")))
1238 (delete-region (point) (match-end 0))
1239 ;; but only do this once
1240 (setq no-terms nil))
1241 (setq beg (point)
1242 ;; move past current item
1243 continue (muse-forward-list-item 'dl-term indent))
1244 (save-restriction
1245 (narrow-to-region beg (point))
1246 (goto-char (point-min))
1247 ;; publish each definition that we find, defaulting to an
1248 ;; empty definition if none are found
1249 (muse-publish-surround-text beg-dde end-dde
1250 (lambda (indent)
1251 (muse-forward-list-item 'dl-entry indent))
1252 indent post-indent
1253 #'muse-publish-determine-dl-indent)
1254 (goto-char (point-max))
1255 (skip-chars-backward (concat muse-regexp-blank "\n"))
1256 (muse-insert-markup-end-list end-item)
1257 (when continue
1258 (goto-char (point-max)))))))
1260 (defun muse-publish-strip-list-indentation (list-item empty-line indent post-indent)
1261 (let ((list-nested nil)
1262 (indent-found nil))
1263 (while (< (point) (point-max))
1264 (when (and (looking-at list-item)
1265 (not (or (get-text-property
1266 (muse-list-item-critical-point) 'read-only)
1267 (get-text-property
1268 (muse-list-item-critical-point) 'muse-link))))
1269 ;; if we encounter a list item, allow no post-indent space
1270 (setq list-nested t))
1271 (when (and (not (looking-at empty-line))
1272 (looking-at (concat indent "\\("
1273 (or (and list-nested "")
1274 post-indent)
1275 "\\)")))
1276 ;; if list is not nested, remove indentation
1277 (unless indent-found
1278 (setq post-indent (match-string 1)
1279 indent-found t))
1280 (replace-match ""))
1281 (forward-line 1))))
1283 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent determine-indent-func list-item)
1284 (unless list-item
1285 (setq list-item (format muse-list-item-regexp
1286 (concat "[" muse-regexp-blank "]*"))))
1287 (let ((continue t)
1288 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
1289 (determine-indent (if determine-indent-func t nil))
1290 (new-indent indent)
1291 (first t)
1292 beg)
1293 (unless indent
1294 (setq indent (concat "[" muse-regexp-blank "]+")))
1295 (if post-indent
1296 (setq post-indent (concat " \\{0," (number-to-string post-indent)
1297 "\\}"))
1298 (setq post-indent ""))
1299 (while continue
1300 (if (or (not end-tag) (string= end-tag ""))
1301 ;; if no end of list item markup exists, treat the beginning
1302 ;; of list item markup as it if it were the end -- this
1303 ;; prevents multiple-level lists from being confused
1304 (muse-insert-markup-end-list beg-tag)
1305 (muse-insert-markup beg-tag))
1306 (setq beg (point)
1307 ;; move past current item; continue is non-nil if there
1308 ;; are more like items to be processed
1309 continue (if (and determine-indent-func first)
1310 (funcall move-func (concat indent post-indent))
1311 (funcall move-func indent)))
1312 (when determine-indent-func
1313 (funcall determine-indent-func continue 'new-indent 'determine-indent))
1314 (when continue
1315 ;; remove list markup if we encountered another item of the
1316 ;; same type
1317 (replace-match "" t t nil 1))
1318 (save-restriction
1319 ;; narrow to current item
1320 (narrow-to-region beg (point))
1321 (goto-char (point-min))
1322 (if (looking-at empty-line)
1323 ;; if initial line is blank, move to first non-blank line
1324 (while (progn (forward-line 1)
1325 (and (< (point) (point-max))
1326 (looking-at empty-line))))
1327 ;; otherwise, move to second line of text
1328 (forward-line 1))
1329 ;; strip list indentation
1330 (muse-publish-strip-list-indentation list-item empty-line
1331 indent post-indent)
1332 (skip-chars-backward (concat muse-regexp-blank "\n"))
1333 (muse-insert-markup-end-list end-tag)
1334 (when determine-indent-func
1335 (setq indent new-indent))
1336 (when first
1337 (setq first nil))
1338 (when continue
1339 (goto-char (point-max)))))))
1341 (defun muse-publish-ensure-blank-line ()
1342 "Make sure that a blank line exists on the line before point."
1343 (let ((pt (point-marker)))
1344 (beginning-of-line)
1345 (cond ((eq (point) (point-min)) nil)
1346 ((prog2 (backward-char) (bolp) (forward-char)) nil)
1347 (t (insert-before-markers "\n")))
1348 (goto-char pt)
1349 (set-marker pt nil)))
1351 (defun muse-publish-markup-list ()
1352 "Markup a list entry.
1353 This function works by marking up items of the same list level
1354 and type, respecting the end-of-list property."
1355 (let* ((str (match-string 1))
1356 (type (muse-list-item-type str))
1357 (indent (buffer-substring (muse-line-beginning-position)
1358 (match-beginning 1)))
1359 (post-indent (length str)))
1360 (cond
1361 ((or (get-text-property (muse-list-item-critical-point) 'read-only)
1362 (get-text-property (muse-list-item-critical-point) 'muse-link))
1363 nil)
1364 ((eq type 'ul)
1365 (unless (eq (char-after (match-end 1)) ?-)
1366 (delete-region (match-beginning 0) (match-end 0))
1367 (muse-publish-ensure-blank-line)
1368 (muse-insert-markup (muse-markup-text 'begin-uli))
1369 (save-excursion
1370 (muse-publish-surround-text
1371 (muse-markup-text 'begin-uli-item)
1372 (muse-markup-text 'end-uli-item)
1373 (lambda (indent)
1374 (muse-forward-list-item 'ul indent))
1375 indent post-indent)
1376 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1377 (forward-line 1)))
1378 ((eq type 'ol)
1379 (delete-region (match-beginning 0) (match-end 0))
1380 (muse-publish-ensure-blank-line)
1381 (muse-insert-markup (muse-markup-text 'begin-oli))
1382 (save-excursion
1383 (muse-publish-surround-text
1384 (muse-markup-text 'begin-oli-item)
1385 (muse-markup-text 'end-oli-item)
1386 (lambda (indent)
1387 (muse-forward-list-item 'ol indent))
1388 indent post-indent)
1389 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1390 (forward-line 1))
1392 (goto-char (match-beginning 0))
1393 (muse-publish-ensure-blank-line)
1394 (muse-insert-markup (muse-markup-text 'begin-dl))
1395 (save-excursion
1396 (muse-publish-surround-dl indent post-indent)
1397 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1398 (forward-line 1))))
1399 nil)
1401 (defun muse-publish-markup-quote ()
1402 "Markup a quoted paragraph.
1403 The reason this function is so funky, is to prevent text properties
1404 like read-only from being inadvertently deleted."
1405 (let* ((ws (match-string 1))
1406 (centered (>= (string-width ws) 6))
1407 (begin-elem (if centered 'begin-center 'begin-quote-item))
1408 (end-elem (if centered 'end-center 'end-quote-item)))
1409 (replace-match "" t t nil 1)
1410 (unless centered
1411 (muse-insert-markup (muse-markup-text 'begin-quote)))
1412 (muse-publish-surround-text (muse-markup-text begin-elem)
1413 (muse-markup-text end-elem)
1414 (function (lambda (indent)
1415 (muse-forward-paragraph)
1416 nil)))
1417 (unless centered
1418 (muse-insert-markup (muse-markup-text 'end-quote)))))
1420 (defun muse-publish-markup-leading-space (markup-space multiple)
1421 (let (count)
1422 (when (and markup-space
1423 (>= (setq count (skip-chars-forward " ")) 0))
1424 (delete-region (muse-line-beginning-position) (point))
1425 (while (> count 0)
1426 (muse-insert-markup markup-space)
1427 (setq count (- count multiple))))))
1429 (defun muse-publish-markup-verse ()
1430 (let ((leader (match-string 0)))
1431 (goto-char (match-beginning 0))
1432 (muse-insert-markup (muse-markup-text 'begin-verse))
1433 (while (looking-at leader)
1434 (replace-match "")
1435 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1436 (let ((beg (point)))
1437 (end-of-line)
1438 (cond
1439 ((bolp)
1440 (let ((text (muse-markup-text 'empty-verse-line)))
1441 (when text (muse-insert-markup text))))
1442 ((save-excursion
1443 (save-match-data
1444 (forward-line 1)
1445 (or (looking-at (concat leader "["
1446 muse-regexp-blank
1447 "]*$"))
1448 (not (looking-at leader)))))
1449 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1450 (end-text (muse-markup-text 'end-last-stanza-line)))
1451 (when end-text (muse-insert-markup end-text))
1452 (goto-char beg)
1453 (when begin-text (muse-insert-markup begin-text))
1454 (end-of-line)))
1456 (let ((begin-text (muse-markup-text 'begin-verse-line))
1457 (end-text (muse-markup-text 'end-verse-line)))
1458 (when end-text (muse-insert-markup end-text))
1459 (goto-char beg)
1460 (when begin-text (muse-insert-markup begin-text))
1461 (end-of-line))))
1462 (forward-line 1))))
1463 (muse-insert-markup (muse-markup-text 'end-verse))
1464 (insert ?\n))
1466 (defun muse-publish-trim-table (table)
1467 "Remove completely blank columns from table, if at start or end of row."
1468 ;; remove first
1469 (catch 'found
1470 (dolist (row (cdr table))
1471 (let ((el (cadr row)))
1472 (when (and (stringp el) (not (string= el "")))
1473 (throw 'found t))))
1474 (dolist (row (cdr table))
1475 (setcdr row (cddr row)))
1476 (setcar table (1- (car table))))
1477 ;; remove last
1478 (catch 'found
1479 (dolist (row (cdr table))
1480 (let ((el (car (last row))))
1481 (when (and (stringp el) (not (string= el "")))
1482 (throw 'found t))))
1483 (dolist (row (cdr table))
1484 (setcdr (last row 2) nil))
1485 (setcar table (1- (car table))))
1486 table)
1488 (defun muse-publish-table-fields (beg end)
1489 "Parse given region as a table, returning a cons cell.
1490 The car is the length of the longest row.
1492 The cdr is a list of the fields of the table, with the first
1493 element indicating the type of the row:
1494 1: body, 2: header, 3: footer, hline: separator.
1496 The existing region will be removed, except for initial blank lines."
1497 (unless (muse-publishing-directive "disable-tables")
1498 (let ((longest 0)
1499 (left 0)
1500 (seen-hline nil)
1501 fields field-list)
1502 (save-restriction
1503 (narrow-to-region beg end)
1504 (goto-char (point-min))
1505 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1506 (forward-line 1))
1507 (setq beg (point))
1508 (while (= left 0)
1509 (cond
1510 ((looking-at muse-table-hline-regexp)
1511 (when field-list ; skip if at the beginning of table
1512 (if seen-hline
1513 (setq field-list (cons (cons 'hline nil) field-list))
1514 (dolist (field field-list)
1515 ;; the preceding fields are header lines
1516 (setcar field 2))
1517 (setq seen-hline t))))
1518 ((looking-at muse-table-line-regexp)
1519 (setq fields (cons (length (match-string 1))
1520 (mapcar #'muse-trim-whitespace
1521 (split-string (match-string 0)
1522 muse-table-field-regexp)))
1523 field-list (cons fields field-list)
1524 longest (max (length fields) longest))
1525 ;; strip initial bars, if they exist
1526 (let ((first (cadr fields)))
1527 (when (and first (string-match "\\`|+\\s-*" first))
1528 (setcar (cdr fields) (replace-match "" t t first))))))
1529 (setq left (forward-line 1))))
1530 (delete-region beg end)
1531 (if (= longest 0)
1532 (cons 0 nil)
1533 ;; if the last line was an hline, remove it
1534 (when (eq (caar field-list) 'hline)
1535 (setq field-list (cdr field-list)))
1536 (muse-publish-trim-table (cons (1- longest) (nreverse field-list)))))))
1538 (defun muse-publish-markup-table ()
1539 "Style does not support tables.\n")
1541 (defun muse-publish-table-el-table (variant)
1542 "Publish table.el-style tables in the format given by VARIANT."
1543 (when (condition-case nil
1544 (progn (require 'table)
1546 (error nil))
1547 (let ((muse-buf (current-buffer)))
1548 (save-restriction
1549 (narrow-to-region (match-beginning 0) (match-end 0))
1550 (goto-char (point-min))
1551 (forward-line 1)
1552 (search-forward "|" nil t)
1553 (with-temp-buffer
1554 (let ((temp-buf (current-buffer)))
1555 (with-current-buffer muse-buf
1556 (table-generate-source variant temp-buf))
1557 (with-current-buffer muse-buf
1558 (delete-region (point-min) (point-max))
1559 (insert-buffer-substring temp-buf)
1560 (muse-publish-mark-read-only (point-min) (point-max)))))))))
1562 (defun muse-publish-markup-table-el ()
1563 "Mark up table.el-style tables."
1564 (cond ((muse-style-derived-p 'html)
1565 (muse-publish-table-el-table 'html))
1566 ((muse-style-derived-p 'latex)
1567 (muse-publish-table-el-table 'latex))
1568 ((muse-style-derived-p 'docbook)
1569 (muse-publish-table-el-table 'cals))
1570 (t "Style does not support table.el tables.\n")))
1572 (defun muse-publish-escape-specials-in-string (string &optional context)
1573 "Escape specials in STRING using style-specific :specials.
1574 CONTEXT is used to figure out what kind of specials to escape.
1576 See the documentation of the `muse-publish-escape-specials'
1577 function for the list of available contexts."
1578 (unless string
1579 (setq string ""))
1580 (let ((specials (muse-style-element :specials nil t)))
1581 (cond ((functionp specials)
1582 (setq specials (funcall specials context)))
1583 ((symbolp specials)
1584 (setq specials (symbol-value specials))))
1585 (if (functionp specials)
1586 (funcall specials string)
1587 (apply (function concat)
1588 (mapcar
1589 (lambda (ch)
1590 (let ((repl (or (assoc ch specials)
1591 (assoc ch muse-publish-markup-specials))))
1592 (if (null repl)
1593 (char-to-string ch)
1594 (cdr repl))))
1595 (append string nil))))))
1597 (defun muse-publish-markup-email ()
1598 (let* ((beg (match-end 1))
1599 (addr (buffer-substring-no-properties beg (match-end 0))))
1600 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1601 (goto-char beg)
1602 (delete-region beg (match-end 0))
1603 (if (or (eq (char-before (match-beginning 0)) ?\")
1604 (eq (char-after (match-end 0)) ?\"))
1605 (insert addr)
1606 (insert (format (muse-markup-text 'email-addr) addr addr)))
1607 (muse-publish-mark-read-only beg (point))))
1609 (defun muse-publish-classify-url (target)
1610 "Transform anchors and get published name, if TARGET is a page.
1611 The return value is two linked cons cells. The car is the type
1612 of link, the cadr is the page name, and the cddr is the anchor."
1613 (save-match-data
1614 (cond ((or (null target) (string= target ""))
1615 nil)
1616 ((string-match "\\`[uU][rR][lL]:\\(.+\\)\\'" target)
1617 (cons 'url (cons (match-string 1 target) nil)))
1618 ((string-match muse-image-regexp target)
1619 (cons 'image (cons target nil)))
1620 ((string-match muse-url-regexp target)
1621 (cons 'url (cons target nil)))
1622 ((string-match muse-file-regexp target)
1623 (cons 'file (cons target nil)))
1624 ((string-match "#" target)
1625 (if (eq (aref target 0) ?\#)
1626 (cons 'anchor-ref (cons nil (substring target 1)))
1627 (cons 'link-and-anchor
1628 (cons (muse-publish-link-page
1629 (substring target 0 (match-beginning 0)))
1630 (substring target (match-end 0))))))
1632 (cons 'link (cons (muse-publish-link-page target) nil))))))
1634 (defun muse-publish-url-desc (desc explicit)
1635 (when desc
1636 (dolist (transform muse-publish-desc-transforms)
1637 (setq desc (save-match-data
1638 (when desc (funcall transform desc explicit)))))
1639 (setq desc (muse-link-unescape desc))
1640 (muse-publish-escape-specials-in-string desc 'url-desc)))
1642 (defun muse-publish-url (url &optional desc orig-url explicit)
1643 "Resolve a URL into its final <a href> form."
1644 (let ((unesc-url url)
1645 (unesc-orig-url orig-url)
1646 type anchor)
1647 (dolist (transform muse-publish-url-transforms)
1648 (setq url (save-match-data (when url (funcall transform url explicit)))))
1649 (if desc
1650 (setq desc (muse-publish-url-desc desc explicit))
1651 (if orig-url
1652 (setq orig-url (muse-publish-url-desc orig-url explicit))))
1653 (let ((target (muse-publish-classify-url url)))
1654 (setq type (car target)
1655 url (if (eq type 'image)
1656 (muse-publish-escape-specials-in-string (cadr target)
1657 'image)
1658 (muse-publish-escape-specials-in-string (cadr target) 'url))
1659 anchor (muse-publish-escape-specials-in-string
1660 (cddr target) 'url)))
1661 (cond ((eq type 'anchor-ref)
1662 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1663 ((and desc (string-match muse-image-regexp desc))
1664 (let ((ext (or (file-name-extension desc) "")))
1665 (setq desc (muse-path-sans-extension desc))
1666 (muse-markup-text 'image-link url desc ext)))
1667 ((string= url "")
1668 desc)
1669 ((eq type 'image)
1670 (let ((ext (or (file-name-extension url) "")))
1671 (setq url (muse-path-sans-extension url))
1672 (if desc
1673 (muse-markup-text 'image-with-desc url ext desc)
1674 (muse-markup-text 'image url ext))))
1675 ((eq type 'link-and-anchor)
1676 (muse-markup-text 'link-and-anchor url anchor
1677 (or desc orig-url)
1678 (muse-path-sans-extension url)))
1679 ((eq type 'link)
1680 (muse-markup-text 'link url (or desc orig-url)))
1682 (or (and (or desc
1683 ;; compare the not-escaped versions of url and
1684 ;; orig-url
1685 (not (string= unesc-url unesc-orig-url)))
1686 (let ((text (muse-markup-text 'url-and-desc url
1687 (or desc orig-url))))
1688 (and (not (string= text ""))
1689 text)))
1690 (muse-markup-text 'url url (or desc orig-url)))))))
1692 (defun muse-publish-insert-url (url &optional desc orig-url explicit)
1693 "Resolve a URL into its final <a href> form."
1694 (delete-region (match-beginning 0) (match-end 0))
1695 (let ((text (muse-publish-url url desc orig-url explicit)))
1696 (when text
1697 (muse-insert-markup text))))
1699 (defun muse-publish-markup-link ()
1700 (let (desc explicit orig-link link)
1701 (setq explicit (save-match-data
1702 (if (string-match muse-explicit-link-regexp
1703 (match-string 0))
1704 t nil)))
1705 (setq orig-link (if explicit (match-string 1) (match-string 0)))
1706 (setq desc (when explicit (match-string 2)))
1707 (setq link (if explicit
1708 (muse-handle-explicit-link orig-link)
1709 (muse-handle-implicit-link orig-link)))
1710 (when (and link
1711 (or explicit
1712 (not (or (eq (char-before (match-beginning 0)) ?\")
1713 (eq (char-after (match-end 0)) ?\")))))
1714 ;; if explicit link has no user-provided description, treat it
1715 ;; as if it were an implicit link
1716 (when (and explicit (not desc))
1717 (setq explicit nil))
1718 (muse-publish-insert-url link desc orig-link explicit))))
1720 (defun muse-publish-markup-url ()
1721 (unless (or (eq (char-before (match-beginning 0)) ?\")
1722 (eq (char-after (match-end 0)) ?\"))
1723 (let ((url (match-string 0)))
1724 (muse-publish-insert-url url nil url))))
1726 ;; Default publishing tags
1728 (defcustom muse-publish-contents-depth 2
1729 "The number of heading levels to include with <contents> tags."
1730 :type 'integer
1731 :group 'muse-publish)
1733 (defun muse-publish-contents-tag (beg end attrs)
1734 (set (make-local-variable 'muse-publish-generate-contents)
1735 (cons (copy-marker (point) t)
1736 (let ((depth (cdr (assoc "depth" attrs))))
1737 (or (and depth (string-to-number depth))
1738 muse-publish-contents-depth)))))
1740 (defun muse-publish-verse-tag (beg end)
1741 (muse-publish-ensure-block beg end)
1742 (save-excursion
1743 (save-restriction
1744 (narrow-to-region beg end)
1745 (goto-char (point-min))
1746 (delete-char 1)
1747 (while (< (point) (point-max))
1748 (insert "> ")
1749 (forward-line))
1750 (if (eq ?\ (char-syntax (char-before)))
1751 (delete-char -1)))))
1753 (defun muse-publish-mark-read-only (beg end)
1754 "Add read-only properties to the given region."
1755 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1756 nil)
1758 (defun muse-publish-mark-link (&optional beg end)
1759 "Indicate that the given region is a Muse link, so that other
1760 markup elements respect it. If a region is not specified, use
1761 the 0th match data to determine it.
1763 This is usually applied to explicit links."
1764 (unless beg (setq beg (match-beginning 0)))
1765 (unless end (setq end (match-end 0)))
1766 (add-text-properties beg end '(muse-link t))
1767 nil)
1769 (defun muse-publish-quote-tag (beg end)
1770 (muse-publish-ensure-block beg)
1771 (save-excursion
1772 (save-restriction
1773 (narrow-to-region beg end)
1774 (let ((quote-regexp "^\\(<\\(/?\\)quote>\\)"))
1775 (muse-insert-markup (muse-markup-text 'begin-quote))
1776 (while (progn
1777 (unless (looking-at (concat "[" muse-regexp-blank "\n]*"
1778 "<quote>"))
1779 (muse-publish-surround-text
1780 (muse-markup-text 'begin-quote-item)
1781 (muse-markup-text 'end-quote-item)
1782 (function
1783 (lambda (indent)
1784 (muse-forward-paragraph)
1785 (goto-char (match-end 0))
1786 (and (< (point) (point-max))
1787 (not (looking-at quote-regexp)))))
1788 nil nil nil
1789 quote-regexp))
1790 (if (>= (point) (point-max))
1792 (and (search-forward "<quote>" nil t)
1793 (muse-goto-tag-end "quote" t)
1794 (progn (forward-line 1) t)
1795 (< (point) (point-max))))))
1796 (goto-char (point-max))
1797 (muse-insert-markup (muse-markup-text 'end-quote))))))
1799 (defun muse-publish-code-tag (beg end)
1800 (muse-publish-escape-specials beg end nil 'literal)
1801 (goto-char beg)
1802 (insert (muse-markup-text 'begin-literal))
1803 (goto-char end)
1804 (insert (muse-markup-text 'end-literal))
1805 (muse-publish-mark-read-only beg (point)))
1807 (defun muse-publish-cite-tag (beg end attrs)
1808 (let* ((type (muse-publish-get-and-delete-attr "type" attrs))
1809 (citetag (cond ((string-equal type "author")
1810 'begin-cite-author)
1811 ((string-equal type "year")
1812 'begin-cite-year)
1814 'begin-cite))))
1815 (goto-char beg)
1816 (insert (muse-markup-text citetag (muse-publishing-directive "bibsource")))
1817 (goto-char end)
1818 (insert (muse-markup-text 'end-cite))
1819 (muse-publish-mark-read-only beg (point))))
1821 (defun muse-publish-src-tag (beg end attrs)
1822 (muse-publish-example-tag beg end))
1824 (defun muse-publish-example-tag (beg end)
1825 (muse-publish-ensure-block beg end)
1826 (muse-publish-escape-specials beg end nil 'example)
1827 (goto-char beg)
1828 (insert (muse-markup-text 'begin-example))
1829 (goto-char end)
1830 (insert (muse-markup-text 'end-example))
1831 (muse-publish-mark-read-only beg (point)))
1833 (defun muse-publish-literal-tag (beg end attrs)
1834 "Ensure that the text between BEG and END is not interpreted later on.
1836 ATTRS is an alist of attributes.
1838 If it contains a \"style\" element, delete the region if the
1839 current style is neither derived from nor equal to this style.
1841 If it contains both a \"style\" element and an \"exact\" element
1842 with the value \"t\", delete the region only if the current style
1843 is exactly this style."
1844 (let* ((style (cdr (assoc "style" attrs)))
1845 (exact (cdr (assoc "exact" attrs)))
1846 (exactp (and (stringp exact) (string= exact "t"))))
1847 (if (or (not style)
1848 (and exactp (equal (muse-style style)
1849 muse-publishing-current-style))
1850 (and (not exactp) (muse-style-derived-p style)))
1851 (muse-publish-mark-read-only beg end)
1852 (delete-region beg end)
1853 (when (and (bolp) (eolp) (not (eobp)))
1854 (delete-char 1)))))
1856 (defun muse-publish-verbatim-tag (beg end)
1857 (muse-publish-escape-specials beg end nil 'verbatim)
1858 (muse-publish-mark-read-only beg end))
1860 (defalias 'muse-publish-class-tag 'ignore)
1862 (defun muse-publish-call-tag-on-buffer (tag &optional attrs)
1863 "Transform the current buffer as if it were surrounded by the tag TAG.
1864 If attributes ATTRS are given, pass them to the tag function."
1865 (let ((tag-info (muse-markup-tag-info tag)))
1866 (when tag-info
1867 (let* ((end (progn (goto-char (point-max)) (point-marker)))
1868 (args (list (point-min) end))
1869 (muse-inhibit-style-tags nil))
1870 (when (nth 2 tag-info)
1871 (nconc args (list attrs)))
1872 (apply (nth 4 tag-info) args)
1873 (set-marker end nil)))))
1875 (defun muse-publish-examplify-buffer (&optional attrs)
1876 "Transform the current buffer as if it were an <example> region."
1877 (muse-publish-call-tag-on-buffer "example" attrs))
1879 (defun muse-publish-srcify-buffer (&optional attrs)
1880 "Transform the current buffer as if it were a <src> region."
1881 (muse-publish-call-tag-on-buffer "src" attrs))
1883 (defun muse-publish-versify-buffer (&optional attrs)
1884 "Transform the current buffer as if it were a <verse> region."
1885 (muse-publish-call-tag-on-buffer "verse" attrs)
1886 (muse-publish-markup ""
1887 `((100 ,(concat "^[" muse-regexp-blank "]*> ") 0
1888 muse-publish-markup-verse)))
1889 (goto-char (point-min)))
1891 (defmacro muse-publish-markup-attribute (beg end attrs reinterp &rest body)
1892 "Evaluate BODY within the bounds of BEG and END.
1893 ATTRS is an alist. Only the \"markup\" element of ATTRS is acted
1896 If it is omitted, publish the region with the normal Muse rules.
1897 If RE-INTERP is specified, this is done immediately in a new
1898 publishing process. Currently, RE-INTERP is specified only by
1899 the <include> tag.
1901 If \"nil\", do not mark up the region at all, but prevent it from
1902 being further interpreted by Muse.
1904 If \"example\", treat the region as if it was surrounded by the
1905 <example> tag.
1907 If \"src\", treat the region as if it was surrounded by the
1908 <src> tag.
1910 If \"verse\", treat the region as if it was surrounded by the
1911 <verse> tag, to preserve newlines.
1913 Otherwise, it should be the name of a function to call in the
1914 narrowed region after evaluating BODY. The function should
1915 take the ATTRS parameter.
1917 BEG is modified to be the start of the published markup."
1918 (let ((attrs-sym (make-symbol "attrs"))
1919 (markup (make-symbol "markup"))
1920 (markup-function (make-symbol "markup-function")))
1921 `(let* ((,attrs-sym ,attrs)
1922 (,markup (muse-publish-get-and-delete-attr "markup" ,attrs-sym)))
1923 (save-restriction
1924 (narrow-to-region ,beg ,end)
1925 (goto-char (point-min))
1926 ,@body
1927 (if (not ,markup)
1928 (when ,reinterp
1929 (muse-publish-markup-region (point-min) (point-max))
1930 (muse-publish-mark-read-only (point-min) (point-max))
1931 (goto-char (point-max)))
1932 (let ((,markup-function (read ,markup)))
1933 (cond ((eq ,markup-function 'example)
1934 (setq ,markup-function #'muse-publish-examplify-buffer))
1935 ((eq ,markup-function 'src)
1936 (setq ,markup-function #'muse-publish-srcify-buffer))
1937 ((eq ,markup-function 'verse)
1938 (setq ,markup-function #'muse-publish-versify-buffer))
1939 ((and ,markup-function (not (functionp ,markup-function)))
1940 (error "Invalid markup function `%s'" ,markup))
1941 (t nil))
1942 (if ,markup-function
1943 (funcall ,markup-function ,attrs-sym)
1944 (muse-publish-mark-read-only (point-min) (point-max))
1945 (goto-char (point-max)))))))))
1947 (put 'muse-publish-markup-attribute 'lisp-indent-function 4)
1948 (put 'muse-publish-markup-attribute 'edebug-form-spec
1949 '(sexp sexp sexp sexp body))
1951 (defun muse-publish-lisp-tag (beg end attrs)
1952 (muse-publish-markup-attribute beg end attrs nil
1953 (save-excursion
1954 (save-restriction
1955 (let ((str (muse-eval-lisp
1956 (prog1
1957 (concat "(progn "
1958 (buffer-substring-no-properties (point-min)
1959 (point-max))
1960 ")")
1961 (delete-region (point-min) (point-max))
1962 (widen)))))
1963 (set-text-properties 0 (length str) nil str)
1964 (insert str))))))
1966 (defun muse-publish-command-tag (beg end attrs)
1967 (muse-publish-markup-attribute beg end attrs nil
1968 (while (looking-at "\\s-*$")
1969 (forward-line))
1970 (let ((interp (muse-publish-get-and-delete-attr "interp" attrs)))
1971 (if interp
1972 (shell-command-on-region (point) (point-max) interp t t)
1973 (shell-command
1974 (prog1
1975 (buffer-substring-no-properties (point) (point-max))
1976 (delete-region (point-min) (point-max)))
1977 t)))
1978 ;; make sure there is a newline at end
1979 (goto-char (point-max))
1980 (forward-line 0)
1981 (unless (looking-at "\\s-*$")
1982 (goto-char (point-max))
1983 (insert ?\n))
1984 (goto-char (point-min))))
1986 (defun muse-publish-perl-tag (beg end attrs)
1987 (muse-publish-command-tag beg end
1988 (cons (cons "interp" (executable-find "perl"))
1989 attrs)))
1991 (defun muse-publish-php-tag (beg end attrs)
1992 (muse-publish-command-tag beg end
1993 (cons (cons "interp" (executable-find "php"))
1994 attrs)))
1996 (defun muse-publish-python-tag (beg end attrs)
1997 (muse-publish-command-tag beg end
1998 (cons (cons "interp" (executable-find "python"))
1999 attrs)))
2001 (defun muse-publish-ruby-tag (beg end attrs)
2002 (muse-publish-command-tag beg end
2003 (cons (cons "interp" (executable-find "ruby"))
2004 attrs)))
2006 (defun muse-publish-comment-tag (beg end)
2007 (if (null muse-publish-comments-p)
2008 (delete-region beg end)
2009 (goto-char end)
2010 (muse-insert-markup (muse-markup-text 'comment-end))
2011 (muse-publish-mark-read-only beg end)
2012 (goto-char beg)
2013 (muse-insert-markup (muse-markup-text 'comment-begin))))
2015 (defun muse-publish-include-tag (beg end attrs)
2016 "Include the named file at the current location during publishing.
2018 <include file=\"...\" markup=\"...\">
2020 The `markup' attribute controls how this file is marked up after
2021 being inserted. See `muse-publish-markup-attribute' for an
2022 explanation of how it works."
2023 (let ((filename (muse-publish-get-and-delete-attr "file" attrs))
2024 (muse-publishing-directives (copy-alist muse-publishing-directives)))
2025 (if filename
2026 (setq filename (expand-file-name
2027 filename
2028 (file-name-directory muse-publishing-current-file)))
2029 (error "No file attribute specified in <include> tag"))
2030 (muse-publish-markup-attribute beg end attrs t
2031 (muse-insert-file-contents filename))))
2033 (defun muse-publish-mark-up-tag (beg end attrs)
2034 "Run an Emacs Lisp function on the region delimted by this tag.
2036 <markup function=\"...\" style=\"...\" exact=\"...\">
2038 The optional \"function\" attribute controls how this section is
2039 marked up. If used, it should be the name of a function to call
2040 with the buffer narrowed to the delimited region. Note that no
2041 further marking-up will be performed on this region.
2043 If \"function\" is omitted, use the standard Muse markup function.
2044 This is useful for marking up content in headers and footers.
2046 The optional \"style\" attribute causes the region to be deleted
2047 if the current style is neither derived from nor equal to this
2048 style.
2050 If both a \"style\" attribute and an \"exact\" attribute are
2051 provided, and \"exact\" is \"t\", delete the region only if the
2052 current style is exactly this style."
2053 (let* ((style (cdr (assoc "style" attrs)))
2054 (exact (cdr (assoc "exact" attrs)))
2055 (exactp (and (stringp exact) (string= exact "t"))))
2056 (if (or (not style)
2057 (and exactp (equal (muse-style style)
2058 muse-publishing-current-style))
2059 (and (not exactp) (muse-style-derived-p style)))
2060 (let* ((function (cdr (assoc "function" attrs)))
2061 (muse-publish-use-header-footer-tags nil)
2062 (markup-function (and function (intern function))))
2063 (if (and markup-function (functionp markup-function))
2064 (save-restriction
2065 (narrow-to-region beg end)
2066 (funcall markup-function)
2067 (goto-char (point-max)))
2068 (let ((muse-publish-inhibit-style-hooks t))
2069 (muse-publish-markup-region beg end)))
2070 (muse-publish-mark-read-only beg (point)))
2071 (delete-region beg end))))
2073 ;; Miscellaneous helper functions
2075 (defun muse-publish-strip-URL (string &rest ignored)
2076 "If the text \"URL:\" exists at the beginning of STRING, remove it.
2077 The text is removed regardless of whether and part of it is uppercase."
2078 (save-match-data
2079 (if (string-match "\\`[uU][rR][lL]:\\(.+\\)\\'" string)
2080 (match-string 1 string)
2081 string)))
2083 (defun muse-publish-markup-type (category default-func)
2084 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
2085 (funcall (or rule default-func))))
2087 (defun muse-published-buffer-contents (buffer)
2088 (with-current-buffer buffer
2089 (goto-char (point-min))
2090 (let ((beg (and (search-forward "Emacs Muse begins here")
2091 (muse-line-end-position)))
2092 (end (and (search-forward "Emacs Muse ends here")
2093 (muse-line-beginning-position))))
2094 (buffer-substring-no-properties beg end))))
2096 (defun muse-published-contents (file)
2097 (when (file-readable-p file)
2098 (muse-with-temp-buffer
2099 (muse-insert-file-contents file)
2100 (muse-published-buffer-contents (current-buffer)))))
2102 (defun muse-publish-transform-output
2103 (file temp-file output-path name gen-func &rest cleanup-exts)
2104 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
2105 (setq file (muse-page-name file))
2106 (message "Generating %s output for %s..." name file)
2107 (if (not (funcall gen-func temp-file output-path))
2108 (message "Generating %s from %s...failed" name file)
2109 (message "Generating %s output for %s...done" name file)
2110 (muse-delete-file-if-exists temp-file)
2111 (dolist (ext cleanup-exts)
2112 (muse-delete-file-if-exists
2113 (expand-file-name (concat file ext)
2114 (file-name-directory output-path))))
2115 (message "Wrote %s" output-path)))
2117 (defun muse-publish-read-only (string)
2118 (let ((end (1- (length string))))
2119 (add-text-properties 0 end
2120 '(rear-nonsticky (read-only) read-only t)
2121 string)
2122 string))
2124 ;;; muse-publish.el ends here