org-publish.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. ;;; org-publish.el --- publish related org-mode files as a website
  2. ;; Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
  3. ;; Author: David O'Toole <dto@gnu.org>
  4. ;; Maintainer: Carsten Dominik <carsten DOT dominik AT gmail DOT com>
  5. ;; Keywords: hypermedia, outlines, wp
  6. ;; Version: 6.34trans
  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. ;; This program allow configurable publishing of related sets of
  21. ;; Org-mode files as a complete website.
  22. ;;
  23. ;; org-publish.el can do the following:
  24. ;;
  25. ;; + Publish all one's org-files to HTML or PDF
  26. ;; + Upload HTML, images, attachments and other files to a web server
  27. ;; + Exclude selected private pages from publishing
  28. ;; + Publish a clickable index of pages
  29. ;; + Manage local timestamps for publishing only changed files
  30. ;; + Accept plugin functions to extend range of publishable content
  31. ;;
  32. ;; Documentation for publishing is in the manual.
  33. ;;; Code:
  34. (eval-when-compile
  35. (require 'cl))
  36. (require 'org)
  37. (require 'org-exp)
  38. (eval-and-compile
  39. (unless (fboundp 'declare-function)
  40. (defmacro declare-function (fn file &optional arglist fileonly))))
  41. (defgroup org-publish nil
  42. "Options for publishing a set of Org-mode and related files."
  43. :tag "Org Publishing"
  44. :group 'org)
  45. (defcustom org-publish-project-alist nil
  46. "Association list to control publishing behavior.
  47. Each element of the alist is a publishing 'project.' The CAR of
  48. each element is a string, uniquely identifying the project. The
  49. CDR of each element is in one of the following forms:
  50. (:property value :property value ... )
  51. OR,
  52. (:components (\"project-1\" \"project-2\" ...))
  53. When the CDR of an element of org-publish-project-alist is in
  54. this second form, the elements of the list after :components are
  55. taken to be components of the project, which group together files
  56. requiring different publishing options. When you publish such a
  57. project with \\[org-publish], the components all publish.
  58. When a property is given a value in org-publish-project-alist, its
  59. setting overrides the value of the corresponding user variable
  60. \(if any) during publishing. However, options set within a file
  61. override everything.
  62. Most properties are optional, but some should always be set:
  63. :base-directory Directory containing publishing source files
  64. :base-extension Extension (without the dot!) of source files.
  65. This can be a regular expression.
  66. :publishing-directory Directory (possibly remote) where output
  67. files will be published
  68. The :exclude property may be used to prevent certain files from
  69. being published. Its value may be a string or regexp matching
  70. file names you don't want to be published.
  71. The :include property may be used to include extra files. Its
  72. value may be a list of filenames to include. The filenames are
  73. considered relative to the base directory.
  74. When both :include and :exclude properties are given values, the
  75. exclusion step happens first.
  76. One special property controls which back-end function to use for
  77. publishing files in the project. This can be used to extend the
  78. set of file types publishable by org-publish, as well as the set
  79. of output formats.
  80. :publishing-function Function to publish file. The default is
  81. `org-publish-org-to-html', but other
  82. values are possible. May also be a
  83. list of functions, in which case
  84. each function in the list is invoked
  85. in turn.
  86. Another property allows you to insert code that prepares a
  87. project for publishing. For example, you could call GNU Make on a
  88. certain makefile, to ensure published files are built up to date.
  89. :preparation-function Function to be called before publishing
  90. this project.
  91. :completion-function Function to be called after publishing
  92. this project.
  93. Some properties control details of the Org publishing process,
  94. and are equivalent to the corresponding user variables listed in
  95. the right column. See the documentation for those variables to
  96. learn more about their use and default values.
  97. :language `org-export-default-language'
  98. :headline-levels `org-export-headline-levels'
  99. :section-numbers `org-export-with-section-numbers'
  100. :table-of-contents `org-export-with-toc'
  101. :emphasize `org-export-with-emphasize'
  102. :sub-superscript `org-export-with-sub-superscripts'
  103. :TeX-macros `org-export-with-TeX-macros'
  104. :fixed-width `org-export-with-fixed-width'
  105. :tables `org-export-with-tables'
  106. :table-auto-headline `org-export-highlight-first-table-line'
  107. :style `org-export-html-style'
  108. :convert-org-links `org-export-html-link-org-files-as-html'
  109. :inline-images `org-export-html-inline-images'
  110. :expand-quoted-html `org-export-html-expand'
  111. :timestamp `org-export-html-with-timestamp'
  112. :publishing-directory `org-export-publishing-directory'
  113. :preamble `org-export-html-preamble'
  114. :postamble `org-export-html-postamble'
  115. :auto-preamble `org-export-html-auto-preamble'
  116. :auto-postamble `org-export-html-auto-postamble'
  117. :author `user-full-name'
  118. :email `user-mail-address'
  119. The following properties may be used to control publishing of an
  120. index of files or summary page for a given project.
  121. :auto-index Whether to publish an index during
  122. `org-publish-current-project' or `org-publish-all'.
  123. :index-filename Filename for output of index. Defaults
  124. to 'sitemap.org' (which becomes 'sitemap.html').
  125. :index-title Title of index page. Defaults to name of file.
  126. :index-function Plugin function to use for generation of index.
  127. Defaults to `org-publish-org-index', which
  128. generates a plain list of links to all files
  129. in the project.
  130. :index-style Can be `list' (index is just an itemized list
  131. of the titles of the files involved) or
  132. `tree' (the directory structure of the source
  133. files is reflected in the index). Defaults to
  134. `tree'."
  135. :group 'org-publish
  136. :type 'alist)
  137. (defcustom org-publish-use-timestamps-flag t
  138. "When non-nil, use timestamp checking to publish only changed files.
  139. When nil, do no timestamp checking and always publish all files."
  140. :group 'org-publish
  141. :type 'boolean)
  142. (defcustom org-publish-timestamp-directory (convert-standard-filename
  143. "~/.org-timestamps/")
  144. "Name of directory in which to store publishing timestamps."
  145. :group 'org-publish
  146. :type 'directory)
  147. (defcustom org-publish-list-skipped-files t
  148. "Non-nil means, show message about files *not* published."
  149. :group 'org-publish
  150. :type 'boolean)
  151. (defcustom org-publish-before-export-hook nil
  152. "Hook run before export on the Org file.
  153. The hook may modify the file in arbitrary ways before publishing happens.
  154. The original version of the buffer will be restored after publishing."
  155. :group 'org-publish
  156. :type 'hook)
  157. (defcustom org-publish-after-export-hook nil
  158. "Hook run after export on the exported buffer.
  159. Any changes made by this hook will be saved."
  160. :group 'org-publish
  161. :type 'hook)
  162. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  163. ;;; Timestamp-related functions
  164. (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
  165. "Return path to timestamp file for filename FILENAME."
  166. (setq filename (concat filename "::" (or pub-dir "") "::"
  167. (format "%s" (or pub-func ""))))
  168. (concat (file-name-as-directory org-publish-timestamp-directory)
  169. "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
  170. (defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir)
  171. "Return `t' if FILENAME should be published in PUB-DIR using PUB-FUNC.
  172. TRUE-PUB-DIR is there the file will truely end up. Currently we are not using
  173. this - maybe it can eventually be used to check if the file is present at
  174. the target location, and how old it is. Right ow we cannot do this, because
  175. we do not know under what file name the file will be stored - the publishing
  176. function can still decide about that independently."
  177. (let ((rtn
  178. (if org-publish-use-timestamps-flag
  179. (if (file-exists-p org-publish-timestamp-directory)
  180. ;; first handle possible wrong timestamp directory
  181. (if (not (file-directory-p org-publish-timestamp-directory))
  182. (error "Org publish timestamp: %s is not a directory"
  183. org-publish-timestamp-directory)
  184. ;; there is a timestamp, check if FILENAME is newer
  185. (file-newer-than-file-p
  186. filename (org-publish-timestamp-filename
  187. filename pub-dir pub-func)))
  188. (make-directory org-publish-timestamp-directory)
  189. t)
  190. ;; don't use timestamps, always return t
  191. t)))
  192. (if rtn
  193. (message "Publishing file %s using `%s'" filename pub-func)
  194. (when org-publish-list-skipped-files
  195. (message "Skipping unmodified file %s" filename)))
  196. rtn))
  197. (defun org-publish-update-timestamp (filename &optional pub-dir pub-func)
  198. "Update publishing timestamp for file FILENAME.
  199. If there is no timestamp, create one."
  200. (let ((timestamp-file (org-publish-timestamp-filename
  201. filename pub-dir pub-func))
  202. newly-created-timestamp)
  203. (if (not (file-exists-p timestamp-file))
  204. ;; create timestamp file if needed
  205. (with-temp-buffer
  206. (make-directory (file-name-directory timestamp-file) t)
  207. (write-file timestamp-file)
  208. (setq newly-created-timestamp t)))
  209. ;; Emacs 21 doesn't have `set-file-times'
  210. (if (and (fboundp 'set-file-times)
  211. (not newly-created-timestamp))
  212. (set-file-times timestamp-file)
  213. (call-process "touch" nil 0 nil (expand-file-name timestamp-file)))))
  214. (defun org-publish-remove-all-timestamps ()
  215. "Remove all files in the timstamp directory."
  216. (let ((dir org-publish-timestamp-directory)
  217. files)
  218. (when (and (file-exists-p dir)
  219. (file-directory-p dir))
  220. (mapc 'delete-file (directory-files dir 'full "[^.]\\'")))))
  221. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  222. ;;; Mapping files to project names
  223. (defvar org-publish-files-alist nil
  224. "Alist of files and their parent projects.
  225. Each element of this alist is of the form:
  226. (file-name . project-name)")
  227. (defvar org-publish-initial-buffer nil
  228. "The buffer `org-publish' has been called from.")
  229. (defvar org-publish-temp-files nil
  230. "Temporary list of files to be published.")
  231. (defun org-publish-initialize-files-alist (&optional refresh)
  232. "Set `org-publish-files-alist' if it is not set.
  233. Also set it if the optional argument REFRESH is non-nil."
  234. (interactive "P")
  235. (when (or refresh (not org-publish-files-alist))
  236. (setq org-publish-files-alist
  237. (org-publish-get-files org-publish-project-alist))))
  238. (defun org-publish-validate-link (link &optional directory)
  239. "Check if LINK points to a file in the current project."
  240. (assoc (expand-file-name link directory) org-publish-files-alist))
  241. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  242. ;;; Compatibility aliases
  243. ;; Delete-dups is not in Emacs <22
  244. (if (fboundp 'delete-dups)
  245. (defalias 'org-publish-delete-dups 'delete-dups)
  246. (defun org-publish-delete-dups (list)
  247. "Destructively remove `equal' duplicates from LIST.
  248. Store the result in LIST and return it. LIST must be a proper list.
  249. Of several `equal' occurrences of an element in LIST, the first
  250. one is kept.
  251. This is a compatibility function for Emacsen without `delete-dups'."
  252. ;; Code from `subr.el' in Emacs 22:
  253. (let ((tail list))
  254. (while tail
  255. (setcdr tail (delete (car tail) (cdr tail)))
  256. (setq tail (cdr tail))))
  257. list))
  258. (declare-function org-publish-delete-dups "org-publish" (list))
  259. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  260. ;;; Getting project information out of org-publish-project-alist
  261. (defun org-publish-get-files (projects-alist &optional no-exclusion)
  262. "Return the list of all publishable files for PROJECTS-ALIST.
  263. If NO-EXCLUSION is non-nil, don't exclude files."
  264. (let (all-files)
  265. ;; add all projects
  266. (mapc
  267. (lambda(p)
  268. (let* ((exclude (plist-get (cdr p) :exclude))
  269. (files (and p (org-publish-get-base-files p exclude))))
  270. ;; add all files from this project
  271. (mapc (lambda(f)
  272. (add-to-list 'all-files
  273. (cons (expand-file-name f) (car p))))
  274. files)))
  275. (org-publish-expand-projects projects-alist))
  276. all-files))
  277. (defun org-publish-expand-projects (projects-alist)
  278. "Expand projects in PROJECTS-ALIST.
  279. This splices all the components into the list."
  280. (let ((rest projects-alist) rtn p components)
  281. (while (setq p (pop rest))
  282. (if (setq components (plist-get (cdr p) :components))
  283. (setq rest (append
  284. (mapcar (lambda (x) (assoc x org-publish-project-alist))
  285. components)
  286. rest))
  287. (push p rtn)))
  288. (nreverse (org-publish-delete-dups (delq nil rtn)))))
  289. (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
  290. "Set `org-publish-temp-files' with files from BASE-DIR directory.
  291. If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
  292. non-nil, restrict this list to the files matching the regexp
  293. MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
  294. SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
  295. matching the regexp SKIP-DIR when recursing through BASE-DIR."
  296. (mapc (lambda (f)
  297. (let ((fd-p (file-directory-p f))
  298. (fnd (file-name-nondirectory f)))
  299. (if (and fd-p recurse
  300. (not (string-match "^\\.+$" fnd))
  301. (if skip-dir (not (string-match skip-dir fnd)) t))
  302. (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
  303. (unless (or fd-p ;; this is a directory
  304. (and skip-file (string-match skip-file fnd))
  305. (not (file-exists-p (file-truename f)))
  306. (not (string-match match fnd)))
  307. (pushnew f org-publish-temp-files)))))
  308. (directory-files base-dir t (unless recurse match))))
  309. (defun org-publish-get-base-files (project &optional exclude-regexp)
  310. "Return a list of all files in PROJECT.
  311. If EXCLUDE-REGEXP is set, this will be used to filter out
  312. matching filenames."
  313. (let* ((project-plist (cdr project))
  314. (base-dir (file-name-as-directory
  315. (plist-get project-plist :base-directory)))
  316. (include-list (plist-get project-plist :include))
  317. (recurse (plist-get project-plist :recursive))
  318. (extension (or (plist-get project-plist :base-extension) "org"))
  319. (match (if (eq extension 'any)
  320. "^[^\\.]"
  321. (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
  322. (setq org-publish-temp-files nil)
  323. (org-publish-get-base-files-1 base-dir recurse match
  324. ;; FIXME distinguish exclude regexp
  325. ;; for skip-file and skip-dir?
  326. exclude-regexp exclude-regexp)
  327. (mapc (lambda (f)
  328. (pushnew
  329. (expand-file-name (concat base-dir f))
  330. org-publish-temp-files))
  331. include-list)
  332. org-publish-temp-files))
  333. (defun org-publish-get-project-from-filename (filename &optional up)
  334. "Return the project FILENAME belongs."
  335. (let* ((project-name (cdr (assoc (expand-file-name filename)
  336. org-publish-files-alist))))
  337. (when up
  338. (dolist (prj org-publish-project-alist)
  339. (if (member project-name (plist-get (cdr prj) :components))
  340. (setq project-name (car prj)))))
  341. (assoc project-name org-publish-project-alist)))
  342. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  343. ;;; Pluggable publishing back-end functions
  344. (defun org-publish-org-to (format plist filename pub-dir)
  345. "Publish an org file to FORMAT.
  346. PLIST is the property list for the given project.
  347. FILENAME is the filename of the org file to be published.
  348. PUB-DIR is the publishing directory."
  349. (require 'org)
  350. (unless (file-exists-p pub-dir)
  351. (make-directory pub-dir t))
  352. (let ((visiting (find-buffer-visiting filename)))
  353. (save-excursion
  354. (switch-to-buffer (or visiting (find-file filename)))
  355. (let* ((plist (cons :buffer-will-be-killed (cons t plist)))
  356. (init-buf (current-buffer))
  357. (init-point (point))
  358. (init-buf-string (buffer-string))
  359. export-buf-or-file)
  360. ;; run hooks before exporting
  361. (run-hooks 'org-publish-before-export-hook)
  362. ;; export the possibly modified buffer
  363. (setq export-buf-or-file
  364. (funcall (intern (concat "org-export-as-" format))
  365. (plist-get plist :headline-levels)
  366. nil plist nil nil pub-dir))
  367. (when (and (bufferp export-buf-or-file)
  368. (buffer-live-p export-buf-or-file))
  369. (set-buffer export-buf-or-file)
  370. ;; run hooks after export and save export
  371. (and (run-hooks 'org-publish-after-export-hook)
  372. (if (buffer-modified-p) (save-buffer)))
  373. (kill-buffer export-buf-or-file))
  374. ;; maybe restore buffer's content
  375. (set-buffer init-buf)
  376. (when (buffer-modified-p init-buf)
  377. (erase-buffer)
  378. (insert init-buf-string)
  379. (save-buffer)
  380. (goto-char init-point))
  381. (unless visiting
  382. (kill-buffer init-buf))))))
  383. (defun org-publish-org-to-latex (plist filename pub-dir)
  384. "Publish an org file to LaTeX.
  385. See `org-publish-org-to' to the list of arguments."
  386. (org-publish-org-to "latex" plist filename pub-dir))
  387. (defun org-publish-org-to-pdf (plist filename pub-dir)
  388. "Publish an org file to PDF (via LaTeX).
  389. See `org-publish-org-to' to the list of arguments."
  390. (org-publish-org-to "pdf" plist filename pub-dir))
  391. (defun org-publish-org-to-html (plist filename pub-dir)
  392. "Publish an org file to HTML.
  393. See `org-publish-org-to' to the list of arguments."
  394. (org-publish-org-to "html" plist filename pub-dir))
  395. (defun org-publish-org-to-org (plist filename pub-dir)
  396. "Publish an org file to HTML.
  397. See `org-publish-org-to' to the list of arguments."
  398. (org-publish-org-to "org" plist filename pub-dir))
  399. (defun org-publish-attachment (plist filename pub-dir)
  400. "Publish a file with no transformation of any kind.
  401. See `org-publish-org-to' to the list of arguments."
  402. ;; make sure eshell/cp code is loaded
  403. (unless (file-directory-p pub-dir)
  404. (make-directory pub-dir t))
  405. (or (equal (expand-file-name (file-name-directory filename))
  406. (file-name-as-directory (expand-file-name pub-dir)))
  407. (copy-file filename
  408. (expand-file-name (file-name-nondirectory filename) pub-dir)
  409. t)))
  410. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  411. ;;; Publishing files, sets of files, and indices
  412. (defun org-publish-file (filename &optional project)
  413. "Publish file FILENAME from PROJECT."
  414. (let* ((project
  415. (or project
  416. (or (org-publish-get-project-from-filename filename)
  417. (if (y-or-n-p
  418. (format "%s is not in a project. Re-read the list of projects files? "
  419. (abbreviate-file-name filename)))
  420. ;; If requested, re-initialize the list of projects files
  421. (progn (org-publish-initialize-files-alist t)
  422. (or (org-publish-get-project-from-filename filename)
  423. (error "File %s not part of any known project"
  424. (abbreviate-file-name filename))))
  425. (error "Can't publish file outside of a project")))))
  426. (project-plist (cdr project))
  427. (ftname (file-truename filename))
  428. (publishing-function
  429. (or (plist-get project-plist :publishing-function)
  430. 'org-publish-org-to-html))
  431. (base-dir (file-name-as-directory
  432. (file-truename (plist-get project-plist :base-directory))))
  433. (pub-dir (file-name-as-directory
  434. (file-truename (plist-get project-plist :publishing-directory))))
  435. tmp-pub-dir)
  436. (setq tmp-pub-dir
  437. (file-name-directory
  438. (concat pub-dir
  439. (and (string-match (regexp-quote base-dir) ftname)
  440. (substring ftname (match-end 0))))))
  441. (if (listp publishing-function)
  442. ;; allow chain of publishing functions
  443. (mapc (lambda (f)
  444. (when (org-publish-needed-p filename pub-dir f tmp-pub-dir)
  445. (funcall f project-plist filename tmp-pub-dir)
  446. (org-publish-update-timestamp filename pub-dir f)))
  447. publishing-function)
  448. (when (org-publish-needed-p filename pub-dir publishing-function
  449. tmp-pub-dir)
  450. (funcall publishing-function project-plist filename tmp-pub-dir)
  451. (org-publish-update-timestamp
  452. filename pub-dir publishing-function)))))
  453. (defun org-publish-projects (projects)
  454. "Publish all files belonging to the PROJECTS alist.
  455. If :auto-index is set, publish the index too."
  456. (mapc
  457. (lambda (project)
  458. (let*
  459. ((project-plist (cdr project))
  460. (exclude-regexp (plist-get project-plist :exclude))
  461. (index-p (plist-get project-plist :auto-index))
  462. (index-filename (or (plist-get project-plist :index-filename)
  463. "sitemap.org"))
  464. (index-function (or (plist-get project-plist :index-function)
  465. 'org-publish-org-index))
  466. (preparation-function (plist-get project-plist :preparation-function))
  467. (completion-function (plist-get project-plist :completion-function))
  468. (files (org-publish-get-base-files project exclude-regexp)) file)
  469. (when preparation-function (funcall preparation-function))
  470. (if index-p (funcall index-function project index-filename))
  471. (while (setq file (pop files))
  472. (org-publish-file file project))
  473. (when completion-function (funcall completion-function))))
  474. (org-publish-expand-projects projects)))
  475. (defun org-publish-org-index (project &optional index-filename)
  476. "Create an index of pages in set defined by PROJECT.
  477. Optionally set the filename of the index with INDEX-FILENAME.
  478. Default for INDEX-FILENAME is 'sitemap.org'."
  479. (let* ((project-plist (cdr project))
  480. (dir (file-name-as-directory
  481. (plist-get project-plist :base-directory)))
  482. (localdir (file-name-directory dir))
  483. (indent-str (make-string 2 ?\ ))
  484. (exclude-regexp (plist-get project-plist :exclude))
  485. (files (nreverse (org-publish-get-base-files project exclude-regexp)))
  486. (index-filename (concat dir (or index-filename "sitemap.org")))
  487. (index-title (or (plist-get project-plist :index-title)
  488. (concat "Index for project " (car project))))
  489. (index-style (or (plist-get project-plist :index-style)
  490. 'tree))
  491. (visiting (find-buffer-visiting index-filename))
  492. (ifn (file-name-nondirectory index-filename))
  493. file index-buffer)
  494. (with-current-buffer (setq index-buffer
  495. (or visiting (find-file index-filename)))
  496. (erase-buffer)
  497. (insert (concat "#+TITLE: " index-title "\n\n"))
  498. (while (setq file (pop files))
  499. (let ((fn (file-name-nondirectory file))
  500. (link (file-relative-name file dir))
  501. (oldlocal localdir))
  502. ;; index shouldn't index itself
  503. (unless (equal (file-truename index-filename)
  504. (file-truename file))
  505. (if (eq index-style 'list)
  506. (message "Generating list-style index for %s" index-title)
  507. (message "Generating tree-style index for %s" index-title)
  508. (setq localdir (concat (file-name-as-directory dir)
  509. (file-name-directory link)))
  510. (unless (string= localdir oldlocal)
  511. (if (string= localdir dir)
  512. (setq indent-str (make-string 2 ?\ ))
  513. (let ((subdirs
  514. (split-string
  515. (directory-file-name
  516. (file-name-directory
  517. (file-relative-name localdir dir))) "/"))
  518. (subdir "")
  519. (old-subdirs (split-string
  520. (file-relative-name oldlocal dir) "/")))
  521. (setq indent-str (make-string 2 ?\ ))
  522. (while (string= (car old-subdirs) (car subdirs))
  523. (setq indent-str (concat indent-str (make-string 2 ?\ )))
  524. (pop old-subdirs)
  525. (pop subdirs))
  526. (dolist (d subdirs)
  527. (setq subdir (concat subdir d "/"))
  528. (insert (concat indent-str " + " d "\n"))
  529. (setq indent-str (make-string
  530. (+ (length indent-str) 2) ?\ )))))))
  531. ;; This is common to 'flat and 'tree
  532. (insert (concat indent-str " + [[file:" link "]["
  533. (org-publish-find-title file)
  534. "]]\n")))))
  535. (save-buffer))
  536. (or visiting (kill-buffer index-buffer))))
  537. (defun org-publish-find-title (file)
  538. "Find the title of file in project."
  539. (let* ((visiting (find-buffer-visiting file))
  540. (buffer (or visiting (find-file-noselect file)))
  541. title)
  542. (with-current-buffer buffer
  543. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  544. (org-infile-export-plist))))
  545. (setq title
  546. (or (plist-get opt-plist :title)
  547. (and (not
  548. (plist-get opt-plist :skip-before-1st-heading))
  549. (org-export-grab-title-from-buffer))
  550. (file-name-nondirectory (file-name-sans-extension file))))))
  551. (unless visiting
  552. (kill-buffer buffer))
  553. title))
  554. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  555. ;;; Interactive publishing functions
  556. ;;;###autoload
  557. (defalias 'org-publish-project 'org-publish)
  558. ;;;###autoload
  559. (defun org-publish (project &optional force)
  560. "Publish PROJECT."
  561. (interactive
  562. (list
  563. (assoc (org-icompleting-read
  564. "Publish project: "
  565. org-publish-project-alist nil t)
  566. org-publish-project-alist)
  567. current-prefix-arg))
  568. (setq org-publish-initial-buffer (current-buffer))
  569. (save-window-excursion
  570. (let* ((org-publish-use-timestamps-flag
  571. (if force nil org-publish-use-timestamps-flag)))
  572. (org-publish-projects (list project)))))
  573. ;;;###autoload
  574. (defun org-publish-all (&optional force)
  575. "Publish all projects.
  576. With prefix argument, remove all files in the timestamp
  577. directory and force publishing all files."
  578. (interactive "P")
  579. (when force
  580. (org-publish-remove-all-timestamps))
  581. (org-publish-initialize-files-alist)
  582. (save-window-excursion
  583. (let ((org-publish-use-timestamps-flag
  584. (if force nil org-publish-use-timestamps-flag)))
  585. (org-publish-projects org-publish-project-alist))))
  586. ;;;###autoload
  587. (defun org-publish-current-file (&optional force)
  588. "Publish the current file.
  589. With prefix argument, force publish the file."
  590. (interactive "P")
  591. (org-publish-initialize-files-alist)
  592. (save-window-excursion
  593. (let ((org-publish-use-timestamps-flag
  594. (if force nil org-publish-use-timestamps-flag)))
  595. (org-publish-file (buffer-file-name)))))
  596. ;;;###autoload
  597. (defun org-publish-current-project (&optional force)
  598. "Publish the project associated with the current file.
  599. With a prefix argument, force publishing of all files in
  600. the project."
  601. (interactive "P")
  602. (org-publish-initialize-files-alist)
  603. (save-window-excursion
  604. (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up))
  605. (org-publish-use-timestamps-flag
  606. (if force nil org-publish-use-timestamps-flag)))
  607. (if (not project)
  608. (error "File %s is not part of any known project" (buffer-file-name)))
  609. (org-publish project))))
  610. (provide 'org-publish)
  611. ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
  612. ;;; org-publish.el ends here