org-publish.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. ;;; org-publish.el --- publish related org-mode files as a website
  2. ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
  3. ;; Author: David O'Toole <dto@gnu.org>
  4. ;; Maintainer: Bastien Guerry <bzg AT altern DOT org>
  5. ;; Keywords: hypermedia, outlines, wp
  6. ;; Version: 5.23a
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 3, or (at your option)
  12. ;; any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; 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.
  21. ;;; Commentary:
  22. ;; Requires at least version 4.27 of org.el
  23. ;; This program allow configurable publishing of related sets of
  24. ;; Org-mode files as a complete website.
  25. ;;
  26. ;; org-publish.el can do the following:
  27. ;;
  28. ;; + Publish all one's org-files to HTML or LaTeX
  29. ;; + Upload HTML, images, attachments and other files to a web server
  30. ;; + Exclude selected private pages from publishing
  31. ;; + Publish a clickable index of pages
  32. ;; + Manage local timestamps for publishing only changed files
  33. ;; + Accept plugin functions to extend range of publishable content
  34. ;;
  35. ;; Special thanks to the org-mode maintainer Carsten Dominik for his
  36. ;; ideas, enthusiasm, and cooperation.
  37. ;;; Installation:
  38. ;; Put org-publish.el in your load path, byte-compile it, and then add
  39. ;; the following lines to your emacs initialization file:
  40. ;; (autoload 'org-publish "org-publish" nil t)
  41. ;; (autoload 'org-publish "org-publish-all" nil t)
  42. ;; (autoload 'org-publish "org-publish-current-file" nil t)
  43. ;; (autoload 'org-publish "org-publish-current-project" nil t)
  44. ;; NOTE: When org-publish.el is included with org.el, those forms are
  45. ;; already in the file org-install.el, and hence don't need to be put
  46. ;; in your emacs initialization file in this case.
  47. ;;; Usage:
  48. ;;
  49. ;; The program's main configuration variable is
  50. ;; `org-publish-project-alist'. See below for example configurations
  51. ;; with commentary.
  52. ;; The main interactive functions are:
  53. ;;
  54. ;; M-x org-publish
  55. ;; M-x org-publish-all
  56. ;; M-x org-publish-current-file
  57. ;; M-x org-publish-current-project
  58. ;;;; Simple example configuration:
  59. ;; (setq org-publish-project-alist
  60. ;; (list
  61. ;; '("org" . (:base-directory "~/org/"
  62. ;; :base-extension "org"
  63. ;; :publishing-directory "~/public_html"
  64. ;; :with-section-numbers nil
  65. ;; :table-of-contents nil
  66. ;; :recursive t
  67. ;; :style "<link rel=stylesheet href=\"../other/mystyle.css\" type=\"text/css\">")))
  68. ;;;; More complex example configuration:
  69. ;; Imagine your *.org files are kept in ~/org, your images in
  70. ;; ~/images, and stylesheets in ~/other. Now imagine you want to
  71. ;; publish the files through an ssh connection to a remote host, via
  72. ;; Tramp-mode. To maintain relative links from *.org files to /images
  73. ;; and /other, we should replicate the same directory structure in
  74. ;; your web server account's designated html root (in this case,
  75. ;; assumed to be ~/html)
  76. ;; Once you've done created the proper directories, you can adapt the
  77. ;; following example configuration to your specific paths, run M-x
  78. ;; org-publish-all, and it should publish the files to the correct
  79. ;; directories on the web server, transforming the *.org files into
  80. ;; HTML, and leaving other files alone.
  81. ;; (setq org-publish-project-alist
  82. ;; (list
  83. ;; '("orgfiles" :base-directory "~/org/"
  84. ;; :base-extension "org"
  85. ;; :publishing-directory "/ssh:user@host:~/html/notebook/"
  86. ;; :publishing-function org-publish-org-to-html
  87. ;; :exclude "PrivatePage.org" ;; regexp
  88. ;; :headline-levels 3
  89. ;; :with-section-numbers nil
  90. ;; :table-of-contents nil
  91. ;; :style "<link rel=stylesheet href=\"../other/mystyle.css\" type=\"text/css\">"
  92. ;; :auto-preamble t
  93. ;; :auto-postamble nil)
  94. ;; ("images" :base-directory "~/images/"
  95. ;; :base-extension "jpg\\|gif\\|png"
  96. ;; :publishing-directory "/ssh:user@host:~/html/images/"
  97. ;; :publishing-function org-publish-attachment)
  98. ;; ("other" :base-directory "~/other/"
  99. ;; :base-extension "css"
  100. ;; :publishing-directory "/ssh:user@host:~/html/other/"
  101. ;; :publishing-function org-publish-attachment)
  102. ;; ("website" :components ("orgfiles" "images" "other"))))
  103. ;; For more information, see the documentation for the variable
  104. ;; `org-publish-project-alist'.
  105. ;; Of course, you don't have to publish to remote directories from
  106. ;; within emacs. You can always just publish to local folders, and
  107. ;; then use the synchronization/upload tool of your choice.
  108. ;;; List of user-visible changes since version 1.27
  109. ;; 1.78: Allow list-valued :publishing-function
  110. ;; 1.77: Added :preparation-function, this allows you to use GNU Make etc.
  111. ;; 1.65: Remove old "composite projects". They're redundant.
  112. ;; 1.64: Allow meta-projects with :components
  113. ;; 1.57: Timestamps flag is now called "org-publish-use-timestamps-flag"
  114. ;; 1.52: Properly set default for :index-filename
  115. ;; 1.48: Composite projects allowed.
  116. ;; :include keyword allowed.
  117. ;; 1.43: Index no longer includes itself in the index.
  118. ;; 1.42: Fix "function definition is void" error
  119. ;; when :publishing-function not set in org-publish-current-file.
  120. ;; 1.41: Fixed bug where index isn't published on first try.
  121. ;; 1.37: Added interactive function "org-publish". Prompts for particular
  122. ;; project name to publish.
  123. ;; 1.34: Added force-publish option to all interactive functions.
  124. ;; 1.32: Fixed "index.org has changed on disk" error during index publishing.
  125. ;; 1.30: Fixed startup error caused by (require 'em-unix)
  126. ;;; Code:
  127. (eval-when-compile
  128. (require 'cl))
  129. (require 'dired-aux)
  130. (defgroup org-publish nil
  131. "Options for publishing a set of Org-mode and related files."
  132. :tag "Org Publishing"
  133. :group 'org)
  134. (defcustom org-publish-project-alist nil
  135. "Association list to control publishing behavior.
  136. Each element of the alist is a publishing 'project.' The CAR of
  137. each element is a string, uniquely identifying the project. The
  138. CDR of each element is in one of the following forms:
  139. (:property value :property value ... )
  140. OR,
  141. (:components (\"project-1\" \"project-2\" ...))
  142. When the CDR of an element of org-publish-project-alist is in
  143. this second form, the elements of the list after :components are
  144. taken to be components of the project, which group together files
  145. requiring different publishing options. When you publish such a
  146. project with M-x org-publish, the components all publish.
  147. When a property is given a value in org-publish-project-alist, its
  148. setting overrides the value of the corresponding user variable
  149. (if any) during publishing. However, options set within a file
  150. override everything.
  151. Most properties are optional, but some should always be set:
  152. :base-directory Directory containing publishing source files
  153. :base-extension Extension (without the dot!) of source files.
  154. This can be a regular expression.
  155. :publishing-directory Directory (possibly remote) where output
  156. files will be published
  157. The :exclude property may be used to prevent certain files from
  158. being published. Its value may be a string or regexp matching
  159. file names you don't want to be published.
  160. The :include property may be used to include extra files. Its
  161. value may be a list of filenames to include. The filenames are
  162. considered relative to the publishing directory.
  163. When both :include and :exclude properties are given values, the
  164. exclusion step happens first.
  165. One special property controls which back-end function to use for
  166. publishing files in the project. This can be used to extend the
  167. set of file types publishable by org-publish, as well as the set
  168. of output formats.
  169. :publishing-function Function to publish file. The default is
  170. org-publish-org-to-html, but other
  171. values are possible. May also be a
  172. list of functions, in which case
  173. each function in the list is invoked
  174. in turn.
  175. Another property allows you to insert code that prepares a
  176. project for publishing. For example, you could call GNU Make on a
  177. certain makefile, to ensure published files are built up to date.
  178. :preparation-function Function to be called before publishing
  179. this project.
  180. Some properties control details of the Org publishing process,
  181. and are equivalent to the corresponding user variables listed in
  182. the right column. See the documentation for those variables to
  183. learn more about their use and default values.
  184. :language org-export-default-language
  185. :headline-levels org-export-headline-levels
  186. :section-numbers org-export-with-section-numbers
  187. :table-of-contents org-export-with-toc
  188. :emphasize org-export-with-emphasize
  189. :sub-superscript org-export-with-sub-superscripts
  190. :TeX-macros org-export-with-TeX-macros
  191. :fixed-width org-export-with-fixed-width
  192. :tables org-export-with-tables
  193. :table-auto-headline org-export-highlight-first-table-line
  194. :style org-export-html-style
  195. :convert-org-links org-export-html-link-org-files-as-html
  196. :inline-images org-export-html-inline-images
  197. :expand-quoted-html org-export-html-expand
  198. :timestamp org-export-html-with-timestamp
  199. :publishing-directory org-export-publishing-directory
  200. :preamble org-export-html-preamble
  201. :postamble org-export-html-postamble
  202. :auto-preamble org-export-html-auto-preamble
  203. :auto-postamble org-export-html-auto-postamble
  204. :author user-full-name
  205. :email user-mail-address
  206. The following properties may be used to control publishing of an
  207. index of files or summary page for a given project.
  208. :auto-index Whether to publish an index during
  209. org-publish-current-project or org-publish-all.
  210. :index-filename Filename for output of index. Defaults
  211. to 'index.org' (which becomes 'index.html')
  212. :index-title Title of index page. Defaults to name of file.
  213. :index-function Plugin function to use for generation of index.
  214. Defaults to 'org-publish-org-index', which
  215. generates a plain list of links to all files
  216. in the project."
  217. :group 'org-publish
  218. :type 'alist)
  219. (defcustom org-publish-use-timestamps-flag t
  220. "When non-nil, use timestamp checking to publish only changed files.
  221. When nil, do no timestamp checking and always publish all
  222. files."
  223. :group 'org-publish
  224. :type 'boolean)
  225. (defcustom org-publish-timestamp-directory "~/.org-timestamps/"
  226. "Name of directory in which to store publishing timestamps."
  227. :group 'org-publish
  228. :type 'directory)
  229. (defcustom org-publish-before-export-hook nil
  230. "Hook run before export on the Org file.
  231. If the functions in this hook modify the original Org buffer, the
  232. modified buffer will be used for export, but the buffer will be
  233. restored and saved back to its initial state after export."
  234. :group 'org-publish
  235. :type 'hook)
  236. (defcustom org-publish-after-export-hook nil
  237. "Hook run after export on the exported buffer.
  238. If functions in this hook modify the buffer, it will be saved."
  239. :group 'org-publish
  240. :type 'hook)
  241. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  242. ;;; Timestamp-related functions
  243. (defun org-publish-timestamp-filename (filename)
  244. "Return path to timestamp file for filename FILENAME."
  245. (while (string-match
  246. (if (eq system-type 'windows-nt) "~\\|/\\|:" "~\\|/") filename)
  247. (setq filename (replace-match "_" nil t filename)))
  248. (concat org-publish-timestamp-directory filename ".timestamp"))
  249. (defun org-publish-needed-p (filename)
  250. "Return `t' if FILENAME should be published."
  251. (if org-publish-use-timestamps-flag
  252. (if (file-exists-p org-publish-timestamp-directory)
  253. ;; first handle possible wrong timestamp directory
  254. (if (not (file-directory-p org-publish-timestamp-directory))
  255. (error "Org publish timestamp: %s is not a directory"
  256. org-publish-timestamp-directory)
  257. ;; there is a timestamp, check if FILENAME is newer
  258. (file-newer-than-file-p
  259. filename (org-publish-timestamp-filename filename))))
  260. ;; don't use timestamps, always return t
  261. t))
  262. (defun org-publish-update-timestamp (filename)
  263. "Update publishing timestamp for file FILENAME.
  264. If there is no timestamp, create one."
  265. (let ((timestamp-file (org-publish-timestamp-filename filename))
  266. newly-created-timestamp)
  267. (if (not (file-exists-p timestamp-file))
  268. ;; create timestamp file if needed
  269. (with-temp-buffer
  270. (make-directory (file-name-directory timestamp-file) t)
  271. (write-file timestamp-file)
  272. (setq newly-created-timestamp t)))
  273. ;; Emacs 21 doesn't have `set-file-times'
  274. (if (and (fboundp 'set-file-times)
  275. (not newly-created-timestamp))
  276. (set-file-times timestamp-file)
  277. (call-process "touch" nil 0 nil timestamp-file))))
  278. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  279. ;;; Mapping files to project names
  280. (defvar org-publish-files-alist nil
  281. "Alist of files and their parent project.
  282. Each element of this alist is of the form:
  283. (file-name . project-name)")
  284. (defvar org-publish-initial-buffer nil
  285. "The buffer `org-publish' has been called from.")
  286. (defun org-publish-initialize-files-alist (&optional refresh)
  287. "Set `org-publish-files-alist' if it is not set.
  288. Also set it if the optional argument REFRESH is non-nil."
  289. (interactive "P")
  290. (when (or refresh (not org-publish-files-alist))
  291. (setq org-publish-files-alist
  292. (org-publish-get-files org-publish-project-alist))))
  293. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  294. ;;; Compatibility aliases
  295. ;; Delete-dups is not in Emacs <22
  296. (if (fboundp 'delete-dups)
  297. (defalias 'org-publish-delete-dups 'delete-dups)
  298. (defun org-publish-delete-dups (list)
  299. "Destructively remove `equal' duplicates from LIST.
  300. Store the result in LIST and return it. LIST must be a proper list.
  301. Of several `equal' occurrences of an element in LIST, the first
  302. one is kept.
  303. This is a compatibility function for Emacsen without `delete-dups'."
  304. ;; Code from `subr.el' in Emacs 22:
  305. (let ((tail list))
  306. (while tail
  307. (setcdr tail (delete (car tail) (cdr tail)))
  308. (setq tail (cdr tail))))
  309. list))
  310. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  311. ;;; Getting project information out of org-publish-project-alist
  312. (defun org-publish-get-files (projects-alist &optional no-exclusion)
  313. "Return the list of all publishable files for PROJECTS-ALIST.
  314. If NO-EXCLUSION is non-nil, don't exclude files."
  315. (let (all-files)
  316. ;; add all projects
  317. (mapc
  318. (lambda(p)
  319. (let* ((exclude (plist-get (cdr p) :exclude))
  320. (files (and p (org-publish-get-base-files p exclude))))
  321. ;; add all files from this project
  322. (mapc (lambda(f)
  323. (add-to-list 'all-files
  324. (cons (expand-file-name f) (car p))))
  325. files)))
  326. (org-publish-expand-projects projects-alist))
  327. all-files))
  328. (defun org-publish-expand-projects (projects-alist)
  329. "Expand projects contained in PROJECTS-ALIST."
  330. (let (without-component with-component)
  331. (mapc (lambda(p)
  332. (add-to-list
  333. (if (plist-get (cdr p) :components)
  334. 'with-component 'without-component) p))
  335. projects-alist)
  336. (org-publish-delete-dups
  337. (append without-component
  338. (car (mapcar (lambda(p) (org-publish-expand-components p))
  339. with-component))))))
  340. (defun org-publish-expand-components (project)
  341. "Expand PROJECT into an alist of its components."
  342. (let* ((components (plist-get (cdr project) :components)))
  343. (org-publish-delete-dups
  344. (delq nil (mapcar (lambda(c) (assoc c org-publish-project-alist))
  345. components)))))
  346. (defun org-publish-get-base-files (project &optional exclude-regexp)
  347. "Return a list of all files in PROJECT.
  348. If EXCLUDE-REGEXP is set, this will be used to filter out
  349. matching filenames."
  350. (let* ((project-plist (cdr project))
  351. (base-dir (file-name-as-directory
  352. (plist-get project-plist :base-directory)))
  353. (include-list (plist-get project-plist :include))
  354. (recursive-p (plist-get project-plist :recursive))
  355. (extension (or (plist-get project-plist :base-extension) "org"))
  356. (regexp (concat "^[^\\.].*\\.\\(" extension "\\)$"))
  357. alldirs allfiles files dir)
  358. ;; Get all files and directories in base-directory
  359. (setq files (dired-files-attributes base-dir))
  360. ;; Get all subdirectories if recursive-p
  361. (setq alldirs
  362. (if recursive-p
  363. (delq nil (mapcar (lambda(f) (if (caaddr f) (cadr f))) files))
  364. (list base-dir)))
  365. (while (setq dir (pop alldirs))
  366. (setq files (directory-files dir t regexp))
  367. ;; Exclude files
  368. (setq files
  369. (if (not exclude-regexp)
  370. files
  371. (delq nil
  372. (mapcar (lambda (x)
  373. (if (string-match exclude-regexp x) nil x))
  374. files))))
  375. ;; Include extra files
  376. (let (inc)
  377. (while (setq inc (pop include-list))
  378. (setq files (cons (expand-file-name inc dir) files))))
  379. (setq allfiles (append allfiles files)))
  380. allfiles))
  381. (defun org-publish-get-project-from-filename (filename)
  382. "Return the project FILENAME belongs."
  383. (let* ((project-name (cdr (assoc (expand-file-name filename)
  384. org-publish-files-alist))))
  385. (assoc project-name org-publish-project-alist)))
  386. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  387. ;;; Pluggable publishing back-end functions
  388. (defun org-publish-org-to (format plist filename pub-dir)
  389. "Publish an org file to FORMAT.
  390. PLIST is the property list for the given project.
  391. FILENAME is the filename of the org file to be published.
  392. PUB-DIR is the publishing directory."
  393. (require 'org)
  394. (unless (file-exists-p pub-dir)
  395. (make-directory pub-dir t))
  396. (find-file filename)
  397. (let ((init-buf (current-buffer))
  398. (init-point (point))
  399. (init-buf-string (buffer-string)) export-buf)
  400. ;; run hooks before exporting
  401. (run-hooks 'org-publish-before-export-hook)
  402. ;; export the possibly modified buffer
  403. (setq export-buf
  404. (funcall (intern (concat "org-export-as-" format))
  405. (plist-get plist :headline-levels)
  406. nil plist nil nil pub-dir))
  407. (set-buffer export-buf)
  408. ;; run hooks after export and save export
  409. (and (run-hooks 'org-publish-after-export-hook)
  410. (if (buffer-modified-p) (save-buffer)))
  411. (kill-buffer export-buf)
  412. ;; maybe restore buffer's content
  413. (set-buffer init-buf)
  414. (when (buffer-modified-p init-buf)
  415. (erase-buffer)
  416. (insert init-buf-string)
  417. (save-buffer)
  418. (goto-char init-point))
  419. (unless (eq init-buf org-publish-initial-buffer)
  420. (kill-buffer init-buf))))
  421. (defun org-publish-org-to-latex (plist filename pub-dir)
  422. "Publish an org file to LaTeX.
  423. See `org-publish-org-to' to the list of arguments."
  424. (org-publish-org-to "latex" plist filename pub-dir))
  425. (defun org-publish-org-to-html (plist filename pub-dir)
  426. "Publish an org file to HTML.
  427. See `org-publish-org-to' to the list of arguments."
  428. (org-publish-org-to "html" plist filename pub-dir))
  429. (defun org-publish-attachment (plist filename pub-dir)
  430. "Publish a file with no transformation of any kind.
  431. See `org-publish-org-to' to the list of arguments."
  432. ;; make sure eshell/cp code is loaded
  433. (eval-and-compile
  434. (require 'eshell)
  435. (require 'esh-maint)
  436. (require 'em-unix))
  437. (eshell/cp filename pub-dir))
  438. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  439. ;;; Publishing files, sets of files, and indices
  440. (defun org-publish-file (filename &optional project)
  441. "Publish file FILENAME from PROJECT."
  442. (when (org-publish-needed-p filename)
  443. (let* ((project
  444. (or project
  445. (or (org-publish-get-project-from-filename filename)
  446. (if (y-or-n-p
  447. (format "%s is not in a project. Re-read the list of projects files? "
  448. (abbreviate-file-name filename)))
  449. ;; If requested, re-initialize the list of projects files
  450. (progn (org-publish-initialize-files-alist t)
  451. (or (org-publish-get-project-from-filename filename)
  452. (error "File %s not part of any known project"
  453. (abbreviate-file-name filename))))
  454. (error "Can't publish file outside of a project")))))
  455. (project-plist (cdr project))
  456. (publishing-function
  457. (or (plist-get project-plist :publishing-function)
  458. 'org-publish-org-to-html))
  459. (base-dir (file-name-as-directory
  460. (file-truename (plist-get project-plist :base-directory))))
  461. (pub-dir (file-name-as-directory
  462. (file-truename (plist-get project-plist :publishing-directory))))
  463. tmp-pub-dir)
  464. (setq tmp-pub-dir
  465. (file-name-directory
  466. (concat pub-dir
  467. (and (string-match (regexp-quote base-dir) filename)
  468. (substring filename (match-end 0))))))
  469. (if (listp publishing-function)
  470. ;; allow chain of publishing functions
  471. (mapc (lambda (f)
  472. (funcall f project-plist filename tmp-pub-dir))
  473. publishing-function)
  474. (funcall publishing-function project-plist filename tmp-pub-dir)))
  475. (org-publish-update-timestamp filename)))
  476. (defun org-publish-projects (projects)
  477. "Publish all files belonging to the PROJECTS alist.
  478. If :auto-index is set, publish the index too."
  479. (mapc
  480. (lambda (project)
  481. (let* ((project-plist (cdr project))
  482. (exclude-regexp (plist-get project-plist :exclude))
  483. (index-p (plist-get project-plist :auto-index))
  484. (index-filename (or (plist-get project-plist :index-filename)
  485. "index.org"))
  486. (index-function (or (plist-get project-plist :index-function)
  487. 'org-publish-org-index))
  488. (preparation-function (plist-get project-plist :preparation-function))
  489. (files (org-publish-get-base-files project exclude-regexp)) file)
  490. (when preparation-function (funcall preparation-function))
  491. (if index-p (funcall index-function project index-filename))
  492. (while (setq file (pop files))
  493. (org-publish-file file project))))
  494. (org-publish-expand-projects projects)))
  495. (defun org-publish-org-index (project &optional index-filename)
  496. "Create an index of pages in set defined by PROJECT.
  497. Optionally set the filename of the index with INDEX-FILENAME.
  498. Default for INDEX-FILENAME is 'index.org'."
  499. (let* ((project-plist (cdr project))
  500. (dir (file-name-as-directory
  501. (plist-get project-plist :base-directory)))
  502. (exclude-regexp (plist-get project-plist :exclude))
  503. (files (org-publish-get-base-files project exclude-regexp))
  504. (index-filename (concat dir (or index-filename "index.org")))
  505. (index-buffer (find-buffer-visiting index-filename))
  506. (ifn (file-name-nondirectory index-filename))
  507. file)
  508. ;; if buffer is already open, kill it to prevent error message
  509. (if index-buffer
  510. (kill-buffer index-buffer))
  511. (with-temp-buffer
  512. (while (setq file (pop files))
  513. (let ((fn (file-name-nondirectory file)))
  514. ;; index shouldn't index itself
  515. (unless (string= fn ifn)
  516. (insert (concat " + [[file:" fn "]["
  517. (file-name-sans-extension fn)
  518. "]]\n")))))
  519. (write-file index-filename)
  520. (kill-buffer (current-buffer)))))
  521. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  522. ;;; Interactive publishing functions
  523. (defalias 'org-publish-project 'org-publish "Publish project.")
  524. ;;;###autoload
  525. (defun org-publish (project &optional force)
  526. "Publish PROJECT."
  527. (interactive "P")
  528. (setq org-publish-initial-buffer (current-buffer))
  529. (save-window-excursion
  530. (let* ((force current-prefix-arg)
  531. (org-publish-use-timestamps-flag
  532. (if force nil org-publish-use-timestamps-flag)))
  533. (org-publish-projects
  534. (list (or project
  535. (assoc (completing-read
  536. "Publish project: "
  537. org-publish-project-alist nil t)
  538. org-publish-project-alist)))))))
  539. ;;;###autoload
  540. (defun org-publish-all (&optional force)
  541. "Publish all projects.
  542. With prefix argument, force publish all files."
  543. (interactive "P")
  544. (org-publish-initialize-files-alist)
  545. (save-window-excursion
  546. (let ((org-publish-use-timestamps-flag
  547. (if force nil org-publish-use-timestamps-flag)))
  548. (org-publish-projects org-publish-project-alist))))
  549. ;;;###autoload
  550. (defun org-publish-current-file (&optional force)
  551. "Publish the current file.
  552. With prefix argument, force publish the file."
  553. (interactive "P")
  554. (org-publish-initialize-files-alist)
  555. (save-window-excursion
  556. (let ((org-publish-use-timestamps-flag
  557. (if force nil org-publish-use-timestamps-flag)))
  558. (org-publish-file (buffer-file-name)))))
  559. ;;;###autoload
  560. (defun org-publish-current-project (&optional force)
  561. "Publish the project associated with the current file.
  562. With a prefix argument, force publishing of all files in
  563. the project."
  564. (interactive "P")
  565. (org-publish-initialize-files-alist)
  566. (save-window-excursion
  567. (let ((project (org-publish-get-project-from-filename (buffer-file-name)))
  568. (org-publish-use-timestamps-flag
  569. (if force nil org-publish-use-timestamps-flag)))
  570. (if (not project)
  571. (error "File %s is not part of any known project" (buffer-file-name)))
  572. (org-publish project))))
  573. (provide 'org-publish)
  574. ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
  575. ;;; org-publish.el ends here