Commit | Line | Data |
---|---|---|
9542795a | 1 | ;;; org-publish.el --- publish related org-mode files as a website |
dcb8ac09 | 2 | ;; Copyright (C) 2006, 2007, 2008 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 | |
20908596 | 7 | ;; Version: 6.02b |
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 | ||
26 | ;; Requires at least version 4.27 of org.el | |
9542795a | 27 | |
699b9291 CD |
28 | ;; This program allow configurable publishing of related sets of |
29 | ;; Org-mode files as a complete website. | |
9542795a CD |
30 | ;; |
31 | ;; org-publish.el can do the following: | |
32 | ;; | |
699b9291 CD |
33 | ;; + Publish all one's org-files to HTML or LaTeX |
34 | ;; + Upload HTML, images, attachments and other files to a web server | |
9542795a CD |
35 | ;; + Exclude selected private pages from publishing |
36 | ;; + Publish a clickable index of pages | |
699b9291 | 37 | ;; + Manage local timestamps for publishing only changed files |
9542795a CD |
38 | ;; + Accept plugin functions to extend range of publishable content |
39 | ;; | |
40 | ;; Special thanks to the org-mode maintainer Carsten Dominik for his | |
41 | ;; ideas, enthusiasm, and cooperation. | |
42 | ||
43 | ;;; Installation: | |
44 | ||
45 | ;; Put org-publish.el in your load path, byte-compile it, and then add | |
46 | ;; the following lines to your emacs initialization file: | |
47 | ||
48 | ;; (autoload 'org-publish "org-publish" nil t) | |
49 | ;; (autoload 'org-publish "org-publish-all" nil t) | |
50 | ;; (autoload 'org-publish "org-publish-current-file" nil t) | |
51 | ;; (autoload 'org-publish "org-publish-current-project" nil t) | |
52 | ||
53 | ;; NOTE: When org-publish.el is included with org.el, those forms are | |
54 | ;; already in the file org-install.el, and hence don't need to be put | |
55 | ;; in your emacs initialization file in this case. | |
56 | ||
57 | ;;; Usage: | |
58 | ;; | |
59 | ;; The program's main configuration variable is | |
60 | ;; `org-publish-project-alist'. See below for example configurations | |
61 | ;; with commentary. | |
62 | ||
63 | ;; The main interactive functions are: | |
64 | ;; | |
65 | ;; M-x org-publish | |
66 | ;; M-x org-publish-all | |
67 | ;; M-x org-publish-current-file | |
68 | ;; M-x org-publish-current-project | |
69 | ||
70 | ;;;; Simple example configuration: | |
71 | ||
72 | ;; (setq org-publish-project-alist | |
73 | ;; (list | |
74 | ;; '("org" . (:base-directory "~/org/" | |
75 | ;; :base-extension "org" | |
76 | ;; :publishing-directory "~/public_html" | |
77 | ;; :with-section-numbers nil | |
78 | ;; :table-of-contents nil | |
699b9291 | 79 | ;; :recursive t |
9542795a CD |
80 | ;; :style "<link rel=stylesheet href=\"../other/mystyle.css\" type=\"text/css\">"))) |
81 | ||
82 | ;;;; More complex example configuration: | |
83 | ||
84 | ;; Imagine your *.org files are kept in ~/org, your images in | |
85 | ;; ~/images, and stylesheets in ~/other. Now imagine you want to | |
86 | ;; publish the files through an ssh connection to a remote host, via | |
87 | ;; Tramp-mode. To maintain relative links from *.org files to /images | |
88 | ;; and /other, we should replicate the same directory structure in | |
89 | ;; your web server account's designated html root (in this case, | |
90 | ;; assumed to be ~/html) | |
91 | ||
92 | ;; Once you've done created the proper directories, you can adapt the | |
93 | ;; following example configuration to your specific paths, run M-x | |
94 | ;; org-publish-all, and it should publish the files to the correct | |
95 | ;; directories on the web server, transforming the *.org files into | |
96 | ;; HTML, and leaving other files alone. | |
97 | ||
98 | ;; (setq org-publish-project-alist | |
99 | ;; (list | |
100 | ;; '("orgfiles" :base-directory "~/org/" | |
101 | ;; :base-extension "org" | |
102 | ;; :publishing-directory "/ssh:user@host:~/html/notebook/" | |
103 | ;; :publishing-function org-publish-org-to-html | |
104 | ;; :exclude "PrivatePage.org" ;; regexp | |
105 | ;; :headline-levels 3 | |
106 | ;; :with-section-numbers nil | |
107 | ;; :table-of-contents nil | |
108 | ;; :style "<link rel=stylesheet href=\"../other/mystyle.css\" type=\"text/css\">" | |
109 | ;; :auto-preamble t | |
110 | ;; :auto-postamble nil) | |
9542795a CD |
111 | ;; ("images" :base-directory "~/images/" |
112 | ;; :base-extension "jpg\\|gif\\|png" | |
113 | ;; :publishing-directory "/ssh:user@host:~/html/images/" | |
114 | ;; :publishing-function org-publish-attachment) | |
9542795a CD |
115 | ;; ("other" :base-directory "~/other/" |
116 | ;; :base-extension "css" | |
117 | ;; :publishing-directory "/ssh:user@host:~/html/other/" | |
118 | ;; :publishing-function org-publish-attachment) | |
119 | ;; ("website" :components ("orgfiles" "images" "other")))) | |
120 | ||
121 | ;; For more information, see the documentation for the variable | |
122 | ;; `org-publish-project-alist'. | |
123 | ||
124 | ;; Of course, you don't have to publish to remote directories from | |
125 | ;; within emacs. You can always just publish to local folders, and | |
126 | ;; then use the synchronization/upload tool of your choice. | |
127 | ||
9542795a CD |
128 | ;;; List of user-visible changes since version 1.27 |
129 | ||
130 | ;; 1.78: Allow list-valued :publishing-function | |
131 | ;; 1.77: Added :preparation-function, this allows you to use GNU Make etc. | |
132 | ;; 1.65: Remove old "composite projects". They're redundant. | |
133 | ;; 1.64: Allow meta-projects with :components | |
134 | ;; 1.57: Timestamps flag is now called "org-publish-use-timestamps-flag" | |
135 | ;; 1.52: Properly set default for :index-filename | |
136 | ;; 1.48: Composite projects allowed. | |
137 | ;; :include keyword allowed. | |
138 | ;; 1.43: Index no longer includes itself in the index. | |
139 | ;; 1.42: Fix "function definition is void" error | |
140 | ;; when :publishing-function not set in org-publish-current-file. | |
141 | ;; 1.41: Fixed bug where index isn't published on first try. | |
142 | ;; 1.37: Added interactive function "org-publish". Prompts for particular | |
143 | ;; project name to publish. | |
144 | ;; 1.34: Added force-publish option to all interactive functions. | |
145 | ;; 1.32: Fixed "index.org has changed on disk" error during index publishing. | |
146 | ;; 1.30: Fixed startup error caused by (require 'em-unix) | |
147 | ||
148 | ;;; Code: | |
149 | ||
9542795a CD |
150 | (eval-when-compile |
151 | (require 'cl)) | |
152 | ||
1e63a7fb GM |
153 | (eval-and-compile |
154 | (unless (fboundp 'declare-function) | |
155 | (defmacro declare-function (fn file &optional arglist fileonly)))) | |
156 | ||
9542795a CD |
157 | (defgroup org-publish nil |
158 | "Options for publishing a set of Org-mode and related files." | |
159 | :tag "Org Publishing" | |
160 | :group 'org) | |
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 | |
165 | each element is a string, uniquely identifying the project. The | |
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 | |
177 | requiring different publishing options. When you publish such a | |
178 | project with M-x org-publish, the components all publish. | |
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 | |
182 | (if any) during publishing. However, options set within a file | |
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 | |
194 | being published. Its value may be a string or regexp matching | |
195 | file names you don't want to be published. | |
196 | ||
197 | The :include property may be used to include extra files. Its | |
198 | value may be a list of filenames to include. The filenames are | |
199 | considered relative to the publishing directory. | |
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 | |
205 | publishing files in the project. This can be used to extend the | |
206 | set of file types publishable by org-publish, as well as the set | |
207 | of output formats. | |
208 | ||
699b9291 CD |
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 | |
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 | |
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. | |
9542795a CD |
222 | |
223 | Some properties control details of the Org publishing process, | |
224 | and are equivalent to the corresponding user variables listed in | |
225 | the right column. See the documentation for those variables to | |
226 | learn more about their use and default values. | |
227 | ||
699b9291 CD |
228 | :language org-export-default-language |
229 | :headline-levels org-export-headline-levels | |
230 | :section-numbers org-export-with-section-numbers | |
231 | :table-of-contents org-export-with-toc | |
232 | :emphasize org-export-with-emphasize | |
233 | :sub-superscript org-export-with-sub-superscripts | |
234 | :TeX-macros org-export-with-TeX-macros | |
235 | :fixed-width org-export-with-fixed-width | |
236 | :tables org-export-with-tables | |
237 | :table-auto-headline org-export-highlight-first-table-line | |
238 | :style org-export-html-style | |
239 | :convert-org-links org-export-html-link-org-files-as-html | |
240 | :inline-images org-export-html-inline-images | |
241 | :expand-quoted-html org-export-html-expand | |
242 | :timestamp org-export-html-with-timestamp | |
243 | :publishing-directory org-export-publishing-directory | |
244 | :preamble org-export-html-preamble | |
245 | :postamble org-export-html-postamble | |
246 | :auto-preamble org-export-html-auto-preamble | |
247 | :auto-postamble org-export-html-auto-postamble | |
248 | :author user-full-name | |
249 | :email user-mail-address | |
9542795a CD |
250 | |
251 | The following properties may be used to control publishing of an | |
252 | index of files or summary page for a given project. | |
253 | ||
699b9291 CD |
254 | :auto-index Whether to publish an index during |
255 | org-publish-current-project or org-publish-all. | |
256 | :index-filename Filename for output of index. Defaults | |
257 | to 'index.org' (which becomes 'index.html') | |
258 | :index-title Title of index page. Defaults to name of file. | |
259 | :index-function Plugin function to use for generation of index. | |
260 | Defaults to 'org-publish-org-index', which | |
261 | generates a plain list of links to all files | |
262 | in the project." | |
9542795a CD |
263 | :group 'org-publish |
264 | :type 'alist) | |
265 | ||
9542795a CD |
266 | (defcustom org-publish-use-timestamps-flag t |
267 | "When non-nil, use timestamp checking to publish only changed files. | |
268 | When nil, do no timestamp checking and always publish all | |
269 | files." | |
270 | :group 'org-publish | |
271 | :type 'boolean) | |
272 | ||
9542795a CD |
273 | (defcustom org-publish-timestamp-directory "~/.org-timestamps/" |
274 | "Name of directory in which to store publishing timestamps." | |
275 | :group 'org-publish | |
699b9291 | 276 | :type 'directory) |
9542795a | 277 | |
699b9291 CD |
278 | (defcustom org-publish-before-export-hook nil |
279 | "Hook run before export on the Org file. | |
280 | If the functions in this hook modify the original Org buffer, the | |
281 | modified buffer will be used for export, but the buffer will be | |
282 | restored and saved back to its initial state after export." | |
283 | :group 'org-publish | |
284 | :type 'hook) | |
9542795a | 285 | |
699b9291 CD |
286 | (defcustom org-publish-after-export-hook nil |
287 | "Hook run after export on the exported buffer. | |
288 | If functions in this hook modify the buffer, it will be saved." | |
289 | :group 'org-publish | |
290 | :type 'hook) | |
9542795a | 291 | |
699b9291 CD |
292 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
293 | ;;; Timestamp-related functions | |
9542795a CD |
294 | |
295 | (defun org-publish-timestamp-filename (filename) | |
296 | "Return path to timestamp file for filename FILENAME." | |
699b9291 CD |
297 | (while (string-match |
298 | (if (eq system-type 'windows-nt) "~\\|/\\|:" "~\\|/") filename) | |
9542795a CD |
299 | (setq filename (replace-match "_" nil t filename))) |
300 | (concat org-publish-timestamp-directory filename ".timestamp")) | |
301 | ||
9542795a | 302 | (defun org-publish-needed-p (filename) |
699b9291 | 303 | "Return `t' if FILENAME should be published." |
9542795a | 304 | (if org-publish-use-timestamps-flag |
699b9291 CD |
305 | (if (file-exists-p org-publish-timestamp-directory) |
306 | ;; first handle possible wrong timestamp directory | |
9542795a | 307 | (if (not (file-directory-p org-publish-timestamp-directory)) |
699b9291 CD |
308 | (error "Org publish timestamp: %s is not a directory" |
309 | org-publish-timestamp-directory) | |
310 | ;; there is a timestamp, check if FILENAME is newer | |
311 | (file-newer-than-file-p | |
312 | filename (org-publish-timestamp-filename filename)))) | |
313 | ;; don't use timestamps, always return t | |
9542795a CD |
314 | t)) |
315 | ||
9542795a | 316 | (defun org-publish-update-timestamp (filename) |
699b9291 CD |
317 | "Update publishing timestamp for file FILENAME. |
318 | If there is no timestamp, create one." | |
319 | (let ((timestamp-file (org-publish-timestamp-filename filename)) | |
320 | newly-created-timestamp) | |
321 | (if (not (file-exists-p timestamp-file)) | |
322 | ;; create timestamp file if needed | |
323 | (with-temp-buffer | |
324 | (make-directory (file-name-directory timestamp-file) t) | |
325 | (write-file timestamp-file) | |
326 | (setq newly-created-timestamp t))) | |
327 | ;; Emacs 21 doesn't have `set-file-times' | |
328 | (if (and (fboundp 'set-file-times) | |
329 | (not newly-created-timestamp)) | |
330 | (set-file-times timestamp-file) | |
331 | (call-process "touch" nil 0 nil timestamp-file)))) | |
332 | ||
333 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
334 | ;;; Mapping files to project names | |
335 | ||
336 | (defvar org-publish-files-alist nil | |
337 | "Alist of files and their parent project. | |
338 | Each element of this alist is of the form: | |
339 | ||
340 | (file-name . project-name)") | |
341 | ||
20908596 CD |
342 | (defvar org-publish-initial-buffer nil |
343 | "The buffer `org-publish' has been called from.") | |
344 | (defvar org-publish-temp-files nil | |
345 | "Temporary list of files to be published.") | |
346 | ||
699b9291 CD |
347 | (defun org-publish-initialize-files-alist (&optional refresh) |
348 | "Set `org-publish-files-alist' if it is not set. | |
349 | Also set it if the optional argument REFRESH is non-nil." | |
350 | (interactive "P") | |
351 | (when (or refresh (not org-publish-files-alist)) | |
352 | (setq org-publish-files-alist | |
353 | (org-publish-get-files org-publish-project-alist)))) | |
354 | ||
355 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
356 | ;;; Compatibility aliases | |
357 | ||
358 | ;; Delete-dups is not in Emacs <22 | |
359 | (if (fboundp 'delete-dups) | |
360 | (defalias 'org-publish-delete-dups 'delete-dups) | |
361 | (defun org-publish-delete-dups (list) | |
362 | "Destructively remove `equal' duplicates from LIST. | |
363 | Store the result in LIST and return it. LIST must be a proper list. | |
364 | Of several `equal' occurrences of an element in LIST, the first | |
365 | one is kept. | |
366 | ||
367 | This is a compatibility function for Emacsen without `delete-dups'." | |
368 | ;; Code from `subr.el' in Emacs 22: | |
369 | (let ((tail list)) | |
370 | (while tail | |
371 | (setcdr tail (delete (car tail) (cdr tail))) | |
372 | (setq tail (cdr tail)))) | |
373 | list)) | |
374 | ||
1e63a7fb GM |
375 | (declare-function org-publish-delete-dups "org-publish" (list)) |
376 | ||
699b9291 CD |
377 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
378 | ;;; Getting project information out of org-publish-project-alist | |
379 | ||
380 | (defun org-publish-get-files (projects-alist &optional no-exclusion) | |
381 | "Return the list of all publishable files for PROJECTS-ALIST. | |
382 | If NO-EXCLUSION is non-nil, don't exclude files." | |
383 | (let (all-files) | |
384 | ;; add all projects | |
385 | (mapc | |
386 | (lambda(p) | |
387 | (let* ((exclude (plist-get (cdr p) :exclude)) | |
388 | (files (and p (org-publish-get-base-files p exclude)))) | |
389 | ;; add all files from this project | |
390 | (mapc (lambda(f) | |
391 | (add-to-list 'all-files | |
392 | (cons (expand-file-name f) (car p)))) | |
393 | files))) | |
394 | (org-publish-expand-projects projects-alist)) | |
395 | all-files)) | |
396 | ||
397 | (defun org-publish-expand-projects (projects-alist) | |
398 | "Expand projects contained in PROJECTS-ALIST." | |
399 | (let (without-component with-component) | |
400 | (mapc (lambda(p) | |
401 | (add-to-list | |
402 | (if (plist-get (cdr p) :components) | |
403 | 'with-component 'without-component) p)) | |
404 | projects-alist) | |
405 | (org-publish-delete-dups | |
406 | (append without-component | |
407 | (car (mapcar (lambda(p) (org-publish-expand-components p)) | |
408 | with-component)))))) | |
409 | ||
410 | (defun org-publish-expand-components (project) | |
411 | "Expand PROJECT into an alist of its components." | |
412 | (let* ((components (plist-get (cdr project) :components))) | |
413 | (org-publish-delete-dups | |
414 | (delq nil (mapcar (lambda(c) (assoc c org-publish-project-alist)) | |
415 | components))))) | |
416 | ||
20908596 CD |
417 | (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir) |
418 | "Set `org-publish-temp-files' with files from BASE-DIR directory. | |
419 | If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is | |
420 | non-nil, restrict this list to the files matching the regexp | |
421 | MATCH. If SKIP-FILE is non-nil, skip file matching the regexp | |
422 | SKIP-FILE. If SKIP-DIR is non-nil, don't check directories | |
423 | matching the regexp SKIP-DIR when recursiing through BASE-DIR." | |
424 | (mapc (lambda (f) | |
425 | (let ((fd-p (car (file-attributes f))) | |
426 | (fnd (file-name-nondirectory f))) | |
427 | (if (and fd-p recurse | |
428 | (not (string-match "^\\.+$" fnd)) | |
429 | (if skip-dir (not (string-match skip-dir fnd)) t)) | |
430 | (org-publish-get-base-files-1 f recurse match skip-file skip-dir) | |
431 | (unless (or fd-p ;; this is a directory | |
432 | (and skip-file (string-match skip-file fnd)) | |
433 | (not (string-match match fnd))) | |
434 | (pushnew f org-publish-temp-files))))) | |
435 | (directory-files base-dir t (unless recurse match)))) | |
436 | ||
699b9291 CD |
437 | (defun org-publish-get-base-files (project &optional exclude-regexp) |
438 | "Return a list of all files in PROJECT. | |
9542795a CD |
439 | If EXCLUDE-REGEXP is set, this will be used to filter out |
440 | matching filenames." | |
699b9291 CD |
441 | (let* ((project-plist (cdr project)) |
442 | (base-dir (file-name-as-directory | |
443 | (plist-get project-plist :base-directory))) | |
444 | (include-list (plist-get project-plist :include)) | |
20908596 | 445 | (recurse (plist-get project-plist :recursive)) |
699b9291 | 446 | (extension (or (plist-get project-plist :base-extension) "org")) |
20908596 CD |
447 | (match (concat "^[^\\.].*\\.\\(" extension "\\)$"))) |
448 | (setq org-publish-temp-files nil) | |
449 | (org-publish-get-base-files-1 base-dir recurse match | |
450 | ;; FIXME distinguish exclude regexp | |
451 | ;; for skip-file and skip-dir? | |
452 | exclude-regexp exclude-regexp) | |
453 | org-publish-temp-files)) | |
9542795a | 454 | |
9542795a | 455 | (defun org-publish-get-project-from-filename (filename) |
699b9291 CD |
456 | "Return the project FILENAME belongs." |
457 | (let* ((project-name (cdr (assoc (expand-file-name filename) | |
458 | org-publish-files-alist)))) | |
459 | (assoc project-name org-publish-project-alist))) | |
daa89d0f | 460 | |
699b9291 CD |
461 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
462 | ;;; Pluggable publishing back-end functions | |
daa89d0f | 463 | |
699b9291 | 464 | (defun org-publish-org-to (format plist filename pub-dir) |
daa89d0f CD |
465 | "Publish an org file to FORMAT. |
466 | PLIST is the property list for the given project. | |
699b9291 CD |
467 | FILENAME is the filename of the org file to be published. |
468 | PUB-DIR is the publishing directory." | |
03f3cf35 | 469 | (require 'org) |
699b9291 CD |
470 | (unless (file-exists-p pub-dir) |
471 | (make-directory pub-dir t)) | |
472 | (find-file filename) | |
473 | (let ((init-buf (current-buffer)) | |
474 | (init-point (point)) | |
475 | (init-buf-string (buffer-string)) export-buf) | |
476 | ;; run hooks before exporting | |
477 | (run-hooks 'org-publish-before-export-hook) | |
478 | ;; export the possibly modified buffer | |
479 | (setq export-buf | |
480 | (funcall (intern (concat "org-export-as-" format)) | |
481 | (plist-get plist :headline-levels) | |
482 | nil plist nil nil pub-dir)) | |
483 | (set-buffer export-buf) | |
484 | ;; run hooks after export and save export | |
485 | (and (run-hooks 'org-publish-after-export-hook) | |
486 | (if (buffer-modified-p) (save-buffer))) | |
20908596 | 487 | (kill-buffer export-buf) |
699b9291 CD |
488 | ;; maybe restore buffer's content |
489 | (set-buffer init-buf) | |
490 | (when (buffer-modified-p init-buf) | |
491 | (erase-buffer) | |
492 | (insert init-buf-string) | |
493 | (save-buffer) | |
20908596 CD |
494 | (goto-char init-point)) |
495 | (unless (eq init-buf org-publish-initial-buffer) | |
496 | (kill-buffer init-buf)))) | |
699b9291 CD |
497 | |
498 | (defun org-publish-org-to-latex (plist filename pub-dir) | |
499 | "Publish an org file to LaTeX. | |
500 | See `org-publish-org-to' to the list of arguments." | |
501 | (org-publish-org-to "latex" plist filename pub-dir)) | |
502 | ||
503 | (defun org-publish-org-to-html (plist filename pub-dir) | |
504 | "Publish an org file to HTML. | |
505 | See `org-publish-org-to' to the list of arguments." | |
506 | (org-publish-org-to "html" plist filename pub-dir)) | |
507 | ||
508 | (defun org-publish-attachment (plist filename pub-dir) | |
9542795a | 509 | "Publish a file with no transformation of any kind. |
699b9291 | 510 | See `org-publish-org-to' to the list of arguments." |
9542795a | 511 | ;; make sure eshell/cp code is loaded |
699b9291 | 512 | (eval-and-compile |
5656d1bf DN |
513 | (require 'eshell) |
514 | (require 'esh-maint) | |
515 | (require 'em-unix)) | |
699b9291 CD |
516 | (eshell/cp filename pub-dir)) |
517 | ||
518 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
519 | ;;; Publishing files, sets of files, and indices | |
520 | ||
521 | (defun org-publish-file (filename &optional project) | |
522 | "Publish file FILENAME from PROJECT." | |
523 | (when (org-publish-needed-p filename) | |
524 | (let* ((project | |
525 | (or project | |
526 | (or (org-publish-get-project-from-filename filename) | |
527 | (if (y-or-n-p | |
528 | (format "%s is not in a project. Re-read the list of projects files? " | |
529 | (abbreviate-file-name filename))) | |
530 | ;; If requested, re-initialize the list of projects files | |
531 | (progn (org-publish-initialize-files-alist t) | |
532 | (or (org-publish-get-project-from-filename filename) | |
533 | (error "File %s not part of any known project" | |
534 | (abbreviate-file-name filename)))) | |
535 | (error "Can't publish file outside of a project"))))) | |
536 | (project-plist (cdr project)) | |
20908596 | 537 | (publishing-function |
699b9291 CD |
538 | (or (plist-get project-plist :publishing-function) |
539 | 'org-publish-org-to-html)) | |
540 | (base-dir (file-name-as-directory | |
541 | (file-truename (plist-get project-plist :base-directory)))) | |
542 | (pub-dir (file-name-as-directory | |
543 | (file-truename (plist-get project-plist :publishing-directory)))) | |
544 | tmp-pub-dir) | |
545 | (setq tmp-pub-dir | |
546 | (file-name-directory | |
547 | (concat pub-dir | |
548 | (and (string-match (regexp-quote base-dir) filename) | |
549 | (substring filename (match-end 0)))))) | |
9542795a CD |
550 | (if (listp publishing-function) |
551 | ;; allow chain of publishing functions | |
552 | (mapc (lambda (f) | |
699b9291 | 553 | (funcall f project-plist filename tmp-pub-dir)) |
9542795a | 554 | publishing-function) |
699b9291 CD |
555 | (funcall publishing-function project-plist filename tmp-pub-dir))) |
556 | (org-publish-update-timestamp filename))) | |
557 | ||
558 | (defun org-publish-projects (projects) | |
559 | "Publish all files belonging to the PROJECTS alist. | |
560 | If :auto-index is set, publish the index too." | |
561 | (mapc | |
562 | (lambda (project) | |
563 | (let* ((project-plist (cdr project)) | |
564 | (exclude-regexp (plist-get project-plist :exclude)) | |
565 | (index-p (plist-get project-plist :auto-index)) | |
566 | (index-filename (or (plist-get project-plist :index-filename) | |
567 | "index.org")) | |
568 | (index-function (or (plist-get project-plist :index-function) | |
569 | 'org-publish-org-index)) | |
570 | (preparation-function (plist-get project-plist :preparation-function)) | |
571 | (files (org-publish-get-base-files project exclude-regexp)) file) | |
572 | (when preparation-function (funcall preparation-function)) | |
573 | (if index-p (funcall index-function project index-filename)) | |
574 | (while (setq file (pop files)) | |
575 | (org-publish-file file project)))) | |
576 | (org-publish-expand-projects projects))) | |
577 | ||
578 | (defun org-publish-org-index (project &optional index-filename) | |
579 | "Create an index of pages in set defined by PROJECT. | |
580 | Optionally set the filename of the index with INDEX-FILENAME. | |
581 | Default for INDEX-FILENAME is 'index.org'." | |
582 | (let* ((project-plist (cdr project)) | |
583 | (dir (file-name-as-directory | |
584 | (plist-get project-plist :base-directory))) | |
585 | (exclude-regexp (plist-get project-plist :exclude)) | |
586 | (files (org-publish-get-base-files project exclude-regexp)) | |
9542795a CD |
587 | (index-filename (concat dir (or index-filename "index.org"))) |
588 | (index-buffer (find-buffer-visiting index-filename)) | |
589 | (ifn (file-name-nondirectory index-filename)) | |
699b9291 | 590 | file) |
9542795a CD |
591 | ;; if buffer is already open, kill it to prevent error message |
592 | (if index-buffer | |
593 | (kill-buffer index-buffer)) | |
594 | (with-temp-buffer | |
699b9291 CD |
595 | (while (setq file (pop files)) |
596 | (let ((fn (file-name-nondirectory file))) | |
597 | ;; index shouldn't index itself | |
598 | (unless (string= fn ifn) | |
9542795a CD |
599 | (insert (concat " + [[file:" fn "][" |
600 | (file-name-sans-extension fn) | |
601 | "]]\n"))))) | |
602 | (write-file index-filename) | |
603 | (kill-buffer (current-buffer))))) | |
604 | ||
699b9291 CD |
605 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
606 | ;;; Interactive publishing functions | |
9542795a | 607 | |
20908596 | 608 | (defalias 'org-publish-project 'org-publish) |
9542795a CD |
609 | |
610 | ;;;###autoload | |
699b9291 CD |
611 | (defun org-publish (project &optional force) |
612 | "Publish PROJECT." | |
613 | (interactive "P") | |
20908596 | 614 | (setq org-publish-initial-buffer (current-buffer)) |
9542795a | 615 | (save-window-excursion |
699b9291 CD |
616 | (let* ((force current-prefix-arg) |
617 | (org-publish-use-timestamps-flag | |
618 | (if force nil org-publish-use-timestamps-flag))) | |
619 | (org-publish-projects | |
620 | (list (or project | |
621 | (assoc (completing-read | |
622 | "Publish project: " | |
623 | org-publish-project-alist nil t) | |
624 | org-publish-project-alist))))))) | |
9542795a CD |
625 | |
626 | ;;;###autoload | |
699b9291 CD |
627 | (defun org-publish-all (&optional force) |
628 | "Publish all projects. | |
629 | With prefix argument, force publish all files." | |
9542795a | 630 | (interactive "P") |
699b9291 | 631 | (org-publish-initialize-files-alist) |
9542795a | 632 | (save-window-excursion |
699b9291 CD |
633 | (let ((org-publish-use-timestamps-flag |
634 | (if force nil org-publish-use-timestamps-flag))) | |
635 | (org-publish-projects org-publish-project-alist)))) | |
9542795a CD |
636 | |
637 | ;;;###autoload | |
638 | (defun org-publish-current-file (&optional force) | |
639 | "Publish the current file. | |
640 | With prefix argument, force publish the file." | |
641 | (interactive "P") | |
699b9291 | 642 | (org-publish-initialize-files-alist) |
9542795a CD |
643 | (save-window-excursion |
644 | (let ((org-publish-use-timestamps-flag | |
699b9291 | 645 | (if force nil org-publish-use-timestamps-flag))) |
9542795a CD |
646 | (org-publish-file (buffer-file-name))))) |
647 | ||
9542795a | 648 | ;;;###autoload |
699b9291 CD |
649 | (defun org-publish-current-project (&optional force) |
650 | "Publish the project associated with the current file. | |
651 | With a prefix argument, force publishing of all files in | |
652 | the project." | |
9542795a | 653 | (interactive "P") |
699b9291 | 654 | (org-publish-initialize-files-alist) |
9542795a | 655 | (save-window-excursion |
699b9291 CD |
656 | (let ((project (org-publish-get-project-from-filename (buffer-file-name))) |
657 | (org-publish-use-timestamps-flag | |
658 | (if force nil org-publish-use-timestamps-flag))) | |
659 | (if (not project) | |
660 | (error "File %s is not part of any known project" (buffer-file-name))) | |
661 | (org-publish project)))) | |
d5098885 | 662 | |
9542795a | 663 | (provide 'org-publish) |
4fbd4750 | 664 | |
699b9291 | 665 | |
4fbd4750 | 666 | ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb |
9542795a | 667 | ;;; org-publish.el ends here |