org-publish.el 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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-sensitively? 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. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  185. ;;; Timestamp-related functions
  186. (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
  187. "Return path to timestamp file for filename FILENAME."
  188. (setq filename (concat filename "::" (or pub-dir "") "::"
  189. (format "%s" (or pub-func ""))))
  190. (concat (file-name-as-directory org-publish-timestamp-directory)
  191. "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
  192. (defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir)
  193. "Return `t' if FILENAME should be published in PUB-DIR using PUB-FUNC.
  194. TRUE-PUB-DIR is there the file will truely end up. Currently we are not using
  195. this - maybe it can eventually be used to check if the file is present at
  196. the target location, and how old it is. Right ow we cannot do this, because
  197. we do not know under what file name the file will be stored - the publishing
  198. function can still decide about that independently."
  199. (let ((rtn
  200. (if org-publish-use-timestamps-flag
  201. (if (file-exists-p org-publish-timestamp-directory)
  202. ;; first handle possible wrong timestamp directory
  203. (if (not (file-directory-p org-publish-timestamp-directory))
  204. (error "Org publish timestamp: %s is not a directory"
  205. org-publish-timestamp-directory)
  206. ;; there is a timestamp, check if FILENAME is newer
  207. (file-newer-than-file-p
  208. filename (org-publish-timestamp-filename
  209. filename pub-dir pub-func)))
  210. (make-directory org-publish-timestamp-directory)
  211. t)
  212. ;; don't use timestamps, always return t
  213. t)))
  214. (if rtn
  215. (message "Publishing file %s using `%s'" filename pub-func)
  216. (when org-publish-list-skipped-files
  217. (message "Skipping unmodified file %s" filename)))
  218. rtn))
  219. (defun org-publish-update-timestamp (filename &optional pub-dir pub-func)
  220. "Update publishing timestamp for file FILENAME.
  221. If there is no timestamp, create one."
  222. (let ((timestamp-file (org-publish-timestamp-filename
  223. filename pub-dir pub-func))
  224. newly-created-timestamp)
  225. (if (not (file-exists-p timestamp-file))
  226. ;; create timestamp file if needed
  227. (with-temp-buffer
  228. (make-directory (file-name-directory timestamp-file) t)
  229. (write-file timestamp-file)
  230. (setq newly-created-timestamp t)))
  231. ;; Emacs 21 doesn't have `set-file-times'
  232. (if (and (fboundp 'set-file-times)
  233. (not newly-created-timestamp))
  234. (set-file-times timestamp-file)
  235. (call-process "touch" nil 0 nil (expand-file-name timestamp-file)))))
  236. (defun org-publish-remove-all-timestamps ()
  237. "Remove all files in the timstamp directory."
  238. (let ((dir org-publish-timestamp-directory)
  239. files)
  240. (when (and (file-exists-p dir)
  241. (file-directory-p dir))
  242. (mapc 'delete-file (directory-files dir 'full "[^.]\\'")))))
  243. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  244. ;;; Mapping files to project names
  245. (defvar org-publish-files-alist nil
  246. "Alist of files and their parent projects.
  247. Each element of this alist is of the form:
  248. (file-name . project-name)")
  249. (defvar org-publish-initial-buffer nil
  250. "The buffer `org-publish' has been called from.")
  251. (defvar org-publish-temp-files nil
  252. "Temporary list of files to be published.")
  253. ;; Here, so you find the variable right before it's used the first time:
  254. (defvar org-publish-file-title-cache nil
  255. "List of absolute filenames and titles.")
  256. (defun org-publish-initialize-files-alist (&optional refresh)
  257. "Set `org-publish-files-alist' if it is not set.
  258. Also set it if the optional argument REFRESH is non-nil."
  259. (interactive "P")
  260. (when (or refresh (not org-publish-files-alist))
  261. (setq org-publish-file-title-cache nil)
  262. (setq org-publish-files-alist
  263. (org-publish-get-files org-publish-project-alist))))
  264. (defun org-publish-validate-link (link &optional directory)
  265. "Check if LINK points to a file in the current project."
  266. (assoc (expand-file-name link directory) org-publish-files-alist))
  267. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  268. ;;; Compatibility aliases
  269. ;; Delete-dups is not in Emacs <22
  270. (if (fboundp 'delete-dups)
  271. (defalias 'org-publish-delete-dups 'delete-dups)
  272. (defun org-publish-delete-dups (list)
  273. "Destructively remove `equal' duplicates from LIST.
  274. Store the result in LIST and return it. LIST must be a proper list.
  275. Of several `equal' occurrences of an element in LIST, the first
  276. one is kept.
  277. This is a compatibility function for Emacsen without `delete-dups'."
  278. ;; Code from `subr.el' in Emacs 22:
  279. (let ((tail list))
  280. (while tail
  281. (setcdr tail (delete (car tail) (cdr tail)))
  282. (setq tail (cdr tail))))
  283. list))
  284. (declare-function org-publish-delete-dups "org-publish" (list))
  285. (declare-function find-lisp-find-files "find-lisp" (directory regexp))
  286. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  287. ;;; Getting project information out of org-publish-project-alist
  288. (defun org-publish-get-files (projects-alist &optional no-exclusion)
  289. "Return the list of all publishable files for PROJECTS-ALIST.
  290. If NO-EXCLUSION is non-nil, don't exclude files."
  291. (let (all-files)
  292. ;; add all projects
  293. (mapc
  294. (lambda(p)
  295. (let* ((exclude (plist-get (cdr p) :exclude))
  296. (files (and p (org-publish-get-base-files p exclude))))
  297. ;; add all files from this project
  298. (mapc (lambda(f)
  299. (add-to-list 'all-files
  300. (cons (expand-file-name f) (car p))))
  301. files)))
  302. (org-publish-expand-projects projects-alist))
  303. all-files))
  304. (defun org-publish-expand-projects (projects-alist)
  305. "Expand projects in PROJECTS-ALIST.
  306. This splices all the components into the list."
  307. (let ((rest projects-alist) rtn p components)
  308. (while (setq p (pop rest))
  309. (if (setq components (plist-get (cdr p) :components))
  310. (setq rest (append
  311. (mapcar (lambda (x) (assoc x org-publish-project-alist))
  312. components)
  313. rest))
  314. (push p rtn)))
  315. (nreverse (org-publish-delete-dups (delq nil rtn)))))
  316. (defvar sitemap-alphabetically)
  317. (defvar sitemap-sort-folders)
  318. (defvar sitemap-ignore-case)
  319. (defun org-publish-compare-directory-files (a b)
  320. "Predicate for `sort', that sorts folders-first/last and
  321. eventually alphabetically."
  322. (let ((retval t))
  323. (when (or sitemap-alphabetically sitemap-sort-folders)
  324. ;; First we sort alphabetically:
  325. (when sitemap-alphabetically
  326. (let* ((adir (file-directory-p a))
  327. (aorg (and (string-match "\\.org$" a) (not adir)))
  328. (bdir (file-directory-p b))
  329. (borg (and (string-match "\\.org$" b) (not bdir)))
  330. (A (if aorg
  331. (concat (file-name-directory a)
  332. (org-publish-find-title a)) a))
  333. (B (if borg
  334. (concat (file-name-directory b)
  335. (org-publish-find-title b)) b)))
  336. (setq retval (if sitemap-ignore-case
  337. (not (string-lessp (upcase B) (upcase A)))
  338. (not (string-lessp B A))))))
  339. ;; Directory-wise wins:
  340. (when sitemap-sort-folders
  341. ;; a is directory, b not:
  342. (cond
  343. ((and (file-directory-p a) (not (file-directory-p b)))
  344. (setq retval (equal sitemap-sort-folders 'first)))
  345. ;; a is not a directory, but b is:
  346. ((and (not (file-directory-p a)) (file-directory-p b))
  347. (setq retval (equal sitemap-sort-folders 'last))))))
  348. retval))
  349. (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
  350. "Set `org-publish-temp-files' with files from BASE-DIR directory.
  351. If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
  352. non-nil, restrict this list to the files matching the regexp
  353. MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
  354. SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
  355. matching the regexp SKIP-DIR when recursing through BASE-DIR."
  356. (mapc (lambda (f)
  357. (let ((fd-p (file-directory-p f))
  358. (fnd (file-name-nondirectory f)))
  359. (if (and fd-p recurse
  360. (not (string-match "^\\.+$" fnd))
  361. (if skip-dir (not (string-match skip-dir fnd)) t))
  362. (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
  363. (unless (or fd-p ;; this is a directory
  364. (and skip-file (string-match skip-file fnd))
  365. (not (file-exists-p (file-truename f)))
  366. (not (string-match match fnd)))
  367. (pushnew f org-publish-temp-files)))))
  368. (sort (directory-files base-dir t (unless recurse match))
  369. 'org-publish-compare-directory-files)))
  370. (defun org-publish-get-base-files (project &optional exclude-regexp)
  371. "Return a list of all files in PROJECT.
  372. If EXCLUDE-REGEXP is set, this will be used to filter out
  373. matching filenames."
  374. (let* ((project-plist (cdr project))
  375. (base-dir (file-name-as-directory
  376. (plist-get project-plist :base-directory)))
  377. (include-list (plist-get project-plist :include))
  378. (recurse (plist-get project-plist :recursive))
  379. (extension (or (plist-get project-plist :base-extension) "org"))
  380. (match (if (eq extension 'any)
  381. "^[^\\.]"
  382. (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
  383. (setq org-publish-temp-files nil)
  384. (org-publish-get-base-files-1 base-dir recurse match
  385. ;; FIXME distinguish exclude regexp
  386. ;; for skip-file and skip-dir?
  387. exclude-regexp exclude-regexp)
  388. (mapc (lambda (f)
  389. (pushnew
  390. (expand-file-name (concat base-dir f))
  391. org-publish-temp-files))
  392. include-list)
  393. org-publish-temp-files))
  394. (defun org-publish-get-project-from-filename (filename &optional up)
  395. "Return the project FILENAME belongs."
  396. (let* ((project-name (cdr (assoc (expand-file-name filename)
  397. org-publish-files-alist))))
  398. (when up
  399. (dolist (prj org-publish-project-alist)
  400. (if (member project-name (plist-get (cdr prj) :components))
  401. (setq project-name (car prj)))))
  402. (assoc project-name org-publish-project-alist)))
  403. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  404. ;;; Pluggable publishing back-end functions
  405. (defun org-publish-org-to (format plist filename pub-dir)
  406. "Publish an org file to FORMAT.
  407. PLIST is the property list for the given project.
  408. FILENAME is the filename of the org file to be published.
  409. PUB-DIR is the publishing directory."
  410. (require 'org)
  411. (unless (file-exists-p pub-dir)
  412. (make-directory pub-dir t))
  413. (let ((visiting (find-buffer-visiting filename)))
  414. (save-excursion
  415. (switch-to-buffer (or visiting (find-file filename)))
  416. (let* ((plist (cons :buffer-will-be-killed (cons t plist)))
  417. (init-buf (current-buffer))
  418. (init-point (point))
  419. (init-buf-string (buffer-string))
  420. export-buf-or-file)
  421. ;; run hooks before exporting
  422. (run-hooks 'org-publish-before-export-hook)
  423. ;; export the possibly modified buffer
  424. (setq export-buf-or-file
  425. (funcall (intern (concat "org-export-as-" format))
  426. (plist-get plist :headline-levels)
  427. nil plist nil nil pub-dir))
  428. (when (and (bufferp export-buf-or-file)
  429. (buffer-live-p export-buf-or-file))
  430. (set-buffer export-buf-or-file)
  431. ;; run hooks after export and save export
  432. (progn (run-hooks 'org-publish-after-export-hook)
  433. (if (buffer-modified-p) (save-buffer)))
  434. (kill-buffer export-buf-or-file))
  435. ;; maybe restore buffer's content
  436. (set-buffer init-buf)
  437. (when (buffer-modified-p init-buf)
  438. (erase-buffer)
  439. (insert init-buf-string)
  440. (save-buffer)
  441. (goto-char init-point))
  442. (unless visiting
  443. (kill-buffer init-buf))))))
  444. (defmacro org-publish-with-aux-preprocess-maybe (&rest body)
  445. "Execute BODY with a modified hook to preprocess for index."
  446. `(let ((org-export-preprocess-after-headline-targets-hook
  447. (if (plist-get project-plist :makeindex)
  448. (cons 'org-publish-aux-preprocess
  449. org-export-preprocess-after-headline-targets-hook)
  450. org-export-preprocess-after-headline-targets-hook)))
  451. ,@body))
  452. (defvar project-plist)
  453. (defun org-publish-org-to-latex (plist filename pub-dir)
  454. "Publish an org file to LaTeX.
  455. See `org-publish-org-to' to the list of arguments."
  456. (org-publish-with-aux-preprocess-maybe
  457. (org-publish-org-to "latex" plist filename pub-dir)))
  458. (defun org-publish-org-to-pdf (plist filename pub-dir)
  459. "Publish an org file to PDF (via LaTeX).
  460. See `org-publish-org-to' to the list of arguments."
  461. (org-publish-with-aux-preprocess-maybe
  462. (org-publish-org-to "pdf" plist filename pub-dir)))
  463. (defun org-publish-org-to-html (plist filename pub-dir)
  464. "Publish an org file to HTML.
  465. See `org-publish-org-to' to the list of arguments."
  466. (org-publish-with-aux-preprocess-maybe
  467. (org-publish-org-to "html" plist filename pub-dir)))
  468. (defun org-publish-org-to-org (plist filename pub-dir)
  469. "Publish an org file to HTML.
  470. See `org-publish-org-to' to the list of arguments."
  471. (org-publish-org-to "org" plist filename pub-dir))
  472. (defun org-publish-attachment (plist filename pub-dir)
  473. "Publish a file with no transformation of any kind.
  474. See `org-publish-org-to' to the list of arguments."
  475. ;; make sure eshell/cp code is loaded
  476. (unless (file-directory-p pub-dir)
  477. (make-directory pub-dir t))
  478. (or (equal (expand-file-name (file-name-directory filename))
  479. (file-name-as-directory (expand-file-name pub-dir)))
  480. (copy-file filename
  481. (expand-file-name (file-name-nondirectory filename) pub-dir)
  482. t)))
  483. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  484. ;;; Publishing files, sets of files, and indices
  485. (defun org-publish-file (filename &optional project)
  486. "Publish file FILENAME from PROJECT."
  487. (let* ((project
  488. (or project
  489. (or (org-publish-get-project-from-filename filename)
  490. (if (y-or-n-p
  491. (format "%s is not in a project. Re-read the list of projects files? "
  492. (abbreviate-file-name filename)))
  493. ;; If requested, re-initialize the list of projects files
  494. (progn (org-publish-initialize-files-alist t)
  495. (or (org-publish-get-project-from-filename filename)
  496. (error "File %s not part of any known project"
  497. (abbreviate-file-name filename))))
  498. (error "Can't publish file outside of a project")))))
  499. (project-plist (cdr project))
  500. (ftname (file-truename filename))
  501. (publishing-function
  502. (or (plist-get project-plist :publishing-function)
  503. 'org-publish-org-to-html))
  504. (base-dir (file-name-as-directory
  505. (file-truename (plist-get project-plist :base-directory))))
  506. (pub-dir (file-name-as-directory
  507. (file-truename (plist-get project-plist :publishing-directory))))
  508. tmp-pub-dir)
  509. (setq tmp-pub-dir
  510. (file-name-directory
  511. (concat pub-dir
  512. (and (string-match (regexp-quote base-dir) ftname)
  513. (substring ftname (match-end 0))))))
  514. (if (listp publishing-function)
  515. ;; allow chain of publishing functions
  516. (mapc (lambda (f)
  517. (when (org-publish-needed-p filename pub-dir f tmp-pub-dir)
  518. (funcall f project-plist filename tmp-pub-dir)
  519. (org-publish-update-timestamp filename pub-dir f)))
  520. publishing-function)
  521. (when (org-publish-needed-p filename pub-dir publishing-function
  522. tmp-pub-dir)
  523. (funcall publishing-function project-plist filename tmp-pub-dir)
  524. (org-publish-update-timestamp
  525. filename pub-dir publishing-function)))))
  526. (defun org-publish-projects (projects)
  527. "Publish all files belonging to the PROJECTS alist.
  528. If :auto-sitemap is set, publish the sitemap too.
  529. If :makeindex is set, also produce a file theindex.org."
  530. (mapc
  531. (lambda (project)
  532. (let*
  533. ((project-plist (cdr project))
  534. (exclude-regexp (plist-get project-plist :exclude))
  535. (sitemap-p (plist-get project-plist :auto-sitemap))
  536. (sitemap-filename (or (plist-get project-plist :sitemap-filename)
  537. "sitemap.org"))
  538. (sitemap-function (or (plist-get project-plist :sitemap-function)
  539. 'org-publish-org-sitemap))
  540. (sitemap-sort-folders
  541. (if (plist-member project-plist :sitemap-sort-folders)
  542. (plist-get project-plist :sitemap-sort-folders)
  543. 'first))
  544. (sitemap-alphabetically
  545. (if (plist-member project-plist :sitemap-alphabetically)
  546. (plist-get project-plist :sitemap-alphabetically) t))
  547. (sitemap-ignore-case (plist-get project-plist :sitemap-ignore-case))
  548. (preparation-function (plist-get project-plist :preparation-function))
  549. (completion-function (plist-get project-plist :completion-function))
  550. (files (org-publish-get-base-files project exclude-regexp)) file)
  551. ;; Make sure sitemap-sort-folders' has an accepted value
  552. (unless (memq sitemap-sort-folders '(first last))
  553. (setq sitemap-sort-folders nil))
  554. (when preparation-function (run-hooks 'preparation-function))
  555. (if sitemap-p (funcall sitemap-function project sitemap-filename))
  556. (while (setq file (pop files))
  557. (org-publish-file file project))
  558. (when (plist-get project-plist :makeindex)
  559. (org-publish-index-generate-theindex.inc
  560. (plist-get project-plist :base-directory))
  561. (org-publish-file (expand-file-name
  562. "theindex.org"
  563. (plist-get project-plist :base-directory))
  564. project))
  565. (when completion-function (run-hooks 'completion-function))))
  566. (org-publish-expand-projects projects)))
  567. (defun org-publish-org-sitemap (project &optional sitemap-filename)
  568. "Create an sitemap of pages in set defined by PROJECT.
  569. Optionally set the filename of the sitemap with SITEMAP-FILENAME.
  570. Default for SITEMAP-FILENAME is 'sitemap.org'."
  571. (let* ((project-plist (cdr project))
  572. (dir (file-name-as-directory
  573. (plist-get project-plist :base-directory)))
  574. (localdir (file-name-directory dir))
  575. (indent-str (make-string 2 ?\ ))
  576. (exclude-regexp (plist-get project-plist :exclude))
  577. (files (nreverse (org-publish-get-base-files project exclude-regexp)))
  578. (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
  579. (sitemap-title (or (plist-get project-plist :sitemap-title)
  580. (concat "Sitemap for project " (car project))))
  581. (sitemap-style (or (plist-get project-plist :sitemap-style)
  582. 'tree))
  583. (visiting (find-buffer-visiting sitemap-filename))
  584. (ifn (file-name-nondirectory sitemap-filename))
  585. file sitemap-buffer)
  586. (with-current-buffer (setq sitemap-buffer
  587. (or visiting (find-file sitemap-filename)))
  588. (erase-buffer)
  589. (insert (concat "#+TITLE: " sitemap-title "\n\n"))
  590. (while (setq file (pop files))
  591. (let ((fn (file-name-nondirectory file))
  592. (link (file-relative-name file dir))
  593. (oldlocal localdir))
  594. ;; sitemap shouldn't list itself
  595. (unless (equal (file-truename sitemap-filename)
  596. (file-truename file))
  597. (if (eq sitemap-style 'list)
  598. (message "Generating list-style sitemap for %s" sitemap-title)
  599. (message "Generating tree-style sitemap for %s" sitemap-title)
  600. (setq localdir (concat (file-name-as-directory dir)
  601. (file-name-directory link)))
  602. (unless (string= localdir oldlocal)
  603. (if (string= localdir dir)
  604. (setq indent-str (make-string 2 ?\ ))
  605. (let ((subdirs
  606. (split-string
  607. (directory-file-name
  608. (file-name-directory
  609. (file-relative-name localdir dir))) "/"))
  610. (subdir "")
  611. (old-subdirs (split-string
  612. (file-relative-name oldlocal dir) "/")))
  613. (setq indent-str (make-string 2 ?\ ))
  614. (while (string= (car old-subdirs) (car subdirs))
  615. (setq indent-str (concat indent-str (make-string 2 ?\ )))
  616. (pop old-subdirs)
  617. (pop subdirs))
  618. (dolist (d subdirs)
  619. (setq subdir (concat subdir d "/"))
  620. (insert (concat indent-str " + " d "\n"))
  621. (setq indent-str (make-string
  622. (+ (length indent-str) 2) ?\ )))))))
  623. ;; This is common to 'flat and 'tree
  624. (insert (concat indent-str " + [[file:" link "]["
  625. (org-publish-find-title file)
  626. "]]\n")))))
  627. (save-buffer))
  628. (or visiting (kill-buffer sitemap-buffer))))
  629. (defun org-publish-find-title (file)
  630. "Find the title of file in project."
  631. (if (member file org-publish-file-title-cache)
  632. (cadr (member file org-publish-file-title-cache))
  633. (let* ((visiting (find-buffer-visiting file))
  634. (buffer (or visiting (find-file-noselect file)))
  635. title)
  636. (with-current-buffer buffer
  637. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  638. (org-infile-export-plist))))
  639. (setq title
  640. (or (plist-get opt-plist :title)
  641. (and (not
  642. (plist-get opt-plist :skip-before-1st-heading))
  643. (org-export-grab-title-from-buffer))
  644. (file-name-nondirectory (file-name-sans-extension file))))))
  645. (unless visiting
  646. (kill-buffer buffer))
  647. (setq org-publish-file-title-cache
  648. (append org-publish-file-title-cache (list file title)))
  649. title)))
  650. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  651. ;;; Interactive publishing functions
  652. ;;;###autoload
  653. (defalias 'org-publish-project 'org-publish)
  654. ;;;###autoload
  655. (defun org-publish (project &optional force)
  656. "Publish PROJECT."
  657. (interactive
  658. (list
  659. (assoc (org-icompleting-read
  660. "Publish project: "
  661. org-publish-project-alist nil t)
  662. org-publish-project-alist)
  663. current-prefix-arg))
  664. (setq org-publish-initial-buffer (current-buffer))
  665. (save-window-excursion
  666. (let* ((org-publish-use-timestamps-flag
  667. (if force nil org-publish-use-timestamps-flag)))
  668. (org-publish-projects (list project)))))
  669. ;;;###autoload
  670. (defun org-publish-all (&optional force)
  671. "Publish all projects.
  672. With prefix argument, remove all files in the timestamp
  673. directory and force publishing all files."
  674. (interactive "P")
  675. (when force
  676. (org-publish-remove-all-timestamps))
  677. (org-publish-initialize-files-alist force)
  678. (save-window-excursion
  679. (let ((org-publish-use-timestamps-flag
  680. (if force nil org-publish-use-timestamps-flag)))
  681. (org-publish-projects org-publish-project-alist))))
  682. ;;;###autoload
  683. (defun org-publish-current-file (&optional force)
  684. "Publish the current file.
  685. With prefix argument, force publish the file."
  686. (interactive "P")
  687. (org-publish-initialize-files-alist force)
  688. (save-window-excursion
  689. (let ((org-publish-use-timestamps-flag
  690. (if force nil org-publish-use-timestamps-flag)))
  691. (org-publish-file (buffer-file-name)))))
  692. ;;;###autoload
  693. (defun org-publish-current-project (&optional force)
  694. "Publish the project associated with the current file.
  695. With a prefix argument, force publishing of all files in
  696. the project."
  697. (interactive "P")
  698. (org-publish-initialize-files-alist force)
  699. (save-window-excursion
  700. (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up))
  701. (org-publish-use-timestamps-flag
  702. (if force nil org-publish-use-timestamps-flag)))
  703. (if (not project)
  704. (error "File %s is not part of any known project" (buffer-file-name)))
  705. (org-publish project))))
  706. ;;; Index generation
  707. (defvar backend) ; dynamically scoped
  708. (defun org-publish-aux-preprocess ()
  709. "Find index entries and write them to an .orgx file."
  710. (let ((case-fold-search t)
  711. entry index target)
  712. (goto-char (point-min))
  713. (while
  714. (and
  715. (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t)
  716. (> (match-end 1) (match-beginning 1)))
  717. (setq entry (match-string 1))
  718. (when (eq backend 'latex)
  719. (replace-match (format "\\index{%s}" entry) t t))
  720. (save-excursion
  721. (ignore-errors (org-back-to-heading t))
  722. (setq target (get-text-property (point) 'target))
  723. (setq target (or (cdr (assoc target org-export-preferred-target-alist))
  724. (cdr (assoc target org-export-id-target-alist))
  725. target ""))
  726. (push (cons entry target) index)))
  727. (with-temp-file
  728. (concat (file-name-sans-extension org-current-export-file) ".orgx")
  729. (dolist (entry (nreverse index))
  730. (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
  731. (defun org-publish-index-generate-theindex.inc (directory)
  732. "Generate the index from all .orgx files in the current directory and below."
  733. (require 'find-lisp)
  734. (let* ((fulldir (file-name-as-directory
  735. (expand-file-name directory)))
  736. (full-files (find-lisp-find-files directory "\\.orgx\\'"))
  737. (re (concat "\\`" fulldir))
  738. (files (mapcar (lambda (f) (if (string-match re f)
  739. (substring f (match-end 0))
  740. f))
  741. full-files))
  742. (default-directory directory)
  743. index origfile buf target entry ibuffer
  744. main last-main letter last-letter file sub link tgext)
  745. ;; `files' contains the list of relative file names
  746. (dolist (file files)
  747. (setq origfile (substring file 0 -1))
  748. (setq buf (find-file-noselect file))
  749. (with-current-buffer buf
  750. (goto-char (point-min))
  751. (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
  752. (setq target (match-string 1)
  753. entry (match-string 2))
  754. (push (list entry origfile target) index)))
  755. (kill-buffer buf))
  756. (setq index (sort index (lambda (a b) (string< (downcase (car a))
  757. (downcase (car b))))))
  758. (setq ibuffer (find-file-noselect (expand-file-name "theindex.inc" directory)))
  759. (with-current-buffer ibuffer
  760. (erase-buffer)
  761. (insert "* Index\n")
  762. (setq last-letter nil)
  763. (dolist (idx index)
  764. (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
  765. (if (and (stringp target) (string-match "\\S-" target))
  766. (setq tgext (concat "::#" target))
  767. (setq tgext ""))
  768. (setq letter (upcase (substring entry 0 1)))
  769. (when (not (equal letter last-letter))
  770. (insert "** " letter "\n")
  771. (setq last-letter letter))
  772. (if (string-match "!" entry)
  773. (setq main (substring entry 0 (match-beginning 0))
  774. sub (substring entry (match-end 0)))
  775. (setq main nil sub nil last-main nil))
  776. (when (and main (not (equal main last-main)))
  777. (insert " - " main "\n")
  778. (setq last-main main))
  779. (setq link (concat "[[file:" file tgext "]"
  780. "[" (or sub entry) "]]"))
  781. (if (and main sub)
  782. (insert " - " link "\n")
  783. (insert " - " link "\n")))
  784. (save-buffer))
  785. (kill-buffer ibuffer)
  786. (let ((index-file (expand-file-name "theindex.org" directory)))
  787. (unless (file-exists-p index-file)
  788. (setq ibuffer (find-file-noselect index-file))
  789. (with-current-buffer ibuffer
  790. (erase-buffer)
  791. (insert "\n\n#+include: \"theindex.inc\"\n\n")
  792. (save-buffer))
  793. (kill-buffer ibuffer)))))
  794. (provide 'org-publish)
  795. ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
  796. ;;; org-publish.el ends here