Fix problem with <include> and <contents> tags.
[muse-el.git] / lisp / muse-project.el
blobd93a33aaaa2a5083c86e9ecbc4e03361b5618c4e
1 ;;; muse-project.el --- handle Muse projects
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 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 "Set variables"
185 (const :tag ":set" ":set")
186 (repeat (list :inline t
187 (symbol :tag "Variable")
188 (sexp :tag "Setting"))))
189 (list :tag "Visit links using"
190 (const :tag ":visit-link" ":visit-link")
191 (choice (function)
192 (sexp :tag "Unknown")))))
193 (repeat :tag "Styles" :format "%{%t%}:\n%v%i\n\n"
194 (set :tag "Style"
195 (list :inline t
196 :tag "Publishing style"
197 (const :tag ":base" ":base")
198 (string :tag "Style"))
199 (list :inline t
200 :tag "Base URL"
201 (const :tag ":base-url" ":base-url")
202 (string :tag "URL"))
203 (list :inline t
204 :tag "Exclude matching"
205 (const :tag ":exclude" ":exclude")
206 (regexp))
207 (list :inline t
208 :tag "Include matching"
209 (const :tag ":include" ":include")
210 (regexp))
211 (list :inline t
212 :tag "Timestamps file"
213 (const :tag ":timestamps" ":timestamps")
214 (file))
215 (list :inline t
216 :tag "Path"
217 (const :tag ":path" ":path")
218 (string :tag "Path"))))))
220 (defcustom muse-project-alist nil
221 "An alist of Muse projects.
222 A project defines a fileset, and a list of custom attributes for use
223 when publishing files in that project."
224 :type '(choice (const :tag "No projects defined." nil)
225 (repeat (cons :format "%{%t%}:\n\n%v"
226 :tag "Project" :indent 4
227 (string :tag "Project name")
228 muse-project))
229 (sexp :tag "Cannot parse expression"))
230 :get 'muse-project-alist-get
231 :set 'muse-project-alist-set
232 :group 'muse-project)
234 ;; Make it easier to specify a muse-project-alist entry
236 (defcustom muse-project-ignore-regexp
237 (concat "\\`\\(#.*#\\|.*,v\\|.*~\\|\\.\\.?\\|\\.#.*\\|,.*\\)\\'\\|"
238 "/\\(CVS\\|RCS\\|\\.arch-ids\\|{arch}\\|,.*\\|\\.svn\\|"
239 "_darcs\\)\\(/\\|\\'\\)")
240 "A regexp matching files to be ignored in Muse directories.
242 You should set `case-fold-search' to nil before using this regexp
243 in code."
244 :type 'regexp
245 :group 'muse-regexp)
247 (defcustom muse-project-publish-private-files t
248 "If this is non-nil, files will be published even if their permissions
249 are set so that no one else on the filesystem can read them.
251 Set this to nil if you would like to indicate that some files
252 should not be published by manually doing \"chmod o-rwx\" on
253 them.
255 This setting has no effect under Windows (that is, all files are
256 published regardless of permissions) because Windows lacks the
257 needed filesystem attributes."
258 :type 'boolean
259 :group 'muse-project)
261 (defun muse-project-recurse-directory (base)
262 "Recusively retrieve all of the directories underneath BASE.
263 A list of these directories is returned.
265 Directories starting with \".\" will be ignored, as well as those
266 which match `muse-project-ignore-regexp'."
267 (let ((case-fold-search nil)
268 list dir)
269 (when (and (file-directory-p base)
270 (not (string-match muse-project-ignore-regexp base)))
271 (dolist (file (directory-files base t "^[^.]"))
272 (when (and (file-directory-p file)
273 (not (string-match muse-project-ignore-regexp file)))
274 (setq dir (file-name-nondirectory file))
275 (push dir list)
276 (nconc list (mapcar #'(lambda (item)
277 (concat dir "/" item))
278 (muse-project-recurse-directory file)))))
279 list)))
281 (defun muse-project-alist-styles (entry-dir output-dir style)
282 "Return a list of styles to use in a `muse-project-alist' entry.
283 ENTRY-DIR is the top-level directory of the project.
284 OUTPUT-DIR is where Muse files are published, keeping directory structure.
285 STYLE is the publishing style to use.
287 For an example of the use of this function, see
288 `examples/mwolson/muse-init.el' from the Muse distribution."
289 (cons `(:base ,style :path ,(expand-file-name output-dir)
290 :include ,(concat "/" (file-name-nondirectory entry-dir)
291 "/[^/]+$"))
292 (mapcar (lambda (dir)
293 `(:base ,style
294 :path ,(expand-file-name dir output-dir)
295 :include ,(concat "/" dir "/[^/]+$")))
296 (muse-project-recurse-directory entry-dir))))
298 (defun muse-project-alist-dirs (entry-dir)
299 "Return a list of directories to use in a `muse-project-alist' entry.
300 ENTRY-DIR is the top-level directory of the project.
302 For an example of the use of this function, see
303 `examples/mwolson/muse-init.el' from the Muse distribution."
304 (cons (expand-file-name entry-dir)
305 (mapcar (lambda (dir) (expand-file-name dir entry-dir))
306 (muse-project-recurse-directory entry-dir))))
308 ;; Constructing the file-alist
310 (defvar muse-project-file-alist nil
311 "This variable is automagically constructed as needed.")
313 (defvar muse-project-file-alist-hook nil
314 "Functions that are to be exectuted immediately after updating
315 `muse-project-file-alist'.")
317 (defvar muse-current-project nil
318 "Project we are currently visiting.")
319 (make-variable-buffer-local 'muse-current-project)
321 (defvar muse-current-output-style nil
322 "The output style that we are currently using for publishing files.")
324 (defsubst muse-project (&optional project)
325 "Resolve the given PROJECT into a full Muse project, if it is a string."
326 (if (null project)
327 (or muse-current-project
328 (muse-project-of-file))
329 (if (stringp project)
330 (assoc project muse-project-alist)
331 (muse-assert (consp project))
332 project)))
334 (defun muse-project-page-file (page project &optional no-check-p)
335 "Return a filename if PAGE exists within the given Muse PROJECT."
336 (setq project (muse-project project))
337 (let ((dir (file-name-directory page))
338 (expanded-path nil))
339 (when dir
340 (setq expanded-path (concat (expand-file-name
341 page
342 (file-name-directory (muse-current-file)))
343 (when muse-file-extension
344 (concat "." muse-file-extension))))
345 (setq page (file-name-nondirectory page)))
346 (let ((files (muse-collect-alist
347 (muse-project-file-alist project no-check-p)
348 page))
349 (matches nil))
350 (if dir
351 (catch 'done
352 (save-match-data
353 (dolist (file files)
354 (if (and expanded-path
355 (string= expanded-path (cdr file)))
356 (throw 'done (cdr file))
357 (let ((pos (string-match (concat (regexp-quote dir) "\\'")
358 (file-name-directory (cdr file)))))
359 (when pos
360 (setq matches (cons (cons pos (cdr file))
361 matches)))))))
362 ;; if we haven't found an exact match, pick a candidate
363 (car (muse-sort-by-rating matches)))
364 (dolist (file files)
365 (setq matches (cons (cons (length (cdr file)) (cdr file))
366 matches)))
367 (car (muse-sort-by-rating matches '<))))))
369 (defun muse-project-private-p (file)
370 "Return non-nil if NAME is a private page with PROJECT."
371 (unless (or muse-under-windows-p
372 muse-project-publish-private-files)
373 (setq file (file-truename file))
374 (if (file-attributes file) ; don't publish if no attributes exist
375 (or (when (eq ?- (aref (nth 8 (file-attributes
376 (file-name-directory file))) 7))
377 (message (concat
378 "The " (file-name-directory file)
379 " directory must be readable by others"
380 " in order for its contents to be published.")))
381 (eq ?- (aref (nth 8 (file-attributes file)) 7)))
382 t)))
384 (defun muse-project-file-entries (path)
385 (let* ((names (list t))
386 (lnames names)
387 (case-fold-search nil))
388 (cond
389 ((file-directory-p path)
390 (dolist (file (directory-files
391 path t (when (and muse-file-extension
392 (not (string= muse-file-extension "")))
393 (concat "." muse-file-extension "\\'"))))
394 (unless (or (string-match muse-project-ignore-regexp file)
395 (string-match muse-project-ignore-regexp
396 (file-name-nondirectory file))
397 (file-directory-p file))
398 (setcdr lnames
399 (cons (cons (muse-page-name file) file) nil))
400 (setq lnames (cdr lnames)))))
401 ((file-readable-p path)
402 (setcdr lnames
403 (cons (cons (muse-page-name path) path) nil))
404 (setq lnames (cdr lnames)))
405 (t ; regexp
406 (muse-assert (file-name-directory path))
407 (dolist (file (directory-files
408 (file-name-directory path) t
409 (file-name-nondirectory path)))
410 (unless (or (string-match muse-project-ignore-regexp file)
411 (string-match muse-project-ignore-regexp
412 (file-name-nondirectory file)))
413 (setcdr lnames
414 (cons (cons (muse-page-name file) file) nil))
415 (setq lnames (cdr lnames))))))
416 (cdr names)))
418 (defvar muse-updating-file-alist-p nil
419 "Make sure that recursive calls to `muse-project-file-alist' are bounded.")
421 (defun muse-project-file-alist (&optional project no-check-p)
422 "Return member filenames for the given Muse PROJECT.
423 On UNIX, this list is only updated if one of the directories'
424 contents have changed. On Windows, it is always reread from
425 disk."
426 (setq project (muse-project project))
427 (let* ((file-alist (assoc (car project) muse-project-file-alist))
428 (last-mod (cdr (cdr file-alist))))
429 ;; Determine the last modified of any directory mentioned in the
430 ;; project's pattern list
431 (unless (or muse-under-windows-p no-check-p)
432 (let ((pats (cadr project)))
433 (while pats
434 (if (symbolp (car pats))
435 (setq pats (cddr pats))
436 (let* ((fnd (file-name-directory (car pats)))
437 (dir (cond ((file-directory-p (car pats))
438 (car pats))
439 ((and (not (file-readable-p (car pats)))
441 (file-directory-p fnd))
442 fnd))))
443 (when dir
444 (let ((mod-time (nth 5 (file-attributes dir))))
445 (when (or (null last-mod)
446 (and mod-time
447 (muse-time-less-p last-mod mod-time)))
448 (setq last-mod mod-time)))))
449 (setq pats (cdr pats))))))
450 ;; Either return the currently known list, or read it again from
451 ;; disk
452 (if (or (and no-check-p (cadr file-alist))
453 muse-updating-file-alist-p
454 (not (or muse-under-windows-p
455 (null (cddr file-alist))
456 (null last-mod)
457 (muse-time-less-p (cddr file-alist) last-mod))))
458 (cadr file-alist)
459 (if file-alist
460 (setcdr (cdr file-alist) last-mod)
461 (setq file-alist (cons (car project) (cons nil last-mod))
462 muse-project-file-alist
463 (cons file-alist muse-project-file-alist)))
464 ;; Read in all of the file entries
465 (let ((muse-updating-file-alist-p t))
466 (prog1
467 (save-match-data
468 (setcar
469 (cdr file-alist)
470 (let* ((names (list t))
471 (pats (cadr project)))
472 (while pats
473 (if (symbolp (car pats))
474 (setq pats (cddr pats))
475 (nconc names (muse-project-file-entries (car pats)))
476 (setq pats (cdr pats))))
477 (cdr names))))
478 (run-hooks 'muse-project-file-alist-hook))))))
480 (defun muse-project-of-file (&optional pathname)
481 "Determine which project the given PATHNAME relates to.
482 If PATHNAME is nil, the current buffer's filename is used."
483 (if (and (null pathname) muse-current-project)
484 muse-current-project
485 (unless pathname (setq pathname (muse-current-file)))
486 (save-match-data
487 (when (and (stringp pathname)
488 (not (string= pathname ""))
489 (not (let ((case-fold-search nil))
490 (or (string-match muse-project-ignore-regexp
491 pathname)
492 (string-match muse-project-ignore-regexp
493 (file-name-nondirectory
494 pathname))))))
495 (let* ((file (file-truename pathname))
496 (dir (file-name-directory file))
497 (project-entry muse-project-alist)
498 found)
499 (while (and project-entry (not found))
500 (let ((pats (car (cdar project-entry))))
501 (while (and pats (not found))
502 (if (symbolp (car pats))
503 (setq pats (cddr pats))
504 (let ((truename (file-truename (car pats))))
505 (if (or (string= truename file)
506 (string= truename dir)
507 (string-match (regexp-quote truename) file))
508 (setq found (car project-entry))))
509 (setq pats (cdr pats))))
510 (setq project-entry (cdr project-entry))))
511 found)))))
513 (defun muse-project-after-save-hook ()
514 "Update Muse's file-alist if we are saving a Muse file."
515 (let ((project (muse-project-of-file)))
516 (when project
517 (muse-project-file-alist project))))
519 (add-hook 'after-save-hook 'muse-project-after-save-hook)
521 (defun muse-read-project (prompt &optional no-check-p no-assume)
522 "Read a project name from the minibuffer, if it can't be figured
523 out."
524 (if (null muse-project-alist)
525 (error "There are no Muse projects defined; see `muse-project-alist'.")
526 (or (unless no-check-p
527 (muse-project-of-file))
528 (if (and (not no-assume)
529 (= 1 (length muse-project-alist)))
530 (car muse-project-alist)
531 (assoc (completing-read prompt muse-project-alist)
532 muse-project-alist)))))
534 (defvar muse-project-page-history nil)
536 (defun muse-read-project-file (project prompt &optional default)
537 (let ((name (completing-read prompt (muse-project-file-alist project)
538 nil nil nil 'muse-project-page-history
539 default)))
540 (cons name (muse-project-page-file name project))))
542 ;;;###autoload
543 (defun muse-project-find-file (name project &optional command directory)
544 "Open the Muse page given by NAME in PROJECT.
545 If COMMAND is non-nil, it is the function used to visit the file.
546 If DIRECTORY is non-nil, it is the directory in which the page
547 will be created if it does not already exist. Otherwise, the
548 first directory within the project's fileset is used."
549 (interactive
550 (let* ((project (muse-read-project "Find in project: "
551 current-prefix-arg))
552 (default (muse-get-keyword :default (cadr project)))
553 (entry (muse-read-project-file
554 project (if default
555 (format "Find page: (default: %s) "
556 default)
557 "Find page: ")
558 default)))
559 (list entry project)))
560 (setq project (muse-project project))
561 (let ((project-name (car project)))
562 (unless (interactive-p)
563 (setq project (muse-project project)
564 name (cons name (muse-project-page-file name project))))
565 ;; If we're given a relative or absolute filename, open it as-is
566 (if (and (car name)
567 (save-match-data
568 (or (string-match "\\`\\.+/" (car name))
569 (string-match muse-file-regexp (car name))
570 (string-match muse-image-regexp (car name)))))
571 (setcdr name (car name))
572 ;; At this point, name is (PAGE . FILE).
573 (unless (cdr name)
574 (let ((pats (cadr project)))
575 (while (and pats (null directory))
576 (if (symbolp (car pats))
577 (setq pats (cddr pats))
578 (if (file-directory-p (car pats))
579 (setq directory (car pats) pats nil)
580 (setq pats (cdr pats))))))
581 (when directory
582 (let ((filename (expand-file-name (car name) directory)))
583 (when (and muse-file-extension
584 (not (string= muse-file-extension ""))
585 (not (file-exists-p (car name))))
586 (setq filename (concat filename "." muse-file-extension)))
587 (unless (file-exists-p directory)
588 (make-directory directory t))
589 (setcdr name filename)))))
590 ;; Open the file
591 (if (cdr name)
592 (funcall (or command 'find-file) (cdr name))
593 (error "There is no page %s in project %s."
594 (car name) project-name))))
596 (defun muse-project-choose-style (closure test styles)
597 "Run TEST on STYLES and return first style where TEST yields non-nil.
598 TEST should take two arguments. The first is CLOSURE, which is
599 passed verbatim. The second if the current style to consider.
601 If no style passes TEST, return the first style."
602 (or (catch 'winner
603 (dolist (style styles)
604 (when (funcall test closure style)
605 (throw 'winner style))))
606 (car styles)))
608 (defun muse-project-choose-style-by-link-suffix (given-suffix style)
609 "If the given STYLE has a link-suffix that equals GIVEN-SUFFIX,
610 return non-nil."
611 (let ((link-suffix (or (muse-style-element :link-suffix style)
612 (muse-style-element :suffix style))))
613 (and (stringp link-suffix)
614 (string= given-suffix link-suffix))))
616 (defun muse-project-applicable-styles (file styles &optional ignore-regexp)
617 "Given STYLES, return a list of the ones that are considered for FILE.
618 The name of a project may be used for STYLES."
619 (when (stringp styles)
620 (setq styles (cddr (muse-project styles))))
621 (when (and file styles)
622 (let ((used-styles nil))
623 (dolist (style styles)
624 (let ((include-regexp (muse-style-element :include style))
625 (exclude-regexp (muse-style-element :exclude style))
626 (rating nil))
627 (when (and (or ignore-regexp
628 (and (null include-regexp)
629 (null exclude-regexp))
630 (if include-regexp
631 (setq rating (string-match include-regexp file))
632 (not (string-match exclude-regexp file))))
633 (or (not (file-exists-p file))
634 (not (muse-project-private-p file))))
635 (setq used-styles (cons (cons rating style) used-styles)))))
636 (muse-sort-by-rating (nreverse used-styles)))))
638 (defun muse-project-get-applicable-style (file styles)
639 "Choose a style from the STYLES that FILE can publish to.
640 The user is prompted if several styles are found."
641 (muse-publish-get-style
642 (mapcar (lambda (style)
643 (cons (muse-get-keyword :base style) style))
644 (muse-project-applicable-styles file styles))))
646 (defun muse-project-resolve-link (page local-style remote-styles)
647 "Return a published relative link from the output path of one file
648 to another file.
650 The best match for PAGE is determined by comparing the link
651 suffix of the given local style and that of the remote styles.
653 The remote styles are usually populated by
654 `muse-project-applicable-styles'."
655 (let ((link-suffix (or (muse-style-element :link-suffix local-style)
656 (muse-style-element :suffix local-style)))
657 remote-style)
658 (if (not (stringp link-suffix))
659 (setq remote-style (car remote-styles))
660 (setq remote-style (muse-project-choose-style
661 link-suffix
662 #'muse-project-choose-style-by-link-suffix
663 remote-styles)))
664 (if (null remote-style)
665 page
666 (muse-publish-link-file
667 (let ((prefix (muse-style-element :base-url remote-style)))
668 (if prefix
669 (concat prefix page)
670 (file-relative-name (expand-file-name
671 (file-name-nondirectory page)
672 (muse-style-element :path remote-style))
673 (expand-file-name
674 (muse-style-element :path local-style)))))
675 remote-style))))
677 (defun muse-project-current-output-style (&optional file project)
678 (or muse-current-output-style
679 (progn
680 (unless file (setq file (muse-current-file)))
681 (unless project (setq project (muse-project-of-file file)))
682 (car (muse-project-applicable-styles file (cddr project))))))
684 (defun muse-project-link-page (page)
685 (let ((project (muse-project-of-file)))
686 (muse-project-resolve-link page
687 (muse-project-current-output-style)
688 (muse-project-applicable-styles
689 (muse-project-page-file page project)
690 (cddr project)))))
692 (defun muse-project-publish-file (file styles &optional force ignore-regexp)
693 (setq styles (muse-project-applicable-styles file styles ignore-regexp))
694 (let (published)
695 (dolist (style styles)
696 (let ((output-dir (muse-style-element :path style))
697 (muse-current-output-style style))
698 ;; ensure the publishing location is available
699 (unless (file-exists-p output-dir)
700 (message "Creating publishing directory %s" output-dir)
701 (make-directory output-dir t))
702 ;; publish the member file!
703 (if (muse-publish-file file style output-dir force)
704 (setq published t))))
705 published))
707 ;;;###autoload
708 (defun muse-project-publish-this-file (&optional force)
709 "Publish the currently-visited file according to `muse-project-alist',
710 prompting if more than one style applies.
712 If FORCE is given, publish the file even if it is up-to-date."
713 (interactive (list current-prefix-arg))
714 (let* ((style (muse-project-get-applicable-style
715 buffer-file-name (cddr muse-current-project)))
716 (output-dir (muse-style-element :path style)))
717 (unless (muse-publish-file buffer-file-name style output-dir force)
718 (message (concat "The published version is up-to-date; use"
719 " C-u C-c C-t to force an update.")))))
721 (defun muse-project-save-buffers (&optional project)
722 (setq project (muse-project project))
723 (map-y-or-n-p
724 (function
725 (lambda (buffer)
726 (and (buffer-modified-p buffer)
727 (not (buffer-base-buffer buffer))
728 (or (buffer-file-name buffer)
729 (progn
730 (set-buffer buffer)
731 (and buffer-offer-save
732 (> (buffer-size) 0))))
733 (with-current-buffer buffer
734 (let ((proj (muse-project-of-file)))
735 (and proj (string= (car proj)
736 (car project)))))
737 (if (buffer-file-name buffer)
738 (format "Save file %s? "
739 (buffer-file-name buffer))
740 (format "Save buffer %s? "
741 (buffer-name buffer))))))
742 (function
743 (lambda (buffer)
744 (set-buffer buffer)
745 (save-buffer)))
746 (buffer-list)
747 '("buffer" "buffers" "save")
748 (if (boundp 'save-some-buffers-action-alist)
749 save-some-buffers-action-alist)))
751 ;;;###autoload
752 (defun muse-project-publish (project &optional force)
753 "Publish the pages of PROJECT that need publishing."
754 (interactive (list (muse-read-project "Publish project: " nil t)
755 current-prefix-arg))
756 (setq project (muse-project project))
757 (let ((styles (cddr project))
758 (muse-current-project project)
759 published)
760 ;; determine the style from the project, or else ask
761 (unless styles
762 (setq styles (list (muse-publish-get-style))))
763 (unless project
764 (error "Cannot find a project to publish"))
765 ;; prompt to save any buffers related to this project
766 (muse-project-save-buffers project)
767 ;; run hook before publishing begins
768 (run-hook-with-args 'muse-before-project-publish-hook project)
769 ;; publish all files in the project, for each style; the actual
770 ;; publishing will only happen if the files are newer than the
771 ;; last published output, or if the file is listed in
772 ;; :force-publish. Files in :force-publish will not trigger the
773 ;; "All pages need to be published" message.
774 (let ((forced-files (muse-get-keyword :force-publish (cadr project)))
775 (file-alist (muse-project-file-alist project)))
776 (dolist (pair file-alist)
777 (when (muse-project-publish-file (cdr pair) styles force)
778 (setq forced-files (delete (car pair) forced-files))
779 (setq published t)))
780 (dolist (file forced-files)
781 (muse-project-publish-file (cdr (assoc file file-alist)) styles t)))
782 ;; run hook after publishing ends
783 (run-hook-with-args 'muse-after-project-publish-hook project)
784 ;; notify the user that everything is now done
785 (if published
786 (message "All pages in %s have been published." (car project))
787 (message "No pages in %s need publishing at this time."
788 (car project)))))
790 (defun muse-project-batch-publish ()
791 "Publish Muse files in batch mode."
792 (let ((muse-batch-publishing-p t)
793 force)
794 (if (string= "--force" (or (car command-line-args-left) ""))
795 (setq force t
796 command-line-args-left (cdr command-line-args-left)))
797 (if command-line-args-left
798 (dolist (project command-line-args-left)
799 (message "Publishing project %s ..." project)
800 (muse-project-publish project force))
801 (message "No projects specified."))))
803 (eval-when-compile
804 (put 'make-local-hook 'byte-compile nil))
806 (defun muse-project-set-variables ()
807 "Load project-specific variables."
808 (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
809 sym custom-set var)
810 (while vars
811 (setq sym (car vars))
812 (setq custom-set (or (get sym 'custom-set) 'set))
813 (setq var (if (eq (get sym 'custom-type) 'hook)
814 (make-local-hook sym)
815 (make-local-variable sym)))
816 (funcall custom-set var (car (cdr vars)))
817 (setq vars (cdr (cdr vars))))))
819 (defun muse-project-delete-output-files (project)
820 (interactive
821 (list (muse-read-project "Remove all output files for project: " nil t)))
822 (setq project (muse-project project))
823 (let ((file-alist (muse-project-file-alist project))
824 (styles (cddr project))
825 output-file path)
826 (dolist (entry file-alist)
827 (dolist (style styles)
828 (setq output-file
829 (and (setq path (muse-style-element :path style))
830 (expand-file-name
831 (concat (muse-style-element :prefix style)
832 (car entry)
833 (or (muse-style-element :osuffix style)
834 (muse-style-element :suffix style)))
835 path)))
836 (if output-file
837 (muse-delete-file-if-exists output-file))))))
839 ;;; muse-project.el ends here