org-publish.el 31 KB

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