org-publish.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. (eval-and-compile
  130. (unless (fboundp 'declare-function)
  131. (defmacro declare-function (fn file &optional arglist fileonly))))
  132. (require 'dired-aux)
  133. (defgroup org-publish nil
  134. "Options for publishing a set of Org-mode and related files."
  135. :tag "Org Publishing"
  136. :group 'org)
  137. (defcustom org-publish-project-alist nil
  138. "Association list to control publishing behavior.
  139. Each element of the alist is a publishing 'project.' The CAR of
  140. each element is a string, uniquely identifying the project. The
  141. CDR of each element is in one of the following forms:
  142. (:property value :property value ... )
  143. OR,
  144. (:components (\"project-1\" \"project-2\" ...))
  145. When the CDR of an element of org-publish-project-alist is in
  146. this second form, the elements of the list after :components are
  147. taken to be components of the project, which group together files
  148. requiring different publishing options. When you publish such a
  149. project with M-x org-publish, the components all publish.
  150. When a property is given a value in org-publish-project-alist, its
  151. setting overrides the value of the corresponding user variable
  152. (if any) during publishing. However, options set within a file
  153. override everything.
  154. Most properties are optional, but some should always be set:
  155. :base-directory Directory containing publishing source files
  156. :base-extension Extension (without the dot!) of source files.
  157. This can be a regular expression.
  158. :publishing-directory Directory (possibly remote) where output
  159. files will be published
  160. The :exclude property may be used to prevent certain files from
  161. being published. Its value may be a string or regexp matching
  162. file names you don't want to be published.
  163. The :include property may be used to include extra files. Its
  164. value may be a list of filenames to include. The filenames are
  165. considered relative to the publishing directory.
  166. When both :include and :exclude properties are given values, the
  167. exclusion step happens first.
  168. One special property controls which back-end function to use for
  169. publishing files in the project. This can be used to extend the
  170. set of file types publishable by org-publish, as well as the set
  171. of output formats.
  172. :publishing-function Function to publish file. The default is
  173. org-publish-org-to-html, but other
  174. values are possible. May also be a
  175. list of functions, in which case
  176. each function in the list is invoked
  177. in turn.
  178. Another property allows you to insert code that prepares a
  179. project for publishing. For example, you could call GNU Make on a
  180. certain makefile, to ensure published files are built up to date.
  181. :preparation-function Function to be called before publishing
  182. this project.
  183. Some properties control details of the Org publishing process,
  184. and are equivalent to the corresponding user variables listed in
  185. the right column. See the documentation for those variables to
  186. learn more about their use and default values.
  187. :language org-export-default-language
  188. :headline-levels org-export-headline-levels
  189. :section-numbers org-export-with-section-numbers
  190. :table-of-contents org-export-with-toc
  191. :emphasize org-export-with-emphasize
  192. :sub-superscript org-export-with-sub-superscripts
  193. :TeX-macros org-export-with-TeX-macros
  194. :fixed-width org-export-with-fixed-width
  195. :tables org-export-with-tables
  196. :table-auto-headline org-export-highlight-first-table-line
  197. :style org-export-html-style
  198. :convert-org-links org-export-html-link-org-files-as-html
  199. :inline-images org-export-html-inline-images
  200. :expand-quoted-html org-export-html-expand
  201. :timestamp org-export-html-with-timestamp
  202. :publishing-directory org-export-publishing-directory
  203. :preamble org-export-html-preamble
  204. :postamble org-export-html-postamble
  205. :auto-preamble org-export-html-auto-preamble
  206. :auto-postamble org-export-html-auto-postamble
  207. :author user-full-name
  208. :email user-mail-address
  209. The following properties may be used to control publishing of an
  210. index of files or summary page for a given project.
  211. :auto-index Whether to publish an index during
  212. org-publish-current-project or org-publish-all.
  213. :index-filename Filename for output of index. Defaults
  214. to 'index.org' (which becomes 'index.html')
  215. :index-title Title of index page. Defaults to name of file.
  216. :index-function Plugin function to use for generation of index.
  217. Defaults to 'org-publish-org-index', which
  218. generates a plain list of links to all files
  219. in the project."
  220. :group 'org-publish
  221. :type 'alist)
  222. (defcustom org-publish-use-timestamps-flag t
  223. "When non-nil, use timestamp checking to publish only changed files.
  224. When nil, do no timestamp checking and always publish all
  225. files."
  226. :group 'org-publish
  227. :type 'boolean)
  228. (defcustom org-publish-timestamp-directory "~/.org-timestamps/"
  229. "Name of directory in which to store publishing timestamps."
  230. :group 'org-publish
  231. :type 'directory)
  232. (defcustom org-publish-before-export-hook nil
  233. "Hook run before export on the Org file.
  234. If the functions in this hook modify the original Org buffer, the
  235. modified buffer will be used for export, but the buffer will be
  236. restored and saved back to its initial state after export."
  237. :group 'org-publish
  238. :type 'hook)
  239. (defcustom org-publish-after-export-hook nil
  240. "Hook run after export on the exported buffer.
  241. If functions in this hook modify the buffer, it will be saved."
  242. :group 'org-publish
  243. :type 'hook)
  244. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  245. ;;; Timestamp-related functions
  246. (defun org-publish-timestamp-filename (filename)
  247. "Return path to timestamp file for filename FILENAME."
  248. (while (string-match
  249. (if (eq system-type 'windows-nt) "~\\|/\\|:" "~\\|/") filename)
  250. (setq filename (replace-match "_" nil t filename)))
  251. (concat org-publish-timestamp-directory filename ".timestamp"))
  252. (defun org-publish-needed-p (filename)
  253. "Return `t' if FILENAME should be published."
  254. (if org-publish-use-timestamps-flag
  255. (if (file-exists-p org-publish-timestamp-directory)
  256. ;; first handle possible wrong timestamp directory
  257. (if (not (file-directory-p org-publish-timestamp-directory))
  258. (error "Org publish timestamp: %s is not a directory"
  259. org-publish-timestamp-directory)
  260. ;; there is a timestamp, check if FILENAME is newer
  261. (file-newer-than-file-p
  262. filename (org-publish-timestamp-filename filename))))
  263. ;; don't use timestamps, always return t
  264. t))
  265. (defun org-publish-update-timestamp (filename)
  266. "Update publishing timestamp for file FILENAME.
  267. If there is no timestamp, create one."
  268. (let ((timestamp-file (org-publish-timestamp-filename filename))
  269. newly-created-timestamp)
  270. (if (not (file-exists-p timestamp-file))
  271. ;; create timestamp file if needed
  272. (with-temp-buffer
  273. (make-directory (file-name-directory timestamp-file) t)
  274. (write-file timestamp-file)
  275. (setq newly-created-timestamp t)))
  276. ;; Emacs 21 doesn't have `set-file-times'
  277. (if (and (fboundp 'set-file-times)
  278. (not newly-created-timestamp))
  279. (set-file-times timestamp-file)
  280. (call-process "touch" nil 0 nil timestamp-file))))
  281. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  282. ;;; Mapping files to project names
  283. (defvar org-publish-files-alist nil
  284. "Alist of files and their parent project.
  285. Each element of this alist is of the form:
  286. (file-name . project-name)")
  287. (defvar org-publish-initial-buffer nil
  288. "The buffer `org-publish' has been called from.")
  289. (defun org-publish-initialize-files-alist (&optional refresh)
  290. "Set `org-publish-files-alist' if it is not set.
  291. Also set it if the optional argument REFRESH is non-nil."
  292. (interactive "P")
  293. (when (or refresh (not org-publish-files-alist))
  294. (setq org-publish-files-alist
  295. (org-publish-get-files org-publish-project-alist))))
  296. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  297. ;;; Compatibility aliases
  298. ;; Delete-dups is not in Emacs <22
  299. (if (fboundp 'delete-dups)
  300. (defalias 'org-publish-delete-dups 'delete-dups)
  301. (defun org-publish-delete-dups (list)
  302. "Destructively remove `equal' duplicates from LIST.
  303. Store the result in LIST and return it. LIST must be a proper list.
  304. Of several `equal' occurrences of an element in LIST, the first
  305. one is kept.
  306. This is a compatibility function for Emacsen without `delete-dups'."
  307. ;; Code from `subr.el' in Emacs 22:
  308. (let ((tail list))
  309. (while tail
  310. (setcdr tail (delete (car tail) (cdr tail)))
  311. (setq tail (cdr tail))))
  312. list))
  313. (declare-function org-publish-delete-dups "org-publish" (list))
  314. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  315. ;;; Getting project information out of org-publish-project-alist
  316. (defun org-publish-get-files (projects-alist &optional no-exclusion)
  317. "Return the list of all publishable files for PROJECTS-ALIST.
  318. If NO-EXCLUSION is non-nil, don't exclude files."
  319. (let (all-files)
  320. ;; add all projects
  321. (mapc
  322. (lambda(p)
  323. (let* ((exclude (plist-get (cdr p) :exclude))
  324. (files (and p (org-publish-get-base-files p exclude))))
  325. ;; add all files from this project
  326. (mapc (lambda(f)
  327. (add-to-list 'all-files
  328. (cons (expand-file-name f) (car p))))
  329. files)))
  330. (org-publish-expand-projects projects-alist))
  331. all-files))
  332. (defun org-publish-expand-projects (projects-alist)
  333. "Expand projects contained in PROJECTS-ALIST."
  334. (let (without-component with-component)
  335. (mapc (lambda(p)
  336. (add-to-list
  337. (if (plist-get (cdr p) :components)
  338. 'with-component 'without-component) p))
  339. projects-alist)
  340. (org-publish-delete-dups
  341. (append without-component
  342. (car (mapcar (lambda(p) (org-publish-expand-components p))
  343. with-component))))))
  344. (defun org-publish-expand-components (project)
  345. "Expand PROJECT into an alist of its components."
  346. (let* ((components (plist-get (cdr project) :components)))
  347. (org-publish-delete-dups
  348. (delq nil (mapcar (lambda(c) (assoc c org-publish-project-alist))
  349. components)))))
  350. (defun org-publish-get-base-files (project &optional exclude-regexp)
  351. "Return a list of all files in PROJECT.
  352. If EXCLUDE-REGEXP is set, this will be used to filter out
  353. matching filenames."
  354. (let* ((project-plist (cdr project))
  355. (base-dir (file-name-as-directory
  356. (plist-get project-plist :base-directory)))
  357. (include-list (plist-get project-plist :include))
  358. (recursive-p (plist-get project-plist :recursive))
  359. (extension (or (plist-get project-plist :base-extension) "org"))
  360. (regexp (concat "^[^\\.].*\\.\\(" extension "\\)$"))
  361. alldirs allfiles files dir)
  362. ;; Get all files and directories in base-directory
  363. (setq files (dired-files-attributes base-dir))
  364. ;; Get all subdirectories if recursive-p
  365. (setq alldirs
  366. (if recursive-p
  367. (delq nil (mapcar (lambda(f) (if (caaddr f) (cadr f))) files))
  368. (list base-dir)))
  369. (while (setq dir (pop alldirs))
  370. (setq files (directory-files dir t regexp))
  371. ;; Exclude files
  372. (setq files
  373. (if (not exclude-regexp)
  374. files
  375. (delq nil
  376. (mapcar (lambda (x)
  377. (if (string-match exclude-regexp x) nil x))
  378. files))))
  379. ;; Include extra files
  380. (let (inc)
  381. (while (setq inc (pop include-list))
  382. (setq files (cons (expand-file-name inc dir) files))))
  383. (setq allfiles (append allfiles files)))
  384. allfiles))
  385. (defun org-publish-get-project-from-filename (filename)
  386. "Return the project FILENAME belongs."
  387. (let* ((project-name (cdr (assoc (expand-file-name filename)
  388. org-publish-files-alist))))
  389. (assoc project-name org-publish-project-alist)))
  390. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  391. ;;; Pluggable publishing back-end functions
  392. (defun org-publish-org-to (format plist filename pub-dir)
  393. "Publish an org file to FORMAT.
  394. PLIST is the property list for the given project.
  395. FILENAME is the filename of the org file to be published.
  396. PUB-DIR is the publishing directory."
  397. (require 'org)
  398. (unless (file-exists-p pub-dir)
  399. (make-directory pub-dir t))
  400. (find-file filename)
  401. (let ((init-buf (current-buffer))
  402. (init-point (point))
  403. (init-buf-string (buffer-string)) export-buf)
  404. ;; run hooks before exporting
  405. (run-hooks 'org-publish-before-export-hook)
  406. ;; export the possibly modified buffer
  407. (setq export-buf
  408. (funcall (intern (concat "org-export-as-" format))
  409. (plist-get plist :headline-levels)
  410. nil plist nil nil pub-dir))
  411. (set-buffer export-buf)
  412. ;; run hooks after export and save export
  413. (and (run-hooks 'org-publish-after-export-hook)
  414. (if (buffer-modified-p) (save-buffer)))
  415. (kill-buffer export-buf)
  416. ;; maybe restore buffer's content
  417. (set-buffer init-buf)
  418. (when (buffer-modified-p init-buf)
  419. (erase-buffer)
  420. (insert init-buf-string)
  421. (save-buffer)
  422. (goto-char init-point))
  423. (unless (eq init-buf org-publish-initial-buffer)
  424. (kill-buffer init-buf))))
  425. (defun org-publish-org-to-latex (plist filename pub-dir)
  426. "Publish an org file to LaTeX.
  427. See `org-publish-org-to' to the list of arguments."
  428. (org-publish-org-to "latex" plist filename pub-dir))
  429. (defun org-publish-org-to-html (plist filename pub-dir)
  430. "Publish an org file to HTML.
  431. See `org-publish-org-to' to the list of arguments."
  432. (org-publish-org-to "html" plist filename pub-dir))
  433. (defun org-publish-attachment (plist filename pub-dir)
  434. "Publish a file with no transformation of any kind.
  435. See `org-publish-org-to' to the list of arguments."
  436. ;; make sure eshell/cp code is loaded
  437. (eval-and-compile
  438. (require 'eshell)
  439. (require 'esh-maint)
  440. (require 'em-unix))
  441. (eshell/cp filename pub-dir))
  442. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  443. ;;; Publishing files, sets of files, and indices
  444. (defun org-publish-file (filename &optional project)
  445. "Publish file FILENAME from PROJECT."
  446. (when (org-publish-needed-p filename)
  447. (let* ((project
  448. (or project
  449. (or (org-publish-get-project-from-filename filename)
  450. (if (y-or-n-p
  451. (format "%s is not in a project. Re-read the list of projects files? "
  452. (abbreviate-file-name filename)))
  453. ;; If requested, re-initialize the list of projects files
  454. (progn (org-publish-initialize-files-alist t)
  455. (or (org-publish-get-project-from-filename filename)
  456. (error "File %s not part of any known project"
  457. (abbreviate-file-name filename))))
  458. (error "Can't publish file outside of a project")))))
  459. (project-plist (cdr project))
  460. (publishing-function
  461. (or (plist-get project-plist :publishing-function)
  462. 'org-publish-org-to-html))
  463. (base-dir (file-name-as-directory
  464. (file-truename (plist-get project-plist :base-directory))))
  465. (pub-dir (file-name-as-directory
  466. (file-truename (plist-get project-plist :publishing-directory))))
  467. tmp-pub-dir)
  468. (setq tmp-pub-dir
  469. (file-name-directory
  470. (concat pub-dir
  471. (and (string-match (regexp-quote base-dir) filename)
  472. (substring filename (match-end 0))))))
  473. (if (listp publishing-function)
  474. ;; allow chain of publishing functions
  475. (mapc (lambda (f)
  476. (funcall f project-plist filename tmp-pub-dir))
  477. publishing-function)
  478. (funcall publishing-function project-plist filename tmp-pub-dir)))
  479. (org-publish-update-timestamp filename)))
  480. (defun org-publish-projects (projects)
  481. "Publish all files belonging to the PROJECTS alist.
  482. If :auto-index is set, publish the index too."
  483. (mapc
  484. (lambda (project)
  485. (let* ((project-plist (cdr project))
  486. (exclude-regexp (plist-get project-plist :exclude))
  487. (index-p (plist-get project-plist :auto-index))
  488. (index-filename (or (plist-get project-plist :index-filename)
  489. "index.org"))
  490. (index-function (or (plist-get project-plist :index-function)
  491. 'org-publish-org-index))
  492. (preparation-function (plist-get project-plist :preparation-function))
  493. (files (org-publish-get-base-files project exclude-regexp)) file)
  494. (when preparation-function (funcall preparation-function))
  495. (if index-p (funcall index-function project index-filename))
  496. (while (setq file (pop files))
  497. (org-publish-file file project))))
  498. (org-publish-expand-projects projects)))
  499. (defun org-publish-org-index (project &optional index-filename)
  500. "Create an index of pages in set defined by PROJECT.
  501. Optionally set the filename of the index with INDEX-FILENAME.
  502. Default for INDEX-FILENAME is 'index.org'."
  503. (let* ((project-plist (cdr project))
  504. (dir (file-name-as-directory
  505. (plist-get project-plist :base-directory)))
  506. (exclude-regexp (plist-get project-plist :exclude))
  507. (files (org-publish-get-base-files project exclude-regexp))
  508. (index-filename (concat dir (or index-filename "index.org")))
  509. (index-buffer (find-buffer-visiting index-filename))
  510. (ifn (file-name-nondirectory index-filename))
  511. file)
  512. ;; if buffer is already open, kill it to prevent error message
  513. (if index-buffer
  514. (kill-buffer index-buffer))
  515. (with-temp-buffer
  516. (while (setq file (pop files))
  517. (let ((fn (file-name-nondirectory file)))
  518. ;; index shouldn't index itself
  519. (unless (string= fn ifn)
  520. (insert (concat " + [[file:" fn "]["
  521. (file-name-sans-extension fn)
  522. "]]\n")))))
  523. (write-file index-filename)
  524. (kill-buffer (current-buffer)))))
  525. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  526. ;;; Interactive publishing functions
  527. (defalias 'org-publish-project 'org-publish "Publish project.")
  528. ;;;###autoload
  529. (defun org-publish (project &optional force)
  530. "Publish PROJECT."
  531. (interactive "P")
  532. (setq org-publish-initial-buffer (current-buffer))
  533. (save-window-excursion
  534. (let* ((force current-prefix-arg)
  535. (org-publish-use-timestamps-flag
  536. (if force nil org-publish-use-timestamps-flag)))
  537. (org-publish-projects
  538. (list (or project
  539. (assoc (completing-read
  540. "Publish project: "
  541. org-publish-project-alist nil t)
  542. org-publish-project-alist)))))))
  543. ;;;###autoload
  544. (defun org-publish-all (&optional force)
  545. "Publish all projects.
  546. With prefix argument, force publish all files."
  547. (interactive "P")
  548. (org-publish-initialize-files-alist)
  549. (save-window-excursion
  550. (let ((org-publish-use-timestamps-flag
  551. (if force nil org-publish-use-timestamps-flag)))
  552. (org-publish-projects org-publish-project-alist))))
  553. ;;;###autoload
  554. (defun org-publish-current-file (&optional force)
  555. "Publish the current file.
  556. With prefix argument, force publish the file."
  557. (interactive "P")
  558. (org-publish-initialize-files-alist)
  559. (save-window-excursion
  560. (let ((org-publish-use-timestamps-flag
  561. (if force nil org-publish-use-timestamps-flag)))
  562. (org-publish-file (buffer-file-name)))))
  563. ;;;###autoload
  564. (defun org-publish-current-project (&optional force)
  565. "Publish the project associated with the current file.
  566. With a prefix argument, force publishing of all files in
  567. the project."
  568. (interactive "P")
  569. (org-publish-initialize-files-alist)
  570. (save-window-excursion
  571. (let ((project (org-publish-get-project-from-filename (buffer-file-name)))
  572. (org-publish-use-timestamps-flag
  573. (if force nil org-publish-use-timestamps-flag)))
  574. (if (not project)
  575. (error "File %s is not part of any known project" (buffer-file-name)))
  576. (org-publish project))))
  577. (provide 'org-publish)
  578. ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
  579. ;;; org-publish.el ends here