org-publish.el 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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 "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
  213. (defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir)
  214. "Return `t' if FILENAME should be published in PUB-DIR using PUB-FUNC.
  215. TRUE-PUB-DIR is where the file will truely end up. Currently we are not using
  216. this - maybe it can eventually be used to check if the file is present at
  217. the target location, and how old it is. Right ow we cannot do this, because
  218. we do not know under what file name the file will be stored - the publishing
  219. function can still decide about that independently."
  220. (let ((rtn
  221. (if org-publish-use-timestamps-flag
  222. (org-publish-cache-file-needs-publishing
  223. filename pub-dir pub-func)
  224. ;; don't use timestamps, always return t
  225. t)))
  226. (if rtn
  227. (message "Publishing file %s using `%s'" filename pub-func)
  228. (when org-publish-list-skipped-files
  229. (message "Skipping unmodified file %s" filename)))
  230. rtn))
  231. (defun org-publish-update-timestamp (filename &optional pub-dir pub-func)
  232. "Update publishing timestamp for file FILENAME.
  233. If there is no timestamp, create one."
  234. (let ((key (org-publish-timestamp-filename filename pub-dir pub-func))
  235. (stamp (org-publish-cache-ctime-of-src filename)))
  236. (org-publish-cache-set key stamp)))
  237. (defun org-publish-remove-all-timestamps ()
  238. "Remove all files in the timstamp directory."
  239. (let ((dir org-publish-timestamp-directory)
  240. files)
  241. (when (and (file-exists-p dir)
  242. (file-directory-p dir))
  243. (mapc 'delete-file (directory-files dir 'full "[^.]\\'"))
  244. (org-publish-reset-cache))))
  245. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  246. ;;;
  247. (defvar org-publish-initial-buffer nil
  248. "The buffer `org-publish' has been called from.")
  249. (defvar org-publish-temp-files nil
  250. "Temporary list of files to be published.")
  251. ;; Here, so you find the variable right before it's used the first time:
  252. (defvar org-publish-cache nil
  253. "This will cache all the timestamps and titles for
  254. files in publishing projects. Blocks could hash sha1 values here.")
  255. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  256. ;;; Compatibility aliases
  257. ;; Delete-dups is not in Emacs <22
  258. (if (fboundp 'delete-dups)
  259. (defalias 'org-publish-delete-dups 'delete-dups)
  260. (defun org-publish-delete-dups (list)
  261. "Destructively remove `equal' duplicates from LIST.
  262. Store the result in LIST and return it. LIST must be a proper list.
  263. Of several `equal' occurrences of an element in LIST, the first
  264. one is kept.
  265. This is a compatibility function for Emacsen without `delete-dups'."
  266. ;; Code from `subr.el' in Emacs 22:
  267. (let ((tail list))
  268. (while tail
  269. (setcdr tail (delete (car tail) (cdr tail)))
  270. (setq tail (cdr tail))))
  271. list))
  272. (declare-function org-publish-delete-dups "org-publish" (list))
  273. (declare-function find-lisp-find-files "find-lisp" (directory regexp))
  274. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  275. ;;; Getting project information out of org-publish-project-alist
  276. (defun org-publish-expand-projects (projects-alist)
  277. "Expand projects in PROJECTS-ALIST.
  278. This splices all the components into the list."
  279. (let ((rest projects-alist) rtn p components)
  280. (while (setq p (pop rest))
  281. (if (setq components (plist-get (cdr p) :components))
  282. (setq rest (append
  283. (mapcar (lambda (x) (assoc x org-publish-project-alist))
  284. components)
  285. rest))
  286. (push p rtn)))
  287. (nreverse (org-publish-delete-dups (delq nil rtn)))))
  288. (defvar sitemap-alphabetically)
  289. (defvar sitemap-sort-folders)
  290. (defvar sitemap-ignore-case)
  291. (defvar sitemap-requested)
  292. (defun org-publish-compare-directory-files (a b)
  293. "Predicate for `sort', that sorts folders-first/last and
  294. eventually alphabetically."
  295. (let ((retval t))
  296. (when (or sitemap-alphabetically sitemap-sort-folders)
  297. ;; First we sort alphabetically:
  298. (when sitemap-alphabetically
  299. (let* ((adir (file-directory-p a))
  300. (aorg (and (string-match "\\.org$" a) (not adir)))
  301. (bdir (file-directory-p b))
  302. (borg (and (string-match "\\.org$" b) (not bdir)))
  303. (A (if aorg
  304. (concat (file-name-directory a)
  305. (org-publish-find-title a)) a))
  306. (B (if borg
  307. (concat (file-name-directory b)
  308. (org-publish-find-title b)) b)))
  309. (setq retval (if sitemap-ignore-case
  310. (not (string-lessp (upcase B) (upcase A)))
  311. (not (string-lessp B A))))))
  312. ;; Directory-wise wins:
  313. (when sitemap-sort-folders
  314. ;; a is directory, b not:
  315. (cond
  316. ((and (file-directory-p a) (not (file-directory-p b)))
  317. (setq retval (equal sitemap-sort-folders 'first)))
  318. ;; a is not a directory, but b is:
  319. ((and (not (file-directory-p a)) (file-directory-p b))
  320. (setq retval (equal sitemap-sort-folders 'last))))))
  321. retval))
  322. (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
  323. "Set `org-publish-temp-files' with files from BASE-DIR directory.
  324. If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
  325. non-nil, restrict this list to the files matching the regexp
  326. MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
  327. SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
  328. matching the regexp SKIP-DIR when recursing through BASE-DIR."
  329. (mapc (lambda (f)
  330. (let ((fd-p (file-directory-p f))
  331. (fnd (file-name-nondirectory f)))
  332. (if (and fd-p recurse
  333. (not (string-match "^\\.+$" fnd))
  334. (if skip-dir (not (string-match skip-dir fnd)) t))
  335. (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
  336. (unless (or fd-p ;; this is a directory
  337. (and skip-file (string-match skip-file fnd))
  338. (not (file-exists-p (file-truename f)))
  339. (not (string-match match fnd)))
  340. (pushnew f org-publish-temp-files)))))
  341. (if sitemap-requested
  342. (sort (directory-files base-dir t (unless recurse match))
  343. 'org-publish-compare-directory-files)
  344. (directory-files base-dir t (unless recurse match)))))
  345. (defun org-publish-get-base-files (project &optional exclude-regexp)
  346. "Return a list of all files in PROJECT.
  347. If EXCLUDE-REGEXP is set, this will be used to filter out
  348. matching filenames."
  349. (let* ((project-plist (cdr project))
  350. (base-dir (file-name-as-directory
  351. (plist-get project-plist :base-directory)))
  352. (include-list (plist-get project-plist :include))
  353. (recurse (plist-get project-plist :recursive))
  354. (extension (or (plist-get project-plist :base-extension) "org"))
  355. ;; sitemap-... variables are dynamically scoped for
  356. ;; org-publish-compare-directory-files:
  357. (sitemap-requested
  358. (plist-get project-plist :auto-sitemap))
  359. (sitemap-sort-folders
  360. (if (plist-member project-plist :sitemap-sort-folders)
  361. (plist-get project-plist :sitemap-sort-folders)
  362. org-publish-sitemap-sort-folders))
  363. (sitemap-alphabetically
  364. (if (plist-member project-plist :sitemap-alphabetically)
  365. (plist-get project-plist :sitemap-alphabetically)
  366. org-publish-sitemap-sort-alphabetically))
  367. (sitemap-ignore-case
  368. (if (plist-member project-plist :sitemap-ignore-case)
  369. (plist-get project-plist :sitemap-ignore-case)
  370. org-publish-sitemap-sort-ignore-case))
  371. (match (if (eq extension 'any)
  372. "^[^\\.]"
  373. (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
  374. ;; Make sure sitemap-sort-folders' has an accepted value
  375. (unless (memq sitemap-sort-folders '(first last))
  376. (setq sitemap-sort-folders nil))
  377. (setq org-publish-temp-files nil)
  378. (org-publish-get-base-files-1 base-dir recurse match
  379. ;; FIXME distinguish exclude regexp
  380. ;; for skip-file and skip-dir?
  381. exclude-regexp exclude-regexp)
  382. (mapc (lambda (f)
  383. (pushnew
  384. (expand-file-name (concat base-dir f))
  385. org-publish-temp-files))
  386. include-list)
  387. org-publish-temp-files))
  388. (defun org-publish-get-project-from-filename (filename &optional up)
  389. "Return the project FILENAME belongs."
  390. (let* ((filename (expand-file-name filename))
  391. project-name)
  392. (catch 'p-found
  393. (dolist (prj org-publish-project-alist)
  394. (unless (plist-get (cdr prj) :components)
  395. ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
  396. (let* ((r (plist-get (cdr prj) :recursive))
  397. (b (expand-file-name (plist-get (cdr prj) :base-directory)))
  398. (x (plist-get (cdr prj) :base-extension))
  399. (e (plist-get (cdr prj) :exclude))
  400. (i (plist-get (cdr prj) :include))
  401. (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
  402. (when (or
  403. (and i (string-match i filename))
  404. (and
  405. (not (and e (string-match e filename)))
  406. (string-match xm filename)))
  407. (setq project-name (car prj))
  408. (throw 'p-found project-name))))))
  409. (when up
  410. (dolist (prj org-publish-project-alist)
  411. (if (member project-name (plist-get (cdr prj) :components))
  412. (setq project-name (car prj)))))
  413. (assoc project-name org-publish-project-alist)))
  414. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  415. ;;; Pluggable publishing back-end functions
  416. (defun org-publish-org-to (format plist filename pub-dir)
  417. "Publish an org file to FORMAT.
  418. PLIST is the property list for the given project.
  419. FILENAME is the filename of the org file to be published.
  420. PUB-DIR is the publishing directory."
  421. (require 'org)
  422. (unless (file-exists-p pub-dir)
  423. (make-directory pub-dir t))
  424. (let ((visiting (find-buffer-visiting filename)))
  425. (save-excursion
  426. (switch-to-buffer (or visiting (find-file filename)))
  427. (let* ((plist (cons :buffer-will-be-killed (cons t plist)))
  428. (init-buf (current-buffer))
  429. (init-point (point))
  430. (init-buf-string (buffer-string))
  431. export-buf-or-file)
  432. ;; run hooks before exporting
  433. (run-hooks 'org-publish-before-export-hook)
  434. ;; export the possibly modified buffer
  435. (setq export-buf-or-file
  436. (funcall (intern (concat "org-export-as-" format))
  437. (plist-get plist :headline-levels)
  438. nil plist nil nil pub-dir))
  439. (when (and (bufferp export-buf-or-file)
  440. (buffer-live-p export-buf-or-file))
  441. (set-buffer export-buf-or-file)
  442. ;; run hooks after export and save export
  443. (progn (run-hooks 'org-publish-after-export-hook)
  444. (if (buffer-modified-p) (save-buffer)))
  445. (kill-buffer export-buf-or-file))
  446. ;; maybe restore buffer's content
  447. (set-buffer init-buf)
  448. (when (buffer-modified-p init-buf)
  449. (erase-buffer)
  450. (insert init-buf-string)
  451. (save-buffer)
  452. (goto-char init-point))
  453. (unless visiting
  454. (kill-buffer init-buf))))))
  455. (defmacro org-publish-with-aux-preprocess-maybe (&rest body)
  456. "Execute BODY with a modified hook to preprocess for index."
  457. `(let ((org-export-preprocess-after-headline-targets-hook
  458. (if (plist-get project-plist :makeindex)
  459. (cons 'org-publish-aux-preprocess
  460. org-export-preprocess-after-headline-targets-hook)
  461. org-export-preprocess-after-headline-targets-hook)))
  462. ,@body))
  463. (defvar project-plist)
  464. (defun org-publish-org-to-latex (plist filename pub-dir)
  465. "Publish an org file to LaTeX.
  466. See `org-publish-org-to' to the list of arguments."
  467. (org-publish-with-aux-preprocess-maybe
  468. (org-publish-org-to "latex" plist filename pub-dir)))
  469. (defun org-publish-org-to-pdf (plist filename pub-dir)
  470. "Publish an org file to PDF (via LaTeX).
  471. See `org-publish-org-to' to the list of arguments."
  472. (org-publish-with-aux-preprocess-maybe
  473. (org-publish-org-to "pdf" plist filename pub-dir)))
  474. (defun org-publish-org-to-html (plist filename pub-dir)
  475. "Publish an org file to HTML.
  476. See `org-publish-org-to' to the list of arguments."
  477. (org-publish-with-aux-preprocess-maybe
  478. (org-publish-org-to "html" plist filename pub-dir)))
  479. (defun org-publish-org-to-org (plist filename pub-dir)
  480. "Publish an org file to HTML.
  481. See `org-publish-org-to' to the list of arguments."
  482. (org-publish-org-to "org" plist filename pub-dir))
  483. (defun org-publish-attachment (plist filename pub-dir)
  484. "Publish a file with no transformation of any kind.
  485. See `org-publish-org-to' to the list of arguments."
  486. ;; make sure eshell/cp code is loaded
  487. (unless (file-directory-p pub-dir)
  488. (make-directory pub-dir t))
  489. (or (equal (expand-file-name (file-name-directory filename))
  490. (file-name-as-directory (expand-file-name pub-dir)))
  491. (copy-file filename
  492. (expand-file-name (file-name-nondirectory filename) pub-dir)
  493. t)))
  494. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  495. ;;; Publishing files, sets of files, and indices
  496. (defun org-publish-file (filename &optional project no-cache)
  497. "Publish file FILENAME from PROJECT.
  498. If NO-CACHE is not nil, do not initialize org-publish-cache and
  499. write it to disk. This is needed, since this function is used to
  500. publish single files, when entire projects are published.
  501. See `org-publish-projects'."
  502. (let* ((project
  503. (or project
  504. (or (org-publish-get-project-from-filename filename)
  505. (error "File %s not part of any known project"
  506. (abbreviate-file-name filename)))))
  507. (project-plist (cdr project))
  508. (ftname (file-truename filename))
  509. (publishing-function
  510. (or (plist-get project-plist :publishing-function)
  511. 'org-publish-org-to-html))
  512. (base-dir (file-name-as-directory
  513. (file-truename (plist-get project-plist :base-directory))))
  514. (pub-dir (file-name-as-directory
  515. (file-truename (plist-get project-plist :publishing-directory))))
  516. tmp-pub-dir)
  517. (unless no-cache
  518. (org-publish-initialize-cache (car project)))
  519. (setq tmp-pub-dir
  520. (file-name-directory
  521. (concat pub-dir
  522. (and (string-match (regexp-quote base-dir) ftname)
  523. (substring ftname (match-end 0))))))
  524. (if (listp publishing-function)
  525. ;; allow chain of publishing functions
  526. (mapc (lambda (f)
  527. (when (org-publish-needed-p filename pub-dir f tmp-pub-dir)
  528. (funcall f project-plist filename tmp-pub-dir)
  529. (org-publish-update-timestamp filename pub-dir f)))
  530. publishing-function)
  531. (when (org-publish-needed-p filename pub-dir publishing-function
  532. tmp-pub-dir)
  533. (funcall publishing-function project-plist filename tmp-pub-dir)
  534. (org-publish-update-timestamp
  535. filename pub-dir publishing-function)))
  536. (unless no-cache (org-publish-write-cache-file))))
  537. (defun org-publish-projects (projects)
  538. "Publish all files belonging to the PROJECTS alist.
  539. If :auto-sitemap is set, publish the sitemap too.
  540. If :makeindex is set, also produce a file theindex.org."
  541. (mapc
  542. (lambda (project)
  543. ;; Each project uses it's own cache file:
  544. (org-publish-initialize-cache (car project))
  545. (let*
  546. ((project-plist (cdr project))
  547. (exclude-regexp (plist-get project-plist :exclude))
  548. (sitemap-p (plist-get project-plist :auto-sitemap))
  549. (sitemap-filename (or (plist-get project-plist :sitemap-filename)
  550. "sitemap.org"))
  551. (sitemap-function (or (plist-get project-plist :sitemap-function)
  552. 'org-publish-org-sitemap))
  553. (preparation-function (plist-get project-plist :preparation-function))
  554. (completion-function (plist-get project-plist :completion-function))
  555. (files (org-publish-get-base-files project exclude-regexp)) file)
  556. (when preparation-function (run-hooks 'preparation-function))
  557. (if sitemap-p (funcall sitemap-function project sitemap-filename))
  558. (while (setq file (pop files))
  559. (org-publish-file file project t))
  560. (when (plist-get project-plist :makeindex)
  561. (org-publish-index-generate-theindex.inc
  562. (plist-get project-plist :base-directory))
  563. (org-publish-file (expand-file-name
  564. "theindex.org"
  565. (plist-get project-plist :base-directory))
  566. project t))
  567. (when completion-function (run-hooks 'completion-function))
  568. (org-publish-write-cache-file)))
  569. (org-publish-expand-projects projects)))
  570. (defun org-publish-org-sitemap (project &optional sitemap-filename)
  571. "Create a sitemap of pages in set defined by PROJECT.
  572. Optionally set the filename of the sitemap with SITEMAP-FILENAME.
  573. Default for SITEMAP-FILENAME is 'sitemap.org'."
  574. (let* ((project-plist (cdr project))
  575. (dir (file-name-as-directory
  576. (plist-get project-plist :base-directory)))
  577. (localdir (file-name-directory dir))
  578. (indent-str (make-string 2 ?\ ))
  579. (exclude-regexp (plist-get project-plist :exclude))
  580. (files (nreverse (org-publish-get-base-files project exclude-regexp)))
  581. (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
  582. (sitemap-title (or (plist-get project-plist :sitemap-title)
  583. (concat "Sitemap for project " (car project))))
  584. (sitemap-style (or (plist-get project-plist :sitemap-style)
  585. 'tree))
  586. (visiting (find-buffer-visiting sitemap-filename))
  587. (ifn (file-name-nondirectory sitemap-filename))
  588. file sitemap-buffer)
  589. (with-current-buffer (setq sitemap-buffer
  590. (or visiting (find-file sitemap-filename)))
  591. (erase-buffer)
  592. (insert (concat "#+TITLE: " sitemap-title "\n\n"))
  593. (while (setq file (pop files))
  594. (let ((fn (file-name-nondirectory file))
  595. (link (file-relative-name file dir))
  596. (oldlocal localdir))
  597. ;; sitemap shouldn't list itself
  598. (unless (equal (file-truename sitemap-filename)
  599. (file-truename file))
  600. (if (eq sitemap-style 'list)
  601. (message "Generating list-style sitemap for %s" sitemap-title)
  602. (message "Generating tree-style sitemap for %s" sitemap-title)
  603. (setq localdir (concat (file-name-as-directory dir)
  604. (file-name-directory link)))
  605. (unless (string= localdir oldlocal)
  606. (if (string= localdir dir)
  607. (setq indent-str (make-string 2 ?\ ))
  608. (let ((subdirs
  609. (split-string
  610. (directory-file-name
  611. (file-name-directory
  612. (file-relative-name localdir dir))) "/"))
  613. (subdir "")
  614. (old-subdirs (split-string
  615. (file-relative-name oldlocal dir) "/")))
  616. (setq indent-str (make-string 2 ?\ ))
  617. (while (string= (car old-subdirs) (car subdirs))
  618. (setq indent-str (concat indent-str (make-string 2 ?\ )))
  619. (pop old-subdirs)
  620. (pop subdirs))
  621. (dolist (d subdirs)
  622. (setq subdir (concat subdir d "/"))
  623. (insert (concat indent-str " + " d "\n"))
  624. (setq indent-str (make-string
  625. (+ (length indent-str) 2) ?\ )))))))
  626. ;; This is common to 'flat and 'tree
  627. (insert (concat indent-str " + [[file:" link "]["
  628. (org-publish-find-title file)
  629. "]]\n")))))
  630. (save-buffer))
  631. (or visiting (kill-buffer sitemap-buffer))))
  632. (defun org-publish-find-title (file)
  633. "Find the title of file in project."
  634. (or
  635. (org-publish-cache-get-file-property file :title nil t)
  636. (let* ((visiting (find-buffer-visiting file))
  637. (buffer (or visiting (find-file-noselect file)))
  638. title)
  639. (with-current-buffer buffer
  640. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  641. (org-infile-export-plist))))
  642. (setq title
  643. (or (plist-get opt-plist :title)
  644. (and (not
  645. (plist-get opt-plist :skip-before-1st-heading))
  646. (org-export-grab-title-from-buffer))
  647. (file-name-nondirectory (file-name-sans-extension file))))))
  648. (unless visiting
  649. (kill-buffer buffer))
  650. (org-publish-cache-set-file-property file :title title)
  651. title)))
  652. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  653. ;;; Interactive publishing functions
  654. ;;;###autoload
  655. (defalias 'org-publish-project 'org-publish)
  656. ;;;###autoload
  657. (defun org-publish (project &optional force)
  658. "Publish PROJECT."
  659. (interactive
  660. (list
  661. (assoc (org-icompleting-read
  662. "Publish project: "
  663. org-publish-project-alist nil t)
  664. org-publish-project-alist)
  665. current-prefix-arg))
  666. (setq org-publish-initial-buffer (current-buffer))
  667. (save-window-excursion
  668. (let* ((org-publish-use-timestamps-flag
  669. (if force nil org-publish-use-timestamps-flag)))
  670. (org-publish-projects (list project)))))
  671. ;;;###autoload
  672. (defun org-publish-all (&optional force)
  673. "Publish all projects.
  674. With prefix argument, remove all files in the timestamp
  675. directory and force publishing all files."
  676. (interactive "P")
  677. (when force
  678. (org-publish-remove-all-timestamps))
  679. ;; (org-publish-initialize-files-alist force)
  680. (save-window-excursion
  681. (let ((org-publish-use-timestamps-flag
  682. (if force nil org-publish-use-timestamps-flag)))
  683. (org-publish-projects org-publish-project-alist))))
  684. ;;;###autoload
  685. (defun org-publish-current-file (&optional force)
  686. "Publish the current file.
  687. With prefix argument, force publish the file."
  688. (interactive "P")
  689. (save-window-excursion
  690. (let ((org-publish-use-timestamps-flag
  691. (if force nil org-publish-use-timestamps-flag)))
  692. (org-publish-file (buffer-file-name)))))
  693. ;;;###autoload
  694. (defun org-publish-current-project (&optional force)
  695. "Publish the project associated with the current file.
  696. With a prefix argument, force publishing of all files in
  697. the project."
  698. (interactive "P")
  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. ;; FIXME: force is not used here?
  706. (org-publish project))))
  707. ;;; Index generation
  708. (defvar backend) ; dynamically scoped
  709. (defun org-publish-aux-preprocess ()
  710. "Find index entries and write them to an .orgx file."
  711. (let ((case-fold-search t)
  712. entry index target)
  713. (goto-char (point-min))
  714. (while
  715. (and
  716. (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t)
  717. (> (match-end 1) (match-beginning 1)))
  718. (setq entry (match-string 1))
  719. (when (eq backend 'latex)
  720. (replace-match (format "\\index{%s}" entry) t t))
  721. (save-excursion
  722. (ignore-errors (org-back-to-heading t))
  723. (setq target (get-text-property (point) 'target))
  724. (setq target (or (cdr (assoc target org-export-preferred-target-alist))
  725. (cdr (assoc target org-export-id-target-alist))
  726. target ""))
  727. (push (cons entry target) index)))
  728. (with-temp-file
  729. (concat (file-name-sans-extension org-current-export-file) ".orgx")
  730. (dolist (entry (nreverse index))
  731. (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
  732. (defun org-publish-index-generate-theindex.inc (directory)
  733. "Generate the index from all .orgx files in the current directory and below."
  734. (require 'find-lisp)
  735. (let* ((fulldir (file-name-as-directory
  736. (expand-file-name directory)))
  737. (full-files (find-lisp-find-files directory "\\.orgx\\'"))
  738. (re (concat "\\`" fulldir))
  739. (files (mapcar (lambda (f) (if (string-match re f)
  740. (substring f (match-end 0))
  741. f))
  742. full-files))
  743. (default-directory directory)
  744. index origfile buf target entry ibuffer
  745. main last-main letter last-letter file sub link tgext)
  746. ;; `files' contains the list of relative file names
  747. (dolist (file files)
  748. (setq origfile (substring file 0 -1))
  749. (setq buf (find-file-noselect file))
  750. (with-current-buffer buf
  751. (goto-char (point-min))
  752. (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
  753. (setq target (match-string 1)
  754. entry (match-string 2))
  755. (push (list entry origfile target) index)))
  756. (kill-buffer buf))
  757. (setq index (sort index (lambda (a b) (string< (downcase (car a))
  758. (downcase (car b))))))
  759. (setq ibuffer (find-file-noselect (expand-file-name "theindex.inc" directory)))
  760. (with-current-buffer ibuffer
  761. (erase-buffer)
  762. (insert "* Index\n")
  763. (setq last-letter nil)
  764. (dolist (idx index)
  765. (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
  766. (if (and (stringp target) (string-match "\\S-" target))
  767. (setq tgext (concat "::#" target))
  768. (setq tgext ""))
  769. (setq letter (upcase (substring entry 0 1)))
  770. (when (not (equal letter last-letter))
  771. (insert "** " letter "\n")
  772. (setq last-letter letter))
  773. (if (string-match "!" entry)
  774. (setq main (substring entry 0 (match-beginning 0))
  775. sub (substring entry (match-end 0)))
  776. (setq main nil sub nil last-main nil))
  777. (when (and main (not (equal main last-main)))
  778. (insert " - " main "\n")
  779. (setq last-main main))
  780. (setq link (concat "[[file:" file tgext "]"
  781. "[" (or sub entry) "]]"))
  782. (if (and main sub)
  783. (insert " - " link "\n")
  784. (insert " - " link "\n")))
  785. (save-buffer))
  786. (kill-buffer ibuffer)
  787. (let ((index-file (expand-file-name "theindex.org" directory)))
  788. (unless (file-exists-p index-file)
  789. (setq ibuffer (find-file-noselect index-file))
  790. (with-current-buffer ibuffer
  791. (erase-buffer)
  792. (insert "\n\n#+include: \"theindex.inc\"\n\n")
  793. (save-buffer))
  794. (kill-buffer ibuffer)))))
  795. ;; Caching functions:
  796. (defun org-publish-write-cache-file (&optional free-cache)
  797. "Write `org-publish-cache' to file.
  798. If FREE-CACHE, empty the cache."
  799. (unless org-publish-cache
  800. (error "%s" "`org-publish-write-cache-file' called, but no cache present"))
  801. (let ((cache-file (org-publish-cache-get ":cache-file:")))
  802. (unless cache-file
  803. (error "%s" "Cannot find cache-file name in `org-publish-write-cache-file'"))
  804. (with-temp-file cache-file
  805. (let ((print-level nil)
  806. (print-length nil))
  807. (insert
  808. "(setq org-publish-cache\n "
  809. (replace-regexp-in-string "\\([^\\ \t]\"\\) \\([^ \t]\\)" "\\1\n\\2"
  810. (format "%S" org-publish-cache))
  811. ")\n")))
  812. (when free-cache (org-publish-reset-cache))))
  813. (defun org-publish-initialize-cache (project-name)
  814. "Initialize the projects cache if not intialized yet
  815. and return it."
  816. (unless project-name
  817. (error "%s%s" "Cannot initialize `org-publish-cache' without projects name"
  818. " in `org-publish-initialize-cache'"))
  819. (unless (file-exists-p org-publish-timestamp-directory)
  820. (make-directory org-publish-timestamp-directory))
  821. (if (not (file-directory-p org-publish-timestamp-directory))
  822. (error "Org publish timestamp: %s is not a directory"
  823. org-publish-timestamp-directory))
  824. (unless (and org-publish-cache
  825. (string= (org-publish-cache-get ":project:") project-name))
  826. (when org-publish-cache (org-publish-reset-cache))
  827. (let* ((cache-file (concat
  828. (expand-file-name org-publish-timestamp-directory)
  829. project-name
  830. ".cache"))
  831. (cexists (file-exists-p cache-file)))
  832. (if cexists (load-file cache-file))
  833. (unless org-publish-cache
  834. (setq org-publish-cache
  835. (make-hash-table :test 'equal :weakness nil :size 100))
  836. (org-publish-cache-set ":project:" project-name)
  837. (org-publish-cache-set ":cache-file:" cache-file))
  838. (unless cexists (org-publish-write-cache-file nil))))
  839. org-publish-cache)
  840. (defun org-publish-reset-cache ()
  841. "Empty org-publish-cache and reset it nil."
  842. (message "%s" "Resetting org-publish-cache")
  843. (if (hash-table-p org-publish-cache)
  844. (clrhash org-publish-cache))
  845. (setq org-publish-cache nil))
  846. (defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func)
  847. "Check the timestamp of the last publishing of FILENAME.
  848. Return `t', if the file needs publishing"
  849. (unless org-publish-cache
  850. (error "%s" "`org-publish-cache-file-needs-publishing' called, but no cache present"))
  851. (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func))
  852. (pstamp (org-publish-cache-get key)))
  853. (if (null pstamp)
  854. t
  855. (let ((ctime (org-publish-cache-ctime-of-src filename)))
  856. (< pstamp ctime)))))
  857. (defun org-publish-cache-set-file-property (filename property value &optional project-name)
  858. "Set the VALUE for a PORPERTY of file FILENAME in publishing cache
  859. to VALUE. Use cache file of PROJECT-NAME.
  860. If the entry does not exist, it will be created.
  861. Return VALUE."
  862. ;; Evtl. load the requested cache file:
  863. (if project-name (org-publish-initialize-cache project-name))
  864. (let ((pl (org-publish-cache-get filename)))
  865. (if pl
  866. (progn
  867. (plist-put pl property value)
  868. value)
  869. (org-publish-cache-get-file-property
  870. filename property value nil project-name))))
  871. (defun org-publish-cache-get-file-property
  872. (filename property &optional default no-create project-name)
  873. "Return the value for a PORPERTY of file FILENAME in publishing cache.
  874. Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
  875. DEFAULT, if the value does not yet exist.
  876. If the entry will be created, unless NO-CREATE is not nil."
  877. ;; Evtl. load the requested cache file:
  878. (if project-name (org-publish-initialize-cache project-name))
  879. (let ((pl (org-publish-cache-get filename))
  880. (retval nil))
  881. (if pl
  882. (if (plist-member pl property)
  883. (setq retval (plist-get pl property))
  884. (setq retval default))
  885. ;; no pl yet:
  886. (unless no-create
  887. (org-publish-cache-set filename (list property default)))
  888. (setq retval default))
  889. retval))
  890. (defun org-publish-cache-get (key)
  891. "Return the value stored in `org-publish-cache' for key KEY.
  892. Returns nil, if no value or nil is found, or the cache does not
  893. exist."
  894. (unless org-publish-cache
  895. (error "%s" "`org-publish-cache-get' called, but no cache present"))
  896. (gethash key org-publish-cache))
  897. (defun org-publish-cache-set (key value)
  898. "Store KEY VLAUE pair in `org-publish-cache'.
  899. Returns value on success, else nil."
  900. (unless org-publish-cache
  901. (error "%s" "`org-publish-cache-set' called, but no cache present"))
  902. (puthash key value org-publish-cache))
  903. (defun org-publish-cache-ctime-of-src (filename)
  904. "Get the files ctime as integer."
  905. (let ((src-attr (file-attributes filename)))
  906. (+
  907. (lsh (car (nth 5 src-attr)) 16)
  908. (cadr (nth 5 src-attr)))))
  909. (provide 'org-publish)
  910. ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
  911. ;;; org-publish.el ends here