1 ;;; muse-book.el --- publish entries into a compilation
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.
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Muse Book Publishing
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 (require 'muse-publish
)
35 (require 'muse-project
)
37 (require 'muse-regexps
)
39 (defgroup muse-book nil
40 "Module for publishing a series of Muse pages as a complete book.
41 Each page will become a separate chapter in the book, unless the
42 style keyword :nochapters is used, in which case they are all run
43 together as if one giant chapter."
46 (defcustom muse-book-before-publish-hook nil
47 "A hook run in the book buffer before it is marked up."
51 (defcustom muse-book-after-publish-hook nil
52 "A hook run in the book buffer after it is marked up."
56 (defcustom muse-book-latex-header
57 "\\documentclass{book}
59 \\usepackage[english]{babel}
60 \\usepackage[latin1]{inputenc}
61 \\usepackage[T1]{fontenc}
65 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
66 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
67 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
72 "Header used for publishing books to LaTeX. This may be text or a filename."
76 (defcustom muse-book-latex-footer
"\n\\end{document}"
77 "Footer used for publishing books to LaTeX. This may be text or a filename."
81 (defun muse-book-publish-chapter (title entry style
&optional nochapters
)
82 "Publish the chapter TITLE for the file ENTRY using STYLE.
83 TITLE is a string, ENTRY is a cons of the form (PAGE-NAME .
84 FILE), and STYLE is a Muse style list.
86 This routine does the same basic work as `muse-publish-markup-buffer',
87 but treating the page as if it were a single chapter within a book."
88 (let ((muse-publishing-directives (list (cons "title" title
)))
89 (muse-publishing-current-file (cdr entry
))
91 (insert-file-contents (cdr entry
))
92 (setq end
(copy-marker (point-max) t
))
93 (muse-publish-markup-region beg end
(car entry
) style
)
95 (unless (or nochapters
96 (muse-style-element :nochapters style
))
98 (muse-insert-markup (muse-markup-text 'chapter
))
99 (insert (let ((chap (muse-publishing-directive "title")))
100 (if (string= chap title
)
103 (muse-insert-markup (muse-markup-text 'chapter-end
))
106 (narrow-to-region beg end
)
107 (muse-publish-markup (or title
"")
108 '((100 "<\\(lisp\\)>" 0
109 muse-publish-markup-tag
)))
110 (muse-style-run-hooks :after style
))
113 (defun muse-book-publish-p (project target
)
114 "Determine whether the book in PROJECT is out-of-date."
115 (let ((pats (cadr project
)))
118 (if (symbolp (car pats
))
119 (if (eq :book-end
(car pats
))
121 ;; skip past symbol-value pair
122 (setq pats
(cddr pats
)))
123 (dolist (entry (muse-project-file-entries (car pats
)))
124 (when (and (not (muse-project-private-p (cdr entry
)))
125 (file-newer-than-file-p (cdr entry
) target
))
127 (setq pats
(cdr pats
)))))))
129 (defun muse-book-get-directives (file)
130 "Interpret any publishing directives contained in FILE.
131 This is meant to be called in a temp buffer that will later be
132 used for publishing."
134 (narrow-to-region (point) (point))
137 (insert-file-contents file
)
140 `(;; Remove leading and trailing whitespace from the file
141 (100 "\\(\\`\n+\\|\n+\\'\\)" 0 "")
142 ;; Remove trailing whitespace from all lines
143 (200 ,(concat "[" muse-regexp-blank
"]+$") 0 "")
144 ;; Handle any leading #directives
145 (300 "\\`#\\([a-zA-Z-]+\\)\\s-+\\(.+\\)\n+"
146 0 muse-publish-markup-directive
))))
147 (delete-region (point-min) (point-max)))))
149 (defun muse-book-publish-project
150 (project book title style
&optional output-dir force
)
151 "Publish PROJECT under the name BOOK with the given TITLE and STYLE.
152 BOOK should be a page name, i.e., letting the style determine the
153 prefix and/or suffix. The book is published to OUTPUT-DIR. If FORCE
154 is nil, the book is only published if at least one of its component
155 pages has changed since it was last published."
157 (let ((project (muse-read-project "Publish project as book: " nil t
)))
158 (append (list project
159 (read-string "Basename of book (without extension): ")
160 (read-string "Title of book: "))
161 (muse-publish-get-info))))
162 (setq project
(muse-project project
))
163 (let ((muse-current-project project
))
164 ;; See if any of the project's files need saving first
165 (muse-project-save-buffers project
)
167 (muse-book-publish book style output-dir force title
)))
169 (defun muse-book-publish (file style
&optional output-dir force title
)
170 "Publish FILE as a book with the given TITLE and STYLE.
171 The book is published to OUTPUT-DIR. If FORCE is nil, the book
172 is only published if at least one of its component pages has
173 changed since it was last published."
174 ;; Cleanup some of the arguments
175 (let ((style-name style
))
176 (setq style
(muse-style style
))
178 (error "There is no style '%s' defined" style-name
)))
179 ;; Publish each page in the project as a chapter in one large book
180 (let* ((output-path (muse-publish-output-file file output-dir style
))
181 (output-suffix (muse-style-element :osuffix style
))
183 (project muse-current-project
)
186 (setq target
(concat (muse-path-sans-extension target
)
188 ;; Unless force is non-nil, determine if the book needs publishing
190 (not (muse-book-publish-p project target
)))
191 (message "The book \"%s\" is up-to-date." file
)
192 ;; Create the book from all its component parts
193 (muse-with-temp-buffer
194 (let ((style-final (muse-style-element :final style t
))
195 (style-header (muse-style-element :header style
))
196 (style-footer (muse-style-element :footer style
))
197 (muse-publishing-current-style style
)
198 (muse-publishing-directives
199 (list (cons "title" (or title
(muse-page-name file
)))
200 (cons "date" (format-time-string "%B %e, %Y"))))
201 (muse-publishing-p t
)
202 (muse-current-project project
)
203 (pats (cadr project
))
205 (run-hooks 'muse-before-book-publish-hook
)
206 (let ((style-final style-final
)
207 (style-header style-header
)
208 (style-footer style-footer
))
210 (muse-book-get-directives file
)
211 (setq title
(muse-publishing-directive "title")))
213 (if (symbolp (car pats
))
215 ((eq :book-part
(car pats
))
217 (muse-insert-markup (muse-markup-text 'part
))
219 (muse-insert-markup (muse-markup-text 'part-end
))
221 (setq pats
(cddr pats
)))
222 ((eq :book-chapter
(car pats
))
224 (muse-insert-markup (muse-markup-text 'chapter
))
226 (muse-insert-markup (muse-markup-text 'chapter-end
))
228 (setq pats
(cddr pats
)))
229 ((eq :nochapters
(car pats
))
232 ((eq :book-style
(car pats
))
233 (setq style
(muse-style (cadr pats
)))
234 (setq style-final
(muse-style-element :final style t
)
235 style-header
(muse-style-element :header style
)
236 style-footer
(muse-style-element :footer style
)
237 muse-publishing-current-style style
)
238 (setq pats
(cddr pats
)))
239 ((eq :book-funcall
(car pats
))
240 (funcall (cadr pats
))
241 (setq pats
(cddr pats
)))
242 ((eq :book-end
(car pats
))
245 (setq pats
(cddr pats
))))
246 (let ((entries (muse-project-file-entries (car pats
))))
247 (while (and entries
(car entries
) (caar entries
))
248 (unless (muse-project-private-p (cdar entries
))
249 (muse-book-publish-chapter title
(car entries
)
252 (setq entries
(cdr entries
))))
253 (setq pats
(cdr pats
)))))
254 (goto-char (point-min))
255 (if style-header
(muse-insert-file-or-string style-header file
))
256 (goto-char (point-max))
257 (if style-footer
(muse-insert-file-or-string style-footer file
))
258 (run-hooks 'muse-after-book-publish-hook
)
259 (let ((backup-inhibited t
))
260 (write-file output-path
))
262 (funcall style-final file output-path target
)))))
264 (message "The book \"%s\" has been published." file
))
267 ;;; Register the Muse BOOK Publishers
269 (muse-derive-style "book-latex" "latex"
270 :header
'muse-book-latex-header
271 :footer
'muse-book-latex-footer
272 :publish
'muse-book-publish
)
274 (muse-derive-style "book-pdf" "pdf"
275 :header
'muse-book-latex-header
276 :footer
'muse-book-latex-footer
277 :publish
'muse-book-publish
)
281 ;;; muse-book.el ends here