org-publish.el 31 KB

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