org-publish.el 28 KB

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