Commit | Line | Data |
---|---|---|
9542795a | 1 | ;;; org-publish.el --- publish related org-mode files as a website |
ae940284 | 2 | ;; Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
9542795a CD |
3 | |
4 | ;; Author: David O'Toole <dto@gnu.org> | |
699b9291 CD |
5 | ;; Maintainer: Bastien Guerry <bzg AT altern DOT org> |
6 | ;; Keywords: hypermedia, outlines, wp | |
5ace2fe5 | 7 | ;; Version: 6.21b |
9542795a | 8 | |
514a6ce6 CD |
9 | ;; This file is part of GNU Emacs. |
10 | ;; | |
b1fc2b50 | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
9542795a | 12 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
9542795a | 15 | |
514a6ce6 | 16 | ;; GNU Emacs is distributed in the hope that it will be useful, |
9542795a CD |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b1fc2b50 | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
9542795a | 23 | |
9542795a CD |
24 | ;;; Commentary: |
25 | ||
699b9291 CD |
26 | ;; This program allow configurable publishing of related sets of |
27 | ;; Org-mode files as a complete website. | |
9542795a CD |
28 | ;; |
29 | ;; org-publish.el can do the following: | |
30 | ;; | |
699b9291 CD |
31 | ;; + Publish all one's org-files to HTML or LaTeX |
32 | ;; + Upload HTML, images, attachments and other files to a web server | |
9542795a CD |
33 | ;; + Exclude selected private pages from publishing |
34 | ;; + Publish a clickable index of pages | |
699b9291 | 35 | ;; + Manage local timestamps for publishing only changed files |
9542795a CD |
36 | ;; + Accept plugin functions to extend range of publishable content |
37 | ;; | |
38 | ;; Special thanks to the org-mode maintainer Carsten Dominik for his | |
39 | ;; ideas, enthusiasm, and cooperation. | |
40 | ||
41 | ;;; Installation: | |
42 | ||
43 | ;; Put org-publish.el in your load path, byte-compile it, and then add | |
44 | ;; the following lines to your emacs initialization file: | |
45 | ||
46 | ;; (autoload 'org-publish "org-publish" nil t) | |
47 | ;; (autoload 'org-publish "org-publish-all" nil t) | |
48 | ;; (autoload 'org-publish "org-publish-current-file" nil t) | |
49 | ;; (autoload 'org-publish "org-publish-current-project" nil t) | |
50 | ||
51 | ;; NOTE: When org-publish.el is included with org.el, those forms are | |
52 | ;; already in the file org-install.el, and hence don't need to be put | |
53 | ;; in your emacs initialization file in this case. | |
54 | ||
55 | ;;; Usage: | |
56 | ;; | |
57 | ;; The program's main configuration variable is | |
58 | ;; `org-publish-project-alist'. See below for example configurations | |
59 | ;; with commentary. | |
60 | ||
61 | ;; The main interactive functions are: | |
62 | ;; | |
63 | ;; M-x org-publish | |
64 | ;; M-x org-publish-all | |
65 | ;; M-x org-publish-current-file | |
66 | ;; M-x org-publish-current-project | |
67 | ||
68 | ;;;; Simple example configuration: | |
69 | ||
70 | ;; (setq org-publish-project-alist | |
71 | ;; (list | |
72 | ;; '("org" . (:base-directory "~/org/" | |
33306645 CD |
73 | ;; :base-extension "org" |
74 | ;; :publishing-directory "~/public_html" | |
75 | ;; :with-section-numbers nil | |
76 | ;; :table-of-contents nil | |
77 | ;; :recursive t | |
78 | ;; :style "<link rel="stylesheet" href=\"../other/mystyle.css\" type=\"text/css\">"))) | |
9542795a CD |
79 | |
80 | ;;;; More complex example configuration: | |
81 | ||
82 | ;; Imagine your *.org files are kept in ~/org, your images in | |
83 | ;; ~/images, and stylesheets in ~/other. Now imagine you want to | |
84 | ;; publish the files through an ssh connection to a remote host, via | |
85 | ;; Tramp-mode. To maintain relative links from *.org files to /images | |
86 | ;; and /other, we should replicate the same directory structure in | |
87 | ;; your web server account's designated html root (in this case, | |
88 | ;; assumed to be ~/html) | |
89 | ||
90 | ;; Once you've done created the proper directories, you can adapt the | |
91 | ;; following example configuration to your specific paths, run M-x | |
92 | ;; org-publish-all, and it should publish the files to the correct | |
93 | ;; directories on the web server, transforming the *.org files into | |
94 | ;; HTML, and leaving other files alone. | |
95 | ||
96 | ;; (setq org-publish-project-alist | |
97 | ;; (list | |
98 | ;; '("orgfiles" :base-directory "~/org/" | |
33306645 CD |
99 | ;; :base-extension "org" |
100 | ;; :publishing-directory "/ssh:user@host:~/html/notebook/" | |
101 | ;; :publishing-function org-publish-org-to-html | |
102 | ;; :exclude "PrivatePage.org" ;; regexp | |
103 | ;; :headline-levels 3 | |
104 | ;; :with-section-numbers nil | |
105 | ;; :table-of-contents nil | |
106 | ;; :style "<link rel="stylesheet" href=\"../other/mystyle.css\" type=\"text/css\">" | |
107 | ;; :auto-preamble t | |
108 | ;; :auto-postamble nil) | |
109 | ;; ("images" :base-directory "~/images/" | |
110 | ;; :base-extension "jpg\\|gif\\|png" | |
111 | ;; :publishing-directory "/ssh:user@host:~/html/images/" | |
112 | ;; :publishing-function org-publish-attachment) | |
113 | ;; ("other" :base-directory "~/other/" | |
114 | ;; :base-extension "css" | |
115 | ;; :publishing-directory "/ssh:user@host:~/html/other/" | |
116 | ;; :publishing-function org-publish-attachment) | |
9542795a CD |
117 | ;; ("website" :components ("orgfiles" "images" "other")))) |
118 | ||
119 | ;; For more information, see the documentation for the variable | |
120 | ;; `org-publish-project-alist'. | |
121 | ||
122 | ;; Of course, you don't have to publish to remote directories from | |
123 | ;; within emacs. You can always just publish to local folders, and | |
124 | ;; then use the synchronization/upload tool of your choice. | |
125 | ||
9542795a CD |
126 | ;;; List of user-visible changes since version 1.27 |
127 | ||
128 | ;; 1.78: Allow list-valued :publishing-function | |
129 | ;; 1.77: Added :preparation-function, this allows you to use GNU Make etc. | |
130 | ;; 1.65: Remove old "composite projects". They're redundant. | |
131 | ;; 1.64: Allow meta-projects with :components | |
132 | ;; 1.57: Timestamps flag is now called "org-publish-use-timestamps-flag" | |
133 | ;; 1.52: Properly set default for :index-filename | |
134 | ;; 1.48: Composite projects allowed. | |
135 | ;; :include keyword allowed. | |
136 | ;; 1.43: Index no longer includes itself in the index. | |
137 | ;; 1.42: Fix "function definition is void" error | |
138 | ;; when :publishing-function not set in org-publish-current-file. | |
139 | ;; 1.41: Fixed bug where index isn't published on first try. | |
140 | ;; 1.37: Added interactive function "org-publish". Prompts for particular | |
141 | ;; project name to publish. | |
142 | ;; 1.34: Added force-publish option to all interactive functions. | |
143 | ;; 1.32: Fixed "index.org has changed on disk" error during index publishing. | |
144 | ;; 1.30: Fixed startup error caused by (require 'em-unix) | |
145 | ||
146 | ;;; Code: | |
147 | ||
9542795a CD |
148 | (eval-when-compile |
149 | (require 'cl)) | |
b349f79f CD |
150 | (require 'org) |
151 | (require 'org-exp) | |
9542795a | 152 | |
1e63a7fb GM |
153 | (eval-and-compile |
154 | (unless (fboundp 'declare-function) | |
155 | (defmacro declare-function (fn file &optional arglist fileonly)))) | |
156 | ||
9542795a | 157 | (defgroup org-publish nil |
b349f79f CD |
158 | "Options for publishing a set of Org-mode and related files." |
159 | :tag "Org Publishing" | |
160 | :group 'org) | |
9542795a | 161 | |
9542795a CD |
162 | (defcustom org-publish-project-alist nil |
163 | "Association list to control publishing behavior. | |
164 | Each element of the alist is a publishing 'project.' The CAR of | |
50243adf | 165 | each element is a string, uniquely identifying the project. The |
9542795a CD |
166 | CDR of each element is in one of the following forms: |
167 | ||
168 | (:property value :property value ... ) | |
169 | ||
170 | OR, | |
171 | ||
172 | (:components (\"project-1\" \"project-2\" ...)) | |
173 | ||
174 | When the CDR of an element of org-publish-project-alist is in | |
175 | this second form, the elements of the list after :components are | |
176 | taken to be components of the project, which group together files | |
50243adf JB |
177 | requiring different publishing options. When you publish such a |
178 | project with \\[org-publish], the components all publish. | |
9542795a CD |
179 | |
180 | When a property is given a value in org-publish-project-alist, its | |
181 | setting overrides the value of the corresponding user variable | |
50243adf | 182 | \(if any) during publishing. However, options set within a file |
9542795a CD |
183 | override everything. |
184 | ||
185 | Most properties are optional, but some should always be set: | |
186 | ||
699b9291 CD |
187 | :base-directory Directory containing publishing source files |
188 | :base-extension Extension (without the dot!) of source files. | |
189 | This can be a regular expression. | |
190 | :publishing-directory Directory (possibly remote) where output | |
191 | files will be published | |
9542795a CD |
192 | |
193 | The :exclude property may be used to prevent certain files from | |
50243adf | 194 | being published. Its value may be a string or regexp matching |
9542795a CD |
195 | file names you don't want to be published. |
196 | ||
50243adf | 197 | The :include property may be used to include extra files. Its |
b349f79f CD |
198 | value may be a list of filenames to include. The filenames are |
199 | considered relative to the base directory. | |
9542795a CD |
200 | |
201 | When both :include and :exclude properties are given values, the | |
202 | exclusion step happens first. | |
203 | ||
204 | One special property controls which back-end function to use for | |
50243adf | 205 | publishing files in the project. This can be used to extend the |
9542795a CD |
206 | set of file types publishable by org-publish, as well as the set |
207 | of output formats. | |
208 | ||
50243adf JB |
209 | :publishing-function Function to publish file. The default is |
210 | `org-publish-org-to-html', but other | |
211 | values are possible. May also be a | |
699b9291 CD |
212 | list of functions, in which case |
213 | each function in the list is invoked | |
214 | in turn. | |
9542795a CD |
215 | |
216 | Another property allows you to insert code that prepares a | |
50243adf | 217 | project for publishing. For example, you could call GNU Make on a |
699b9291 | 218 | certain makefile, to ensure published files are built up to date. |
9542795a | 219 | |
699b9291 CD |
220 | :preparation-function Function to be called before publishing |
221 | this project. | |
b349f79f CD |
222 | :completion-function Function to be called after publishing |
223 | this project. | |
9542795a CD |
224 | |
225 | Some properties control details of the Org publishing process, | |
226 | and are equivalent to the corresponding user variables listed in | |
50243adf | 227 | the right column. See the documentation for those variables to |
9542795a CD |
228 | learn more about their use and default values. |
229 | ||
50243adf JB |
230 | :language `org-export-default-language' |
231 | :headline-levels `org-export-headline-levels' | |
232 | :section-numbers `org-export-with-section-numbers' | |
233 | :table-of-contents `org-export-with-toc' | |
234 | :emphasize `org-export-with-emphasize' | |
235 | :sub-superscript `org-export-with-sub-superscripts' | |
236 | :TeX-macros `org-export-with-TeX-macros' | |
237 | :fixed-width `org-export-with-fixed-width' | |
238 | :tables `org-export-with-tables' | |
239 | :table-auto-headline `org-export-highlight-first-table-line' | |
240 | :style `org-export-html-style' | |
241 | :convert-org-links `org-export-html-link-org-files-as-html' | |
242 | :inline-images `org-export-html-inline-images' | |
243 | :expand-quoted-html `org-export-html-expand' | |
244 | :timestamp `org-export-html-with-timestamp' | |
245 | :publishing-directory `org-export-publishing-directory' | |
246 | :preamble `org-export-html-preamble' | |
247 | :postamble `org-export-html-postamble' | |
248 | :auto-preamble `org-export-html-auto-preamble' | |
249 | :auto-postamble `org-export-html-auto-postamble' | |
250 | :author `user-full-name' | |
251 | :email `user-mail-address' | |
9542795a CD |
252 | |
253 | The following properties may be used to control publishing of an | |
254 | index of files or summary page for a given project. | |
255 | ||
699b9291 | 256 | :auto-index Whether to publish an index during |
50243adf JB |
257 | `org-publish-current-project' or `org-publish-all'. |
258 | :index-filename Filename for output of index. Defaults | |
259 | to 'index.org' (which becomes 'index.html'). | |
260 | :index-title Title of index page. Defaults to name of file. | |
699b9291 | 261 | :index-function Plugin function to use for generation of index. |
50243adf | 262 | Defaults to `org-publish-org-index', which |
699b9291 | 263 | generates a plain list of links to all files |
2c3ad40d CD |
264 | in the project. |
265 | :index-style Can be `list' (index is just an itemized list | |
ff4be292 | 266 | of the titles of the files involved) or |
2c3ad40d CD |
267 | `tree' (the directory structure of the source |
268 | files is reflected in the index). Defaults to | |
269 | `tree'." | |
9542795a CD |
270 | :group 'org-publish |
271 | :type 'alist) | |
272 | ||
9542795a CD |
273 | (defcustom org-publish-use-timestamps-flag t |
274 | "When non-nil, use timestamp checking to publish only changed files. | |
50243adf | 275 | When nil, do no timestamp checking and always publish all files." |
9542795a CD |
276 | :group 'org-publish |
277 | :type 'boolean) | |
278 | ||
ff4be292 | 279 | (defcustom org-publish-timestamp-directory (convert-standard-filename |
71d35b24 | 280 | "~/.org-timestamps/") |
9542795a CD |
281 | "Name of directory in which to store publishing timestamps." |
282 | :group 'org-publish | |
699b9291 | 283 | :type 'directory) |
9542795a | 284 | |
699b9291 CD |
285 | (defcustom org-publish-before-export-hook nil |
286 | "Hook run before export on the Org file. | |
287 | If the functions in this hook modify the original Org buffer, the | |
288 | modified buffer will be used for export, but the buffer will be | |
289 | restored and saved back to its initial state after export." | |
290 | :group 'org-publish | |
291 | :type 'hook) | |
9542795a | 292 | |
699b9291 CD |
293 | (defcustom org-publish-after-export-hook nil |
294 | "Hook run after export on the exported buffer. | |
295 | If functions in this hook modify the buffer, it will be saved." | |
296 | :group 'org-publish | |
297 | :type 'hook) | |
9542795a | 298 | |
699b9291 CD |
299 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
300 | ;;; Timestamp-related functions | |
9542795a CD |
301 | |
302 | (defun org-publish-timestamp-filename (filename) | |
303 | "Return path to timestamp file for filename FILENAME." | |
2c3ad40d | 304 | (concat (file-name-as-directory org-publish-timestamp-directory) |
33306645 | 305 | "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename)))) |
9542795a | 306 | |
9542795a | 307 | (defun org-publish-needed-p (filename) |
699b9291 | 308 | "Return `t' if FILENAME should be published." |
2c3ad40d CD |
309 | (let ((rtn |
310 | (if org-publish-use-timestamps-flag | |
311 | (if (file-exists-p org-publish-timestamp-directory) | |
312 | ;; first handle possible wrong timestamp directory | |
313 | (if (not (file-directory-p org-publish-timestamp-directory)) | |
314 | (error "Org publish timestamp: %s is not a directory" | |
315 | org-publish-timestamp-directory) | |
316 | ;; there is a timestamp, check if FILENAME is newer | |
317 | (file-newer-than-file-p | |
318 | filename (org-publish-timestamp-filename filename))) | |
319 | (make-directory org-publish-timestamp-directory) | |
320 | t) | |
321 | ;; don't use timestamps, always return t | |
322 | t))) | |
323 | (if rtn | |
324 | (message "Publishing file %s" filename) | |
325 | (message "Skipping unmodified file %s" filename)) | |
326 | rtn)) | |
9542795a | 327 | |
9542795a | 328 | (defun org-publish-update-timestamp (filename) |
699b9291 CD |
329 | "Update publishing timestamp for file FILENAME. |
330 | If there is no timestamp, create one." | |
331 | (let ((timestamp-file (org-publish-timestamp-filename filename)) | |
332 | newly-created-timestamp) | |
333 | (if (not (file-exists-p timestamp-file)) | |
334 | ;; create timestamp file if needed | |
335 | (with-temp-buffer | |
336 | (make-directory (file-name-directory timestamp-file) t) | |
337 | (write-file timestamp-file) | |
338 | (setq newly-created-timestamp t))) | |
339 | ;; Emacs 21 doesn't have `set-file-times' | |
340 | (if (and (fboundp 'set-file-times) | |
341 | (not newly-created-timestamp)) | |
33306645 | 342 | (set-file-times timestamp-file) |
699b9291 CD |
343 | (call-process "touch" nil 0 nil timestamp-file)))) |
344 | ||
345 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
346 | ;;; Mapping files to project names | |
347 | ||
348 | (defvar org-publish-files-alist nil | |
50243adf | 349 | "Alist of files and their parent projects. |
699b9291 CD |
350 | Each element of this alist is of the form: |
351 | ||
352 | (file-name . project-name)") | |
353 | ||
20908596 CD |
354 | (defvar org-publish-initial-buffer nil |
355 | "The buffer `org-publish' has been called from.") | |
356 | (defvar org-publish-temp-files nil | |
357 | "Temporary list of files to be published.") | |
358 | ||
699b9291 CD |
359 | (defun org-publish-initialize-files-alist (&optional refresh) |
360 | "Set `org-publish-files-alist' if it is not set. | |
361 | Also set it if the optional argument REFRESH is non-nil." | |
362 | (interactive "P") | |
363 | (when (or refresh (not org-publish-files-alist)) | |
364 | (setq org-publish-files-alist | |
365 | (org-publish-get-files org-publish-project-alist)))) | |
366 | ||
93b62de8 CD |
367 | (defun org-publish-validate-link (link &optional directory) |
368 | "Check if LINK points to a file in the current project." | |
369 | (assoc (expand-file-name link directory) org-publish-files-alist)) | |
370 | ||
699b9291 CD |
371 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
372 | ;;; Compatibility aliases | |
373 | ||
374 | ;; Delete-dups is not in Emacs <22 | |
375 | (if (fboundp 'delete-dups) | |
376 | (defalias 'org-publish-delete-dups 'delete-dups) | |
377 | (defun org-publish-delete-dups (list) | |
378 | "Destructively remove `equal' duplicates from LIST. | |
379 | Store the result in LIST and return it. LIST must be a proper list. | |
380 | Of several `equal' occurrences of an element in LIST, the first | |
381 | one is kept. | |
382 | ||
383 | This is a compatibility function for Emacsen without `delete-dups'." | |
384 | ;; Code from `subr.el' in Emacs 22: | |
385 | (let ((tail list)) | |
386 | (while tail | |
387 | (setcdr tail (delete (car tail) (cdr tail))) | |
388 | (setq tail (cdr tail)))) | |
389 | list)) | |
390 | ||
1e63a7fb GM |
391 | (declare-function org-publish-delete-dups "org-publish" (list)) |
392 | ||
699b9291 CD |
393 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
394 | ;;; Getting project information out of org-publish-project-alist | |
395 | ||
396 | (defun org-publish-get-files (projects-alist &optional no-exclusion) | |
397 | "Return the list of all publishable files for PROJECTS-ALIST. | |
398 | If NO-EXCLUSION is non-nil, don't exclude files." | |
399 | (let (all-files) | |
400 | ;; add all projects | |
401 | (mapc | |
402 | (lambda(p) | |
403 | (let* ((exclude (plist-get (cdr p) :exclude)) | |
404 | (files (and p (org-publish-get-base-files p exclude)))) | |
405 | ;; add all files from this project | |
406 | (mapc (lambda(f) | |
407 | (add-to-list 'all-files | |
408 | (cons (expand-file-name f) (car p)))) | |
409 | files))) | |
410 | (org-publish-expand-projects projects-alist)) | |
411 | all-files)) | |
412 | ||
413 | (defun org-publish-expand-projects (projects-alist) | |
621f83e4 CD |
414 | "Expand projects in PROJECTS-ALIST. |
415 | This splices all the components into the list." | |
416 | (let ((rest projects-alist) rtn p components) | |
417 | (while (setq p (pop rest)) | |
418 | (if (setq components (plist-get (cdr p) :components)) | |
419 | (setq rest (append | |
420 | (mapcar (lambda (x) (assoc x org-publish-project-alist)) | |
421 | components) | |
422 | rest)) | |
423 | (push p rtn))) | |
424 | (nreverse (org-publish-delete-dups (delq nil rtn))))) | |
ff4be292 | 425 | |
20908596 CD |
426 | (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir) |
427 | "Set `org-publish-temp-files' with files from BASE-DIR directory. | |
428 | If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is | |
429 | non-nil, restrict this list to the files matching the regexp | |
430 | MATCH. If SKIP-FILE is non-nil, skip file matching the regexp | |
431 | SKIP-FILE. If SKIP-DIR is non-nil, don't check directories | |
33306645 | 432 | matching the regexp SKIP-DIR when recursing through BASE-DIR." |
20908596 | 433 | (mapc (lambda (f) |
93b62de8 | 434 | (let ((fd-p (file-directory-p f)) |
20908596 CD |
435 | (fnd (file-name-nondirectory f))) |
436 | (if (and fd-p recurse | |
437 | (not (string-match "^\\.+$" fnd)) | |
438 | (if skip-dir (not (string-match skip-dir fnd)) t)) | |
439 | (org-publish-get-base-files-1 f recurse match skip-file skip-dir) | |
440 | (unless (or fd-p ;; this is a directory | |
441 | (and skip-file (string-match skip-file fnd)) | |
93b62de8 | 442 | (not (file-exists-p (file-truename f))) |
20908596 CD |
443 | (not (string-match match fnd))) |
444 | (pushnew f org-publish-temp-files))))) | |
445 | (directory-files base-dir t (unless recurse match)))) | |
446 | ||
699b9291 CD |
447 | (defun org-publish-get-base-files (project &optional exclude-regexp) |
448 | "Return a list of all files in PROJECT. | |
9542795a CD |
449 | If EXCLUDE-REGEXP is set, this will be used to filter out |
450 | matching filenames." | |
699b9291 CD |
451 | (let* ((project-plist (cdr project)) |
452 | (base-dir (file-name-as-directory | |
453 | (plist-get project-plist :base-directory))) | |
33306645 CD |
454 | (include-list (plist-get project-plist :include)) |
455 | (recurse (plist-get project-plist :recursive)) | |
456 | (extension (or (plist-get project-plist :base-extension) "org")) | |
457 | (match (concat "^[^\\.].*\\.\\(" extension "\\)$"))) | |
20908596 CD |
458 | (setq org-publish-temp-files nil) |
459 | (org-publish-get-base-files-1 base-dir recurse match | |
460 | ;; FIXME distinguish exclude regexp | |
461 | ;; for skip-file and skip-dir? | |
462 | exclude-regexp exclude-regexp) | |
b349f79f | 463 | (mapc (lambda (f) |
ff4be292 | 464 | (pushnew |
b349f79f CD |
465 | (expand-file-name (concat base-dir f)) |
466 | org-publish-temp-files)) | |
467 | include-list) | |
20908596 | 468 | org-publish-temp-files)) |
9542795a | 469 | |
9542795a | 470 | (defun org-publish-get-project-from-filename (filename) |
699b9291 CD |
471 | "Return the project FILENAME belongs." |
472 | (let* ((project-name (cdr (assoc (expand-file-name filename) | |
33306645 | 473 | org-publish-files-alist)))) |
ff4be292 CD |
474 | (dolist (prj org-publish-project-alist) |
475 | (if (member project-name (plist-get (cdr prj) :components)) | |
33306645 | 476 | (setq project-name (car prj)))) |
699b9291 | 477 | (assoc project-name org-publish-project-alist))) |
daa89d0f | 478 | |
699b9291 CD |
479 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
480 | ;;; Pluggable publishing back-end functions | |
daa89d0f | 481 | |
699b9291 | 482 | (defun org-publish-org-to (format plist filename pub-dir) |
daa89d0f CD |
483 | "Publish an org file to FORMAT. |
484 | PLIST is the property list for the given project. | |
699b9291 CD |
485 | FILENAME is the filename of the org file to be published. |
486 | PUB-DIR is the publishing directory." | |
03f3cf35 | 487 | (require 'org) |
699b9291 CD |
488 | (unless (file-exists-p pub-dir) |
489 | (make-directory pub-dir t)) | |
71d35b24 CD |
490 | (let ((visiting (find-buffer-visiting filename))) |
491 | (save-excursion | |
492 | (switch-to-buffer (or visiting (find-file filename))) | |
493 | (let* ((plist (cons :buffer-will-be-killed (cons t plist))) | |
494 | (init-buf (current-buffer)) | |
495 | (init-point (point)) | |
496 | (init-buf-string (buffer-string)) | |
497 | export-buf-or-file) | |
498 | ;; run hooks before exporting | |
499 | (run-hooks 'org-publish-before-export-hook) | |
500 | ;; export the possibly modified buffer | |
501 | (setq export-buf-or-file | |
502 | (funcall (intern (concat "org-export-as-" format)) | |
503 | (plist-get plist :headline-levels) | |
504 | nil plist nil nil pub-dir)) | |
505 | (when (and (bufferp export-buf-or-file) | |
506 | (buffer-live-p export-buf-or-file)) | |
507 | (set-buffer export-buf-or-file) | |
508 | ;; run hooks after export and save export | |
509 | (and (run-hooks 'org-publish-after-export-hook) | |
510 | (if (buffer-modified-p) (save-buffer))) | |
511 | (kill-buffer export-buf-or-file)) | |
512 | ;; maybe restore buffer's content | |
513 | (set-buffer init-buf) | |
514 | (when (buffer-modified-p init-buf) | |
515 | (erase-buffer) | |
516 | (insert init-buf-string) | |
517 | (save-buffer) | |
518 | (goto-char init-point)) | |
519 | (unless visiting | |
520 | (kill-buffer init-buf)))))) | |
699b9291 CD |
521 | |
522 | (defun org-publish-org-to-latex (plist filename pub-dir) | |
523 | "Publish an org file to LaTeX. | |
524 | See `org-publish-org-to' to the list of arguments." | |
525 | (org-publish-org-to "latex" plist filename pub-dir)) | |
526 | ||
71d35b24 CD |
527 | (defun org-publish-org-to-pdf (plist filename pub-dir) |
528 | "Publish an org file to PDF (via LaTeX). | |
529 | See `org-publish-org-to' to the list of arguments." | |
530 | (org-publish-org-to "pdf" plist filename pub-dir)) | |
531 | ||
699b9291 CD |
532 | (defun org-publish-org-to-html (plist filename pub-dir) |
533 | "Publish an org file to HTML. | |
534 | See `org-publish-org-to' to the list of arguments." | |
535 | (org-publish-org-to "html" plist filename pub-dir)) | |
536 | ||
537 | (defun org-publish-attachment (plist filename pub-dir) | |
9542795a | 538 | "Publish a file with no transformation of any kind. |
699b9291 | 539 | See `org-publish-org-to' to the list of arguments." |
93b62de8 | 540 | ;; make sure eshell/cp code is loaded |
b349f79f CD |
541 | (unless (file-directory-p pub-dir) |
542 | (make-directory pub-dir t)) | |
d7dbaa7d | 543 | (copy-file filename pub-dir t)) |
699b9291 CD |
544 | |
545 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
546 | ;;; Publishing files, sets of files, and indices | |
547 | ||
548 | (defun org-publish-file (filename &optional project) | |
549 | "Publish file FILENAME from PROJECT." | |
550 | (when (org-publish-needed-p filename) | |
551 | (let* ((project | |
552 | (or project | |
553 | (or (org-publish-get-project-from-filename filename) | |
554 | (if (y-or-n-p | |
555 | (format "%s is not in a project. Re-read the list of projects files? " | |
556 | (abbreviate-file-name filename))) | |
557 | ;; If requested, re-initialize the list of projects files | |
558 | (progn (org-publish-initialize-files-alist t) | |
559 | (or (org-publish-get-project-from-filename filename) | |
560 | (error "File %s not part of any known project" | |
561 | (abbreviate-file-name filename)))) | |
562 | (error "Can't publish file outside of a project"))))) | |
563 | (project-plist (cdr project)) | |
0627c265 | 564 | (ftname (file-truename filename)) |
20908596 | 565 | (publishing-function |
699b9291 CD |
566 | (or (plist-get project-plist :publishing-function) |
567 | 'org-publish-org-to-html)) | |
568 | (base-dir (file-name-as-directory | |
569 | (file-truename (plist-get project-plist :base-directory)))) | |
570 | (pub-dir (file-name-as-directory | |
571 | (file-truename (plist-get project-plist :publishing-directory)))) | |
572 | tmp-pub-dir) | |
573 | (setq tmp-pub-dir | |
574 | (file-name-directory | |
575 | (concat pub-dir | |
0627c265 CD |
576 | (and (string-match (regexp-quote base-dir) ftname) |
577 | (substring ftname (match-end 0)))))) | |
9542795a CD |
578 | (if (listp publishing-function) |
579 | ;; allow chain of publishing functions | |
580 | (mapc (lambda (f) | |
699b9291 | 581 | (funcall f project-plist filename tmp-pub-dir)) |
9542795a | 582 | publishing-function) |
699b9291 CD |
583 | (funcall publishing-function project-plist filename tmp-pub-dir))) |
584 | (org-publish-update-timestamp filename))) | |
585 | ||
586 | (defun org-publish-projects (projects) | |
587 | "Publish all files belonging to the PROJECTS alist. | |
588 | If :auto-index is set, publish the index too." | |
589 | (mapc | |
590 | (lambda (project) | |
b349f79f CD |
591 | (let* |
592 | ((project-plist (cdr project)) | |
593 | (exclude-regexp (plist-get project-plist :exclude)) | |
594 | (index-p (plist-get project-plist :auto-index)) | |
595 | (index-filename (or (plist-get project-plist :index-filename) | |
596 | "index.org")) | |
597 | (index-function (or (plist-get project-plist :index-function) | |
598 | 'org-publish-org-index)) | |
599 | (preparation-function (plist-get project-plist :preparation-function)) | |
600 | (completion-function (plist-get project-plist :completion-function)) | |
601 | (files (org-publish-get-base-files project exclude-regexp)) file) | |
699b9291 CD |
602 | (when preparation-function (funcall preparation-function)) |
603 | (if index-p (funcall index-function project index-filename)) | |
604 | (while (setq file (pop files)) | |
b349f79f CD |
605 | (org-publish-file file project)) |
606 | (when completion-function (funcall completion-function)))) | |
699b9291 CD |
607 | (org-publish-expand-projects projects))) |
608 | ||
609 | (defun org-publish-org-index (project &optional index-filename) | |
610 | "Create an index of pages in set defined by PROJECT. | |
611 | Optionally set the filename of the index with INDEX-FILENAME. | |
612 | Default for INDEX-FILENAME is 'index.org'." | |
613 | (let* ((project-plist (cdr project)) | |
614 | (dir (file-name-as-directory | |
615 | (plist-get project-plist :base-directory))) | |
b349f79f CD |
616 | (localdir (file-name-directory dir)) |
617 | (indent-str (make-string 2 ?\ )) | |
699b9291 | 618 | (exclude-regexp (plist-get project-plist :exclude)) |
b349f79f | 619 | (files (nreverse (org-publish-get-base-files project exclude-regexp))) |
9542795a | 620 | (index-filename (concat dir (or index-filename "index.org"))) |
b349f79f CD |
621 | (index-title (or (plist-get project-plist :index-title) |
622 | (concat "Index for project " (car project)))) | |
2c3ad40d CD |
623 | (index-style (or (plist-get project-plist :index-style) |
624 | 'tree)) | |
ff4be292 | 625 | (visiting (find-buffer-visiting index-filename)) |
9542795a | 626 | (ifn (file-name-nondirectory index-filename)) |
ff4be292 CD |
627 | file index-buffer) |
628 | (with-current-buffer (setq index-buffer | |
629 | (or visiting (find-file index-filename))) | |
630 | (erase-buffer) | |
ce4fdcb9 | 631 | (insert (concat "#+TITLE: " index-title "\n\n")) |
699b9291 | 632 | (while (setq file (pop files)) |
b349f79f CD |
633 | (let ((fn (file-name-nondirectory file)) |
634 | (link (file-relative-name file dir)) | |
635 | (oldlocal localdir)) | |
699b9291 | 636 | ;; index shouldn't index itself |
ff4be292 CD |
637 | (unless (equal (file-truename index-filename) |
638 | (file-truename file)) | |
2c3ad40d CD |
639 | (if (eq index-style 'list) |
640 | (message "Generating list-style index for %s" index-title) | |
641 | (message "Generating tree-style index for %s" index-title) | |
642 | (setq localdir (concat (file-name-as-directory dir) | |
643 | (file-name-directory link))) | |
644 | (unless (string= localdir oldlocal) | |
645 | (if (string= localdir dir) | |
646 | (setq indent-str (make-string 2 ?\ )) | |
647 | (let ((subdirs | |
648 | (split-string | |
649 | (directory-file-name | |
650 | (file-name-directory | |
651 | (file-relative-name localdir dir))) "/")) | |
93b62de8 CD |
652 | (subdir "") |
653 | (old-subdirs (split-string | |
654 | (file-relative-name oldlocal dir) "/"))) | |
2c3ad40d | 655 | (setq indent-str (make-string 2 ?\ )) |
93b62de8 CD |
656 | (while (string= (car old-subdirs) (car subdirs)) |
657 | (setq indent-str (concat indent-str (make-string 2 ?\ ))) | |
658 | (pop old-subdirs) | |
659 | (pop subdirs)) | |
2c3ad40d CD |
660 | (dolist (d subdirs) |
661 | (setq subdir (concat subdir d "/")) | |
93b62de8 CD |
662 | (insert (concat indent-str " + " d "\n")) |
663 | (setq indent-str (make-string | |
2c3ad40d CD |
664 | (+ (length indent-str) 2) ?\ ))))))) |
665 | ;; This is common to 'flat and 'tree | |
b349f79f | 666 | (insert (concat indent-str " + [[file:" link "][" |
2c3ad40d | 667 | (org-publish-find-title file) |
ff4be292 CD |
668 | "]]\n"))))) |
669 | (save-buffer)) | |
670 | (or visiting (kill-buffer index-buffer)))) | |
9542795a | 671 | |
b349f79f CD |
672 | (defun org-publish-find-title (file) |
673 | "Find the title of file in project." | |
93b62de8 CD |
674 | (let* ((visiting (find-buffer-visiting file)) |
675 | (buffer (or visiting (find-file-noselect file))) | |
676 | title) | |
677 | (save-excursion | |
678 | (set-buffer buffer) | |
679 | (let* ((opt-plist (org-combine-plists (org-default-export-plist) | |
680 | (org-infile-export-plist)))) | |
681 | (setq title | |
682 | (or (plist-get opt-plist :title) | |
683 | (and (not | |
684 | (plist-get opt-plist :skip-before-1st-heading)) | |
685 | (org-export-grab-title-from-buffer)) | |
686 | (file-name-nondirectory (file-name-sans-extension file)))))) | |
687 | (unless visiting | |
688 | (kill-buffer buffer)) | |
689 | title)) | |
b349f79f | 690 | |
699b9291 CD |
691 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
692 | ;;; Interactive publishing functions | |
9542795a | 693 | |
71d35b24 | 694 | ;;;###autoload |
20908596 | 695 | (defalias 'org-publish-project 'org-publish) |
9542795a CD |
696 | |
697 | ;;;###autoload | |
699b9291 CD |
698 | (defun org-publish (project &optional force) |
699 | "Publish PROJECT." | |
700 | (interactive "P") | |
20908596 | 701 | (setq org-publish-initial-buffer (current-buffer)) |
9542795a | 702 | (save-window-excursion |
699b9291 CD |
703 | (let* ((force current-prefix-arg) |
704 | (org-publish-use-timestamps-flag | |
705 | (if force nil org-publish-use-timestamps-flag))) | |
706 | (org-publish-projects | |
707 | (list (or project | |
ce4fdcb9 | 708 | (assoc (org-ido-completing-read |
699b9291 CD |
709 | "Publish project: " |
710 | org-publish-project-alist nil t) | |
711 | org-publish-project-alist))))))) | |
9542795a CD |
712 | |
713 | ;;;###autoload | |
699b9291 CD |
714 | (defun org-publish-all (&optional force) |
715 | "Publish all projects. | |
716 | With prefix argument, force publish all files." | |
9542795a | 717 | (interactive "P") |
699b9291 | 718 | (org-publish-initialize-files-alist) |
9542795a | 719 | (save-window-excursion |
699b9291 CD |
720 | (let ((org-publish-use-timestamps-flag |
721 | (if force nil org-publish-use-timestamps-flag))) | |
722 | (org-publish-projects org-publish-project-alist)))) | |
9542795a CD |
723 | |
724 | ;;;###autoload | |
725 | (defun org-publish-current-file (&optional force) | |
726 | "Publish the current file. | |
727 | With prefix argument, force publish the file." | |
728 | (interactive "P") | |
699b9291 | 729 | (org-publish-initialize-files-alist) |
9542795a CD |
730 | (save-window-excursion |
731 | (let ((org-publish-use-timestamps-flag | |
699b9291 | 732 | (if force nil org-publish-use-timestamps-flag))) |
9542795a CD |
733 | (org-publish-file (buffer-file-name))))) |
734 | ||
9542795a | 735 | ;;;###autoload |
699b9291 CD |
736 | (defun org-publish-current-project (&optional force) |
737 | "Publish the project associated with the current file. | |
738 | With a prefix argument, force publishing of all files in | |
739 | the project." | |
9542795a | 740 | (interactive "P") |
699b9291 | 741 | (org-publish-initialize-files-alist) |
9542795a | 742 | (save-window-excursion |
699b9291 CD |
743 | (let ((project (org-publish-get-project-from-filename (buffer-file-name))) |
744 | (org-publish-use-timestamps-flag | |
745 | (if force nil org-publish-use-timestamps-flag))) | |
746 | (if (not project) | |
747 | (error "File %s is not part of any known project" (buffer-file-name))) | |
748 | (org-publish project)))) | |
d5098885 | 749 | |
9542795a | 750 | (provide 'org-publish) |
4fbd4750 | 751 | |
699b9291 | 752 | |
4fbd4750 | 753 | ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb |
b349f79f | 754 | |
9542795a | 755 | ;;; org-publish.el ends here |