org-publish.el 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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.36trans
  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. (defvar sitemap-alphabetically)
  339. (defvar sitemap-sort-folders)
  340. (defvar sitemap-ignore-case)
  341. (defun org-publish-compare-directory-files (a b)
  342. "Predicate for `sort', that sorts folders-first/last and
  343. eventually alphabetically."
  344. (let ((retval t))
  345. (when (or sitemap-alphabetically sitemap-sort-folders)
  346. ;; First we sort alphabetically:
  347. (when sitemap-alphabetically
  348. (let* ((adir (file-directory-p a))
  349. (aorg (and (string-match "\\.org$" a) (not adir)))
  350. (bdir (file-directory-p b))
  351. (borg (and (string-match "\\.org$" b) (not bdir)))
  352. (A (if aorg
  353. (concat (file-name-directory a)
  354. (org-publish-find-title a)) a))
  355. (B (if borg
  356. (concat (file-name-directory b)
  357. (org-publish-find-title b)) b)))
  358. (setq retval (if sitemap-ignore-case
  359. (not (string-lessp (upcase B) (upcase A)))
  360. (not (string-lessp B A))))))
  361. ;; Directory-wise wins:
  362. (when sitemap-sort-folders
  363. ;; a is directory, b not:
  364. (cond
  365. ((and (file-directory-p a) (not (file-directory-p b)))
  366. (setq retval (equal sitemap-sort-folders 'first)))
  367. ;; a is not a directory, but b is:
  368. ((and (not (file-directory-p a)) (file-directory-p b))
  369. (setq retval (equal sitemap-sort-folders 'last))))))
  370. retval))
  371. (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
  372. "Set `org-publish-temp-files' with files from BASE-DIR directory.
  373. If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
  374. non-nil, restrict this list to the files matching the regexp
  375. MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
  376. SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
  377. matching the regexp SKIP-DIR when recursing through BASE-DIR."
  378. (mapc (lambda (f)
  379. (let ((fd-p (file-directory-p f))
  380. (fnd (file-name-nondirectory f)))
  381. (if (and fd-p recurse
  382. (not (string-match "^\\.+$" fnd))
  383. (if skip-dir (not (string-match skip-dir fnd)) t))
  384. (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
  385. (unless (or fd-p ;; this is a directory
  386. (and skip-file (string-match skip-file fnd))
  387. (not (file-exists-p (file-truename f)))
  388. (not (string-match match fnd)))
  389. (pushnew f org-publish-temp-files)))))
  390. (sort (directory-files base-dir t (unless recurse match))
  391. 'org-publish-compare-directory-files)))
  392. (defun org-publish-get-base-files (project &optional exclude-regexp)
  393. "Return a list of all files in PROJECT.
  394. If EXCLUDE-REGEXP is set, this will be used to filter out
  395. matching filenames."
  396. (let* ((project-plist (cdr project))
  397. (base-dir (file-name-as-directory
  398. (plist-get project-plist :base-directory)))
  399. (include-list (plist-get project-plist :include))
  400. (recurse (plist-get project-plist :recursive))
  401. (extension (or (plist-get project-plist :base-extension) "org"))
  402. ;; sitemap-... variables are dynamically scoped for
  403. ;; org-publish-compare-directory-files:
  404. (sitemap-sort-folders
  405. (if (plist-member project-plist :sitemap-sort-folders)
  406. (plist-get project-plist :sitemap-sort-folders)
  407. org-publish-sitemap-sort-folders))
  408. (sitemap-alphabetically
  409. (if (plist-member project-plist :sitemap-alphabetically)
  410. (plist-get project-plist :sitemap-alphabetically)
  411. org-publish-sitemap-sort-alphabetically))
  412. (sitemap-ignore-case
  413. (if (plist-member project-plist :sitemap-ignore-case)
  414. (plist-get project-plist :sitemap-ignore-case)
  415. org-publish-sitemap-sort-ignore-case))
  416. (match (if (eq extension 'any)
  417. "^[^\\.]"
  418. (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
  419. ;; Make sure sitemap-sort-folders' has an accepted value
  420. (unless (memq sitemap-sort-folders '(first last))
  421. (setq sitemap-sort-folders nil))
  422. (setq org-publish-temp-files nil)
  423. (org-publish-get-base-files-1 base-dir recurse match
  424. ;; FIXME distinguish exclude regexp
  425. ;; for skip-file and skip-dir?
  426. exclude-regexp exclude-regexp)
  427. (mapc (lambda (f)
  428. (pushnew
  429. (expand-file-name (concat base-dir f))
  430. org-publish-temp-files))
  431. include-list)
  432. org-publish-temp-files))
  433. (defun org-publish-get-project-from-filename (filename &optional up)
  434. "Return the project FILENAME belongs."
  435. (let* ((project-name (cdr (assoc (expand-file-name filename)
  436. org-publish-files-alist))))
  437. (when up
  438. (dolist (prj org-publish-project-alist)
  439. (if (member project-name (plist-get (cdr prj) :components))
  440. (setq project-name (car prj)))))
  441. (assoc project-name org-publish-project-alist)))
  442. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  443. ;;; Pluggable publishing back-end functions
  444. (defun org-publish-org-to (format plist filename pub-dir)
  445. "Publish an org file to FORMAT.
  446. PLIST is the property list for the given project.
  447. FILENAME is the filename of the org file to be published.
  448. PUB-DIR is the publishing directory."
  449. (require 'org)
  450. (unless (file-exists-p pub-dir)
  451. (make-directory pub-dir t))
  452. (let ((visiting (find-buffer-visiting filename)))
  453. (save-excursion
  454. (switch-to-buffer (or visiting (find-file filename)))
  455. (let* ((plist (cons :buffer-will-be-killed (cons t plist)))
  456. (init-buf (current-buffer))
  457. (init-point (point))
  458. (init-buf-string (buffer-string))
  459. export-buf-or-file)
  460. ;; run hooks before exporting
  461. (run-hooks 'org-publish-before-export-hook)
  462. ;; export the possibly modified buffer
  463. (setq export-buf-or-file
  464. (funcall (intern (concat "org-export-as-" format))
  465. (plist-get plist :headline-levels)
  466. nil plist nil nil pub-dir))
  467. (when (and (bufferp export-buf-or-file)
  468. (buffer-live-p export-buf-or-file))
  469. (set-buffer export-buf-or-file)
  470. ;; run hooks after export and save export
  471. (progn (run-hooks 'org-publish-after-export-hook)
  472. (if (buffer-modified-p) (save-buffer)))
  473. (kill-buffer export-buf-or-file))
  474. ;; maybe restore buffer's content
  475. (set-buffer init-buf)
  476. (when (buffer-modified-p init-buf)
  477. (erase-buffer)
  478. (insert init-buf-string)
  479. (save-buffer)
  480. (goto-char init-point))
  481. (unless visiting
  482. (kill-buffer init-buf))))))
  483. (defmacro org-publish-with-aux-preprocess-maybe (&rest body)
  484. "Execute BODY with a modified hook to preprocess for index."
  485. `(let ((org-export-preprocess-after-headline-targets-hook
  486. (if (plist-get project-plist :makeindex)
  487. (cons 'org-publish-aux-preprocess
  488. org-export-preprocess-after-headline-targets-hook)
  489. org-export-preprocess-after-headline-targets-hook)))
  490. ,@body))
  491. (defvar project-plist)
  492. (defun org-publish-org-to-latex (plist filename pub-dir)
  493. "Publish an org file to LaTeX.
  494. See `org-publish-org-to' to the list of arguments."
  495. (org-publish-with-aux-preprocess-maybe
  496. (org-publish-org-to "latex" plist filename pub-dir)))
  497. (defun org-publish-org-to-pdf (plist filename pub-dir)
  498. "Publish an org file to PDF (via LaTeX).
  499. See `org-publish-org-to' to the list of arguments."
  500. (org-publish-with-aux-preprocess-maybe
  501. (org-publish-org-to "pdf" plist filename pub-dir)))
  502. (defun org-publish-org-to-html (plist filename pub-dir)
  503. "Publish an org file to HTML.
  504. See `org-publish-org-to' to the list of arguments."
  505. (org-publish-with-aux-preprocess-maybe
  506. (org-publish-org-to "html" plist filename pub-dir)))
  507. (defun org-publish-org-to-org (plist filename pub-dir)
  508. "Publish an org file to HTML.
  509. See `org-publish-org-to' to the list of arguments."
  510. (org-publish-org-to "org" plist filename pub-dir))
  511. (defun org-publish-attachment (plist filename pub-dir)
  512. "Publish a file with no transformation of any kind.
  513. See `org-publish-org-to' to the list of arguments."
  514. ;; make sure eshell/cp code is loaded
  515. (unless (file-directory-p pub-dir)
  516. (make-directory pub-dir t))
  517. (or (equal (expand-file-name (file-name-directory filename))
  518. (file-name-as-directory (expand-file-name pub-dir)))
  519. (copy-file filename
  520. (expand-file-name (file-name-nondirectory filename) pub-dir)
  521. t)))
  522. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  523. ;;; Publishing files, sets of files, and indices
  524. (defun org-publish-file (filename &optional project)
  525. "Publish file FILENAME from PROJECT."
  526. (let* ((project
  527. (or project
  528. (or (org-publish-get-project-from-filename filename)
  529. (if (y-or-n-p
  530. (format "%s is not in a project. Re-read the list of projects files? "
  531. (abbreviate-file-name filename)))
  532. ;; If requested, re-initialize the list of projects files
  533. (progn (org-publish-initialize-files-alist t)
  534. (or (org-publish-get-project-from-filename filename)
  535. (error "File %s not part of any known project"
  536. (abbreviate-file-name filename))))
  537. (error "Can't publish file outside of a project")))))
  538. (project-plist (cdr project))
  539. (ftname (file-truename filename))
  540. (publishing-function
  541. (or (plist-get project-plist :publishing-function)
  542. 'org-publish-org-to-html))
  543. (base-dir (file-name-as-directory
  544. (file-truename (plist-get project-plist :base-directory))))
  545. (pub-dir (file-name-as-directory
  546. (file-truename (plist-get project-plist :publishing-directory))))
  547. tmp-pub-dir)
  548. (setq tmp-pub-dir
  549. (file-name-directory
  550. (concat pub-dir
  551. (and (string-match (regexp-quote base-dir) ftname)
  552. (substring ftname (match-end 0))))))
  553. (if (listp publishing-function)
  554. ;; allow chain of publishing functions
  555. (mapc (lambda (f)
  556. (when (org-publish-needed-p filename pub-dir f tmp-pub-dir)
  557. (funcall f project-plist filename tmp-pub-dir)
  558. (org-publish-update-timestamp filename pub-dir f)))
  559. publishing-function)
  560. (when (org-publish-needed-p filename pub-dir publishing-function
  561. tmp-pub-dir)
  562. (funcall publishing-function project-plist filename tmp-pub-dir)
  563. (org-publish-update-timestamp
  564. filename pub-dir publishing-function)))))
  565. (defun org-publish-projects (projects)
  566. "Publish all files belonging to the PROJECTS alist.
  567. If :auto-sitemap is set, publish the sitemap too.
  568. If :makeindex is set, also produce a file theindex.org."
  569. (mapc
  570. (lambda (project)
  571. (let*
  572. ((project-plist (cdr project))
  573. (exclude-regexp (plist-get project-plist :exclude))
  574. (sitemap-p (plist-get project-plist :auto-sitemap))
  575. (sitemap-filename (or (plist-get project-plist :sitemap-filename)
  576. "sitemap.org"))
  577. (sitemap-function (or (plist-get project-plist :sitemap-function)
  578. 'org-publish-org-sitemap))
  579. (preparation-function (plist-get project-plist :preparation-function))
  580. (completion-function (plist-get project-plist :completion-function))
  581. (files (org-publish-get-base-files project exclude-regexp)) file)
  582. (when preparation-function (run-hooks 'preparation-function))
  583. (if sitemap-p (funcall sitemap-function project sitemap-filename))
  584. (while (setq file (pop files))
  585. (org-publish-file file project))
  586. (when (plist-get project-plist :makeindex)
  587. (org-publish-index-generate-theindex.inc
  588. (plist-get project-plist :base-directory))
  589. (org-publish-file (expand-file-name
  590. "theindex.org"
  591. (plist-get project-plist :base-directory))
  592. project))
  593. (when completion-function (run-hooks 'completion-function))))
  594. (org-publish-expand-projects projects)))
  595. (defun org-publish-org-sitemap (project &optional sitemap-filename)
  596. "Create an sitemap of pages in set defined by PROJECT.
  597. Optionally set the filename of the sitemap with SITEMAP-FILENAME.
  598. Default for SITEMAP-FILENAME is 'sitemap.org'."
  599. (let* ((project-plist (cdr project))
  600. (dir (file-name-as-directory
  601. (plist-get project-plist :base-directory)))
  602. (localdir (file-name-directory dir))
  603. (indent-str (make-string 2 ?\ ))
  604. (exclude-regexp (plist-get project-plist :exclude))
  605. (files (nreverse (org-publish-get-base-files project exclude-regexp)))
  606. (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
  607. (sitemap-title (or (plist-get project-plist :sitemap-title)
  608. (concat "Sitemap for project " (car project))))
  609. (sitemap-style (or (plist-get project-plist :sitemap-style)
  610. 'tree))
  611. (visiting (find-buffer-visiting sitemap-filename))
  612. (ifn (file-name-nondirectory sitemap-filename))
  613. file sitemap-buffer)
  614. (with-current-buffer (setq sitemap-buffer
  615. (or visiting (find-file sitemap-filename)))
  616. (erase-buffer)
  617. (insert (concat "#+TITLE: " sitemap-title "\n\n"))
  618. (while (setq file (pop files))
  619. (let ((fn (file-name-nondirectory file))
  620. (link (file-relative-name file dir))
  621. (oldlocal localdir))
  622. ;; sitemap shouldn't list itself
  623. (unless (equal (file-truename sitemap-filename)
  624. (file-truename file))
  625. (if (eq sitemap-style 'list)
  626. (message "Generating list-style sitemap for %s" sitemap-title)
  627. (message "Generating tree-style sitemap for %s" sitemap-title)
  628. (setq localdir (concat (file-name-as-directory dir)
  629. (file-name-directory link)))
  630. (unless (string= localdir oldlocal)
  631. (if (string= localdir dir)
  632. (setq indent-str (make-string 2 ?\ ))
  633. (let ((subdirs
  634. (split-string
  635. (directory-file-name
  636. (file-name-directory
  637. (file-relative-name localdir dir))) "/"))
  638. (subdir "")
  639. (old-subdirs (split-string
  640. (file-relative-name oldlocal dir) "/")))
  641. (setq indent-str (make-string 2 ?\ ))
  642. (while (string= (car old-subdirs) (car subdirs))
  643. (setq indent-str (concat indent-str (make-string 2 ?\ )))
  644. (pop old-subdirs)
  645. (pop subdirs))
  646. (dolist (d subdirs)
  647. (setq subdir (concat subdir d "/"))
  648. (insert (concat indent-str " + " d "\n"))
  649. (setq indent-str (make-string
  650. (+ (length indent-str) 2) ?\ )))))))
  651. ;; This is common to 'flat and 'tree
  652. (insert (concat indent-str " + [[file:" link "]["
  653. (org-publish-find-title file)
  654. "]]\n")))))
  655. (save-buffer))
  656. (or visiting (kill-buffer sitemap-buffer))))
  657. (defun org-publish-find-title (file)
  658. "Find the title of file in project."
  659. (if (member file org-publish-file-title-cache)
  660. (cadr (member file org-publish-file-title-cache))
  661. (let* ((visiting (find-buffer-visiting file))
  662. (buffer (or visiting (find-file-noselect file)))
  663. title)
  664. (with-current-buffer buffer
  665. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  666. (org-infile-export-plist))))
  667. (setq title
  668. (or (plist-get opt-plist :title)
  669. (and (not
  670. (plist-get opt-plist :skip-before-1st-heading))
  671. (org-export-grab-title-from-buffer))
  672. (file-name-nondirectory (file-name-sans-extension file))))))
  673. (unless visiting
  674. (kill-buffer buffer))
  675. (setq org-publish-file-title-cache
  676. (append org-publish-file-title-cache (list file title)))
  677. title)))
  678. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  679. ;;; Interactive publishing functions
  680. ;;;###autoload
  681. (defalias 'org-publish-project 'org-publish)
  682. ;;;###autoload
  683. (defun org-publish (project &optional force)
  684. "Publish PROJECT."
  685. (interactive
  686. (list
  687. (assoc (org-icompleting-read
  688. "Publish project: "
  689. org-publish-project-alist nil t)
  690. org-publish-project-alist)
  691. current-prefix-arg))
  692. (setq org-publish-initial-buffer (current-buffer))
  693. (save-window-excursion
  694. (let* ((org-publish-use-timestamps-flag
  695. (if force nil org-publish-use-timestamps-flag)))
  696. (org-publish-projects (list project)))))
  697. ;;;###autoload
  698. (defun org-publish-all (&optional force)
  699. "Publish all projects.
  700. With prefix argument, remove all files in the timestamp
  701. directory and force publishing all files."
  702. (interactive "P")
  703. (when force
  704. (org-publish-remove-all-timestamps))
  705. (org-publish-initialize-files-alist force)
  706. (save-window-excursion
  707. (let ((org-publish-use-timestamps-flag
  708. (if force nil org-publish-use-timestamps-flag)))
  709. (org-publish-projects org-publish-project-alist))))
  710. ;;;###autoload
  711. (defun org-publish-current-file (&optional force)
  712. "Publish the current file.
  713. With prefix argument, force publish the file."
  714. (interactive "P")
  715. (org-publish-initialize-files-alist force)
  716. (save-window-excursion
  717. (let ((org-publish-use-timestamps-flag
  718. (if force nil org-publish-use-timestamps-flag)))
  719. (org-publish-file (buffer-file-name)))))
  720. ;;;###autoload
  721. (defun org-publish-current-project (&optional force)
  722. "Publish the project associated with the current file.
  723. With a prefix argument, force publishing of all files in
  724. the project."
  725. (interactive "P")
  726. (org-publish-initialize-files-alist force)
  727. (save-window-excursion
  728. (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up))
  729. (org-publish-use-timestamps-flag
  730. (if force nil org-publish-use-timestamps-flag)))
  731. (if (not project)
  732. (error "File %s is not part of any known project" (buffer-file-name)))
  733. (org-publish project))))
  734. ;;; Index generation
  735. (defvar backend) ; dynamically scoped
  736. (defun org-publish-aux-preprocess ()
  737. "Find index entries and write them to an .orgx file."
  738. (let ((case-fold-search t)
  739. entry index target)
  740. (goto-char (point-min))
  741. (while
  742. (and
  743. (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t)
  744. (> (match-end 1) (match-beginning 1)))
  745. (setq entry (match-string 1))
  746. (when (eq backend 'latex)
  747. (replace-match (format "\\index{%s}" entry) t t))
  748. (save-excursion
  749. (ignore-errors (org-back-to-heading t))
  750. (setq target (get-text-property (point) 'target))
  751. (setq target (or (cdr (assoc target org-export-preferred-target-alist))
  752. (cdr (assoc target org-export-id-target-alist))
  753. target ""))
  754. (push (cons entry target) index)))
  755. (with-temp-file
  756. (concat (file-name-sans-extension org-current-export-file) ".orgx")
  757. (dolist (entry (nreverse index))
  758. (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
  759. (defun org-publish-index-generate-theindex.inc (directory)
  760. "Generate the index from all .orgx files in the current directory and below."
  761. (require 'find-lisp)
  762. (let* ((fulldir (file-name-as-directory
  763. (expand-file-name directory)))
  764. (full-files (find-lisp-find-files directory "\\.orgx\\'"))
  765. (re (concat "\\`" fulldir))
  766. (files (mapcar (lambda (f) (if (string-match re f)
  767. (substring f (match-end 0))
  768. f))
  769. full-files))
  770. (default-directory directory)
  771. index origfile buf target entry ibuffer
  772. main last-main letter last-letter file sub link tgext)
  773. ;; `files' contains the list of relative file names
  774. (dolist (file files)
  775. (setq origfile (substring file 0 -1))
  776. (setq buf (find-file-noselect file))
  777. (with-current-buffer buf
  778. (goto-char (point-min))
  779. (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
  780. (setq target (match-string 1)
  781. entry (match-string 2))
  782. (push (list entry origfile target) index)))
  783. (kill-buffer buf))
  784. (setq index (sort index (lambda (a b) (string< (downcase (car a))
  785. (downcase (car b))))))
  786. (setq ibuffer (find-file-noselect (expand-file-name "theindex.inc" directory)))
  787. (with-current-buffer ibuffer
  788. (erase-buffer)
  789. (insert "* Index\n")
  790. (setq last-letter nil)
  791. (dolist (idx index)
  792. (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
  793. (if (and (stringp target) (string-match "\\S-" target))
  794. (setq tgext (concat "::#" target))
  795. (setq tgext ""))
  796. (setq letter (upcase (substring entry 0 1)))
  797. (when (not (equal letter last-letter))
  798. (insert "** " letter "\n")
  799. (setq last-letter letter))
  800. (if (string-match "!" entry)
  801. (setq main (substring entry 0 (match-beginning 0))
  802. sub (substring entry (match-end 0)))
  803. (setq main nil sub nil last-main nil))
  804. (when (and main (not (equal main last-main)))
  805. (insert " - " main "\n")
  806. (setq last-main main))
  807. (setq link (concat "[[file:" file tgext "]"
  808. "[" (or sub entry) "]]"))
  809. (if (and main sub)
  810. (insert " - " link "\n")
  811. (insert " - " link "\n")))
  812. (save-buffer))
  813. (kill-buffer ibuffer)
  814. (let ((index-file (expand-file-name "theindex.org" directory)))
  815. (unless (file-exists-p index-file)
  816. (setq ibuffer (find-file-noselect index-file))
  817. (with-current-buffer ibuffer
  818. (erase-buffer)
  819. (insert "\n\n#+include: \"theindex.inc\"\n\n")
  820. (save-buffer))
  821. (kill-buffer ibuffer)))))
  822. (provide 'org-publish)
  823. ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
  824. ;;; org-publish.el ends here