org-publish.el 29 KB

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