org-publish.el 26 KB

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