Make command, lisp, perl, python, and ruby tags interpret "markup".
[muse-el.git] / lisp / muse-publish.el
blobf564dc8ef920c2f748bbad4b44f887ecf7079916
1 ;;; muse-publish.el --- base publishing implementation
3 ;; Copyright (C) 2004, 2005, 2006 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 2, 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.
35 ;;; Code:
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 ;; Muse Publishing
41 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 (require 'muse)
44 (require 'muse-regexps)
46 (defgroup muse-publish nil
47 "Options controlling the general behavior of Muse publishing."
48 :group 'muse)
50 (defcustom muse-before-publish-hook nil
51 "A hook run in the buffer to be published, before it is done."
52 :type 'hook
53 :group 'muse-publish)
55 (defcustom muse-after-publish-hook nil
56 "A hook run in the buffer to be published, after it is done."
57 :type 'hook
58 :group 'muse-publish)
60 (defcustom muse-publish-url-transforms
61 '(muse-resolve-url)
62 "A list of functions used to prepare URLs for publication.
63 Each is passed the URL. The transformed URL should be returned."
64 :type 'hook
65 :options '(muse-resolve-url)
66 :group 'muse-publish)
68 (defcustom muse-publish-desc-transforms nil
69 "A list of functions used to prepare URL desciptions for publication.
70 Each is passed the description. The modified description should
71 be returned."
72 :type 'hook
73 :group 'muse-publish)
75 (defcustom muse-publish-comments-p nil
76 "If nil, remove comments before publishing.
77 If non-nil, publish comments using the markup of the current style."
78 :type 'boolean
79 :group 'muse-publish)
81 (defcustom muse-publish-report-threshhold 100000
82 "If a file is this size or larger, report publishing progress."
83 :type 'integer
84 :group 'muse-publish)
86 (defcustom muse-publish-markup-regexps
87 `(;; Remove leading and trailing whitespace from the file
88 (1000 "\\(\\`\n+\\|\n+\\'\\)" 0 "")
90 ;; Remove trailing whitespace from all lines
91 (1100 ,(concat "[" muse-regexp-blank "]+$") 0 "")
93 ;; Handle any leading #directives
94 (1200 "\\`#\\([a-zA-Z-]+\\)\\s-+\\(.+\\)\n+" 0 directive)
96 ;; markup tags
97 (1300 muse-tag-regexp 0 tag)
99 ;; commented lines
100 (1350 "^;\\s-+\\(.+\\)" 0 comment)
102 ;; prevent emphasis characters in explicit links from being marked
103 (1400 muse-explicit-link-regexp 0 muse-publish-mark-link)
105 ;; emphasized or literal text
106 (1600 ,(concat "\\(^\\|[-[" muse-regexp-blank
107 "<('`\"\n]\\)\\(=[^=" muse-regexp-blank
108 "\n]\\|_[^_" muse-regexp-blank
109 "\n]\\|\\*+[^*" muse-regexp-blank
110 "\n]\\)")
111 2 word)
113 ;; headings, outline-mode style
114 (1700 "^\\(\\*+\\)\\s-+" 0 heading)
116 ;; ellipses
117 (1800 "\\.\\.\\.\\." 0 enddots)
118 (1850 "\\.\\.\\." 0 dots)
120 ;; horizontal rule, or section separator
121 (1900 "^----+" 0 rule)
123 ;; non-breaking space
124 (1950 "~~" 0 no-break-space)
126 ;; beginning of footnotes section
127 (2000 "^Footnotes:?\\s-*" 0 fn-sep)
128 ;; footnote definition/reference (def if at beginning of line)
129 (2100 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote)
131 ;; unnumbered List items begin with a -. numbered list items
132 ;; begin with number and a period. definition lists have a
133 ;; leading term separated from the body with ::. centered
134 ;; paragraphs begin with at least six columns of whitespace; any
135 ;; other whitespace at the beginning indicates a blockquote. The
136 ;; reason all of these rules are handled here, is so that
137 ;; blockquote detection doesn't interfere with indented list
138 ;; members.
139 (2200 ,(format muse-list-item-regexp (concat "[" muse-regexp-blank "]*"))
140 0 list)
142 ;; simple table markup is supported, nothing fancy. use | to
143 ;; separate cells, || to separate header cells, and ||| for footer
144 ;; cells
145 (2300 ,(concat "\\(\\([" muse-regexp-blank "]*\n\\)?"
146 "\\(" muse-table-line-regexp "\\(?:\n\\|\\'\\)\\)\\)+")
147 0 table)
149 ;; blockquote and centered text
150 (2400 ,(concat "^\\([" muse-regexp-blank "]+\\).+") 0 quote)
152 ;; the emdash ("--")
153 (2500 ,(concat "\\(^\\|[" muse-regexp-blank "]*\\)--\\($\\|["
154 muse-regexp-blank "]*\\)")
155 0 emdash)
157 ;; "verse" text is indicated the same way as a quoted e-mail
158 ;; response: "> text", where text may contain initial whitespace
159 ;; (see below).
160 (2600 ,(concat "^[" muse-regexp-blank "]*> ") 0 verse)
162 ;; define anchor points
163 (2700 "^\\(\\W*\\)#\\(\\S-+\\)\\s-*" 0 anchor)
165 ;; replace links in the buffer (links to other pages)
166 (2900 muse-explicit-link-regexp 0 link)
168 ;; bare URLs
169 (3000 muse-url-regexp 0 url)
171 ;; bare email addresses
172 (3500
173 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email)
175 "List of markup rules for publishing a page with Muse.
176 The rules given in this variable are invoked first, followed by
177 whatever rules are specified by the current style.
179 Each member of the list is either a function, or a list of the form:
181 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
183 REGEXP is a regular expression, or symbol whose value is a regular
184 expression, which is searched for using `re-search-forward'.
185 TEXT-BEGIN-GROUP is the matching group within that regexp which
186 denotes the beginning of the actual text to be marked up.
187 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
188 If it is not a string, but a function, it will be called to determine
189 what the replacement text should be (it must return a string). If it
190 is a symbol, the value of that symbol should be a string.
192 The replacements are done in order, one rule at a time. Writing
193 the regular expressions can be a tricky business. Note that case
194 is never ignored. `case-fold-search' is always bound to nil
195 while processing the markup rules."
196 :type '(repeat (choice
197 (list :tag "Markup rule"
198 integer
199 (choice regexp symbol)
200 integer
201 (choice string function symbol))
202 function))
203 :group 'muse-publish)
205 (defcustom muse-publish-markup-functions
206 '((directive . muse-publish-markup-directive)
207 (comment . muse-publish-markup-comment)
208 (anchor . muse-publish-markup-anchor)
209 (tag . muse-publish-markup-tag)
210 (word . muse-publish-markup-word)
211 (emdash . muse-publish-markup-emdash)
212 (enddots . muse-publish-markup-enddots)
213 (dots . muse-publish-markup-dots)
214 (rule . muse-publish-markup-rule)
215 (no-break-space . muse-publish-markup-no-break-space)
216 (heading . muse-publish-markup-heading)
217 (footnote . muse-publish-markup-footnote)
218 (fn-sep . muse-publish-markup-fn-sep)
219 (list . muse-publish-markup-list)
220 (quote . muse-publish-markup-quote)
221 (verse . muse-publish-markup-verse)
222 (table . muse-publish-markup-table)
223 (email . muse-publish-markup-email)
224 (link . muse-publish-markup-link)
225 (url . muse-publish-markup-url))
226 "An alist of style types to custom functions for that kind of text.
228 Each member of the list is of the form:
230 (SYMBOL FUNCTION)
232 SYMBOL describes the type of text to associate with this rule.
233 `muse-publish-markup-regexps' maps regexps to these symbols.
235 FUNCTION is the function to use to mark up this kind of rule if
236 no suitable function is found through the :functions tag of the
237 current style."
238 :type '(alist :key-type symbol :value-type function)
239 :group 'muse-publish)
241 (defcustom muse-publish-markup-tags
242 '(("contents" nil t muse-publish-contents-tag)
243 ("verse" t nil muse-publish-verse-tag)
244 ("example" t nil muse-publish-example-tag)
245 ("code" t nil muse-publish-code-tag)
246 ("quote" t nil muse-publish-quote-tag)
247 ("literal" t nil muse-publish-mark-read-only)
248 ("verbatim" t nil muse-publish-verbatim-tag)
249 ("lisp" t t muse-publish-lisp-tag)
250 ("class" t t muse-publish-class-tag)
251 ("command" t t muse-publish-command-tag)
252 ("perl" t t muse-publish-perl-tag)
253 ("python" t t muse-publish-python-tag)
254 ("ruby" t t muse-publish-ruby-tag)
255 ("comment" t nil muse-publish-comment-tag)
256 ("include" nil t muse-publish-include-tag)
257 ("markup" t t muse-publish-mark-up-tag))
258 "A list of tag specifications, for specially marking up text.
259 XML-style tags are the best way to add custom markup to Muse.
260 This is easily accomplished by customizing this list of markup tags.
262 For each entry, the name of the tag is given, whether it expects
263 a closing tag and/or an optional set of attributes, and a
264 function that performs whatever action is desired within the
265 delimited region.
267 The tags themselves are deleted during publishing, before the
268 function is called. The function is called with three arguments,
269 the beginning and end of the region surrounded by the tags. If
270 properties are allowed, they are passed as a third argument in
271 the form of an alist. The `end' argument to the function is
272 always a marker.
274 Point is always at the beginning of the region within the tags, when
275 the function is called. Wherever point is when the function finishes
276 is where tag markup will resume.
278 These tag rules are processed once at the beginning of markup, and
279 once at the end, to catch any tags which may have been inserted
280 in-between."
281 :type '(repeat (list (string :tag "Markup tag")
282 (boolean :tag "Expect closing tag" :value t)
283 (boolean :tag "Parse attributes" :value nil)
284 function))
285 :group 'muse-publish)
287 (defcustom muse-publish-markup-specials nil
288 "A table of characters which must be represented specially."
289 :type '(alist :key-type character :value-type string)
290 :group 'muse-publish)
292 (defvar muse-publishing-p nil
293 "Set to t while a page is being published.")
294 (defvar muse-batch-publishing-p nil
295 "Set to t while a page is being batch published.")
296 (defvar muse-publishing-styles nil
297 "The publishing styles that Muse recognizes.
298 This is automatically generated when loading publishing styles.")
299 (defvar muse-publishing-current-file nil
300 "The file that is currently being published.")
301 (defvar muse-publishing-current-output-path nil
302 "The path where the current file will be published to.")
303 (defvar muse-publishing-current-style nil
304 "The style of the file that is currently being published.")
305 (defvar muse-publishing-directives nil
306 "An alist of publishing directives from the top of a file.")
307 (defvar muse-publish-generate-contents nil
308 "Non-nil if a table of contents should be generated.
309 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
310 tell where the <contents> was seen, and to what depth the
311 contents were requested.")
312 (defvar muse-publishing-last-position nil
313 "Last position of the point when publishing.
314 This is used to make sure that publishing doesn't get stalled.")
316 ;; Functions for handling style information
318 (defsubst muse-style (&optional style)
319 "Resolve the given STYLE into a Muse style, if it is a string."
320 (if (null style)
321 muse-publishing-current-style
322 (if (stringp style)
323 (assoc style muse-publishing-styles)
324 (muse-assert (consp style))
325 style)))
327 (defun muse-define-style (name &rest elements)
328 (let ((entry (assoc name muse-publishing-styles)))
329 (if entry
330 (setcdr entry elements)
331 (setq muse-publishing-styles
332 (cons (append (list name) elements)
333 muse-publishing-styles)))))
335 (defun muse-derive-style (new-name base-name &rest elements)
336 (apply 'muse-define-style new-name
337 (append elements (list :base base-name))))
339 (defsubst muse-get-keyword (keyword list &optional direct)
340 (let ((value (cadr (memq keyword list))))
341 (if (and (not direct) (symbolp value))
342 (symbol-value value)
343 value)))
345 (defun muse-style-elements-list (elem &optional style)
346 "Return a list all references to ELEM in STYLE, including base styles.
347 If STYLE is not specified, use current style."
348 (let (base elements)
349 (while style
350 (setq style (muse-style style))
351 (setq elements (append elements
352 (muse-get-keyword elem style)))
353 (setq style (muse-get-keyword :base style)))
354 elements))
356 (defun muse-style-element (elem &optional style direct)
357 "Search for ELEM in STYLE, including base styles.
358 If STYLE is not specified, use current style."
359 (setq style (muse-style style))
360 (let ((value (muse-get-keyword elem style direct)))
361 (if value
362 value
363 (let ((base (muse-get-keyword :base style)))
364 (if base
365 (muse-style-element elem base direct))))))
367 (defun muse-find-markup-element (keyword ident style)
368 (let ((def (assq ident (muse-style-element keyword style))))
369 (if def
370 (cdr def)
371 (let ((base (muse-style-element :base style)))
372 (if base
373 (muse-find-markup-element keyword ident base))))))
375 (defun muse-markup-text (ident &rest args)
376 "Insert ARGS into the text markup associated with IDENT.
377 If the markup text has sections like %N%, this will be replaced
378 with the N-1th argument in ARGS. After that, `format' is applied
379 to the text with ARGS as parameters."
380 (let ((text (muse-find-markup-element :strings ident (muse-style))))
381 (if (and text args)
382 (progn
383 (let (start repl-text)
384 (while (setq start (string-match "%\\([1-9][0-9]*\\)%" text start))
385 ;; escape '%' in the argument text, since we will be
386 ;; using format on it
387 (setq repl-text (muse-replace-regexp-in-string
388 "%" "%%"
389 (nth (1- (string-to-number
390 (match-string 1 text))) args)
391 t t)
392 start (+ start (length repl-text))
393 text (replace-match repl-text t t text))))
394 (apply 'format text args))
395 (or text ""))))
397 (defun muse-insert-markup (&rest args)
398 (let ((beg (point)))
399 (apply 'insert args)
400 (muse-publish-mark-read-only beg (point))))
402 (defun muse-find-markup-tag (keyword tagname style)
403 (let ((def (assoc tagname (muse-style-element keyword style))))
404 (or def
405 (let ((base (muse-style-element :base style)))
406 (if base
407 (muse-find-markup-tag keyword tagname base))))))
409 (defsubst muse-markup-tag-info (tagname &rest args)
410 (let ((tag-info (muse-find-markup-tag :tags tagname (muse-style))))
411 (or tag-info
412 (assoc (match-string 1) muse-publish-markup-tags))))
414 (defsubst muse-markup-function (category)
415 (let ((func (muse-find-markup-element :functions category (muse-style))))
416 (or func
417 (cdr (assq category muse-publish-markup-functions)))))
419 ;; Publishing routines
421 (defun muse-publish-markup (name rules)
422 (let* ((case-fold-search nil)
423 (inhibit-read-only t)
424 (limit (* (length rules) (point-max)))
425 (verbose (and muse-publish-report-threshhold
426 (> (point-max) muse-publish-report-threshhold)))
427 (base 0))
428 (while rules
429 (goto-char (point-min))
430 (let ((regexp (nth 1 (car rules)))
431 (group (nth 2 (car rules)))
432 (repl (nth 3 (car rules)))
433 pos)
434 (setq muse-publishing-last-position nil)
435 (if (symbolp regexp)
436 (setq regexp (symbol-value regexp)))
437 (if (and verbose (not muse-batch-publishing-p))
438 (message "Publishing %s...%d%%" name
439 (* (/ (float (+ (point) base)) limit) 100)))
440 (while (and regexp (setq pos (re-search-forward regexp nil t)))
441 (if (and verbose (not muse-batch-publishing-p))
442 (message "Publishing %s...%d%%" name
443 (* (/ (float (+ (point) base)) limit) 100)))
444 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
445 (match-beginning group)
446 (get-text-property (match-beginning group) 'read-only))
447 (let* (func
448 (text (cond
449 ((and (symbolp repl)
450 (setq func (muse-markup-function repl)))
451 (funcall func))
452 ((functionp repl)
453 (funcall repl))
454 ((symbolp repl)
455 (symbol-value repl))
456 (t repl))))
457 (if (stringp text)
458 (replace-match text t))))
459 (if (and muse-publishing-last-position
460 (= pos muse-publishing-last-position))
461 (if (eobp)
462 (setq regexp nil)
463 (forward-char 1)))
464 (setq muse-publishing-last-position pos)))
465 (setq rules (cdr rules)
466 base (+ base (point-max))))
467 (if (and verbose (not muse-batch-publishing-p))
468 (message "Publishing %s...done" name))))
470 (defcustom muse-publish-markup-header-footer-tags
471 '(("lisp" t nil muse-publish-lisp-tag)
472 ("markup" t t muse-publish-mark-up-tag))
473 "Tags used when publishing headers and footers.
474 See `muse-publish-markup-tags' for details."
475 :type '(repeat (list (string :tag "Markup tag")
476 (boolean :tag "Expect closing tag" :value t)
477 (boolean :tag "Parse attributes" :value nil)
478 function))
479 :group 'muse-publish)
481 (defun muse-insert-file-or-string (file-or-string &optional title)
482 (let ((beg (point)) end)
483 (if (and (not (string-equal file-or-string ""))
484 (not (string-match "\n" file-or-string))
485 (file-readable-p file-or-string))
486 (setq end (+ beg (cadr (insert-file-contents file-or-string))))
487 (insert file-or-string)
488 (setq end (point)))
489 (save-restriction
490 (narrow-to-region beg end)
491 (remove-text-properties (point-min) (point-max)
492 '(read-only nil rear-nonsticky nil))
493 (goto-char (point-min))
494 (let ((muse-inhibit-style-tags t))
495 (muse-publish-markup (or title "")
496 '((100 muse-tag-regexp 0
497 muse-publish-markup-tag)))))))
499 (defun muse-style-run-hooks (keyword style &rest args)
500 (catch 'handled
501 (while (and style
502 (setq style (muse-style style)))
503 (let ((func (muse-get-keyword keyword style t)))
504 (when (and func
505 (apply func args))
506 (throw 'handled t)))
507 (setq style (muse-style-element :base style)))))
509 (defun muse-publish-markup-region (beg end &optional title style)
510 "Apply the given STYLE's markup rules to the given region.
511 TITLE is used when indicating the publishing progress; it may be nil."
512 (unless title (setq title ""))
513 (unless style
514 (or (setq style muse-publishing-current-style)
515 (error "Cannot find any publishing styles to use")))
516 (save-restriction
517 (narrow-to-region beg end)
518 (muse-style-run-hooks :before style)
519 (muse-publish-markup
520 title
521 (sort (copy-alist (append muse-publish-markup-regexps
522 (muse-style-elements-list :regexps style)))
523 (function
524 (lambda (l r)
525 (< (car l) (car r))))))
526 (muse-style-run-hooks :before-end style)
527 (muse-publish-escape-specials (point-min) (point-max) nil 'document)))
529 (defun muse-publish-markup-buffer (title style)
530 "Apply the given STYLE's markup rules to the current buffer."
531 (setq style (muse-style style))
532 (let ((style-header (muse-style-element :header style))
533 (style-footer (muse-style-element :footer style))
534 (muse-publishing-current-style style)
535 (muse-publishing-directives
536 (list (cons "title" title)
537 (cons "author" (user-full-name))
538 (cons "date" (format-time-string
539 "%B %e, %Y"
540 (if muse-publishing-current-file
541 (nth 5 (file-attributes
542 muse-publishing-current-file))
543 (current-time))))))
544 (muse-publishing-p t)
545 (inhibit-read-only t))
546 (run-hooks 'muse-update-values-hook)
547 (run-hooks 'muse-before-publish-hook)
548 (muse-publish-markup-region (point-min) (point-max) title style)
549 (goto-char (point-min))
550 (when style-header
551 (muse-insert-file-or-string style-header title))
552 (goto-char (point-max))
553 (when style-footer
554 (muse-insert-file-or-string style-footer title))
555 (muse-style-run-hooks :after style)
556 (run-hooks 'muse-after-publish-hook)))
558 (defun muse-publish-markup-string (string &optional style)
559 "Markup STRING using the given STYLE's markup rules."
560 (setq style (muse-style style))
561 (muse-with-temp-buffer
562 (insert string)
563 (let ((muse-publishing-current-style style)
564 (muse-publishing-p t))
565 (muse-publish-markup "*string*" (muse-style-element :rules style)))
566 (buffer-string)))
568 ;; Commands for publishing files
570 (defsubst muse-publish-get-style (&optional styles)
571 (unless styles (setq styles muse-publishing-styles))
572 (if (= 1 (length styles))
573 (car styles)
574 (assoc (completing-read "Publish with style: " styles nil t)
575 styles)))
577 (defsubst muse-publish-get-output-dir (style)
578 (let ((default-directory (or (muse-style-element :path style)
579 default-directory)))
580 (muse-read-directory-name "Publish to directory: " nil default-directory)))
582 (defsubst muse-publish-get-info ()
583 (let ((style (muse-publish-get-style)))
584 (list style (muse-publish-get-output-dir style)
585 current-prefix-arg)))
587 (defsubst muse-publish-output-name (&optional file style)
588 (setq style (muse-style style))
589 (concat (muse-style-element :prefix style)
590 (muse-page-name file)
591 (muse-style-element :suffix style)))
593 (defsubst muse-publish-output-file (file &optional output-dir style)
594 (setq style (muse-style style))
595 (if output-dir
596 (expand-file-name (muse-publish-output-name file style) output-dir)
597 (concat (file-name-directory file)
598 (muse-publish-output-name file style))))
600 (defsubst muse-publish-link-name (&optional file style)
601 (setq style (muse-style style))
602 (concat (muse-style-element :prefix style)
603 (muse-page-name file)
604 (or (muse-style-element :link-suffix style)
605 (muse-style-element :suffix style))))
607 (defsubst muse-publish-link-file (file &optional output-dir style)
608 (setq style (muse-style style))
609 (if output-dir
610 (expand-file-name (muse-publish-link-name file style) output-dir)
611 (concat (file-name-directory file)
612 (muse-publish-link-name file style))))
614 (defsubst muse-publish-link-page (page)
615 (if (fboundp 'muse-project-link-page)
616 (muse-project-link-page page)
617 (muse-publish-link-file page)))
619 ;;;###autoload
620 (defun muse-publish-file (file style &optional output-dir force)
621 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
622 If the argument FORCE is nil, each file is only published if it is
623 newer than the published version. If the argument FORCE is non-nil,
624 the file is published no matter what."
625 (interactive (cons (read-file-name "Publish file: ")
626 (muse-publish-get-info)))
627 (let ((style-name style))
628 (setq style (muse-style style))
629 (unless style
630 (error "There is no style '%s' defined." style-name)))
631 (let* ((output-path (muse-publish-output-file file output-dir style))
632 (output-suffix (muse-style-element :osuffix style))
633 (muse-publishing-current-file file)
634 (muse-publishing-current-output-path output-path)
635 (target (if output-suffix
636 (concat (file-name-sans-extension output-path)
637 output-suffix)
638 output-path))
639 (threshhold (nth 7 (file-attributes file))))
640 (if (not threshhold)
641 (message "Please save %s before publishing" file)
642 (when (or force (file-newer-than-file-p file target))
643 (if (and muse-publish-report-threshhold
644 (> threshhold
645 muse-publish-report-threshhold))
646 (message "Publishing %s ..." file))
647 (muse-with-temp-buffer
648 (insert-file-contents file)
649 (muse-publish-markup-buffer (muse-page-name file) style)
650 (let ((backup-inhibited t))
651 (write-file output-path))
652 (muse-style-run-hooks :final style file output-path target))
653 t))))
655 ;;;###autoload
656 (defun muse-publish-this-file (style output-dir &optional force)
657 "Publish the currently-visited file.
658 Prompt for both the STYLE and OUTPUT-DIR if they are not
659 supplied."
660 (interactive (muse-publish-get-info))
661 (unless (muse-publish-file buffer-file-name style output-dir force)
662 (message (concat "The published version is up-to-date; use"
663 " C-u C-c C-T to force an update."))))
665 (defun muse-batch-publish-files ()
666 "Publish Muse files in batch mode."
667 (let ((muse-batch-publishing-p t)
668 style output-dir)
669 (setq style (car command-line-args-left)
670 command-line-args-left (cdr command-line-args-left)
671 output-dir (car command-line-args-left)
672 output-dir
673 (if (string-match "\\`--output-dir=" output-dir)
674 (prog1
675 (substring output-dir (match-end 0))
676 (setq command-line-args-left (cdr command-line-args-left)))))
677 (setq auto-mode-alist
678 (delete (cons (concat "\\." muse-file-extension "\\'")
679 'muse-mode-choose-mode)
680 auto-mode-alist))
681 (dolist (file command-line-args-left)
682 (muse-publish-file file style output-dir t))))
684 ;; Default publishing rules
686 (defun muse-publish-section-close (depth)
687 "Seach forward for the closing tag of given DEPTH."
688 (let (not-end)
689 (save-excursion
690 (while (and (setq not-end (re-search-forward
691 (concat "^\\*\\{1," (number-to-string depth)
692 "\\}\\s-+")
693 nil t))
694 (get-text-property (match-beginning 0) 'read-only)))
695 (if not-end
696 (forward-line 0)
697 (goto-char (point-max)))
698 (cond ((not (eq (char-before) ?\n))
699 (insert "\n\n"))
700 ((not (eq (char-before (1- (point))) ?\n))
701 (insert "\n")))
702 (muse-insert-markup (muse-markup-text 'section-close depth))
703 (insert "\n"))))
705 (defun muse-publish-markup-directive (&optional name value)
706 (unless name (setq name (match-string 1)))
707 (unless value (setq value (match-string 2)))
708 (let ((elem (assoc name muse-publishing-directives)))
709 (if elem
710 (setcdr elem value)
711 (setq muse-publishing-directives
712 (cons (cons name value)
713 muse-publishing-directives))))
714 ;; Make sure we don't ever try to move the point forward (past the
715 ;; beginning of buffer) while we're still searching for directives.
716 (setq muse-publishing-last-position nil)
717 (delete-region (match-beginning 0) (match-end 0)))
719 (defsubst muse-publishing-directive (name)
720 (cdr (assoc name muse-publishing-directives)))
722 (defun muse-publish-markup-anchor ()
723 (unless (get-text-property (match-end 1) 'muse-link)
724 (let ((text (muse-markup-text 'anchor (match-string 2))))
725 (unless (string= text "")
726 (save-match-data
727 (skip-chars-forward (concat muse-regexp-blank "\n"))
728 (muse-insert-markup text)))
729 (match-string 1))))
731 (defun muse-publish-markup-comment ()
732 (if (null muse-publish-comments-p)
734 (goto-char (match-end 0))
735 (muse-insert-markup (muse-markup-text 'comment-end))
736 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
737 (delete-region (match-beginning 0) (match-beginning 1))
738 (goto-char (match-beginning 0))
739 (muse-insert-markup (muse-markup-text 'comment-begin))))
741 (defvar muse-inhibit-style-tags nil
742 "If non-nil, do not search for style-specific tags.
743 This is used when publishing headers and footers.")
745 (defun muse-publish-markup-tag ()
746 (let ((tag-info (if muse-inhibit-style-tags
747 (assoc (match-string 1) muse-publish-markup-tags)
748 (muse-markup-tag-info (match-string 1)))))
749 (when (and tag-info
750 (not (get-text-property (match-beginning 0) 'read-only)))
751 (let ((closed-tag (match-string 3))
752 (start (match-beginning 0))
753 (beg (point))
754 end attrs)
755 (when (nth 2 tag-info)
756 (let ((attrstr (match-string 2)))
757 (while (and attrstr
758 (string-match (concat "\\([^"
759 muse-regexp-blank
760 "=\n]+\\)\\(=\"\\"
761 "([^\"]+\\)\"\\)?")
762 attrstr))
763 (let ((attr (cons (downcase
764 (muse-match-string-no-properties 1 attrstr))
765 (muse-match-string-no-properties 3 attrstr))))
766 (setq attrstr (replace-match "" t t attrstr))
767 (if attrs
768 (nconc attrs (list attr))
769 (setq attrs (list attr)))))))
770 (if (and (cadr tag-info) (not closed-tag))
771 (if (search-forward (concat "</" (car tag-info) ">") nil t)
772 (delete-region (match-beginning 0) (point))
773 (setq tag-info nil)))
774 (when tag-info
775 (setq end (point-marker))
776 (delete-region start beg)
777 (goto-char start)
778 (let ((args (list start end)))
779 (if (nth 2 tag-info)
780 (nconc args (list attrs)))
781 (let ((muse-inhibit-style-tags nil))
782 ;; remove the inhibition
783 (apply (nth 3 tag-info) args)))))))
784 nil)
786 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
787 "Escape specials from BEG to END using style-specific :specials.
788 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
789 CONTEXT is used to figure out what kind of specials to escape.
791 The following contexts exist in Muse.
792 'underline _underlined text_
793 'literal =monospaced text= or <code> region (monospaced, escaped)
794 'emphasis *emphasized text*
795 'email email@example.com
796 'url http://example.com
797 'url-desc [[...][description of an explicit link]]
798 'example <example> region (monospaced, block context, escaped)
799 'verbatim <verbatim> region (escaped)
800 'document normal text"
801 (let ((specials (muse-style-element :specials nil t)))
802 (cond ((functionp specials)
803 (setq specials (funcall specials context)))
804 ((symbolp specials)
805 (setq specials (symbol-value specials))))
806 (if (functionp specials)
807 (funcall specials beg end ignore-read-only)
808 (save-excursion
809 (save-restriction
810 (narrow-to-region beg end)
811 (goto-char (point-min))
812 (while (< (point) (point-max))
813 (if (and (not ignore-read-only)
814 (get-text-property (point) 'read-only))
815 (goto-char (or (next-single-property-change (point) 'read-only)
816 (point-max)))
817 (let ((repl (or (assoc (char-after) specials)
818 (assoc (char-after)
819 muse-publish-markup-specials))))
820 (if (null repl)
821 (forward-char 1)
822 (delete-char 1)
823 (insert-before-markers (cdr repl)))))))))))
825 (defun muse-publish-markup-word ()
826 (let* ((beg (match-beginning 2))
827 (end (1- (match-end 2)))
828 (leader (buffer-substring-no-properties beg end))
829 open-tag close-tag mark-read-only loc context)
830 (cond
831 ((string= leader "_")
832 (setq context 'underline
833 open-tag (muse-markup-text 'begin-underline)
834 close-tag (muse-markup-text 'end-underline)))
835 ((string= leader "=")
836 (setq context 'literal
837 open-tag (muse-markup-text 'begin-literal)
838 close-tag (muse-markup-text 'end-literal))
839 (setq mark-read-only t))
841 (let ((l (length leader)))
842 (setq context 'emphasis)
843 (cond
844 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
845 close-tag (muse-markup-text 'end-emph)))
846 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
847 close-tag (muse-markup-text 'end-more-emph)))
848 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
849 close-tag (muse-markup-text 'end-most-emph)))
850 (t (setq context nil))))))
851 (if (and context
852 (not (get-text-property beg 'muse-link))
853 (setq loc (search-forward leader nil t))
854 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
855 (not (eq (char-syntax (char-before (point))) ?\ ))
856 (not (get-text-property (point) 'muse-link)))
857 (progn
858 (replace-match "")
859 (delete-region beg end)
860 (setq end (point-marker))
861 (muse-insert-markup close-tag)
862 (goto-char beg)
863 (muse-insert-markup open-tag)
864 (setq beg (point))
865 (when mark-read-only
866 (muse-publish-escape-specials beg end t context)
867 (muse-publish-mark-read-only beg end)))
868 (backward-char))
869 nil))
871 (defun muse-publish-markup-emdash ()
872 (unless (get-text-property (match-beginning 0) 'muse-link)
873 (let ((prespace (match-string 1))
874 (postspace (match-string 2)))
875 (delete-region (match-beginning 0) (match-end 0))
876 (muse-insert-markup (muse-markup-text 'emdash prespace postspace))
877 (when (eq (char-after) ?\<)
878 (insert ?\n)))))
880 (defun muse-publish-markup-enddots ()
881 (unless (get-text-property (match-beginning 0) 'muse-link)
882 (delete-region (match-beginning 0) (match-end 0))
883 (muse-insert-markup (muse-markup-text 'enddots))))
885 (defun muse-publish-markup-dots ()
886 (unless (get-text-property (match-beginning 0) 'muse-link)
887 (delete-region (match-beginning 0) (match-end 0))
888 (muse-insert-markup (muse-markup-text 'dots))))
890 (defun muse-publish-markup-rule ()
891 (unless (get-text-property (match-beginning 0) 'muse-link)
892 (delete-region (match-beginning 0) (match-end 0))
893 (muse-insert-markup (muse-markup-text 'rule))))
895 (defun muse-publish-markup-no-break-space ()
896 (unless (get-text-property (match-beginning 0) 'muse-link)
897 (delete-region (match-beginning 0) (match-end 0))
898 (muse-insert-markup (muse-markup-text 'no-break-space))))
900 (defun muse-publish-markup-heading ()
901 (let* ((len (length (match-string 1)))
902 (start (muse-markup-text
903 (cond ((= len 1) 'section)
904 ((= len 2) 'subsection)
905 ((= len 3) 'subsubsection)
906 (t 'section-other))
907 len))
908 (end (muse-markup-text
909 (cond ((= len 1) 'section-end)
910 ((= len 2) 'subsection-end)
911 ((= len 3) 'subsubsection-end)
912 (t 'section-other-end))
913 len)))
914 (delete-region (match-beginning 0) (match-end 0))
915 (muse-insert-markup start)
916 (end-of-line)
917 (when end
918 (muse-insert-markup end))
919 (muse-publish-section-close len)))
921 (defvar muse-publish-footnotes nil)
923 (defun muse-publish-markup-footnote ()
924 "Scan ahead and snarf up the footnote body"
925 (cond
926 ((get-text-property (match-beginning 0) 'muse-link)
927 nil)
928 ((= (muse-line-beginning-position) (match-beginning 0))
931 (let ((footnote (save-match-data
932 (string-to-number (match-string 1))))
933 footnotemark)
934 (delete-region (match-beginning 0) (match-end 0))
935 (save-excursion
936 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
937 (let* ((start (match-beginning 0))
938 (beg (goto-char (match-end 0)))
939 (end (save-excursion
940 (if (search-forward "\n\n" nil t)
941 (copy-marker (match-beginning 0))
942 (goto-char (point-max))
943 (skip-chars-backward "\n")
944 (point-marker)))))
945 (while (re-search-forward
946 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
947 end t)
948 (replace-match "\\1" t))
949 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
950 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
951 (if (string= "" footnotemark-cmd)
952 (setq footnotemark
953 (concat (muse-markup-text 'footnote)
954 (buffer-substring-no-properties beg end)
955 (muse-markup-text 'footnote-end)))
956 (setq footnotemark (format footnotemark-cmd footnote
957 footnotemark-end-cmd))
958 (unless muse-publish-footnotes
959 (set (make-local-variable 'muse-publish-footnotes)
960 (make-vector 256 nil)))
961 (unless (aref muse-publish-footnotes footnote)
962 (setq footnotemark
963 (concat
964 footnotemark
965 (concat (format (muse-markup-text 'footnotetext)
966 footnote)
967 (buffer-substring-no-properties beg end)
968 (muse-markup-text 'footnotetext-end))))
969 (aset muse-publish-footnotes footnote footnotemark))))
970 (goto-char end)
971 (skip-chars-forward "\n")
972 (delete-region start (point)))))
973 (muse-insert-markup (or footnotemark footnote))))))
975 (defun muse-publish-markup-fn-sep ()
976 (delete-region (match-beginning 0) (match-end 0))
977 (muse-insert-markup (muse-markup-text 'fn-sep)))
979 (defun muse-insert-markup-end-list (&rest args)
980 (let ((beg (point)))
981 (apply 'insert args)
982 (add-text-properties beg (point) '(end-list t))
983 (muse-publish-mark-read-only beg (point))))
985 (defun muse-publish-surround-dl (indent post-indent)
986 (let* ((beg-item (muse-markup-text 'begin-dl-item))
987 (end-item (muse-markup-text 'end-dl-item))
988 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
989 (end-ddt (muse-markup-text 'end-ddt))
990 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
991 (end-dde (muse-markup-text 'end-dde))
992 (continue t)
993 def-on-same-line beg)
994 (while continue
995 ;; envelope this as one term+definitions unit -- HTML does not
996 ;; need this, but DocBook and Muse's custom XML format do
997 (muse-insert-markup beg-item)
998 (when (looking-at muse-dl-term-regexp)
999 ;; find the term and wrap it with published markup
1000 (setq beg (point))
1001 (goto-char (match-end 1))
1002 (delete-region (point) (match-end 0))
1003 (muse-insert-markup-end-list end-ddt)
1004 ;; if definition is immediately after term, move to next line
1005 (unless (eq (char-after) ?\n)
1006 (insert ?\n))
1007 (save-excursion
1008 (goto-char beg)
1009 (delete-region (point) (match-beginning 1))
1010 (muse-insert-markup beg-ddt)))
1011 (setq beg (point)
1012 ;; move past current item
1013 continue (muse-forward-list-item 'dl-term indent))
1014 (save-restriction
1015 (narrow-to-region beg (point))
1016 (goto-char (point-min))
1017 ;; publish each definition that we find, defaulting to an
1018 ;; empty definition if none are found
1019 (muse-publish-surround-text beg-dde end-dde
1020 (lambda (indent)
1021 (muse-forward-list-item 'dl-entry indent))
1022 nil nil t)
1023 (goto-char (point-max))
1024 (skip-chars-backward (concat muse-regexp-blank "\n"))
1025 (muse-insert-markup-end-list end-item)
1026 (when continue
1027 (goto-char (point-max)))))))
1029 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent determine-indent)
1030 (let ((continue t)
1031 (list-item (format muse-list-item-regexp
1032 (concat "[" muse-regexp-blank "]*")))
1033 init-indent beg)
1034 (unless indent
1035 (setq indent (concat "[" muse-regexp-blank "]+")))
1036 (if post-indent
1037 (setq post-indent (concat " \\{0," (number-to-string post-indent)
1038 "\\}"))
1039 (setq post-indent ""))
1040 (while continue
1041 (muse-insert-markup beg-tag)
1042 (setq beg (point)
1043 ;; move past current item; continue is non-nil if there
1044 ;; are more like items to be processed
1045 continue (funcall move-func indent))
1046 (save-restriction
1047 (when determine-indent
1048 ;; if the caller doesn't know how much indentation
1049 ;; to use, figure it out ourselves
1050 (if (not continue)
1051 (setq indent "")
1052 (save-match-data
1053 ;; snarf all leading whitespace
1054 (let ((this-indent (and (match-beginning 2)
1055 (buffer-substring (match-beginning 1)
1056 (match-beginning 2)))))
1057 (when (and this-indent
1058 (not (string= this-indent "")))
1059 (setq indent this-indent
1060 determine-indent nil))))))
1061 (when continue
1062 ;; remove list markup if we encountered another item of the
1063 ;; same type
1064 (replace-match "" t t nil 1))
1065 (narrow-to-region beg (point))
1066 ;; narrow to current item
1067 (goto-char (point-min))
1068 (forward-line 1)
1069 (let ((list-nested nil)
1070 (indent-found nil)
1071 (post-indent post-indent))
1072 (while (< (point) (point-max))
1073 (when (and (looking-at list-item)
1074 (not (or (get-text-property
1075 (muse-list-item-critical-point) 'read-only)
1076 (get-text-property
1077 (muse-list-item-critical-point) 'muse-link))))
1078 ;; if we encounter a list item, allow no post-indent
1079 ;; space
1080 (setq list-nested t))
1081 (when (looking-at (concat indent "\\("
1082 (or (and list-nested "")
1083 post-indent)
1084 "\\)"))
1085 ;; if list is not nested, remove indentation
1086 (unless indent-found
1087 (setq post-indent (match-string 1)
1088 indent-found t))
1089 (replace-match ""))
1090 (forward-line 1)))
1091 (skip-chars-backward (concat muse-regexp-blank "\n"))
1092 (muse-insert-markup-end-list end-tag)
1093 (when continue
1094 (goto-char (point-max)))))))
1096 (defun muse-publish-markup-list ()
1097 "Markup a list entry.
1098 This function works by marking up items of the same list level
1099 and type, respecting the end-of-list property."
1100 (let* ((str (match-string 1))
1101 (type (muse-list-item-type str))
1102 (indent (buffer-substring (muse-line-beginning-position)
1103 (match-beginning 1)))
1104 (post-indent (length str))
1105 (last (match-beginning 0)))
1106 (cond
1107 ((or (get-text-property (muse-list-item-critical-point) 'read-only)
1108 (get-text-property (muse-list-item-critical-point) 'muse-link))
1109 nil)
1110 ((eq type 'ul)
1111 (unless (eq (char-after (match-end 1)) ?-)
1112 (delete-region (match-beginning 0) (match-end 0))
1113 (muse-insert-markup (muse-markup-text 'begin-uli))
1114 (save-excursion
1115 (muse-publish-surround-text
1116 (muse-markup-text 'begin-uli-item)
1117 (muse-markup-text 'end-uli-item)
1118 (lambda (indent)
1119 (muse-forward-list-item 'ul indent))
1120 indent post-indent)
1121 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1122 (forward-line 1)))
1123 ((eq type 'ol)
1124 (delete-region (match-beginning 0) (match-end 0))
1125 (muse-insert-markup (muse-markup-text 'begin-oli))
1126 (save-excursion
1127 (muse-publish-surround-text
1128 (muse-markup-text 'begin-oli-item)
1129 (muse-markup-text 'end-oli-item)
1130 (lambda (indent)
1131 (muse-forward-list-item 'ol indent))
1132 indent post-indent)
1133 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1134 (forward-line 1))
1135 ((not (string= (match-string 2) ""))
1136 ;; must have an initial term
1137 (goto-char (match-beginning 0))
1138 (muse-insert-markup (muse-markup-text 'begin-dl))
1139 (save-excursion
1140 (muse-publish-surround-dl indent post-indent)
1141 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1142 (forward-line 1))))
1143 nil)
1145 (defun muse-publish-markup-quote ()
1146 "Markup a quoted paragraph.
1147 The reason this function is so funky, is to prevent text properties
1148 like read-only from being inadvertently deleted."
1149 (let* ((ws (match-string 1))
1150 (centered (>= (string-width ws) 6))
1151 (begin-elem (if centered 'begin-center 'begin-quote-item))
1152 (end-elem (if centered 'end-center 'end-quote-item)))
1153 (replace-match "" t t nil 1)
1154 (unless centered
1155 (muse-insert-markup (muse-markup-text 'begin-quote)))
1156 (muse-publish-surround-text (muse-markup-text begin-elem)
1157 (muse-markup-text end-elem)
1158 (function (lambda (indent)
1159 (muse-forward-paragraph)
1160 nil)))
1161 (unless centered
1162 (muse-insert-markup (muse-markup-text 'end-quote)))))
1164 (defun muse-publish-markup-leading-space (markup-space multiple)
1165 (let (count)
1166 (when (and markup-space
1167 (>= (setq count (skip-chars-forward " ")) 0))
1168 (delete-region (muse-line-beginning-position) (point))
1169 (while (> count 0)
1170 (muse-insert-markup markup-space)
1171 (setq count (- count multiple))))))
1173 (defun muse-publish-markup-verse ()
1174 (let ((leader (match-string 0)))
1175 (goto-char (match-beginning 0))
1176 (muse-insert-markup (muse-markup-text 'begin-verse))
1177 (while (looking-at leader)
1178 (replace-match "")
1179 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1180 (let ((beg (point)))
1181 (end-of-line)
1182 (cond
1183 ((bolp)
1184 (let ((text (muse-markup-text 'empty-verse-line)))
1185 (when text (muse-insert-markup text))))
1186 ((save-excursion
1187 (save-match-data
1188 (forward-line 1)
1189 (or (looking-at (concat leader "["
1190 muse-regexp-blank
1191 "]*$"))
1192 (not (looking-at leader)))))
1193 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1194 (end-text (muse-markup-text 'end-last-stanza-line)))
1195 (when end-text (muse-insert-markup end-text))
1196 (goto-char beg)
1197 (when begin-text (muse-insert-markup begin-text))
1198 (end-of-line)))
1200 (let ((begin-text (muse-markup-text 'begin-verse-line))
1201 (end-text (muse-markup-text 'end-verse-line)))
1202 (when end-text (muse-insert-markup end-text))
1203 (goto-char beg)
1204 (when begin-text (muse-insert-markup begin-text))
1205 (end-of-line))))
1206 (forward-line 1))))
1207 (muse-insert-markup (muse-markup-text 'end-verse))
1208 (insert ?\n))
1210 (defun muse-publish-table-fields (beg end)
1211 "Parse given region as a table, returning a cons cell.
1212 The car is the length of the longest row.
1214 The cdr is a list of the fields of the table, with the first
1215 element indicating the type of the row:
1216 1: body, 2: header, 3: footer.
1218 The existing region will be removed, except for initial blank lines."
1219 (unless (muse-publishing-directive "disable-tables")
1220 (let ((longest 0)
1221 (left 0)
1222 fields field-list)
1223 (save-restriction
1224 (narrow-to-region beg end)
1225 (goto-char (point-min))
1226 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1227 (forward-line 1))
1228 (setq beg (point))
1229 (while (= left 0)
1230 (when (looking-at muse-table-line-regexp)
1231 (setq fields (cons (length (match-string 1))
1232 (split-string (match-string 0)
1233 muse-table-field-regexp))
1234 field-list (cons fields field-list)
1235 longest (max (length fields) longest)))
1236 (setq left (forward-line 1))))
1237 (delete-region beg end)
1238 (if (= longest 0)
1239 (cons 0 nil)
1240 (cons (1- longest) (nreverse field-list))))))
1242 (defun muse-publish-markup-table ()
1243 "Style does not support tables.")
1245 (defun muse-publish-escape-specials-in-string (string &optional context)
1246 "Escape specials in STRING using style-specific :specials.
1247 CONTEXT is used to figure out what kind of specials to escape.
1249 See the documentation of the `muse-publish-escape-specials'
1250 function for the list of available contexts."
1251 (unless string
1252 (setq string ""))
1253 (let ((specials (muse-style-element :specials nil t)))
1254 (cond ((functionp specials)
1255 (setq specials (funcall specials context)))
1256 ((symbolp specials)
1257 (setq specials (symbol-value specials))))
1258 (if (functionp specials)
1259 (funcall specials string)
1260 (apply (function concat)
1261 (mapcar
1262 (lambda (ch)
1263 (let ((repl (or (assoc ch specials)
1264 (assoc ch muse-publish-markup-specials))))
1265 (if (null repl)
1266 (char-to-string ch)
1267 (cdr repl))))
1268 (append string nil))))))
1270 (defun muse-publish-markup-email ()
1271 (let* ((beg (match-end 1))
1272 (addr (buffer-substring-no-properties beg (match-end 0))))
1273 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1274 (goto-char beg)
1275 (delete-region beg (match-end 0))
1276 (if (or (eq (char-before (match-beginning 0)) ?\")
1277 (eq (char-after (match-end 0)) ?\"))
1278 (insert addr)
1279 (insert (format (muse-markup-text 'email-addr) addr addr)))
1280 (muse-publish-mark-read-only beg (point))))
1282 (defun muse-publish-classify-url (target)
1283 "Transform anchors and get published name, if TARGET is a page.
1284 The return value is a cons cell. The car is the type of link,
1285 the cadr is the page name, and the cddr is the anchor."
1286 (save-match-data
1287 (cond ((or (null target) (string= target ""))
1288 nil)
1289 ((string-match muse-url-regexp target)
1290 (cons 'url (cons target nil)))
1291 ((string-match muse-image-regexp target)
1292 (cons 'image (cons target nil)))
1293 ((string-match muse-file-regexp target)
1294 (cons 'file (cons target nil)))
1295 ((string-match "#" target)
1296 (if (eq (aref target 0) ?\#)
1297 (cons 'anchor-ref (cons nil (substring target 1)))
1298 (cons 'link-and-anchor
1299 (cons (muse-publish-link-page
1300 (substring target 0 (match-beginning 0)))
1301 (substring target (match-end 0))))))
1303 (cons 'link (cons (muse-publish-link-page target) nil))))))
1305 (defun muse-publish-url (url &optional desc explicit)
1306 "Resolve a URL into its final <a href> form."
1307 (let ((orig-url url)
1308 type anchor)
1309 (dolist (transform muse-publish-url-transforms)
1310 (setq url (save-match-data (when url (funcall transform url explicit)))))
1311 (dolist (transform muse-publish-desc-transforms)
1312 (setq desc (save-match-data
1313 (when desc (funcall transform desc explicit)))))
1314 (when desc
1315 (setq desc (muse-link-unescape desc))
1316 (setq desc (muse-publish-escape-specials-in-string desc 'url-desc)))
1317 (setq orig-url
1318 (muse-publish-escape-specials-in-string orig-url 'url-desc))
1319 (let ((target (muse-publish-classify-url url)))
1320 (setq type (car target)
1321 url (muse-publish-escape-specials-in-string (cadr target) 'url)
1322 anchor (muse-publish-escape-specials-in-string
1323 (cddr target) 'url)))
1324 (cond ((eq type 'anchor-ref)
1325 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1326 ((string= url "")
1327 desc)
1328 ((eq type 'image)
1329 (if desc
1330 (muse-markup-text 'image-with-desc url desc)
1331 (muse-markup-text 'image-link url)))
1332 ((eq type 'link-and-anchor)
1333 (muse-markup-text 'link-and-anchor url anchor
1334 (or desc orig-url)))
1335 ((and desc (string-match muse-image-regexp desc))
1336 (muse-markup-text 'url-with-image url desc))
1337 ((eq type 'link)
1338 (muse-markup-text 'link url (or desc orig-url)))
1340 (or (and desc
1341 (let ((text (muse-markup-text 'url-and-desc url desc)))
1342 (and (not (string= text ""))
1343 text)))
1344 (muse-markup-text 'url url (or desc orig-url)))))))
1346 (defun muse-publish-insert-url (url &optional desc explicit)
1347 "Resolve a URL into its final <a href> form."
1348 (delete-region (match-beginning 0) (match-end 0))
1349 (let ((text (muse-publish-url url desc explicit)))
1350 (when text
1351 (muse-insert-markup text))))
1353 (defun muse-publish-markup-link ()
1354 (let (desc explicit link)
1355 (setq explicit (save-match-data
1356 (if (string-match muse-explicit-link-regexp
1357 (match-string 0))
1358 t nil)))
1359 (setq desc (if explicit
1360 (or (match-string 2) (match-string 1))
1361 (match-string 0)))
1362 (setq link (if explicit
1363 (muse-handle-explicit-link (match-string 1))
1364 (muse-handle-implicit-link (match-string 0))))
1365 (when (and link
1366 (or explicit
1367 (not (or (eq (char-before (match-beginning 0)) ?\")
1368 (eq (char-after (match-end 0)) ?\")))))
1369 ;; if explicit link has no user-provided description, treat it
1370 ;; as if it were an implicit link
1371 (when (and explicit (not (match-string 2)))
1372 (setq explicit nil))
1373 (muse-publish-insert-url link desc explicit))))
1375 (defun muse-publish-markup-url ()
1376 (unless (or (eq (char-before (match-beginning 0)) ?\")
1377 (eq (char-after (match-end 0)) ?\"))
1378 (muse-publish-insert-url (match-string 0))))
1380 ;; Default publishing tags
1382 (defcustom muse-publish-contents-depth 2
1383 "The number of heading levels to include with <contents> tags."
1384 :type 'integer
1385 :group 'muse-publish)
1387 (defun muse-publish-contents-tag (beg end attrs)
1388 (set (make-local-variable 'muse-publish-generate-contents)
1389 (cons (copy-marker (point) t)
1390 (let ((depth (cdr (assoc "depth" attrs))))
1391 (or (and depth (string-to-number depth))
1392 muse-publish-contents-depth)))))
1394 (defun muse-publish-verse-tag (beg end)
1395 (save-excursion
1396 (save-restriction
1397 (narrow-to-region beg end)
1398 (goto-char (point-min))
1399 (while (eq ?\ (char-syntax (char-after)))
1400 (delete-char 1))
1401 (while (< (point) (point-max))
1402 (insert "> ")
1403 (forward-line))
1404 (if (eq ?\ (char-syntax (char-before)))
1405 (delete-char -1)))))
1407 (defun muse-publish-mark-read-only (beg end)
1408 "Add read-only properties to the given region."
1409 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1410 nil)
1412 (defun muse-publish-mark-link (&optional beg end)
1413 "Indicate that the given region is a Muse link, so that other
1414 markup elements respect it. If a region is not specified, use
1415 the 0th match data to determine it.
1417 This is usually applied to explicit links."
1418 (unless beg (setq beg (match-beginning 0)))
1419 (unless end (setq end (match-end 0)))
1420 (add-text-properties beg end '(muse-link t))
1421 nil)
1423 (defun muse-publish-quote-tag (beg end)
1424 (save-excursion
1425 (save-restriction
1426 (narrow-to-region beg end)
1427 (muse-insert-markup (muse-markup-text 'begin-quote))
1428 (muse-publish-surround-text (muse-markup-text 'begin-quote-item)
1429 (muse-markup-text 'end-quote-item)
1430 (function (lambda (indent)
1431 (muse-forward-paragraph)
1432 (goto-char (match-end 0))
1433 (< (point) (point-max)))))
1434 (muse-insert-markup (muse-markup-text 'end-quote)))))
1436 (defun muse-publish-code-tag (beg end)
1437 (muse-publish-escape-specials beg end nil 'literal)
1438 (goto-char beg)
1439 (insert (muse-markup-text 'begin-literal))
1440 (goto-char end)
1441 (insert (muse-markup-text 'end-literal))
1442 (muse-publish-mark-read-only beg (point)))
1444 (defun muse-publish-example-tag (beg end)
1445 (muse-publish-escape-specials beg end nil 'example)
1446 (goto-char beg)
1447 (insert (muse-markup-text 'begin-example))
1448 (goto-char end)
1449 (insert (muse-markup-text 'end-example))
1450 (muse-publish-mark-read-only beg (point)))
1452 (defun muse-publish-verbatim-tag (beg end)
1453 (muse-publish-escape-specials beg end nil 'verbatim)
1454 (muse-publish-mark-read-only beg end))
1456 (defalias 'muse-publish-class-tag 'ignore)
1458 (defun muse-publish-examplify-buffer ()
1459 "Transform the current buffer as if it were an <example> region."
1460 (let ((end (progn (goto-char (point-max)) (point-marker))))
1461 (muse-publish-example-tag (point-min) end)))
1463 (defun muse-publish-versify-buffer ()
1464 "Transform the current buffer as if it were a <verse> region."
1465 (muse-publish-verse-tag (point-min) (point-max))
1466 (muse-publish-markup ""
1467 `((100 ,(concat "^[" muse-regexp-blank "]*> ") 0
1468 muse-publish-markup-verse)))
1469 (goto-char (point-min)))
1471 (defmacro muse-publish-markup-attribute (beg end attrs reinterp &rest body)
1472 "Evaluate BODY within the bounds of BEG and END.
1473 ATTRS is an alist. Only the \"markup\" element of ATTRS is acted
1476 If it is omitted, publish the region with the normal Muse rules.
1477 If RE-INTERP is specified, this is done immediately in a new
1478 publishing process. Currently, RE-INTERP is specified only by
1479 the <include> tag.
1481 If \"nil\", do not mark up the region at all, but prevent it from
1482 being further interpreted by Muse.
1484 If \"example\", treat the region as if it was surrounded by the
1485 <example> tag.
1487 If \"verse\", treat the region as if it was surrounded by the
1488 <verse> tag, to preserve newlines.
1490 Otherwise, it should be the name of a function to call in the
1491 narrowed region after evaluating BODY."
1492 (let ((markup (make-symbol "markup"))
1493 (markup-function (make-symbol "markup-function")))
1494 `(let ((,markup (cdr (assoc "markup" ,attrs))))
1495 (save-restriction
1496 (narrow-to-region ,beg ,end)
1497 (goto-char (point-min))
1498 ,@body
1499 (if (not ,markup)
1500 (when ,reinterp
1501 (muse-publish-markup-region (point-min) (point-max))
1502 (muse-publish-mark-read-only (point-min) (point-max))
1503 (goto-char (point-max)))
1504 (let ((,markup-function (read ,markup)))
1505 (cond ((eq ,markup-function 'example)
1506 (setq ,markup-function #'muse-publish-examplify-buffer))
1507 ((eq ,markup-function 'verse)
1508 (setq ,markup-function #'muse-publish-versify-buffer))
1509 ((and ,markup-function (not (functionp ,markup-function)))
1510 (error "Invalid markup function `%s'" ,markup))
1511 (t nil))
1512 (if ,markup-function
1513 (funcall ,markup-function)
1514 (muse-publish-mark-read-only (point-min) (point-max))
1515 (goto-char (point-max)))))))))
1517 (put 'muse-publish-markup-attribute 'lisp-indent-function 4)
1518 (put 'muse-publish-markup-attribute 'edebug-form-spec
1519 '(form form form form body))
1521 (defun muse-publish-lisp-tag (beg end attrs)
1522 (muse-publish-markup-attribute beg end attrs nil
1523 (save-excursion
1524 (let ((str (muse-eval-lisp
1525 (prog1
1526 (concat "(progn "
1527 (buffer-substring-no-properties (point-min)
1528 (point-max))
1529 ")")
1530 (delete-region beg end)))))
1531 (set-text-properties 0 (length str) nil str)
1532 (insert str)))))
1534 (defun muse-publish-command-tag (beg end attrs)
1535 (muse-publish-markup-attribute beg end attrs nil
1536 (while (looking-at "\\s-*$")
1537 (forward-line))
1538 (let ((interp (cdr (assoc "interp" attrs))))
1539 (if interp
1540 (shell-command-on-region (point) (point-max) interp t t)
1541 (shell-command
1542 (prog1
1543 (buffer-substring-no-properties (point) (point-max))
1544 (delete-region (point-min) (point-max)))
1545 t)))
1546 ;; make sure there is a newline at end
1547 (goto-char (point-max))
1548 (forward-line 0)
1549 (unless (looking-at "\\s-*$")
1550 (goto-char (point-max))
1551 (insert ?\n))
1552 (goto-char (point-min))))
1554 (defun muse-publish-perl-tag (beg end attrs)
1555 (muse-publish-command-tag beg end
1556 (cons (cons "interp" (executable-find "perl"))
1557 attrs)))
1559 (defun muse-publish-python-tag (beg end attrs)
1560 (muse-publish-command-tag beg end
1561 (cons (cons "interp" (executable-find "python"))
1562 attrs)))
1564 (defun muse-publish-ruby-tag (beg end attrs)
1565 (muse-publish-command-tag beg end
1566 (cons (cons "interp" (executable-find "ruby"))
1567 attrs)))
1569 (defun muse-publish-comment-tag (beg end)
1570 (if (null muse-publish-comments-p)
1571 (delete-region beg end)
1572 (goto-char end)
1573 (muse-insert-markup (muse-markup-text 'comment-end))
1574 (muse-publish-mark-read-only beg end)
1575 (goto-char beg)
1576 (muse-insert-markup (muse-markup-text 'comment-begin))))
1578 (defun muse-publish-include-tag (beg end attrs)
1579 "Include the named file at the current location during publishing.
1581 <include file=\"...\" markup=\"...\">
1583 The `markup' attribute controls how this file is marked up after
1584 being inserted. See `muse-publish-markup-attribute' for an
1585 explanation of how it works."
1586 (let ((filename (cdr (assoc "file" attrs)))
1587 (muse-publishing-directives muse-publishing-directives))
1588 (if filename
1589 (setq filename (expand-file-name
1590 filename
1591 (file-name-directory muse-publishing-current-file)))
1592 (error "No file attribute specified in <include> tag"))
1593 (muse-publish-markup-attribute beg end attrs t
1594 (insert-file-contents filename))))
1596 (defun muse-publish-mark-up-tag (beg end attrs)
1597 "Run an Emacs Lisp function on the region delimted by this tag.
1599 <markup function=\"...\">
1601 The optional `function' attribute controls how this section is
1602 marked up. If used, it should be the name of a function to call
1603 with the buffer narrowed to the delimited region. Note that no
1604 further marking-up will be performed on this region.
1606 If `function' is ommitted, use the standard Muse markup function.
1607 This is useful for marking up content in headers and footers."
1608 (let ((function (cdr (assoc "function" attrs)))
1609 (muse-publishing-directives muse-publishing-directives))
1610 (if function
1611 (let ((markup-function (intern function)))
1612 (if (and markup-function (functionp markup-function))
1613 (save-restriction
1614 (narrow-to-region beg end)
1615 (funcall markup-function)
1616 (goto-char (point-max)))
1617 (error "Invalid markup function `%s'" function)))
1618 (muse-publish-markup-region beg end))
1619 (muse-publish-mark-read-only beg (point))))
1621 ;; Miscellaneous helper functions
1623 (defun muse-publish-strip-tags (string)
1624 "Remove all tags from the string."
1625 (while (string-match "<.*?>" string)
1626 (setq string (replace-match "" nil t string)))
1627 string)
1629 (defun muse-publish-markup-type (category default-func)
1630 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1631 (funcall (or rule default-func))))
1633 (defun muse-published-buffer-contents (buffer)
1634 (with-current-buffer buffer
1635 (goto-char (point-min))
1636 (let ((beg (and (search-forward "Emacs Muse begins here")
1637 (muse-line-end-position)))
1638 (end (and (search-forward "Emacs Muse ends here")
1639 (muse-line-beginning-position))))
1640 (buffer-substring-no-properties beg end))))
1642 (defun muse-published-contents (file)
1643 (when (file-readable-p file)
1644 (muse-with-temp-buffer
1645 (insert-file-contents file)
1646 (muse-published-buffer-contents (current-buffer)))))
1648 (defun muse-publish-transform-output
1649 (file temp-file output-path name gen-func &rest cleanup-exts)
1650 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1651 (setq file (muse-page-name file))
1652 (message "Generating %s output for %s..." name file)
1653 (if (not (funcall gen-func temp-file output-path))
1654 (message "Generating %s from %s...failed" name file)
1655 (message "Generating %s output for %s...done" name file)
1656 (muse-delete-file-if-exists temp-file)
1657 (dolist (ext cleanup-exts)
1658 (muse-delete-file-if-exists
1659 (expand-file-name (concat file ext)
1660 (file-name-directory output-path))))
1661 (message "Wrote %s" output-path)))
1663 (defun muse-publish-read-only (string)
1664 (let ((end (1- (length string))))
1665 (add-text-properties 0 end
1666 '(rear-nonsticky (read-only) read-only t)
1667 string)
1668 string))
1670 (provide 'muse-publish)
1672 ;;; muse-publish.el ends here