org-publish.el 26 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: 6.02b
  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 of the License, or
  12. ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; Requires at least version 4.27 of org.el
  21. ;; This program allow configurable publishing of related sets of
  22. ;; Org-mode files as a complete website.
  23. ;;
  24. ;; org-publish.el can do the following:
  25. ;;
  26. ;; + Publish all one's org-files to HTML or LaTeX
  27. ;; + Upload HTML, images, attachments and other files to a web server
  28. ;; + Exclude selected private pages from publishing
  29. ;; + Publish a clickable index of pages
  30. ;; + Manage local timestamps for publishing only changed files
  31. ;; + Accept plugin functions to extend range of publishable content
  32. ;;
  33. ;; Special thanks to the org-mode maintainer Carsten Dominik for his
  34. ;; ideas, enthusiasm, and cooperation.
  35. ;;; Installation:
  36. ;; Put org-publish.el in your load path, byte-compile it, and then add
  37. ;; the following lines to your emacs initialization file:
  38. ;; (autoload 'org-publish "org-publish" nil t)
  39. ;; (autoload 'org-publish "org-publish-all" nil t)
  40. ;; (autoload 'org-publish "org-publish-current-file" nil t)
  41. ;; (autoload 'org-publish "org-publish-current-project" nil t)
  42. ;; NOTE: When org-publish.el is included with org.el, those forms are
  43. ;; already in the file org-install.el, and hence don't need to be put
  44. ;; in your emacs initialization file in this case.
  45. ;;; Usage:
  46. ;;
  47. ;; The program's main configuration variable is
  48. ;; `org-publish-project-alist'. See below for example configurations
  49. ;; with commentary.
  50. ;; The main interactive functions are:
  51. ;;
  52. ;; M-x org-publish
  53. ;; M-x org-publish-all
  54. ;; M-x org-publish-current-file
  55. ;; M-x org-publish-current-project
  56. ;;;; Simple example configuration:
  57. ;; (setq org-publish-project-alist
  58. ;; (list
  59. ;; '("org" . (:base-directory "~/org/"
  60. ;; :base-extension "org"
  61. ;; :publishing-directory "~/public_html"
  62. ;; :with-section-numbers nil
  63. ;; :table-of-contents nil
  64. ;; :recursive t
  65. ;; :style "<link rel=stylesheet href=\"../other/mystyle.css\" type=\"text/css\">")))
  66. ;;;; More complex example configuration:
  67. ;; Imagine your *.org files are kept in ~/org, your images in
  68. ;; ~/images, and stylesheets in ~/other. Now imagine you want to
  69. ;; publish the files through an ssh connection to a remote host, via
  70. ;; Tramp-mode. To maintain relative links from *.org files to /images
  71. ;; and /other, we should replicate the same directory structure in
  72. ;; your web server account's designated html root (in this case,
  73. ;; assumed to be ~/html)
  74. ;; Once you've done created the proper directories, you can adapt the
  75. ;; following example configuration to your specific paths, run M-x
  76. ;; org-publish-all, and it should publish the files to the correct
  77. ;; directories on the web server, transforming the *.org files into
  78. ;; HTML, and leaving other files alone.
  79. ;; (setq org-publish-project-alist
  80. ;; (list
  81. ;; '("orgfiles" :base-directory "~/org/"
  82. ;; :base-extension "org"
  83. ;; :publishing-directory "/ssh:user@host:~/html/notebook/"
  84. ;; :publishing-function org-publish-org-to-html
  85. ;; :exclude "PrivatePage.org" ;; regexp
  86. ;; :headline-levels 3
  87. ;; :with-section-numbers nil
  88. ;; :table-of-contents nil
  89. ;; :style "<link rel=stylesheet href=\"../other/mystyle.css\" type=\"text/css\">"
  90. ;; :auto-preamble t
  91. ;; :auto-postamble nil)
  92. ;; ("images" :base-directory "~/images/"
  93. ;; :base-extension "jpg\\|gif\\|png"
  94. ;; :publishing-directory "/ssh:user@host:~/html/images/"
  95. ;; :publishing-function org-publish-attachment)
  96. ;; ("other" :base-directory "~/other/"
  97. ;; :base-extension "css"
  98. ;; :publishing-directory "/ssh:user@host:~/html/other/"
  99. ;; :publishing-function org-publish-attachment)
  100. ;; ("website" :components ("orgfiles" "images" "other"))))
  101. ;; For more information, see the documentation for the variable
  102. ;; `org-publish-project-alist'.
  103. ;; Of course, you don't have to publish to remote directories from
  104. ;; within emacs. You can always just publish to local folders, and
  105. ;; then use the synchronization/upload tool of your choice.
  106. ;;; List of user-visible changes since version 1.27
  107. ;; 1.78: Allow list-valued :publishing-function
  108. ;; 1.77: Added :preparation-function, this allows you to use GNU Make etc.
  109. ;; 1.65: Remove old "composite projects". They're redundant.
  110. ;; 1.64: Allow meta-projects with :components
  111. ;; 1.57: Timestamps flag is now called "org-publish-use-timestamps-flag"
  112. ;; 1.52: Properly set default for :index-filename
  113. ;; 1.48: Composite projects allowed.
  114. ;; :include keyword allowed.
  115. ;; 1.43: Index no longer includes itself in the index.
  116. ;; 1.42: Fix "function definition is void" error
  117. ;; when :publishing-function not set in org-publish-current-file.
  118. ;; 1.41: Fixed bug where index isn't published on first try.
  119. ;; 1.37: Added interactive function "org-publish". Prompts for particular
  120. ;; project name to publish.
  121. ;; 1.34: Added force-publish option to all interactive functions.
  122. ;; 1.32: Fixed "index.org has changed on disk" error during index publishing.
  123. ;; 1.30: Fixed startup error caused by (require 'em-unix)
  124. ;;; Code:
  125. (eval-when-compile
  126. (require 'cl))
  127. (eval-and-compile
  128. (unless (fboundp 'declare-function)
  129. (defmacro declare-function (fn file &optional arglist fileonly))))
  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. (defvar org-publish-temp-files nil
  287. "Temporary list of files to be published.")
  288. (defun org-publish-initialize-files-alist (&optional refresh)
  289. "Set `org-publish-files-alist' if it is not set.
  290. Also set it if the optional argument REFRESH is non-nil."
  291. (interactive "P")
  292. (when (or refresh (not org-publish-files-alist))
  293. (setq org-publish-files-alist
  294. (org-publish-get-files org-publish-project-alist))))
  295. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  296. ;;; Compatibility aliases
  297. ;; Delete-dups is not in Emacs <22
  298. (if (fboundp 'delete-dups)
  299. (defalias 'org-publish-delete-dups 'delete-dups)
  300. (defun org-publish-delete-dups (list)
  301. "Destructively remove `equal' duplicates from LIST.
  302. Store the result in LIST and return it. LIST must be a proper list.
  303. Of several `equal' occurrences of an element in LIST, the first
  304. one is kept.
  305. This is a compatibility function for Emacsen without `delete-dups'."
  306. ;; Code from `subr.el' in Emacs 22:
  307. (let ((tail list))
  308. (while tail
  309. (setcdr tail (delete (car tail) (cdr tail)))
  310. (setq tail (cdr tail))))
  311. list))
  312. (declare-function org-publish-delete-dups "org-publish" (list))
  313. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  314. ;;; Getting project information out of org-publish-project-alist
  315. (defun org-publish-get-files (projects-alist &optional no-exclusion)
  316. "Return the list of all publishable files for PROJECTS-ALIST.
  317. If NO-EXCLUSION is non-nil, don't exclude files."
  318. (let (all-files)
  319. ;; add all projects
  320. (mapc
  321. (lambda(p)
  322. (let* ((exclude (plist-get (cdr p) :exclude))
  323. (files (and p (org-publish-get-base-files p exclude))))
  324. ;; add all files from this project
  325. (mapc (lambda(f)
  326. (add-to-list 'all-files
  327. (cons (expand-file-name f) (car p))))
  328. files)))
  329. (org-publish-expand-projects projects-alist))
  330. all-files))
  331. (defun org-publish-expand-projects (projects-alist)
  332. "Expand projects contained in PROJECTS-ALIST."
  333. (let (without-component with-component)
  334. (mapc (lambda(p)
  335. (add-to-list
  336. (if (plist-get (cdr p) :components)
  337. 'with-component 'without-component) p))
  338. projects-alist)
  339. (org-publish-delete-dups
  340. (append without-component
  341. (car (mapcar (lambda(p) (org-publish-expand-components p))
  342. with-component))))))
  343. (defun org-publish-expand-components (project)
  344. "Expand PROJECT into an alist of its components."
  345. (let* ((components (plist-get (cdr project) :components)))
  346. (org-publish-delete-dups
  347. (delq nil (mapcar (lambda(c) (assoc c org-publish-project-alist))
  348. components)))))
  349. (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
  350. "Set `org-publish-temp-files' with files from BASE-DIR directory.
  351. If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
  352. non-nil, restrict this list to the files matching the regexp
  353. MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
  354. SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
  355. matching the regexp SKIP-DIR when recursiing through BASE-DIR."
  356. (mapc (lambda (f)
  357. (let ((fd-p (car (file-attributes f)))
  358. (fnd (file-name-nondirectory f)))
  359. (if (and fd-p recurse
  360. (not (string-match "^\\.+$" fnd))
  361. (if skip-dir (not (string-match skip-dir fnd)) t))
  362. (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
  363. (unless (or fd-p ;; this is a directory
  364. (and skip-file (string-match skip-file fnd))
  365. (not (string-match match fnd)))
  366. (pushnew f org-publish-temp-files)))))
  367. (directory-files base-dir t (unless recurse match))))
  368. (defun org-publish-get-base-files (project &optional exclude-regexp)
  369. "Return a list of all files in PROJECT.
  370. If EXCLUDE-REGEXP is set, this will be used to filter out
  371. matching filenames."
  372. (let* ((project-plist (cdr project))
  373. (base-dir (file-name-as-directory
  374. (plist-get project-plist :base-directory)))
  375. (include-list (plist-get project-plist :include))
  376. (recurse (plist-get project-plist :recursive))
  377. (extension (or (plist-get project-plist :base-extension) "org"))
  378. (match (concat "^[^\\.].*\\.\\(" extension "\\)$")))
  379. (setq org-publish-temp-files nil)
  380. (org-publish-get-base-files-1 base-dir recurse match
  381. ;; FIXME distinguish exclude regexp
  382. ;; for skip-file and skip-dir?
  383. exclude-regexp exclude-regexp)
  384. org-publish-temp-files))
  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)
  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