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