Release Emacs Muse 3.03!
[muse-el.git] / lisp / muse-project.el
blobb1ea33b1777405a032c4a6321c32b541122c5778
1 ;;; muse-project.el --- handle Muse projects
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 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 Project Maintainance
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 (provide 'muse-project)
36 (require 'muse)
37 (require 'muse-publish)
38 (require 'cus-edit)
40 (defgroup muse-project nil
41 "Options controlling the behavior of Muse project handling."
42 :group 'muse)
44 (defcustom muse-before-project-publish-hook nil
45 "A hook run before a project is published.
46 Each function is passed the project object, a cons with the format
47 (PROJNAME . SETTINGS)"
48 :type 'hook
49 :group 'muse-project)
51 (defcustom muse-after-project-publish-hook nil
52 "A hook run after a project is published.
53 Each function is passed the project object, a cons with the format
54 (PROJNAME . SETTINGS)"
55 :type 'hook
56 :group 'muse-project)
58 (defvar muse-project-alist-using-customize nil
59 "Used internally by Muse to indicate whether `muse-project-alist'
60 has been modified via the customize interface.")
61 (make-variable-buffer-local 'muse-project-alist-using-customize)
63 (defmacro with-muse-project (project &rest body)
64 `(progn
65 (unless (muse-project ,project)
66 (error "Can't find project %s" ,project))
67 (with-temp-buffer
68 (muse-mode)
69 (setq muse-current-project (muse-project ,project))
70 (muse-project-set-variables)
71 ,@body)))
73 (put 'with-muse-project 'lisp-indent-function 0)
74 (put 'with-muse-project 'edebug-form-spec '(sexp body))
76 (defun muse-project-alist-get (sym)
77 "Turn `muse-project-alist' into something we can customize easily."
78 (when (boundp sym)
79 (setq muse-project-alist-using-customize t)
80 (let* ((val (copy-alist (symbol-value sym)))
81 (head val))
82 (while val
83 (let ((head (car (cdar val)))
84 res)
85 ;; Turn settings of first part into cons cells, symbol->string
86 (while head
87 (cond ((stringp (car head))
88 (add-to-list 'res (car head) t)
89 (setq head (cdr head)))
90 ((symbolp (car head))
91 (add-to-list 'res (list (symbol-name (car head))
92 (cadr head)) t)
93 (setq head (cddr head)))
95 (setq head (cdr head)))))
96 (setcdr (car val) (cons res (cdr (cdar val)))))
97 (let ((styles (cdar val)))
98 ;; Symbol->string in every style
99 (while (cdr styles)
100 (let ((head (cadr styles))
101 res)
102 (while (consp head)
103 (setq res (plist-put res (symbol-name (car head))
104 (cadr head)))
105 (setq head (cddr head)))
106 (setcdr styles (cons res (cddr styles))))
107 (setq styles (cdr styles))))
108 (setq val (cdr val)))
109 head)))
111 (defun muse-project-alist-set (sym val)
112 "Turn customized version of `muse-project-alist' into something
113 Muse can make use of."
114 (set sym val)
115 (when muse-project-alist-using-customize
116 ;; Make sure the unescaped version is written to .emacs
117 (put sym 'saved-value (list (custom-quote val)))
118 ;; Perform unescaping
119 (while val
120 (let ((head (car (cdar val)))
121 res)
122 ;; Turn cons cells into flat list, string->symbol
123 (while head
124 (cond ((stringp (car head))
125 (add-to-list 'res (car head) t))
126 ((consp (car head))
127 (add-to-list 'res (intern (caar head)) t)
128 (add-to-list 'res (car (cdar head)) t)))
129 (setq head (cdr head)))
130 (setcdr (car val) (cons res (cdr (cdar val)))))
131 (let ((styles (cdar val)))
132 ;; String->symbol in every style
133 (while (cdr styles)
134 (let ((head (cadr styles))
135 res)
136 (while (consp head)
137 (setq res (plist-put res (intern (car head))
138 (cadr head)))
139 (setq head (cddr head)))
140 (setcdr styles (cons res (cddr styles))))
141 (setq styles (cdr styles))))
142 (setq val (cdr val)))))
144 (define-widget 'muse-project 'default
145 "A widget that defines a Muse project."
146 :format "\n%v"
147 :value-create 'muse-widget-type-value-create
148 :value-get 'muse-widget-child-value-get
149 :value-delete 'ignore
150 :match 'muse-widget-type-match
151 :type '(cons :format " %v"
152 (repeat :tag "Settings" :format "%{%t%}:\n%v%i\n\n"
153 (choice
154 (string :tag "Directory")
155 (list :tag "Book function"
156 (const :tag ":book-funcall" ":book-funcall")
157 (choice (function)
158 (sexp :tag "Unknown")))
159 (list :tag "Book part"
160 (const :tag ":book-part" ":book-part")
161 (string :tag "Name"))
162 (list :tag "Book style"
163 (const :tag ":book-style" ":book-style")
164 (string :tag "Style"))
165 (list :tag "Default file"
166 (const :tag ":default" ":default")
167 (string :tag "File"))
168 (list :tag "End of book"
169 (const :tag ":book-end" ":book-end")
170 (const t))
171 (list :tag "Force publishing"
172 (const :tag ":force-publish" ":force-publish")
173 (repeat (string :tag "File")))
174 (list :tag "Major mode"
175 (const :tag ":major-mode" ":major-mode")
176 (choice (function :tag "Mode")
177 (sexp :tag "Unknown")))
178 (list :tag "New chapter"
179 (const :tag ":book-chapter" ":book-chapter")
180 (string :tag "Name"))
181 (list :tag "No chapters"
182 (const :tag ":nochapters" ":nochapters")
183 (const t))
184 (list :tag "Project-level publishing function"
185 (const :tag ":publish-project"
186 ":publish-project")
187 (choice (function :tag "Function")
188 (sexp :tag "Unknown")))
189 (list :tag "Set variables"
190 (const :tag ":set" ":set")
191 (repeat (list :inline t
192 (symbol :tag "Variable")
193 (sexp :tag "Setting"))))
194 (list :tag "Visit links using"
195 (const :tag ":visit-link" ":visit-link")
196 (choice (function)
197 (sexp :tag "Unknown")))))
198 (repeat :tag "Output styles" :format "%{%t%}:\n%v%i\n\n"
199 (set :tag "Style"
200 (list :inline t
201 :tag "Publishing style"
202 (const :tag ":base" ":base")
203 (string :tag "Style"))
204 (list :inline t
205 :tag "Base URL"
206 (const :tag ":base-url" ":base-url")
207 (string :tag "URL"))
208 (list :inline t
209 :tag "Exclude matching"
210 (const :tag ":exclude" ":exclude")
211 (regexp))
212 (list :inline t
213 :tag "Include matching"
214 (const :tag ":include" ":include")
215 (regexp))
216 (list :inline t
217 :tag "Timestamps file"
218 (const :tag ":timestamps" ":timestamps")
219 (file))
220 (list :inline t
221 :tag "Path"
222 (const :tag ":path" ":path")
223 (string :tag "Path"))))))
225 (defcustom muse-project-alist nil
226 "An alist of Muse projects.
227 A project defines a fileset, and a list of custom attributes for use
228 when publishing files in that project."
229 :type '(choice (const :tag "No projects defined." nil)
230 (repeat (cons :format "%{%t%}:\n\n%v"
231 :tag "Project" :indent 4
232 (string :tag "Project name")
233 muse-project))
234 (sexp :tag "Cannot parse expression"))
235 :get 'muse-project-alist-get
236 :set 'muse-project-alist-set
237 :group 'muse-project)
239 ;; Make it easier to specify a muse-project-alist entry
241 (defcustom muse-project-ignore-regexp
242 (concat "\\`\\(#.*#\\|.*,v\\|.*~\\|\\.\\.?\\|\\.#.*\\|,.*\\)\\'\\|"
243 "/\\(CVS\\|RCS\\|\\.arch-ids\\|{arch}\\|,.*\\|\\.svn\\|"
244 "\\.hg\\|\\.bzr\\|_darcs\\)\\(/\\|\\'\\)")
245 "A regexp matching files to be ignored in Muse directories.
247 You should set `case-fold-search' to nil before using this regexp
248 in code."
249 :type 'regexp
250 :group 'muse-regexp)
252 (defcustom muse-project-publish-private-files t
253 "If this is non-nil, files will be published even if their permissions
254 are set so that no one else on the filesystem can read them.
256 Set this to nil if you would like to indicate that some files
257 should not be published by manually doing \"chmod o-rwx\" on
258 them.
260 This setting has no effect under Windows (that is, all files are
261 published regardless of permissions) because Windows lacks the
262 needed filesystem attributes."
263 :type 'boolean
264 :group 'muse-project)
266 (defun muse-project-recurse-directory (base)
267 "Recusively retrieve all of the directories underneath BASE.
268 A list of these directories is returned.
270 Directories starting with \".\" will be ignored, as well as those
271 which match `muse-project-ignore-regexp'."
272 (let ((case-fold-search nil)
273 list dir)
274 (when (and (file-directory-p base)
275 (not (string-match muse-project-ignore-regexp base)))
276 (dolist (file (directory-files base t "^[^.]"))
277 (when (and (file-directory-p file)
278 (not (string-match muse-project-ignore-regexp file)))
279 (setq dir (file-name-nondirectory file))
280 (push dir list)
281 (nconc list (mapcar #'(lambda (item)
282 (concat dir "/" item))
283 (muse-project-recurse-directory file)))))
284 list)))
286 (defun muse-project-alist-styles (entry-dir output-dir style &rest other)
287 "Return a list of styles to use in a `muse-project-alist' entry.
288 ENTRY-DIR is the top-level directory of the project.
289 OUTPUT-DIR is where Muse files are published, keeping directory structure.
290 STYLE is the publishing style to use.
292 OTHER contains other definitions to add to each style. It is optional.
294 For an example of the use of this function, see
295 `examples/mwolson/muse-init.el' from the Muse distribution."
296 (let ((fnd (file-name-nondirectory entry-dir)))
297 (when (string= fnd "")
298 ;; deal with cases like "foo/" that have a trailing slash
299 (setq fnd (file-name-nondirectory (substring entry-dir 0 -1))))
300 (cons `(:base ,style :path ,(expand-file-name output-dir)
301 :include ,(concat "/" fnd "/[^/]+$")
302 ,@other)
303 (mapcar (lambda (dir)
304 `(:base ,style
305 :path ,(expand-file-name dir output-dir)
306 :include ,(concat "/" dir "/[^/]+$")
307 ,@other))
308 (muse-project-recurse-directory entry-dir)))))
310 (defun muse-project-alist-dirs (entry-dir)
311 "Return a list of directories to use in a `muse-project-alist' entry.
312 ENTRY-DIR is the top-level directory of the project.
314 For an example of the use of this function, see
315 `examples/mwolson/muse-init.el' from the Muse distribution."
316 (cons (expand-file-name entry-dir)
317 (mapcar (lambda (dir) (expand-file-name dir entry-dir))
318 (muse-project-recurse-directory entry-dir))))
320 ;; Constructing the file-alist
322 (defvar muse-project-file-alist nil
323 "This variable is automagically constructed as needed.")
325 (defvar muse-project-file-alist-hook nil
326 "Functions that are to be exectuted immediately after updating
327 `muse-project-file-alist'.")
329 (defvar muse-current-project nil
330 "Project we are currently visiting.")
331 (make-variable-buffer-local 'muse-current-project)
333 (defvar muse-current-output-style nil
334 "The output style that we are currently using for publishing files.")
336 (defsubst muse-project (&optional project)
337 "Resolve the given PROJECT into a full Muse project, if it is a string."
338 (if (null project)
339 (or muse-current-project
340 (muse-project-of-file))
341 (if (stringp project)
342 (assoc project muse-project-alist)
343 (muse-assert (consp project))
344 project)))
346 (defun muse-project-page-file (page project &optional no-check-p)
347 "Return a filename if PAGE exists within the given Muse PROJECT."
348 (setq project (muse-project project))
349 (let ((dir (file-name-directory page))
350 (expanded-path nil))
351 (when dir
352 (setq expanded-path (concat (expand-file-name
353 page
354 (file-name-directory (muse-current-file)))
355 (when muse-file-extension
356 (concat "." muse-file-extension))))
357 (setq page (file-name-nondirectory page)))
358 (let ((files (muse-collect-alist
359 (muse-project-file-alist project no-check-p)
360 page))
361 (matches nil))
362 (if dir
363 (catch 'done
364 (save-match-data
365 (dolist (file files)
366 (if (and expanded-path
367 (string= expanded-path (cdr file)))
368 (throw 'done (cdr file))
369 (let ((pos (string-match (concat (regexp-quote dir) "\\'")
370 (file-name-directory (cdr file)))))
371 (when pos
372 (setq matches (cons (cons pos (cdr file))
373 matches)))))))
374 ;; if we haven't found an exact match, pick a candidate
375 (car (muse-sort-by-rating matches)))
376 (dolist (file files)
377 (setq matches (cons (cons (length (cdr file)) (cdr file))
378 matches)))
379 (car (muse-sort-by-rating matches '<))))))
381 (defun muse-project-private-p (file)
382 "Return non-nil if NAME is a private page with PROJECT."
383 (unless (or muse-under-windows-p
384 muse-project-publish-private-files)
385 (setq file (file-truename file))
386 (if (file-attributes file) ; don't publish if no attributes exist
387 (or (when (eq ?- (aref (nth 8 (file-attributes
388 (file-name-directory file))) 7))
389 (message (concat
390 "The " (file-name-directory file)
391 " directory must be readable by others"
392 " in order for its contents to be published.")))
393 (eq ?- (aref (nth 8 (file-attributes file)) 7)))
394 t)))
396 (defun muse-project-file-entries (path)
397 (let* ((names (list t))
398 (lnames names)
399 (case-fold-search nil))
400 (cond
401 ((file-directory-p path)
402 (dolist (file (directory-files
403 path t (when (and muse-file-extension
404 (not (string= muse-file-extension "")))
405 (concat "." muse-file-extension "\\'"))))
406 (unless (or (string-match muse-project-ignore-regexp file)
407 (string-match muse-project-ignore-regexp
408 (file-name-nondirectory file))
409 (file-directory-p file))
410 (setcdr lnames
411 (cons (cons (muse-page-name file) file) nil))
412 (setq lnames (cdr lnames)))))
413 ((file-readable-p path)
414 (setcdr lnames
415 (cons (cons (muse-page-name path) path) nil))
416 (setq lnames (cdr lnames)))
417 (t ; regexp
418 (muse-assert (file-name-directory path))
419 (dolist (file (directory-files
420 (file-name-directory path) t
421 (file-name-nondirectory path)))
422 (unless (or (string-match muse-project-ignore-regexp file)
423 (string-match muse-project-ignore-regexp
424 (file-name-nondirectory file)))
425 (setcdr lnames
426 (cons (cons (muse-page-name file) file) nil))
427 (setq lnames (cdr lnames))))))
428 (cdr names)))
430 (defvar muse-updating-file-alist-p nil
431 "Make sure that recursive calls to `muse-project-file-alist' are bounded.")
433 (defun muse-project-file-alist (&optional project no-check-p)
434 "Return member filenames for the given Muse PROJECT.
435 On UNIX, this list is only updated if one of the directories'
436 contents have changed. On Windows, it is always reread from
437 disk."
438 (setq project (muse-project project))
439 (let* ((file-alist (assoc (car project) muse-project-file-alist))
440 (last-mod (cdr (cdr file-alist))))
441 ;; Determine the last modified of any directory mentioned in the
442 ;; project's pattern list
443 (unless (or muse-under-windows-p no-check-p)
444 (let ((pats (cadr project)))
445 (while pats
446 (if (symbolp (car pats))
447 (setq pats (cddr pats))
448 (let* ((fnd (file-name-directory (car pats)))
449 (dir (cond ((file-directory-p (car pats))
450 (car pats))
451 ((and (not (file-readable-p (car pats)))
453 (file-directory-p fnd))
454 fnd))))
455 (when dir
456 (let ((mod-time (nth 5 (file-attributes dir))))
457 (when (or (null last-mod)
458 (and mod-time
459 (muse-time-less-p last-mod mod-time)))
460 (setq last-mod mod-time)))))
461 (setq pats (cdr pats))))))
462 ;; Either return the currently known list, or read it again from
463 ;; disk
464 (if (or (and no-check-p (cadr file-alist))
465 muse-updating-file-alist-p
466 (not (or muse-under-windows-p
467 (null (cddr file-alist))
468 (null last-mod)
469 (muse-time-less-p (cddr file-alist) last-mod))))
470 (cadr file-alist)
471 (if file-alist
472 (setcdr (cdr file-alist) last-mod)
473 (setq file-alist (cons (car project) (cons nil last-mod))
474 muse-project-file-alist
475 (cons file-alist muse-project-file-alist)))
476 ;; Read in all of the file entries
477 (let ((muse-updating-file-alist-p t))
478 (prog1
479 (save-match-data
480 (setcar
481 (cdr file-alist)
482 (let* ((names (list t))
483 (pats (cadr project)))
484 (while pats
485 (if (symbolp (car pats))
486 (setq pats (cddr pats))
487 (nconc names (muse-project-file-entries (car pats)))
488 (setq pats (cdr pats))))
489 (cdr names))))
490 (run-hooks 'muse-project-file-alist-hook))))))
492 (defun muse-project-of-file (&optional pathname)
493 "Determine which project the given PATHNAME relates to.
494 If PATHNAME is nil, the current buffer's filename is used."
495 (if (and (null pathname) muse-current-project)
496 muse-current-project
497 (unless pathname (setq pathname (muse-current-file)))
498 (save-match-data
499 (when (and (stringp pathname)
500 (not (string= pathname ""))
501 (not (let ((case-fold-search nil))
502 (or (string-match muse-project-ignore-regexp
503 pathname)
504 (string-match muse-project-ignore-regexp
505 (file-name-nondirectory
506 pathname))))))
507 (let* ((file (file-truename pathname))
508 (dir (file-name-directory file))
509 (project-entry muse-project-alist)
510 found)
511 (while (and project-entry (not found))
512 (let ((pats (car (cdar project-entry))))
513 (while (and pats (not found))
514 (if (symbolp (car pats))
515 (setq pats (cddr pats))
516 (let ((truename (file-truename (car pats))))
517 (if (or (string= truename file)
518 (string= truename dir)
519 (string-match (regexp-quote truename) file))
520 (setq found (car project-entry))))
521 (setq pats (cdr pats))))
522 (setq project-entry (cdr project-entry))))
523 found)))))
525 (defun muse-project-after-save-hook ()
526 "Update Muse's file-alist if we are saving a Muse file."
527 (let ((project (muse-project-of-file)))
528 (when project
529 (muse-project-file-alist project))))
531 (add-hook 'after-save-hook 'muse-project-after-save-hook)
533 (defun muse-read-project (prompt &optional no-check-p no-assume)
534 "Read a project name from the minibuffer, if it can't be figured
535 out."
536 (if (null muse-project-alist)
537 (error "There are no Muse projects defined; see `muse-project-alist'")
538 (or (unless no-check-p
539 (muse-project-of-file))
540 (if (and (not no-assume)
541 (= 1 (length muse-project-alist)))
542 (car muse-project-alist)
543 (assoc (completing-read prompt muse-project-alist)
544 muse-project-alist)))))
546 (defvar muse-project-page-history nil)
548 (defun muse-read-project-file (project prompt &optional default)
549 (let ((name (completing-read prompt (muse-project-file-alist project)
550 nil nil nil 'muse-project-page-history
551 default)))
552 (cons name (muse-project-page-file name project))))
554 ;;;###autoload
555 (defun muse-project-find-file (name project &optional command directory)
556 "Open the Muse page given by NAME in PROJECT.
557 If COMMAND is non-nil, it is the function used to visit the file.
558 If DIRECTORY is non-nil, it is the directory in which the page
559 will be created if it does not already exist. Otherwise, the
560 first directory within the project's fileset is used."
561 (interactive
562 (let* ((project (muse-read-project "Find in project: "
563 current-prefix-arg))
564 (default (muse-get-keyword :default (cadr project)))
565 (entry (muse-read-project-file
566 project (if default
567 (format "Find page: (default: %s) "
568 default)
569 "Find page: ")
570 default)))
571 (list entry project)))
572 (setq project (muse-project project))
573 (let ((project-name (car project)))
574 (unless (interactive-p)
575 (setq project (muse-project project)
576 name (cons name (muse-project-page-file name project))))
577 ;; If we're given a relative or absolute filename, open it as-is
578 (if (and (car name)
579 (save-match-data
580 (or (string-match "\\`\\.+/" (car name))
581 (string-match muse-file-regexp (car name))
582 (string-match muse-image-regexp (car name)))))
583 (setcdr name (car name))
584 ;; At this point, name is (PAGE . FILE).
585 (unless (cdr name)
586 (let ((pats (cadr project)))
587 (while (and pats (null directory))
588 (if (symbolp (car pats))
589 (setq pats (cddr pats))
590 (if (file-directory-p (car pats))
591 (setq directory (car pats) pats nil)
592 (setq pats (cdr pats))))))
593 (when directory
594 (let ((filename (expand-file-name (car name) directory)))
595 (when (and muse-file-extension
596 (not (string= muse-file-extension ""))
597 (not (file-exists-p (car name))))
598 (setq filename (concat filename "." muse-file-extension)))
599 (unless (file-exists-p directory)
600 (make-directory directory t))
601 (setcdr name filename)))))
602 ;; Open the file
603 (if (cdr name)
604 (funcall (or command 'find-file) (cdr name))
605 (error "There is no page %s in project %s"
606 (car name) project-name))))
608 (defun muse-project-choose-style (closure test styles)
609 "Run TEST on STYLES and return first style where TEST yields non-nil.
610 TEST should take two arguments. The first is CLOSURE, which is
611 passed verbatim. The second if the current style to consider.
613 If no style passes TEST, return the first style."
614 (or (catch 'winner
615 (dolist (style styles)
616 (when (funcall test closure style)
617 (throw 'winner style))))
618 (car styles)))
620 (defun muse-project-choose-style-by-link-suffix (given-suffix style)
621 "If the given STYLE has a link-suffix that equals GIVEN-SUFFIX,
622 return non-nil."
623 (let ((link-suffix (or (muse-style-element :link-suffix style)
624 (muse-style-element :suffix style))))
625 (and (stringp link-suffix)
626 (string= given-suffix link-suffix))))
628 (defun muse-project-applicable-styles (file styles &optional ignore-regexp)
629 "Given STYLES, return a list of the ones that are considered for FILE.
630 The name of a project may be used for STYLES."
631 (when (stringp styles)
632 (setq styles (cddr (muse-project styles))))
633 (when (and file styles)
634 (let ((used-styles nil))
635 (dolist (style styles)
636 (let ((include-regexp (muse-style-element :include style))
637 (exclude-regexp (muse-style-element :exclude style))
638 (rating nil))
639 (when (and (or ignore-regexp
640 (and (null include-regexp)
641 (null exclude-regexp))
642 (if include-regexp
643 (setq rating (string-match include-regexp file))
644 (not (string-match exclude-regexp file))))
645 (or (not (file-exists-p file))
646 (not (muse-project-private-p file))))
647 (setq used-styles (cons (cons rating style) used-styles)))))
648 (muse-sort-by-rating (nreverse used-styles)))))
650 (defun muse-project-get-applicable-style (file styles)
651 "Choose a style from the STYLES that FILE can publish to.
652 The user is prompted if several styles are found."
653 (muse-publish-get-style
654 (mapcar (lambda (style)
655 (cons (muse-get-keyword :base style) style))
656 (muse-project-applicable-styles file styles))))
658 (defun muse-project-resolve-link (page local-style remote-styles)
659 "Return a published relative link from the output path of one file
660 to another file.
662 The best match for PAGE is determined by comparing the link
663 suffix of the given local style and that of the remote styles.
665 The remote styles are usually populated by
666 `muse-project-applicable-styles'."
667 (let ((link-suffix (or (muse-style-element :link-suffix local-style)
668 (muse-style-element :suffix local-style)))
669 remote-style)
670 (if (not (stringp link-suffix))
671 (setq remote-style (car remote-styles))
672 (setq remote-style (muse-project-choose-style
673 link-suffix
674 #'muse-project-choose-style-by-link-suffix
675 remote-styles)))
676 (if (null remote-style)
677 page
678 (muse-publish-link-file
679 (let ((prefix (muse-style-element :base-url remote-style)))
680 (if prefix
681 (concat prefix page)
682 (file-relative-name (expand-file-name
683 (file-name-nondirectory page)
684 (muse-style-element :path remote-style))
685 (expand-file-name
686 (muse-style-element :path local-style)))))
687 remote-style))))
689 (defun muse-project-current-output-style (&optional file project)
690 (or muse-current-output-style
691 (progn
692 (unless file (setq file (muse-current-file)))
693 (unless project (setq project (muse-project-of-file file)))
694 (car (muse-project-applicable-styles file (cddr project))))))
696 (defun muse-project-link-page (page)
697 (let ((project (muse-project-of-file)))
698 (muse-project-resolve-link page
699 (muse-project-current-output-style)
700 (muse-project-applicable-styles
701 (muse-project-page-file page project)
702 (cddr project)))))
704 (defun muse-project-publish-file-default (file style output-dir force)
705 ;; ensure the publishing location is available
706 (unless (file-exists-p output-dir)
707 (message "Creating publishing directory %s" output-dir)
708 (make-directory output-dir t))
709 ;; publish the member file!
710 (muse-publish-file file style output-dir force))
712 (defun muse-project-publish-file (file styles &optional force ignore-regexp)
713 (setq styles (muse-project-applicable-styles file styles ignore-regexp))
714 (let (published)
715 (dolist (style styles)
716 (if (or (not (listp style))
717 (not (cdr style)))
718 (muse-display-warning
719 (concat "Skipping malformed muse-project-alist style."
720 "\nPlease double-check your configuration,"))
721 (let ((output-dir (muse-style-element :path style))
722 (muse-current-output-style style)
723 (fun (or (muse-style-element :publish style t)
724 'muse-project-publish-file-default)))
725 (when (funcall fun file style output-dir force)
726 (setq published t)))))
727 published))
729 ;;;###autoload
730 (defun muse-project-publish-this-file (&optional force)
731 "Publish the currently-visited file according to `muse-project-alist',
732 prompting if more than one style applies.
734 If FORCE is given, publish the file even if it is up-to-date."
735 (interactive (list current-prefix-arg))
736 (let* ((style (muse-project-get-applicable-style
737 buffer-file-name (cddr muse-current-project)))
738 (output-dir (muse-style-element :path style)))
739 (unless (muse-publish-file buffer-file-name style output-dir force)
740 (message (concat "The published version is up-to-date; use"
741 " C-u C-c C-t to force an update.")))))
743 (defun muse-project-save-buffers (&optional project)
744 (setq project (muse-project project))
745 (map-y-or-n-p
746 (function
747 (lambda (buffer)
748 (and (buffer-modified-p buffer)
749 (not (buffer-base-buffer buffer))
750 (or (buffer-file-name buffer)
751 (progn
752 (set-buffer buffer)
753 (and buffer-offer-save
754 (> (buffer-size) 0))))
755 (with-current-buffer buffer
756 (let ((proj (muse-project-of-file)))
757 (and proj (string= (car proj)
758 (car project)))))
759 (if (buffer-file-name buffer)
760 (format "Save file %s? "
761 (buffer-file-name buffer))
762 (format "Save buffer %s? "
763 (buffer-name buffer))))))
764 (function
765 (lambda (buffer)
766 (set-buffer buffer)
767 (save-buffer)))
768 (buffer-list)
769 '("buffer" "buffers" "save")
770 (if (boundp 'save-some-buffers-action-alist)
771 save-some-buffers-action-alist)))
773 (defun muse-project-publish-default (project styles &optional force)
774 "Publish the pages of PROJECT that need publishing."
775 (setq project (muse-project project))
776 (let ((published nil))
777 ;; publish all files in the project, for each style; the actual
778 ;; publishing will only happen if the files are newer than the
779 ;; last published output, or if the file is listed in
780 ;; :force-publish. Files in :force-publish will not trigger the
781 ;; "All pages need to be published" message.
782 (let ((forced-files (muse-get-keyword :force-publish (cadr project)))
783 (file-alist (muse-project-file-alist project)))
784 (dolist (pair file-alist)
785 (when (muse-project-publish-file (cdr pair) styles force)
786 (setq forced-files (delete (car pair) forced-files))
787 (setq published t)))
788 (dolist (file forced-files)
789 (muse-project-publish-file (cdr (assoc file file-alist)) styles t)))
790 ;; run hook after publishing ends
791 (run-hook-with-args 'muse-after-project-publish-hook project)
792 ;; notify the user that everything is now done
793 (if published
794 (message "All pages in %s have been published." (car project))
795 (message "No pages in %s need publishing at this time."
796 (car project)))))
798 ;;;###autoload
799 (defun muse-project-publish (project &optional force)
800 "Publish the pages of PROJECT that need publishing."
801 (interactive (list (muse-read-project "Publish project: " nil t)
802 current-prefix-arg))
803 (setq project (muse-project project))
804 (let ((styles (cddr project))
805 (muse-current-project project))
806 ;; determine the style from the project, or else ask
807 (unless styles
808 (setq styles (list (muse-publish-get-style))))
809 (unless project
810 (error "Cannot find a project to publish"))
811 ;; prompt to save any buffers related to this project
812 (muse-project-save-buffers project)
813 ;; run hook before publishing begins
814 (run-hook-with-args 'muse-before-project-publish-hook project)
815 ;; run the project-level publisher
816 (let ((fun (or (muse-get-keyword :publish-project (cadr project) t)
817 'muse-project-publish-default)))
818 (funcall fun project styles force))))
820 (defun muse-project-batch-publish ()
821 "Publish Muse files in batch mode."
822 (let ((muse-batch-publishing-p t)
823 force)
824 (if (string= "--force" (or (car command-line-args-left) ""))
825 (setq force t
826 command-line-args-left (cdr command-line-args-left)))
827 (if command-line-args-left
828 (dolist (project command-line-args-left)
829 (message "Publishing project %s ..." project)
830 (muse-project-publish project force))
831 (message "No projects specified."))))
833 (eval-when-compile
834 (put 'make-local-hook 'byte-compile nil))
836 (defun muse-project-set-variables ()
837 "Load project-specific variables."
838 (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
839 sym custom-set var)
840 (while vars
841 (setq sym (car vars))
842 (setq custom-set (or (get sym 'custom-set) 'set))
843 (setq var (if (eq (get sym 'custom-type) 'hook)
844 (make-local-hook sym)
845 (make-local-variable sym)))
846 (funcall custom-set var (car (cdr vars)))
847 (setq vars (cdr (cdr vars))))))
849 (defun muse-project-delete-output-files (project)
850 (interactive
851 (list (muse-read-project "Remove all output files for project: " nil t)))
852 (setq project (muse-project project))
853 (let ((file-alist (muse-project-file-alist project))
854 (styles (cddr project))
855 output-file path)
856 (dolist (entry file-alist)
857 (dolist (style styles)
858 (setq output-file
859 (and (setq path (muse-style-element :path style))
860 (expand-file-name
861 (concat (muse-style-element :prefix style)
862 (car entry)
863 (or (muse-style-element :osuffix style)
864 (muse-style-element :suffix style)))
865 path)))
866 (if output-file
867 (muse-delete-file-if-exists output-file))))))
869 ;;; muse-project.el ends here