org-publish.el 31 KB

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