org-publish.el 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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.03
  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 \\[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 files."
  222. :group 'org-publish
  223. :type 'boolean)
  224. (defcustom org-publish-timestamp-directory "~/.org-timestamps/"
  225. "Name of directory in which to store publishing timestamps."
  226. :group 'org-publish
  227. :type 'directory)
  228. (defcustom org-publish-before-export-hook nil
  229. "Hook run before export on the Org file.
  230. If the functions in this hook modify the original Org buffer, the
  231. modified buffer will be used for export, but the buffer will be
  232. restored and saved back to its initial state after export."
  233. :group 'org-publish
  234. :type 'hook)
  235. (defcustom org-publish-after-export-hook nil
  236. "Hook run after export on the exported buffer.
  237. If functions in this hook modify the buffer, it will be saved."
  238. :group 'org-publish
  239. :type 'hook)
  240. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  241. ;;; Timestamp-related functions
  242. (defun org-publish-timestamp-filename (filename)
  243. "Return path to timestamp file for filename FILENAME."
  244. (while (string-match
  245. (if (eq system-type 'windows-nt) "~\\|/\\|:" "~\\|/") filename)
  246. (setq filename (replace-match "_" nil t filename)))
  247. (concat org-publish-timestamp-directory filename ".timestamp"))
  248. (defun org-publish-needed-p (filename)
  249. "Return `t' if FILENAME should be published."
  250. (if org-publish-use-timestamps-flag
  251. (if (file-exists-p org-publish-timestamp-directory)
  252. ;; first handle possible wrong timestamp directory
  253. (if (not (file-directory-p org-publish-timestamp-directory))
  254. (error "Org publish timestamp: %s is not a directory"
  255. org-publish-timestamp-directory)
  256. ;; there is a timestamp, check if FILENAME is newer
  257. (file-newer-than-file-p
  258. filename (org-publish-timestamp-filename filename))))
  259. ;; don't use timestamps, always return t
  260. t))
  261. (defun org-publish-update-timestamp (filename)
  262. "Update publishing timestamp for file FILENAME.
  263. If there is no timestamp, create one."
  264. (let ((timestamp-file (org-publish-timestamp-filename filename))
  265. newly-created-timestamp)
  266. (if (not (file-exists-p timestamp-file))
  267. ;; create timestamp file if needed
  268. (with-temp-buffer
  269. (make-directory (file-name-directory timestamp-file) t)
  270. (write-file timestamp-file)
  271. (setq newly-created-timestamp t)))
  272. ;; Emacs 21 doesn't have `set-file-times'
  273. (if (and (fboundp 'set-file-times)
  274. (not newly-created-timestamp))
  275. (set-file-times timestamp-file)
  276. (call-process "touch" nil 0 nil timestamp-file))))
  277. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  278. ;;; Mapping files to project names
  279. (defvar org-publish-files-alist nil
  280. "Alist of files and their parent projects.
  281. Each element of this alist is of the form:
  282. (file-name . project-name)")
  283. (defvar org-publish-initial-buffer nil
  284. "The buffer `org-publish' has been called from.")
  285. (defvar org-publish-temp-files nil
  286. "Temporary list of files to be published.")
  287. (defun org-publish-initialize-files-alist (&optional refresh)
  288. "Set `org-publish-files-alist' if it is not set.
  289. Also set it if the optional argument REFRESH is non-nil."
  290. (interactive "P")
  291. (when (or refresh (not org-publish-files-alist))
  292. (setq org-publish-files-alist
  293. (org-publish-get-files org-publish-project-alist))))
  294. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  295. ;;; Compatibility aliases
  296. ;; Delete-dups is not in Emacs <22
  297. (if (fboundp 'delete-dups)
  298. (defalias 'org-publish-delete-dups 'delete-dups)
  299. (defun org-publish-delete-dups (list)
  300. "Destructively remove `equal' duplicates from LIST.
  301. Store the result in LIST and return it. LIST must be a proper list.
  302. Of several `equal' occurrences of an element in LIST, the first
  303. one is kept.
  304. This is a compatibility function for Emacsen without `delete-dups'."
  305. ;; Code from `subr.el' in Emacs 22:
  306. (let ((tail list))
  307. (while tail
  308. (setcdr tail (delete (car tail) (cdr tail)))
  309. (setq tail (cdr tail))))
  310. list))
  311. (declare-function org-publish-delete-dups "org-publish" (list))
  312. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  313. ;;; Getting project information out of org-publish-project-alist
  314. (defun org-publish-get-files (projects-alist &optional no-exclusion)
  315. "Return the list of all publishable files for PROJECTS-ALIST.
  316. If NO-EXCLUSION is non-nil, don't exclude files."
  317. (let (all-files)
  318. ;; add all projects
  319. (mapc
  320. (lambda(p)
  321. (let* ((exclude (plist-get (cdr p) :exclude))
  322. (files (and p (org-publish-get-base-files p exclude))))
  323. ;; add all files from this project
  324. (mapc (lambda(f)
  325. (add-to-list 'all-files
  326. (cons (expand-file-name f) (car p))))
  327. files)))
  328. (org-publish-expand-projects projects-alist))
  329. all-files))
  330. (defun org-publish-expand-projects (projects-alist)
  331. "Expand projects contained in PROJECTS-ALIST."
  332. (let (without-component with-component)
  333. (mapc (lambda(p)
  334. (add-to-list
  335. (if (plist-get (cdr p) :components)
  336. 'with-component 'without-component) p))
  337. projects-alist)
  338. (org-publish-delete-dups
  339. (append without-component
  340. (car (mapcar (lambda(p) (org-publish-expand-components p))
  341. with-component))))))
  342. (defun org-publish-expand-components (project)
  343. "Expand PROJECT into an alist of its components."
  344. (let* ((components (plist-get (cdr project) :components)))
  345. (org-publish-delete-dups
  346. (delq nil (mapcar (lambda(c) (assoc c org-publish-project-alist))
  347. components)))))
  348. (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
  349. "Set `org-publish-temp-files' with files from BASE-DIR directory.
  350. If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
  351. non-nil, restrict this list to the files matching the regexp
  352. MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
  353. SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
  354. matching the regexp SKIP-DIR when recursiing through BASE-DIR."
  355. (mapc (lambda (f)
  356. (let ((fd-p (car (file-attributes f)))
  357. (fnd (file-name-nondirectory f)))
  358. (if (and fd-p recurse
  359. (not (string-match "^\\.+$" fnd))
  360. (if skip-dir (not (string-match skip-dir fnd)) t))
  361. (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
  362. (unless (or fd-p ;; this is a directory
  363. (and skip-file (string-match skip-file fnd))
  364. (not (string-match match fnd)))
  365. (pushnew f org-publish-temp-files)))))
  366. (directory-files base-dir t (unless recurse match))))
  367. (defun org-publish-get-base-files (project &optional exclude-regexp)
  368. "Return a list of all files in PROJECT.
  369. If EXCLUDE-REGEXP is set, this will be used to filter out
  370. matching filenames."
  371. (let* ((project-plist (cdr project))
  372. (base-dir (file-name-as-directory
  373. (plist-get project-plist :base-directory)))
  374. (include-list (plist-get project-plist :include))
  375. (recurse (plist-get project-plist :recursive))
  376. (extension (or (plist-get project-plist :base-extension) "org"))
  377. (match (concat "^[^\\.].*\\.\\(" extension "\\)$")))
  378. (setq org-publish-temp-files nil)
  379. (org-publish-get-base-files-1 base-dir recurse match
  380. ;; FIXME distinguish exclude regexp
  381. ;; for skip-file and skip-dir?
  382. exclude-regexp exclude-regexp)
  383. org-publish-temp-files))
  384. (defun org-publish-get-project-from-filename (filename)
  385. "Return the project FILENAME belongs."
  386. (let* ((project-name (cdr (assoc (expand-file-name filename)
  387. org-publish-files-alist))))
  388. (assoc project-name org-publish-project-alist)))
  389. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  390. ;;; Pluggable publishing back-end functions
  391. (defun org-publish-org-to (format plist filename pub-dir)
  392. "Publish an org file to FORMAT.
  393. PLIST is the property list for the given project.
  394. FILENAME is the filename of the org file to be published.
  395. PUB-DIR is the publishing directory."
  396. (require 'org)
  397. (unless (file-exists-p pub-dir)
  398. (make-directory pub-dir t))
  399. (find-file filename)
  400. (let ((init-buf (current-buffer))
  401. (init-point (point))
  402. (init-buf-string (buffer-string)) export-buf)
  403. ;; run hooks before exporting
  404. (run-hooks 'org-publish-before-export-hook)
  405. ;; export the possibly modified buffer
  406. (setq export-buf
  407. (funcall (intern (concat "org-export-as-" format))
  408. (plist-get plist :headline-levels)
  409. nil plist nil nil pub-dir))
  410. (set-buffer export-buf)
  411. ;; run hooks after export and save export
  412. (and (run-hooks 'org-publish-after-export-hook)
  413. (if (buffer-modified-p) (save-buffer)))
  414. (kill-buffer export-buf)
  415. ;; maybe restore buffer's content
  416. (set-buffer init-buf)
  417. (when (buffer-modified-p init-buf)
  418. (erase-buffer)
  419. (insert init-buf-string)
  420. (save-buffer)
  421. (goto-char init-point))
  422. (unless (eq init-buf org-publish-initial-buffer)
  423. (kill-buffer init-buf))))
  424. (defun org-publish-org-to-latex (plist filename pub-dir)
  425. "Publish an org file to LaTeX.
  426. See `org-publish-org-to' to the list of arguments."
  427. (org-publish-org-to "latex" plist filename pub-dir))
  428. (defun org-publish-org-to-html (plist filename pub-dir)
  429. "Publish an org file to HTML.
  430. See `org-publish-org-to' to the list of arguments."
  431. (org-publish-org-to "html" plist filename pub-dir))
  432. (defun org-publish-attachment (plist filename pub-dir)
  433. "Publish a file with no transformation of any kind.
  434. See `org-publish-org-to' to the list of arguments."
  435. ;; make sure eshell/cp code is loaded
  436. (eval-and-compile
  437. (require 'eshell)
  438. (require 'esh-maint)
  439. (require 'em-unix))
  440. (eshell/cp filename pub-dir))
  441. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  442. ;;; Publishing files, sets of files, and indices
  443. (defun org-publish-file (filename &optional project)
  444. "Publish file FILENAME from PROJECT."
  445. (when (org-publish-needed-p filename)
  446. (let* ((project
  447. (or project
  448. (or (org-publish-get-project-from-filename filename)
  449. (if (y-or-n-p
  450. (format "%s is not in a project. Re-read the list of projects files? "
  451. (abbreviate-file-name filename)))
  452. ;; If requested, re-initialize the list of projects files
  453. (progn (org-publish-initialize-files-alist t)
  454. (or (org-publish-get-project-from-filename filename)
  455. (error "File %s not part of any known project"
  456. (abbreviate-file-name filename))))
  457. (error "Can't publish file outside of a project")))))
  458. (project-plist (cdr project))
  459. (publishing-function
  460. (or (plist-get project-plist :publishing-function)
  461. 'org-publish-org-to-html))
  462. (base-dir (file-name-as-directory
  463. (file-truename (plist-get project-plist :base-directory))))
  464. (pub-dir (file-name-as-directory
  465. (file-truename (plist-get project-plist :publishing-directory))))
  466. tmp-pub-dir)
  467. (setq tmp-pub-dir
  468. (file-name-directory
  469. (concat pub-dir
  470. (and (string-match (regexp-quote base-dir) filename)
  471. (substring filename (match-end 0))))))
  472. (if (listp publishing-function)
  473. ;; allow chain of publishing functions
  474. (mapc (lambda (f)
  475. (funcall f project-plist filename tmp-pub-dir))
  476. publishing-function)
  477. (funcall publishing-function project-plist filename tmp-pub-dir)))
  478. (org-publish-update-timestamp filename)))
  479. (defun org-publish-projects (projects)
  480. "Publish all files belonging to the PROJECTS alist.
  481. If :auto-index is set, publish the index too."
  482. (mapc
  483. (lambda (project)
  484. (let* ((project-plist (cdr project))
  485. (exclude-regexp (plist-get project-plist :exclude))
  486. (index-p (plist-get project-plist :auto-index))
  487. (index-filename (or (plist-get project-plist :index-filename)
  488. "index.org"))
  489. (index-function (or (plist-get project-plist :index-function)
  490. 'org-publish-org-index))
  491. (preparation-function (plist-get project-plist :preparation-function))
  492. (files (org-publish-get-base-files project exclude-regexp)) file)
  493. (when preparation-function (funcall preparation-function))
  494. (if index-p (funcall index-function project index-filename))
  495. (while (setq file (pop files))
  496. (org-publish-file file project))))
  497. (org-publish-expand-projects projects)))
  498. (defun org-publish-org-index (project &optional index-filename)
  499. "Create an index of pages in set defined by PROJECT.
  500. Optionally set the filename of the index with INDEX-FILENAME.
  501. Default for INDEX-FILENAME is 'index.org'."
  502. (let* ((project-plist (cdr project))
  503. (dir (file-name-as-directory
  504. (plist-get project-plist :base-directory)))
  505. (exclude-regexp (plist-get project-plist :exclude))
  506. (files (org-publish-get-base-files project exclude-regexp))
  507. (index-filename (concat dir (or index-filename "index.org")))
  508. (index-buffer (find-buffer-visiting index-filename))
  509. (ifn (file-name-nondirectory index-filename))
  510. file)
  511. ;; if buffer is already open, kill it to prevent error message
  512. (if index-buffer
  513. (kill-buffer index-buffer))
  514. (with-temp-buffer
  515. (while (setq file (pop files))
  516. (let ((fn (file-name-nondirectory file)))
  517. ;; index shouldn't index itself
  518. (unless (string= fn ifn)
  519. (insert (concat " + [[file:" fn "]["
  520. (file-name-sans-extension fn)
  521. "]]\n")))))
  522. (write-file index-filename)
  523. (kill-buffer (current-buffer)))))
  524. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  525. ;;; Interactive publishing functions
  526. (defalias 'org-publish-project 'org-publish)
  527. ;;;###autoload
  528. (defun org-publish (project &optional force)
  529. "Publish PROJECT."
  530. (interactive "P")
  531. (setq org-publish-initial-buffer (current-buffer))
  532. (save-window-excursion
  533. (let* ((force current-prefix-arg)
  534. (org-publish-use-timestamps-flag
  535. (if force nil org-publish-use-timestamps-flag)))
  536. (org-publish-projects
  537. (list (or project
  538. (assoc (completing-read
  539. "Publish project: "
  540. org-publish-project-alist nil t)
  541. org-publish-project-alist)))))))
  542. ;;;###autoload
  543. (defun org-publish-all (&optional force)
  544. "Publish all projects.
  545. With prefix argument, force publish all files."
  546. (interactive "P")
  547. (org-publish-initialize-files-alist)
  548. (save-window-excursion
  549. (let ((org-publish-use-timestamps-flag
  550. (if force nil org-publish-use-timestamps-flag)))
  551. (org-publish-projects org-publish-project-alist))))
  552. ;;;###autoload
  553. (defun org-publish-current-file (&optional force)
  554. "Publish the current file.
  555. With prefix argument, force publish the file."
  556. (interactive "P")
  557. (org-publish-initialize-files-alist)
  558. (save-window-excursion
  559. (let ((org-publish-use-timestamps-flag
  560. (if force nil org-publish-use-timestamps-flag)))
  561. (org-publish-file (buffer-file-name)))))
  562. ;;;###autoload
  563. (defun org-publish-current-project (&optional force)
  564. "Publish the project associated with the current file.
  565. With a prefix argument, force publishing of all files in
  566. the project."
  567. (interactive "P")
  568. (org-publish-initialize-files-alist)
  569. (save-window-excursion
  570. (let ((project (org-publish-get-project-from-filename (buffer-file-name)))
  571. (org-publish-use-timestamps-flag
  572. (if force nil org-publish-use-timestamps-flag)))
  573. (if (not project)
  574. (error "File %s is not part of any known project" (buffer-file-name)))
  575. (org-publish project))))
  576. (provide 'org-publish)
  577. ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
  578. ;;; org-publish.el ends here