muse-publish: Implement muse-publish-enable-dangerous-tags.
[muse-el.git] / lisp / muse-project.el
blob5603aa4c05ccaf360caa567e24c71865a3354659
1 ;;; muse-project.el --- handle Muse projects
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
7 ;; Emacs Muse is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published
9 ;; by the Free Software Foundation; either version 3, or (at your
10 ;; option) any later version.
12 ;; Emacs Muse is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Emacs Muse; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;;; 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\\|\\.git\\|\\.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 (if (null page)
350 ;; if not given a page, return the first directory instead
351 (let ((pats (cadr project)))
352 (catch 'done
353 (while pats
354 (if (symbolp (car pats))
355 (setq pats (cddr pats))
356 (throw 'done (file-name-as-directory (car pats)))))))
357 (let ((dir (file-name-directory page))
358 (expanded-path nil))
359 (when dir
360 (setq expanded-path (concat (expand-file-name
361 page
362 (file-name-directory (muse-current-file)))
363 (when muse-file-extension
364 (concat "." muse-file-extension))))
365 (setq page (file-name-nondirectory page)))
366 (let ((files (muse-collect-alist
367 (muse-project-file-alist project no-check-p)
368 page))
369 (matches nil))
370 (if dir
371 (catch 'done
372 (save-match-data
373 (dolist (file files)
374 (if (and expanded-path
375 (string= expanded-path (cdr file)))
376 (throw 'done (cdr file))
377 (let ((pos (string-match (concat (regexp-quote dir) "\\'")
378 (file-name-directory
379 (cdr file)))))
380 (when pos
381 (setq matches (cons (cons pos (cdr file))
382 matches)))))))
383 ;; if we haven't found an exact match, pick a candidate
384 (car (muse-sort-by-rating matches)))
385 (dolist (file files)
386 (setq matches (cons (cons (length (cdr file)) (cdr file))
387 matches)))
388 (car (muse-sort-by-rating matches '<)))))))
390 (defun muse-project-private-p (file)
391 "Return non-nil if NAME is a private page with PROJECT."
392 (unless (or muse-under-windows-p
393 muse-project-publish-private-files)
394 (setq file (file-truename file))
395 (if (file-attributes file) ; don't publish if no attributes exist
396 (or (when (eq ?- (aref (nth 8 (file-attributes
397 (file-name-directory file))) 7))
398 (message (concat
399 "The " (file-name-directory file)
400 " directory must be readable by others"
401 " in order for its contents to be published.")))
402 (eq ?- (aref (nth 8 (file-attributes file)) 7)))
403 t)))
405 (defun muse-project-file-entries (path)
406 (let* ((names (list t))
407 (lnames names)
408 (case-fold-search nil))
409 (cond
410 ((file-directory-p path)
411 (dolist (file (directory-files
412 path t (when (and muse-file-extension
413 (not (string= muse-file-extension "")))
414 (concat "." muse-file-extension "\\'"))))
415 (unless (or (string-match muse-project-ignore-regexp file)
416 (string-match muse-project-ignore-regexp
417 (file-name-nondirectory file))
418 (file-directory-p file))
419 (setcdr lnames
420 (cons (cons (muse-page-name file) file) nil))
421 (setq lnames (cdr lnames)))))
422 ((file-readable-p path)
423 (setcdr lnames
424 (cons (cons (muse-page-name path) path) nil))
425 (setq lnames (cdr lnames)))
426 (t ; regexp
427 (muse-assert (file-name-directory path))
428 (dolist (file (directory-files
429 (file-name-directory path) t
430 (file-name-nondirectory path)))
431 (unless (or (string-match muse-project-ignore-regexp file)
432 (string-match muse-project-ignore-regexp
433 (file-name-nondirectory file)))
434 (setcdr lnames
435 (cons (cons (muse-page-name file) file) nil))
436 (setq lnames (cdr lnames))))))
437 (cdr names)))
439 (defvar muse-updating-file-alist-p nil
440 "Make sure that recursive calls to `muse-project-file-alist' are bounded.")
442 (defun muse-project-determine-last-mod (project &optional no-check-p)
443 "Return the most recent last-modified timestamp of dirs in PROJECT."
444 (let ((last-mod nil))
445 (unless (or muse-under-windows-p no-check-p)
446 (let ((pats (cadr project)))
447 (while pats
448 (if (symbolp (car pats))
449 (setq pats (cddr pats))
450 (let* ((fnd (file-name-directory (car pats)))
451 (dir (cond ((file-directory-p (car pats))
452 (car pats))
453 ((and (not (file-readable-p (car pats)))
455 (file-directory-p fnd))
456 fnd))))
457 (when dir
458 (let ((mod-time (nth 5 (file-attributes dir))))
459 (when (or (null last-mod)
460 (and mod-time
461 (muse-time-less-p last-mod mod-time)))
462 (setq last-mod mod-time)))))
463 (setq pats (cdr pats))))))
464 last-mod))
466 (defun muse-project-file-alist (&optional project no-check-p)
467 "Return member filenames for the given Muse PROJECT.
468 Also, update the `muse-project-file-alist' variable.
470 On UNIX, this alist is only updated if one of the directories'
471 contents have changed. On Windows, it is always reread from
472 disk.
474 If NO-CHECK-P is non-nil, do not update the alist, just return
475 the current one."
476 (setq project (muse-project project))
477 (when (and project muse-project-alist)
478 (let* ((file-alist (assoc (car project) muse-project-file-alist))
479 (last-mod (muse-project-determine-last-mod project no-check-p)))
480 ;; Either return the currently known list, or read it again from
481 ;; disk
482 (if (or (and no-check-p (cadr file-alist))
483 muse-updating-file-alist-p
484 (not (or muse-under-windows-p
485 (null (cddr file-alist))
486 (null last-mod)
487 (muse-time-less-p (cddr file-alist) last-mod))))
488 (cadr file-alist)
489 (if file-alist
490 (setcdr (cdr file-alist) last-mod)
491 (setq file-alist (cons (car project) (cons nil last-mod))
492 muse-project-file-alist
493 (cons file-alist muse-project-file-alist)))
494 ;; Read in all of the file entries
495 (let ((muse-updating-file-alist-p t))
496 (prog1
497 (save-match-data
498 (setcar
499 (cdr file-alist)
500 (let* ((names (list t))
501 (pats (cadr project)))
502 (while pats
503 (if (symbolp (car pats))
504 (setq pats (cddr pats))
505 (nconc names (muse-project-file-entries (car pats)))
506 (setq pats (cdr pats))))
507 (cdr names))))
508 (run-hooks 'muse-project-file-alist-hook)))))))
510 (defun muse-project-add-to-alist (file &optional project)
511 "Make sure FILE is added to `muse-project-file-alist'.
513 It works by either calling the `muse-project-file-alist' function
514 if a directory has been modified since we last checked, or
515 manually forcing the file entry to exist in the alist. This
516 works around an issue where if several files being saved at the
517 same time, only the first one will make it into the alist. It is
518 meant to be called by `muse-project-after-save-hook'.
520 The project of the file is determined by either the PROJECT
521 argument, or `muse-project-of-file' if PROJECT is not specified."
522 (setq project (or (muse-project project) (muse-project-of-file file)))
523 (when (and project muse-project-alist)
524 (let* ((file-alist (assoc (car project) muse-project-file-alist))
525 (last-mod (muse-project-determine-last-mod project)))
526 ;; Determine whether we need to call this
527 (if (or (null (cddr file-alist))
528 (null last-mod)
529 (muse-time-less-p (cddr file-alist) last-mod))
530 ;; The directory will show up as modified, so go ahead and
531 ;; call `muse-project-file-alist'
532 (muse-project-file-alist project)
533 ;; It is not showing as modified, so forcefully add the
534 ;; current file to the project file-alist
535 (let ((muse-updating-file-alist-p t))
536 (prog1
537 (save-match-data
538 (setcar (cdr file-alist)
539 (nconc (muse-project-file-entries file)
540 (cadr file-alist))))
541 (run-hooks 'muse-project-file-alist-hook)))))))
543 (defun muse-project-of-file (&optional pathname)
544 "Determine which project the given PATHNAME relates to.
545 If PATHNAME is nil, the current buffer's filename is used."
546 (if (and (null pathname) muse-current-project)
547 muse-current-project
548 (unless pathname (setq pathname (muse-current-file)))
549 (save-match-data
550 (when (and (stringp pathname)
551 muse-project-alist
552 (not (string= pathname ""))
553 (not (let ((case-fold-search nil))
554 (or (string-match muse-project-ignore-regexp
555 pathname)
556 (string-match muse-project-ignore-regexp
557 (file-name-nondirectory
558 pathname))))))
559 (let* ((file (file-truename pathname))
560 (dir (file-name-directory file))
561 found rating matches)
562 (catch 'found
563 (dolist (project-entry muse-project-alist)
564 (let ((pats (cadr project-entry)))
565 (while pats
566 (if (symbolp (car pats))
567 (setq pats (cddr pats))
568 (let ((tname (file-truename (car pats))))
569 (cond ((or (string= tname file)
570 (string= (file-name-as-directory tname) dir))
571 (throw 'found project-entry))
572 ((string-match (concat "\\`" (regexp-quote tname))
573 file)
574 (setq matches (cons (cons (match-end 0)
575 project-entry)
576 matches)))))
577 (setq pats (cdr pats))))))
578 ;; if we haven't found an exact match, pick a candidate
579 (car (muse-sort-by-rating matches))))))))
581 (defun muse-project-after-save-hook ()
582 "Update Muse's file-alist if we are saving a Muse file."
583 (let ((project (muse-project-of-file)))
584 (when project
585 (muse-project-add-to-alist (buffer-file-name) project))))
587 (add-hook 'after-save-hook 'muse-project-after-save-hook)
589 (defun muse-read-project (prompt &optional no-check-p no-assume)
590 "Read a project name from the minibuffer, if it can't be figured
591 out."
592 (if (null muse-project-alist)
593 (error "There are no Muse projects defined; see `muse-project-alist'")
594 (or (unless no-check-p
595 (muse-project-of-file))
596 (if (and (not no-assume)
597 (= 1 (length muse-project-alist)))
598 (car muse-project-alist)
599 (assoc (funcall muse-completing-read-function
600 prompt muse-project-alist)
601 muse-project-alist)))))
603 (defvar muse-project-page-history nil)
605 (defun muse-read-project-file (project prompt &optional default)
606 (let ((name (funcall muse-completing-read-function
607 prompt (muse-project-file-alist project)
608 nil nil nil 'muse-project-page-history
609 default)))
610 (cons name (muse-project-page-file name project))))
612 ;;;###autoload
613 (defun muse-project-find-file (name project &optional command directory)
614 "Open the Muse page given by NAME in PROJECT.
615 If COMMAND is non-nil, it is the function used to visit the file.
616 If DIRECTORY is non-nil, it is the directory in which the page
617 will be created if it does not already exist. Otherwise, the
618 first directory within the project's fileset is used."
619 (interactive
620 (let* ((project (muse-read-project "Find in project: "
621 current-prefix-arg))
622 (default (muse-get-keyword :default (cadr project)))
623 (entry (muse-read-project-file
624 project (if default
625 (format "Find page: (default: %s) "
626 default)
627 "Find page: ")
628 default)))
629 (list entry project)))
630 (setq project (muse-project project))
631 (let ((project-name (car project)))
632 (unless (interactive-p)
633 (setq project (muse-project project)
634 name (cons name (muse-project-page-file name project))))
635 ;; If we're given a relative or absolute filename, open it as-is
636 (if (and (car name)
637 (save-match-data
638 (or (string-match "\\`\\.+/" (car name))
639 (string-match muse-file-regexp (car name))
640 (string-match muse-image-regexp (car name)))))
641 (setcdr name (car name))
642 ;; At this point, name is (PAGE . FILE).
643 (unless (cdr name)
644 (let ((pats (cadr project)))
645 (while (and pats (null directory))
646 (if (symbolp (car pats))
647 (setq pats (cddr pats))
648 (if (file-directory-p (car pats))
649 (setq directory (car pats) pats nil)
650 (setq pats (cdr pats))))))
651 (when directory
652 (let ((filename (expand-file-name (car name) directory)))
653 (when (and muse-file-extension
654 (not (string= muse-file-extension ""))
655 (not (file-exists-p (car name))))
656 (setq filename (concat filename "." muse-file-extension)))
657 (unless (file-exists-p directory)
658 (make-directory directory t))
659 (setcdr name filename)))))
660 ;; Open the file
661 (if (cdr name)
662 (funcall (or command 'find-file) (cdr name))
663 (error "There is no page %s in project %s"
664 (car name) project-name))))
666 (defun muse-project-choose-style (closure test styles)
667 "Run TEST on STYLES and return first style where TEST yields non-nil.
668 TEST should take two arguments. The first is CLOSURE, which is
669 passed verbatim. The second if the current style to consider.
671 If no style passes TEST, return the first style."
672 (or (catch 'winner
673 (dolist (style styles)
674 (when (funcall test closure style)
675 (throw 'winner style))))
676 (car styles)))
678 (defun muse-project-choose-style-by-link-suffix (given-suffix style)
679 "If the given STYLE has a link-suffix that equals GIVEN-SUFFIX,
680 return non-nil."
681 (let ((link-suffix (or (muse-style-element :link-suffix style)
682 (muse-style-element :suffix style))))
683 (and (stringp link-suffix)
684 (string= given-suffix link-suffix))))
686 (defun muse-project-applicable-styles (file styles)
687 "Given STYLES, return a list of the ones that are considered for FILE.
688 The name of a project may be used for STYLES."
689 (when (stringp styles)
690 (setq styles (cddr (muse-project styles))))
691 (when (and file styles)
692 (let ((used-styles nil))
693 (dolist (style styles)
694 (let ((include-regexp (muse-style-element :include style))
695 (exclude-regexp (muse-style-element :exclude style))
696 (rating nil))
697 (when (and (or (and (null include-regexp)
698 (null exclude-regexp))
699 (if include-regexp
700 (setq rating (string-match include-regexp file))
701 (not (string-match exclude-regexp file))))
702 (file-exists-p file)
703 (not (muse-project-private-p file)))
704 (setq used-styles (cons (cons rating style) used-styles)))))
705 (muse-sort-by-rating (nreverse used-styles)))))
707 (defun muse-project-get-applicable-style (file styles)
708 "Choose a style from the STYLES that FILE can publish to.
709 The user is prompted if several styles are found."
710 (muse-publish-get-style
711 (mapcar (lambda (style)
712 (cons (muse-get-keyword :base style) style))
713 (muse-project-applicable-styles file styles))))
715 (defun muse-project-resolve-directory (page local-style remote-style)
716 "Figure out the directory part of the path that provides a link to PAGE.
717 LOCAL-STYLE is the style of the current Muse file, and
718 REMOTE-STYLE is the style associated with PAGE.
720 If REMOTE-STYLE has a :base-url element, concatenate it and PAGE.
721 Otherwise, return a relative link."
722 (let ((prefix (muse-style-element :base-url remote-style)))
723 (if prefix
724 (concat prefix page)
725 (file-relative-name (expand-file-name
726 (file-name-nondirectory page)
727 (muse-style-element :path remote-style))
728 (expand-file-name
729 (muse-style-element :path local-style))))))
731 (defun muse-project-resolve-link (page local-style remote-styles)
732 "Return a published link from the output path of one file to another file.
734 The best match for PAGE is determined by comparing the link
735 suffix of the given local style and that of the remote styles.
737 The remote styles are usually populated by
738 `muse-project-applicable-styles'.
740 If no remote style is found, return PAGE verbatim
742 If PAGE has a :base-url associated with it, return the
743 concatenation of the :base-url value and PAGE.
745 Otherwise, return a relative path from the directory of
746 LOCAL-STYLE to the best directory among REMOTE-STYLES."
747 (let ((link-suffix (or (muse-style-element :link-suffix local-style)
748 (muse-style-element :suffix local-style)))
749 remote-style)
750 (if (not (stringp link-suffix))
751 (setq remote-style (car remote-styles))
752 (setq remote-style (muse-project-choose-style
753 link-suffix
754 #'muse-project-choose-style-by-link-suffix
755 remote-styles)))
756 (if (null remote-style)
757 page
758 (setq page (muse-project-resolve-directory
759 page local-style remote-style))
760 (concat (file-name-directory page)
761 (muse-publish-link-name page remote-style)))))
763 (defun muse-project-current-output-style (&optional file project)
764 (or muse-current-output-style
765 (progn
766 (unless file (setq file (muse-current-file)))
767 (unless project (setq project (muse-project-of-file file)))
768 (car (muse-project-applicable-styles file (cddr project))))))
770 (defun muse-project-link-page (page)
771 (let ((project (muse-project-of-file)))
772 (muse-project-resolve-link page
773 (muse-project-current-output-style)
774 (muse-project-applicable-styles
775 (muse-project-page-file page project)
776 (cddr project)))))
778 (defun muse-project-publish-file-default (file style output-dir force)
779 ;; ensure the publishing location is available
780 (unless (file-exists-p output-dir)
781 (message "Creating publishing directory %s" output-dir)
782 (make-directory output-dir t))
783 ;; publish the member file!
784 (muse-publish-file file style output-dir force))
786 (defun muse-project-publish-file (file styles &optional force)
787 (setq styles (muse-project-applicable-styles file styles))
788 (let (published)
789 (dolist (style styles)
790 (if (or (not (listp style))
791 (not (cdr style)))
792 (muse-display-warning
793 (concat "Skipping malformed muse-project-alist style."
794 "\nPlease double-check your configuration,"))
795 (let ((output-dir (muse-style-element :path style))
796 (muse-current-output-style style)
797 (fun (or (muse-style-element :publish style t)
798 'muse-project-publish-file-default)))
799 (when (funcall fun file style output-dir force)
800 (setq published t)))))
801 published))
803 ;;;###autoload
804 (defun muse-project-publish-this-file (&optional force)
805 "Publish the currently-visited file according to `muse-project-alist',
806 prompting if more than one style applies.
808 If FORCE is given, publish the file even if it is up-to-date."
809 (interactive (list current-prefix-arg))
810 (let ((muse-current-project (muse-project-of-file)))
811 (if (not muse-current-project)
812 ;; file is not part of a project, so fall back to muse-publish
813 (if (interactive-p) (call-interactively 'muse-publish-this-file)
814 (muse-publish-this-file nil nil force))
815 (let* ((style (muse-project-get-applicable-style
816 buffer-file-name (cddr muse-current-project)))
817 (output-dir (muse-style-element :path style))
818 (muse-current-output-style (list :base (car style)
819 :path output-dir)))
820 (unless (muse-publish-file buffer-file-name style output-dir force)
821 (message (concat "The published version is up-to-date; use"
822 " C-u C-c C-t to force an update.")))))))
824 (defun muse-project-save-buffers (&optional project)
825 (setq project (muse-project project))
826 (when project
827 (map-y-or-n-p
828 (function
829 (lambda (buffer)
830 (and (buffer-modified-p buffer)
831 (not (buffer-base-buffer buffer))
832 (or (buffer-file-name buffer)
833 (progn
834 (set-buffer buffer)
835 (and buffer-offer-save
836 (> (buffer-size) 0))))
837 (with-current-buffer buffer
838 (let ((proj (muse-project-of-file)))
839 (and proj (string= (car proj)
840 (car project)))))
841 (if (buffer-file-name buffer)
842 (format "Save file %s? "
843 (buffer-file-name buffer))
844 (format "Save buffer %s? "
845 (buffer-name buffer))))))
846 (function
847 (lambda (buffer)
848 (set-buffer buffer)
849 (save-buffer)))
850 (buffer-list)
851 '("buffer" "buffers" "save")
852 (if (boundp 'save-some-buffers-action-alist)
853 save-some-buffers-action-alist))))
855 (defun muse-project-publish-default (project styles &optional force)
856 "Publish the pages of PROJECT that need publishing."
857 (setq project (muse-project project))
858 (let ((published nil))
859 ;; publish all files in the project, for each style; the actual
860 ;; publishing will only happen if the files are newer than the
861 ;; last published output, or if the file is listed in
862 ;; :force-publish. Files in :force-publish will not trigger the
863 ;; "All pages need to be published" message.
864 (let ((forced-files (muse-get-keyword :force-publish (cadr project)))
865 (file-alist (muse-project-file-alist project)))
866 (dolist (pair file-alist)
867 (when (muse-project-publish-file (cdr pair) styles force)
868 (setq forced-files (delete (car pair) forced-files))
869 (setq published t)))
870 (dolist (file forced-files)
871 (muse-project-publish-file (cdr (assoc file file-alist)) styles t)))
872 ;; run hook after publishing ends
873 (run-hook-with-args 'muse-after-project-publish-hook project)
874 ;; notify the user that everything is now done
875 (if published
876 (message "All pages in %s have been published." (car project))
877 (message "No pages in %s need publishing at this time."
878 (car project)))))
880 ;;;###autoload
881 (defun muse-project-publish (project &optional force)
882 "Publish the pages of PROJECT that need publishing."
883 (interactive (list (muse-read-project "Publish project: " nil t)
884 current-prefix-arg))
885 (setq project (muse-project project))
886 (let ((styles (cddr project))
887 (muse-current-project project))
888 ;; determine the style from the project, or else ask
889 (unless styles
890 (setq styles (list (muse-publish-get-style))))
891 (unless project
892 (error "Cannot find a project to publish"))
893 ;; prompt to save any buffers related to this project
894 (muse-project-save-buffers project)
895 ;; run hook before publishing begins
896 (run-hook-with-args 'muse-before-project-publish-hook project)
897 ;; run the project-level publisher
898 (let ((fun (or (muse-get-keyword :publish-project (cadr project) t)
899 'muse-project-publish-default)))
900 (funcall fun project styles force))))
902 (defun muse-project-batch-publish ()
903 "Publish Muse files in batch mode."
904 (let ((muse-batch-publishing-p t)
905 force)
906 (if (string= "--force" (or (car command-line-args-left) ""))
907 (setq force t
908 command-line-args-left (cdr command-line-args-left)))
909 (if command-line-args-left
910 (dolist (project command-line-args-left)
911 (message "Publishing project %s ..." project)
912 (muse-project-publish project force))
913 (message "No projects specified."))))
915 (eval-when-compile
916 (put 'make-local-hook 'byte-compile nil))
918 (defun muse-project-set-variables ()
919 "Load project-specific variables."
920 (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
921 sym custom-set var)
922 (while vars
923 (setq sym (car vars))
924 (setq custom-set (or (get sym 'custom-set) 'set))
925 (setq var (if (eq (get sym 'custom-type) 'hook)
926 (make-local-hook sym)
927 (make-local-variable sym)))
928 (funcall custom-set var (car (cdr vars)))
929 (setq vars (cdr (cdr vars))))))
931 (custom-add-option 'muse-before-publish-hook 'muse-project-set-variables)
932 (add-to-list 'muse-before-publish-hook 'muse-project-set-variables)
934 (defun muse-project-delete-output-files (project)
935 (interactive
936 (list (muse-read-project "Remove all output files for project: " nil t)))
937 (setq project (muse-project project))
938 (let ((file-alist (muse-project-file-alist project))
939 (styles (cddr project))
940 output-file path)
941 (dolist (entry file-alist)
942 (dolist (style styles)
943 (setq output-file
944 (and (setq path (muse-style-element :path style))
945 (expand-file-name
946 (concat (muse-style-element :prefix style)
947 (car entry)
948 (or (muse-style-element :osuffix style)
949 (muse-style-element :suffix style)))
950 path)))
951 (if output-file
952 (muse-delete-file-if-exists output-file))))))
954 ;;; muse-project.el ends here