org-publish.el 36 KB

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