No, we really did need the auto-mode-alist part at top-level
[muse-el.git] / lisp / muse-book.el
blob506b83afeb65faced91f9052ad369f27be03c519
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.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;;; Code:
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Muse Book Publishing
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 (require 'muse-publish)
35 (require 'muse-project)
36 (require 'muse-latex)
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."
44 :group 'muse-publish)
46 (defcustom muse-book-before-publish-hook nil
47 "A hook run in the book buffer before it is marked up."
48 :type 'hook
49 :group 'muse-book)
51 (defcustom muse-book-after-publish-hook nil
52 "A hook run in the book buffer after it is marked up."
53 :type 'hook
54 :group 'muse-book)
56 (defcustom muse-book-latex-header
57 "\\documentclass{book}
59 \\usepackage[english]{babel}
60 \\usepackage[latin1]{inputenc}
61 \\usepackage[T1]{fontenc}
63 \\begin{document}
65 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
66 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
67 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
69 \\maketitle
71 \\tableofcontents\n"
72 "Header used for publishing books to LaTeX. This may be text or a filename."
73 :type 'string
74 :group 'muse-book)
76 (defcustom muse-book-latex-footer "\n\\end{document}"
77 "Footer used for publishing books to LaTeX. This may be text or a filename."
78 :type 'string
79 :group 'muse-book)
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))
90 (beg (point)) end)
91 (insert-file-contents (cdr entry))
92 (setq end (copy-marker (point-max) t))
93 (muse-publish-markup-region beg end (car entry) style)
94 (goto-char beg)
95 (unless (or nochapters
96 (muse-style-element :nochapters style))
97 (insert "\n")
98 (muse-insert-markup (muse-markup-text 'chapter))
99 (insert (let ((chap (muse-publishing-directive "title")))
100 (if (string= chap title)
101 (car entry)
102 chap)))
103 (muse-insert-markup (muse-markup-text 'chapter-end))
104 (insert "\n\n"))
105 (save-restriction
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))
111 (goto-char end)))
113 (defun muse-book-publish-p (project target)
114 "Determine whether the book in PROJECT is out-of-date."
115 (let ((pats (cadr project)))
116 (catch 'publish
117 (while pats
118 (if (symbolp (car pats))
119 (if (eq :book-end (car pats))
120 (throw 'publish nil)
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))
126 (throw 'publish t)))
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."
133 (save-restriction
134 (narrow-to-region (point) (point))
135 (unwind-protect
136 (progn
137 (insert-file-contents file)
138 (muse-publish-markup
139 "attributes"
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."
156 (interactive
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)
166 ;; Publish the book
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))
177 (unless 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))
182 (target output-path)
183 (project muse-current-project)
184 (published nil))
185 (when output-suffix
186 (setq target (concat (muse-path-sans-extension target)
187 output-suffix)))
188 ;; Unless force is non-nil, determine if the book needs publishing
189 (if (and (not force)
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))
204 (nochapters nil))
205 (run-hooks 'muse-before-book-publish-hook)
206 (let ((style-final style-final)
207 (style-header style-header)
208 (style-footer style-footer))
209 (unless title
210 (muse-book-get-directives file)
211 (setq title (muse-publishing-directive "title")))
212 (while pats
213 (if (symbolp (car pats))
214 (cond
215 ((eq :book-part (car pats))
216 (insert "\n")
217 (muse-insert-markup (muse-markup-text 'part))
218 (insert (cadr pats))
219 (muse-insert-markup (muse-markup-text 'part-end))
220 (insert "\n")
221 (setq pats (cddr pats)))
222 ((eq :book-chapter (car pats))
223 (insert "\n")
224 (muse-insert-markup (muse-markup-text 'chapter))
225 (insert (cadr pats))
226 (muse-insert-markup (muse-markup-text 'chapter-end))
227 (insert "\n")
228 (setq pats (cddr pats)))
229 ((eq :nochapters (car pats))
230 (setq nochapters t
231 pats (cddr 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))
243 (setq pats nil))
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)
250 style nochapters)
251 (setq published t))
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))
261 (if style-final
262 (funcall style-final file output-path target)))))
263 (if published
264 (message "The book \"%s\" has been published." file))
265 published))
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)
279 (provide 'muse-book)
281 ;;; muse-book.el ends here