org-publish.el 29 KB

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