org-publish.el 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. ;;; org-publish.el --- publish related org-mode files as a website
  2. ;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
  3. ;; Author: David O'Toole <dto@gnu.org>
  4. ;; Maintainer: Carsten Dominik <carsten DOT dominik AT gmail DOT com>
  5. ;; Keywords: hypermedia, outlines, wp
  6. ;; This file is part of GNU Emacs.
  7. ;;
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This program allow configurable publishing of related sets of
  20. ;; Org-mode files as a complete website.
  21. ;;
  22. ;; org-publish.el can do the following:
  23. ;;
  24. ;; + Publish all one's org-files to HTML or PDF
  25. ;; + Upload HTML, images, attachments and other files to a web server
  26. ;; + Exclude selected private pages from publishing
  27. ;; + Publish a clickable sitemap of pages
  28. ;; + Manage local timestamps for publishing only changed files
  29. ;; + Accept plugin functions to extend range of publishable content
  30. ;;
  31. ;; Documentation for publishing is in the manual.
  32. ;;; Code:
  33. (eval-when-compile
  34. (require 'cl))
  35. (require 'org)
  36. (require 'org-exp)
  37. (require 'format-spec)
  38. (eval-and-compile
  39. (unless (fboundp 'declare-function)
  40. (defmacro declare-function (fn file &optional arglist fileonly))))
  41. (defvar org-publish-initial-buffer nil
  42. "The buffer `org-publish' has been called from.")
  43. (defvar org-publish-temp-files nil
  44. "Temporary list of files to be published.")
  45. ;; Here, so you find the variable right before it's used the first time:
  46. (defvar org-publish-cache nil
  47. "This will cache timestamps and titles for files in publishing projects.
  48. Blocks could hash sha1 values here.")
  49. (defgroup org-publish nil
  50. "Options for publishing a set of Org-mode and related files."
  51. :tag "Org Publishing"
  52. :group 'org)
  53. (defcustom org-publish-project-alist nil
  54. "Association list to control publishing behavior.
  55. Each element of the alist is a publishing 'project.' The CAR of
  56. each element is a string, uniquely identifying the project. The
  57. CDR of each element is in one of the following forms:
  58. 1. A well-formed property list with an even number of elements, alternating
  59. keys and values, specifying parameters for the publishing process.
  60. (:property value :property value ... )
  61. 2. A meta-project definition, specifying of a list of sub-projects:
  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. If not given,
  76. \"org\" will be used as default extension.
  77. :publishing-directory Directory (possibly remote) where output
  78. files will be published
  79. The :exclude property may be used to prevent certain files from
  80. being published. Its value may be a string or regexp matching
  81. file names you don't want to be published.
  82. The :include property may be used to include extra files. Its
  83. value may be a list of filenames to include. The filenames are
  84. considered relative to the base directory.
  85. When both :include and :exclude properties are given values, the
  86. exclusion step happens first.
  87. One special property controls which back-end function to use for
  88. publishing files in the project. This can be used to extend the
  89. set of file types publishable by org-publish, as well as the set
  90. of output formats.
  91. :publishing-function Function to publish file. The default is
  92. `org-publish-org-to-html', but other
  93. values are possible. May also be a
  94. list of functions, in which case
  95. each function in the list is invoked
  96. in turn.
  97. Another property allows you to insert code that prepares a
  98. project for publishing. For example, you could call GNU Make on a
  99. certain makefile, to ensure published files are built up to date.
  100. :preparation-function Function to be called before publishing
  101. this project. This may also be a list
  102. of functions.
  103. :completion-function Function to be called after publishing
  104. this project. This may also be a list
  105. of functions.
  106. Some properties control details of the Org publishing process,
  107. and are equivalent to the corresponding user variables listed in
  108. the right column. See the documentation for those variables to
  109. learn more about their use and default values.
  110. :language `org-export-default-language'
  111. :headline-levels `org-export-headline-levels'
  112. :section-numbers `org-export-with-section-numbers'
  113. :table-of-contents `org-export-with-toc'
  114. :emphasize `org-export-with-emphasize'
  115. :sub-superscript `org-export-with-sub-superscripts'
  116. :TeX-macros `org-export-with-TeX-macros'
  117. :fixed-width `org-export-with-fixed-width'
  118. :tables `org-export-with-tables'
  119. :table-auto-headline `org-export-highlight-first-table-line'
  120. :style `org-export-html-style'
  121. :convert-org-links `org-export-html-link-org-files-as-html'
  122. :inline-images `org-export-html-inline-images'
  123. :expand-quoted-html `org-export-html-expand'
  124. :timestamp `org-export-html-with-timestamp'
  125. :publishing-directory `org-export-publishing-directory'
  126. :html-preamble `org-export-html-preamble'
  127. :html-postamble `org-export-html-postamble'
  128. :author `user-full-name'
  129. :email `user-mail-address'
  130. The following properties may be used to control publishing of a
  131. sitemap of files or summary page for a given project.
  132. :auto-sitemap Whether to publish a sitemap during
  133. `org-publish-current-project' or `org-publish-all'.
  134. :sitemap-filename Filename for output of sitemap. Defaults
  135. to 'sitemap.org' (which becomes 'sitemap.html').
  136. :sitemap-title Title of sitemap page. Defaults to name of file.
  137. :sitemap-function Plugin function to use for generation of sitemap.
  138. Defaults to `org-publish-org-sitemap', which
  139. generates a plain list of links to all files
  140. in the project.
  141. :sitemap-style Can be `list' (sitemap is just an itemized list
  142. of the titles of the files involved) or
  143. `tree' (the directory structure of the source
  144. files is reflected in the sitemap). Defaults to
  145. `tree'.
  146. :sitemap-sans-extension Remove extension from sitemap's
  147. filenames. Useful to have cool
  148. URIs (see
  149. http://www.w3.org/Provider/Style/URI).
  150. Defaults to nil.
  151. If you create a sitemap file, adjust the sorting like this:
  152. :sitemap-sort-folders Where folders should appear in the sitemap.
  153. Set this to `first' (default) or `last' to
  154. display folders first or last, respectively.
  155. Any other value will mix files and folders.
  156. :sitemap-sort-files The site map is normally sorted alphabetically.
  157. You can change this behaviour setting this to
  158. `chronologically', `anti-chronologically' or nil.
  159. :sitemap-ignore-case Should sorting be case-sensitive? Default nil.
  160. The following properties control the creation of a concept index.
  161. :makeindex Create a concept index.
  162. Other properties affecting publication.
  163. :body-only Set this to 't' to publish only the body of the
  164. documents, excluding everything outside and
  165. including the <body> tags in HTML, or
  166. \begin{document}..\end{document} in LaTeX."
  167. :group 'org-publish
  168. :type 'alist)
  169. (defcustom org-publish-use-timestamps-flag t
  170. "Non-nil means use timestamp checking to publish only changed files.
  171. When nil, do no timestamp checking and always publish all files."
  172. :group 'org-publish
  173. :type 'boolean)
  174. (defcustom org-publish-timestamp-directory (convert-standard-filename
  175. "~/.org-timestamps/")
  176. "Name of directory in which to store publishing timestamps."
  177. :group 'org-publish
  178. :type 'directory)
  179. (defcustom org-publish-list-skipped-files t
  180. "Non-nil means show message about files *not* published."
  181. :group 'org-publish
  182. :type 'boolean)
  183. (defcustom org-publish-before-export-hook nil
  184. "Hook run before export on the Org file.
  185. The hook may modify the file in arbitrary ways before publishing happens.
  186. The original version of the buffer will be restored after publishing."
  187. :group 'org-publish
  188. :type 'hook)
  189. (defcustom org-publish-after-export-hook nil
  190. "Hook run after export on the exported buffer.
  191. Any changes made by this hook will be saved."
  192. :group 'org-publish
  193. :type 'hook)
  194. (defcustom org-publish-sitemap-sort-files 'alphabetically
  195. "How sitemaps files should be sorted by default?
  196. Possible values are `alphabetically', `chronologically', `anti-chronologically' and nil.
  197. If `alphabetically', files will be sorted alphabetically.
  198. If `chronologically', files will be sorted with older modification time first.
  199. If `anti-chronologically', files will be sorted with newer modification time first.
  200. nil won't sort files.
  201. You can overwrite this default per project in your
  202. `org-publish-project-alist', using `:sitemap-sort-files'."
  203. :group 'org-publish
  204. :version "24.1"
  205. :type 'symbol)
  206. (defcustom org-publish-sitemap-sort-folders 'first
  207. "A symbol, denoting if folders are sorted first in sitemaps.
  208. Possible values are `first', `last', and nil.
  209. If `first', folders will be sorted before files.
  210. If `last', folders are sorted to the end after the files.
  211. Any other value will not mix files and folders.
  212. You can overwrite this default per project in your
  213. `org-publish-project-alist', using `:sitemap-sort-folders'."
  214. :group 'org-publish
  215. :version "24.1"
  216. :type 'symbol)
  217. (defcustom org-publish-sitemap-sort-ignore-case nil
  218. "Sort sitemaps case insensitively by default?
  219. You can overwrite this default per project in your
  220. `org-publish-project-alist', using `:sitemap-ignore-case'."
  221. :group 'org-publish
  222. :version "24.1"
  223. :type 'boolean)
  224. (defcustom org-publish-sitemap-date-format "%Y-%m-%d"
  225. "Format for `format-time-string' which is used to print a date
  226. in the sitemap."
  227. :group 'org-publish
  228. :version "24.1"
  229. :type 'string)
  230. (defcustom org-publish-sitemap-file-entry-format "%t"
  231. "How a sitemap file entry is formatted.
  232. You could use brackets to delimit on what part the link will be.
  233. %t is the title.
  234. %a is the author.
  235. %d is the date formatted using `org-publish-sitemap-date-format'."
  236. :group 'org-publish
  237. :version "24.1"
  238. :type 'string)
  239. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  240. ;;; Sanitize-plist (FIXME why?)
  241. (defun org-publish-sanitize-plist (plist)
  242. ;; FIXME document
  243. (mapcar (lambda (x)
  244. (or (cdr (assq x '((:index-filename . :sitemap-filename)
  245. (:index-title . :sitemap-title)
  246. (:index-function . :sitemap-function)
  247. (:index-style . :sitemap-style)
  248. (:auto-index . :auto-sitemap))))
  249. x))
  250. plist))
  251. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  252. ;;; Timestamp-related functions
  253. (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
  254. "Return path to timestamp file for filename FILENAME."
  255. (setq filename (concat filename "::" (or pub-dir "") "::"
  256. (format "%s" (or pub-func ""))))
  257. (concat "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
  258. (defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir base-dir)
  259. "Return t if FILENAME should be published in PUB-DIR using PUB-FUNC.
  260. TRUE-PUB-DIR is where the file will truly end up. Currently we are not using
  261. this - maybe it can eventually be used to check if the file is present at
  262. the target location, and how old it is. Right now we cannot do this, because
  263. we do not know under what file name the file will be stored - the publishing
  264. function can still decide about that independently."
  265. (let ((rtn
  266. (if org-publish-use-timestamps-flag
  267. (org-publish-cache-file-needs-publishing
  268. filename pub-dir pub-func base-dir)
  269. ;; don't use timestamps, always return t
  270. t)))
  271. (if rtn
  272. (message "Publishing file %s using `%s'" filename pub-func)
  273. (when org-publish-list-skipped-files
  274. (message "Skipping unmodified file %s" filename)))
  275. rtn))
  276. (defun org-publish-update-timestamp (filename &optional pub-dir pub-func base-dir)
  277. "Update publishing timestamp for file FILENAME.
  278. If there is no timestamp, create one."
  279. (let ((key (org-publish-timestamp-filename filename pub-dir pub-func))
  280. (stamp (org-publish-cache-ctime-of-src filename)))
  281. (org-publish-cache-set key stamp)))
  282. (defun org-publish-remove-all-timestamps ()
  283. "Remove all files in the timestamp directory."
  284. (let ((dir org-publish-timestamp-directory)
  285. files)
  286. (when (and (file-exists-p dir)
  287. (file-directory-p dir))
  288. (mapc 'delete-file (directory-files dir 'full "[^.]\\'"))
  289. (org-publish-reset-cache))))
  290. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  291. ;;; Compatibility aliases
  292. ;; Delete-dups is not in Emacs <22
  293. (if (fboundp 'delete-dups)
  294. (defalias 'org-publish-delete-dups 'delete-dups)
  295. (defun org-publish-delete-dups (list)
  296. "Destructively remove `equal' duplicates from LIST.
  297. Store the result in LIST and return it. LIST must be a proper list.
  298. Of several `equal' occurrences of an element in LIST, the first
  299. one is kept.
  300. This is a compatibility function for Emacsen without `delete-dups'."
  301. ;; Code from `subr.el' in Emacs 22:
  302. (let ((tail list))
  303. (while tail
  304. (setcdr tail (delete (car tail) (cdr tail)))
  305. (setq tail (cdr tail))))
  306. list))
  307. (declare-function org-publish-delete-dups "org-publish" (list))
  308. (declare-function find-lisp-find-files "find-lisp" (directory regexp))
  309. (declare-function org-pop-to-buffer-same-window
  310. "org-compat" (&optional buffer-or-name norecord label))
  311. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  312. ;;; Getting project information out of org-publish-project-alist
  313. (defun org-publish-expand-projects (projects-alist)
  314. "Expand projects in PROJECTS-ALIST.
  315. This splices all the components into the list."
  316. (let ((rest projects-alist) rtn p components)
  317. (while (setq p (pop rest))
  318. (if (setq components (plist-get (cdr p) :components))
  319. (setq rest (append
  320. (mapcar (lambda (x) (assoc x org-publish-project-alist))
  321. components)
  322. rest))
  323. (push p rtn)))
  324. (nreverse (org-publish-delete-dups (delq nil rtn)))))
  325. (defvar org-sitemap-sort-files)
  326. (defvar org-sitemap-sort-folders)
  327. (defvar org-sitemap-ignore-case)
  328. (defvar org-sitemap-requested)
  329. (defvar org-sitemap-date-format)
  330. (defvar org-sitemap-file-entry-format)
  331. (defun org-publish-compare-directory-files (a b)
  332. "Predicate for `sort', that sorts folders and files for sitemap."
  333. (let ((retval t))
  334. (when (or org-sitemap-sort-files org-sitemap-sort-folders)
  335. ;; First we sort files:
  336. (when org-sitemap-sort-files
  337. (cond ((equal org-sitemap-sort-files 'alphabetically)
  338. (let* ((adir (file-directory-p a))
  339. (aorg (and (string-match "\\.org$" a) (not adir)))
  340. (bdir (file-directory-p b))
  341. (borg (and (string-match "\\.org$" b) (not bdir)))
  342. (A (if aorg
  343. (concat (file-name-directory a)
  344. (org-publish-find-title a)) a))
  345. (B (if borg
  346. (concat (file-name-directory b)
  347. (org-publish-find-title b)) b)))
  348. (setq retval (if org-sitemap-ignore-case
  349. (not (string-lessp (upcase B) (upcase A)))
  350. (not (string-lessp B A))))))
  351. ((or (equal org-sitemap-sort-files 'chronologically)
  352. (equal org-sitemap-sort-files 'anti-chronologically))
  353. (let* ((adate (org-publish-find-date a))
  354. (bdate (org-publish-find-date b))
  355. (A (+ (lsh (car adate) 16) (cadr adate)))
  356. (B (+ (lsh (car bdate) 16) (cadr bdate))))
  357. (setq retval (if (equal org-sitemap-sort-files 'chronologically)
  358. (<= A B)
  359. (>= A B)))))))
  360. ;; Directory-wise wins:
  361. (when org-sitemap-sort-folders
  362. ;; a is directory, b not:
  363. (cond
  364. ((and (file-directory-p a) (not (file-directory-p b)))
  365. (setq retval (equal org-sitemap-sort-folders 'first)))
  366. ;; a is not a directory, but b is:
  367. ((and (not (file-directory-p a)) (file-directory-p b))
  368. (setq retval (equal org-sitemap-sort-folders 'last))))))
  369. retval))
  370. (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
  371. "Set `org-publish-temp-files' with files from BASE-DIR directory.
  372. If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
  373. non-nil, restrict this list to the files matching the regexp
  374. MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
  375. SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
  376. matching the regexp SKIP-DIR when recursing through BASE-DIR."
  377. (mapc (lambda (f)
  378. (let ((fd-p (file-directory-p f))
  379. (fnd (file-name-nondirectory f)))
  380. (if (and fd-p recurse
  381. (not (string-match "^\\.+$" fnd))
  382. (if skip-dir (not (string-match skip-dir fnd)) t))
  383. (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
  384. (unless (or fd-p ;; this is a directory
  385. (and skip-file (string-match skip-file fnd))
  386. (not (file-exists-p (file-truename f)))
  387. (not (string-match match fnd)))
  388. (pushnew f org-publish-temp-files)))))
  389. (if org-sitemap-requested
  390. (sort (directory-files base-dir t (unless recurse match))
  391. 'org-publish-compare-directory-files)
  392. (directory-files base-dir t (unless recurse match)))))
  393. (defun org-publish-get-base-files (project &optional exclude-regexp)
  394. "Return a list of all files in PROJECT.
  395. If EXCLUDE-REGEXP is set, this will be used to filter out
  396. matching filenames."
  397. (let* ((project-plist (cdr project))
  398. (base-dir (file-name-as-directory
  399. (plist-get project-plist :base-directory)))
  400. (include-list (plist-get project-plist :include))
  401. (recurse (plist-get project-plist :recursive))
  402. (extension (or (plist-get project-plist :base-extension) "org"))
  403. ;; sitemap-... variables are dynamically scoped for
  404. ;; org-publish-compare-directory-files:
  405. (org-sitemap-requested
  406. (plist-get project-plist :auto-sitemap))
  407. (sitemap-filename
  408. (or (plist-get project-plist :sitemap-filename)
  409. "sitemap.org"))
  410. (org-sitemap-sort-folders
  411. (if (plist-member project-plist :sitemap-sort-folders)
  412. (plist-get project-plist :sitemap-sort-folders)
  413. org-publish-sitemap-sort-folders))
  414. (org-sitemap-sort-files
  415. (cond ((plist-member project-plist :sitemap-sort-files)
  416. (plist-get project-plist :sitemap-sort-files))
  417. ;; For backward compatibility:
  418. ((plist-member project-plist :sitemap-alphabetically)
  419. (if (plist-get project-plist :sitemap-alphabetically)
  420. 'alphabetically nil))
  421. (t org-publish-sitemap-sort-files)))
  422. (org-sitemap-ignore-case
  423. (if (plist-member project-plist :sitemap-ignore-case)
  424. (plist-get project-plist :sitemap-ignore-case)
  425. org-publish-sitemap-sort-ignore-case))
  426. (match (if (eq extension 'any)
  427. "^[^\\.]"
  428. (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
  429. ;; Make sure `org-sitemap-sort-folders' has an accepted value
  430. (unless (memq org-sitemap-sort-folders '(first last))
  431. (setq org-sitemap-sort-folders nil))
  432. (setq org-publish-temp-files nil)
  433. (if org-sitemap-requested
  434. (pushnew (expand-file-name (concat base-dir sitemap-filename))
  435. org-publish-temp-files))
  436. (org-publish-get-base-files-1 base-dir recurse match
  437. ;; FIXME distinguish exclude regexp
  438. ;; for skip-file and skip-dir?
  439. exclude-regexp exclude-regexp)
  440. (mapc (lambda (f)
  441. (pushnew
  442. (expand-file-name (concat base-dir f))
  443. org-publish-temp-files))
  444. include-list)
  445. org-publish-temp-files))
  446. (defun org-publish-get-project-from-filename (filename &optional up)
  447. "Return the project that FILENAME belongs to."
  448. (let* ((filename (expand-file-name filename))
  449. project-name)
  450. (catch 'p-found
  451. (dolist (prj org-publish-project-alist)
  452. (unless (plist-get (cdr prj) :components)
  453. ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
  454. (let* ((r (plist-get (cdr prj) :recursive))
  455. (b (expand-file-name (file-name-as-directory
  456. (plist-get (cdr prj) :base-directory))))
  457. (x (or (plist-get (cdr prj) :base-extension) "org"))
  458. (e (plist-get (cdr prj) :exclude))
  459. (i (plist-get (cdr prj) :include))
  460. (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
  461. (when
  462. (or
  463. (and
  464. i (member filename
  465. (mapcar
  466. (lambda (file) (expand-file-name file b))
  467. i)))
  468. (and
  469. (not (and e (string-match e filename)))
  470. (string-match xm filename)))
  471. (setq project-name (car prj))
  472. (throw 'p-found project-name))))))
  473. (when up
  474. (dolist (prj org-publish-project-alist)
  475. (if (member project-name (plist-get (cdr prj) :components))
  476. (setq project-name (car prj)))))
  477. (assoc project-name org-publish-project-alist)))
  478. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  479. ;;; Pluggable publishing back-end functions
  480. (defun org-publish-org-to (format plist filename pub-dir)
  481. "Publish an org file to FORMAT.
  482. PLIST is the property list for the given project.
  483. FILENAME is the filename of the org file to be published.
  484. PUB-DIR is the publishing directory."
  485. (require 'org)
  486. (unless (file-exists-p pub-dir)
  487. (make-directory pub-dir t))
  488. (let ((visiting (find-buffer-visiting filename)))
  489. (save-excursion
  490. (org-pop-to-buffer-same-window (or visiting (find-file filename)))
  491. (let* ((plist (cons :buffer-will-be-killed (cons t plist)))
  492. (init-buf (current-buffer))
  493. (init-point (point))
  494. (init-buf-string (buffer-string))
  495. export-buf-or-file)
  496. ;; run hooks before exporting
  497. (run-hooks 'org-publish-before-export-hook)
  498. ;; export the possibly modified buffer
  499. (setq export-buf-or-file
  500. (funcall (intern (concat "org-export-as-" format))
  501. (plist-get plist :headline-levels)
  502. nil plist nil
  503. (plist-get plist :body-only)
  504. pub-dir))
  505. (when (and (bufferp export-buf-or-file)
  506. (buffer-live-p export-buf-or-file))
  507. (set-buffer export-buf-or-file)
  508. ;; run hooks after export and save export
  509. (progn (run-hooks 'org-publish-after-export-hook)
  510. (if (buffer-modified-p) (save-buffer)))
  511. (kill-buffer export-buf-or-file))
  512. ;; maybe restore buffer's content
  513. (set-buffer init-buf)
  514. (when (buffer-modified-p init-buf)
  515. (erase-buffer)
  516. (insert init-buf-string)
  517. (save-buffer)
  518. (goto-char init-point))
  519. (unless visiting
  520. (kill-buffer init-buf))))))
  521. (defmacro org-publish-with-aux-preprocess-maybe (&rest body)
  522. "Execute BODY with a modified hook to preprocess for index."
  523. `(let ((org-export-preprocess-after-headline-targets-hook
  524. (if (plist-get project-plist :makeindex)
  525. (cons 'org-publish-aux-preprocess
  526. org-export-preprocess-after-headline-targets-hook)
  527. org-export-preprocess-after-headline-targets-hook)))
  528. ,@body))
  529. (def-edebug-spec org-publish-with-aux-preprocess-maybe (body))
  530. (defvar project-plist)
  531. (defun org-publish-org-to-latex (plist filename pub-dir)
  532. "Publish an org file to LaTeX.
  533. See `org-publish-org-to' to the list of arguments."
  534. (org-publish-with-aux-preprocess-maybe
  535. (org-publish-org-to "latex" plist filename pub-dir)))
  536. (defun org-publish-org-to-pdf (plist filename pub-dir)
  537. "Publish an org file to PDF (via LaTeX).
  538. See `org-publish-org-to' to the list of arguments."
  539. (org-publish-with-aux-preprocess-maybe
  540. (org-publish-org-to "pdf" plist filename pub-dir)))
  541. (defun org-publish-org-to-html (plist filename pub-dir)
  542. "Publish an org file to HTML.
  543. See `org-publish-org-to' to the list of arguments."
  544. (org-publish-with-aux-preprocess-maybe
  545. (org-publish-org-to "html" plist filename pub-dir)))
  546. (defun org-publish-org-to-org (plist filename pub-dir)
  547. "Publish an org file to HTML.
  548. See `org-publish-org-to' to the list of arguments."
  549. (org-publish-org-to "org" plist filename pub-dir))
  550. (defun org-publish-org-to-ascii (plist filename pub-dir)
  551. "Publish an org file to ASCII.
  552. See `org-publish-org-to' to the list of arguments."
  553. (org-publish-with-aux-preprocess-maybe
  554. (org-publish-org-to "ascii" plist filename pub-dir)))
  555. (defun org-publish-org-to-latin1 (plist filename pub-dir)
  556. "Publish an org file to Latin-1.
  557. See `org-publish-org-to' to the list of arguments."
  558. (org-publish-with-aux-preprocess-maybe
  559. (org-publish-org-to "latin1" plist filename pub-dir)))
  560. (defun org-publish-org-to-utf8 (plist filename pub-dir)
  561. "Publish an org file to UTF-8.
  562. See `org-publish-org-to' to the list of arguments."
  563. (org-publish-with-aux-preprocess-maybe
  564. (org-publish-org-to "utf8" plist filename pub-dir)))
  565. (defun org-publish-org-to-taskjuggler (plist filename pub-dir)
  566. "Publish an org file to TaskJuggler.
  567. See `org-publish-org-to' to the list of arguments."
  568. (org-publish-with-aux-preprocess-maybe
  569. (org-publish-org-to "taskjuggler" plist filename pub-dir)))
  570. (defun org-publish-attachment (plist filename pub-dir)
  571. "Publish a file with no transformation of any kind.
  572. See `org-publish-org-to' to the list of arguments."
  573. ;; make sure eshell/cp code is loaded
  574. (unless (file-directory-p pub-dir)
  575. (make-directory pub-dir t))
  576. (or (equal (expand-file-name (file-name-directory filename))
  577. (file-name-as-directory (expand-file-name pub-dir)))
  578. (copy-file filename
  579. (expand-file-name (file-name-nondirectory filename) pub-dir)
  580. t)))
  581. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  582. ;;; Publishing files, sets of files, and indices
  583. (defun org-publish-file (filename &optional project no-cache)
  584. "Publish file FILENAME from PROJECT.
  585. If NO-CACHE is not nil, do not initialize org-publish-cache and
  586. write it to disk. This is needed, since this function is used to
  587. publish single files, when entire projects are published.
  588. See `org-publish-projects'."
  589. (let* ((project
  590. (or project
  591. (or (org-publish-get-project-from-filename filename)
  592. (error "File %s not part of any known project"
  593. (abbreviate-file-name filename)))))
  594. (project-plist (cdr project))
  595. (ftname (expand-file-name filename))
  596. (publishing-function
  597. (or (plist-get project-plist :publishing-function)
  598. 'org-publish-org-to-html))
  599. (base-dir
  600. (file-name-as-directory
  601. (expand-file-name
  602. (or (plist-get project-plist :base-directory)
  603. (error "Project %s does not have :base-directory defined"
  604. (car project))))))
  605. (pub-dir
  606. (file-name-as-directory
  607. (file-truename
  608. (or (eval (plist-get project-plist :publishing-directory))
  609. (error "Project %s does not have :publishing-directory defined"
  610. (car project))))))
  611. tmp-pub-dir)
  612. (unless no-cache
  613. (org-publish-initialize-cache (car project)))
  614. (setq tmp-pub-dir
  615. (file-name-directory
  616. (concat pub-dir
  617. (and (string-match (regexp-quote base-dir) ftname)
  618. (substring ftname (match-end 0))))))
  619. (if (listp publishing-function)
  620. ;; allow chain of publishing functions
  621. (mapc (lambda (f)
  622. (when (org-publish-needed-p filename pub-dir f tmp-pub-dir base-dir)
  623. (funcall f project-plist filename tmp-pub-dir)
  624. (org-publish-update-timestamp filename pub-dir f base-dir)))
  625. publishing-function)
  626. (when (org-publish-needed-p filename pub-dir publishing-function tmp-pub-dir base-dir)
  627. (funcall publishing-function project-plist filename tmp-pub-dir)
  628. (org-publish-update-timestamp
  629. filename pub-dir publishing-function base-dir)))
  630. (unless no-cache (org-publish-write-cache-file))))
  631. (defun org-publish-projects (projects)
  632. "Publish all files belonging to the PROJECTS alist.
  633. If :auto-sitemap is set, publish the sitemap too.
  634. If :makeindex is set, also produce a file theindex.org."
  635. (mapc
  636. (lambda (project)
  637. ;; Each project uses its own cache file:
  638. (org-publish-initialize-cache (car project))
  639. (let*
  640. ((project-plist (cdr project))
  641. (exclude-regexp (plist-get project-plist :exclude))
  642. (sitemap-p (plist-get project-plist :auto-sitemap))
  643. (sitemap-filename (or (plist-get project-plist :sitemap-filename)
  644. "sitemap.org"))
  645. (sitemap-function (or (plist-get project-plist :sitemap-function)
  646. 'org-publish-org-sitemap))
  647. (org-sitemap-date-format (or (plist-get project-plist :sitemap-date-format)
  648. org-publish-sitemap-date-format))
  649. (org-sitemap-file-entry-format (or (plist-get project-plist :sitemap-file-entry-format)
  650. org-publish-sitemap-file-entry-format))
  651. (preparation-function (plist-get project-plist :preparation-function))
  652. (completion-function (plist-get project-plist :completion-function))
  653. (files (org-publish-get-base-files project exclude-regexp)) file)
  654. (when preparation-function (run-hooks 'preparation-function))
  655. (if sitemap-p (funcall sitemap-function project sitemap-filename))
  656. (while (setq file (pop files))
  657. (org-publish-file file project t))
  658. (when (plist-get project-plist :makeindex)
  659. (org-publish-index-generate-theindex
  660. (plist-get project-plist :base-directory))
  661. (org-publish-file (expand-file-name
  662. "theindex.org"
  663. (plist-get project-plist :base-directory))
  664. project t))
  665. (when completion-function (run-hooks 'completion-function))
  666. (org-publish-write-cache-file)))
  667. (org-publish-expand-projects projects)))
  668. (defun org-publish-org-sitemap (project &optional sitemap-filename)
  669. "Create a sitemap of pages in set defined by PROJECT.
  670. Optionally set the filename of the sitemap with SITEMAP-FILENAME.
  671. Default for SITEMAP-FILENAME is 'sitemap.org'."
  672. (let* ((project-plist (cdr project))
  673. (dir (file-name-as-directory
  674. (plist-get project-plist :base-directory)))
  675. (localdir (file-name-directory dir))
  676. (indent-str (make-string 2 ?\ ))
  677. (exclude-regexp (plist-get project-plist :exclude))
  678. (files (nreverse (org-publish-get-base-files project exclude-regexp)))
  679. (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
  680. (sitemap-title (or (plist-get project-plist :sitemap-title)
  681. (concat "Sitemap for project " (car project))))
  682. (sitemap-style (or (plist-get project-plist :sitemap-style)
  683. 'tree))
  684. (sitemap-sans-extension (plist-get project-plist :sitemap-sans-extension))
  685. (visiting (find-buffer-visiting sitemap-filename))
  686. (ifn (file-name-nondirectory sitemap-filename))
  687. file sitemap-buffer)
  688. (with-current-buffer (setq sitemap-buffer
  689. (or visiting (find-file sitemap-filename)))
  690. (erase-buffer)
  691. (insert (concat "#+TITLE: " sitemap-title "\n\n"))
  692. (while (setq file (pop files))
  693. (let ((fn (file-name-nondirectory file))
  694. (link (file-relative-name file dir))
  695. (oldlocal localdir))
  696. (when sitemap-sans-extension
  697. (setq link (file-name-sans-extension link)))
  698. ;; sitemap shouldn't list itself
  699. (unless (equal (file-truename sitemap-filename)
  700. (file-truename file))
  701. (if (eq sitemap-style 'list)
  702. (message "Generating list-style sitemap for %s" sitemap-title)
  703. (message "Generating tree-style sitemap for %s" sitemap-title)
  704. (setq localdir (concat (file-name-as-directory dir)
  705. (file-name-directory link)))
  706. (unless (string= localdir oldlocal)
  707. (if (string= localdir dir)
  708. (setq indent-str (make-string 2 ?\ ))
  709. (let ((subdirs
  710. (split-string
  711. (directory-file-name
  712. (file-name-directory
  713. (file-relative-name localdir dir))) "/"))
  714. (subdir "")
  715. (old-subdirs (split-string
  716. (file-relative-name oldlocal dir) "/")))
  717. (setq indent-str (make-string 2 ?\ ))
  718. (while (string= (car old-subdirs) (car subdirs))
  719. (setq indent-str (concat indent-str (make-string 2 ?\ )))
  720. (pop old-subdirs)
  721. (pop subdirs))
  722. (dolist (d subdirs)
  723. (setq subdir (concat subdir d "/"))
  724. (insert (concat indent-str " + " d "\n"))
  725. (setq indent-str (make-string
  726. (+ (length indent-str) 2) ?\ )))))))
  727. ;; This is common to 'flat and 'tree
  728. (let ((entry
  729. (org-publish-format-file-entry org-sitemap-file-entry-format
  730. file project-plist))
  731. (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
  732. (cond ((string-match-p regexp entry)
  733. (string-match regexp entry)
  734. (insert (concat indent-str " + " (match-string 1 entry)
  735. "[[file:" link "]["
  736. (match-string 2 entry)
  737. "]]" (match-string 3 entry) "\n")))
  738. (t
  739. (insert (concat indent-str " + [[file:" link "]["
  740. entry
  741. "]]\n"))))))))
  742. (save-buffer))
  743. (or visiting (kill-buffer sitemap-buffer))))
  744. (defun org-publish-format-file-entry (fmt file project-plist)
  745. (format-spec fmt
  746. `((?t . ,(org-publish-find-title file t))
  747. (?d . ,(format-time-string org-sitemap-date-format
  748. (org-publish-find-date file)))
  749. (?a . ,(or (plist-get project-plist :author) user-full-name)))))
  750. (defun org-publish-find-title (file &optional reset)
  751. "Find the title of FILE in project."
  752. (or
  753. (and (not reset) (org-publish-cache-get-file-property file :title nil t))
  754. (let* ((visiting (find-buffer-visiting file))
  755. (buffer (or visiting (find-file-noselect file)))
  756. title)
  757. (with-current-buffer buffer
  758. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  759. (org-infile-export-plist))))
  760. (setq title
  761. (or (plist-get opt-plist :title)
  762. (and (not
  763. (plist-get opt-plist :skip-before-1st-heading))
  764. (org-export-grab-title-from-buffer))
  765. (file-name-nondirectory (file-name-sans-extension file))))))
  766. (unless visiting
  767. (kill-buffer buffer))
  768. (org-publish-cache-set-file-property file :title title)
  769. title)))
  770. (defun org-publish-find-date (file)
  771. "Find the date of FILE in project.
  772. If FILE provides a #+date keyword use it else use the file
  773. system's modification time.
  774. It returns time in `current-time' format."
  775. (let ((visiting (find-buffer-visiting file)))
  776. (save-excursion
  777. (org-pop-to-buffer-same-window (or visiting (find-file-noselect file nil t)))
  778. (let* ((plist (org-infile-export-plist))
  779. (date (plist-get plist :date)))
  780. (unless visiting
  781. (kill-buffer (current-buffer)))
  782. (if date
  783. (org-time-string-to-time date)
  784. (when (file-exists-p file)
  785. (nth 5 (file-attributes file))))))))
  786. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  787. ;;; Interactive publishing functions
  788. ;;;###autoload
  789. (defalias 'org-publish-project 'org-publish)
  790. ;;;###autoload
  791. (defun org-publish (project &optional force)
  792. "Publish PROJECT."
  793. (interactive
  794. (list
  795. (assoc (org-icompleting-read
  796. "Publish project: "
  797. org-publish-project-alist nil t)
  798. org-publish-project-alist)
  799. current-prefix-arg))
  800. (setq org-publish-initial-buffer (current-buffer))
  801. (save-window-excursion
  802. (let* ((org-publish-use-timestamps-flag
  803. (if force nil org-publish-use-timestamps-flag)))
  804. (org-publish-projects
  805. (if (stringp project)
  806. ;; If this function is called in batch mode,
  807. ;; project is still a string here.
  808. (list (assoc project org-publish-project-alist))
  809. (list project))))))
  810. ;;;###autoload
  811. (defun org-publish-all (&optional force)
  812. "Publish all projects.
  813. With prefix argument, remove all files in the timestamp
  814. directory and force publishing all files."
  815. (interactive "P")
  816. (when force
  817. (org-publish-remove-all-timestamps))
  818. (save-window-excursion
  819. (let ((org-publish-use-timestamps-flag
  820. (if force nil org-publish-use-timestamps-flag)))
  821. (org-publish-projects org-publish-project-alist))))
  822. ;;;###autoload
  823. (defun org-publish-current-file (&optional force)
  824. "Publish the current file.
  825. With prefix argument, force publish the file."
  826. (interactive "P")
  827. (save-window-excursion
  828. (let ((org-publish-use-timestamps-flag
  829. (if force nil org-publish-use-timestamps-flag)))
  830. (org-publish-file (buffer-file-name)))))
  831. ;;;###autoload
  832. (defun org-publish-current-project (&optional force)
  833. "Publish the project associated with the current file.
  834. With a prefix argument, force publishing of all files in
  835. the project."
  836. (interactive "P")
  837. (save-window-excursion
  838. (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up))
  839. (org-publish-use-timestamps-flag
  840. (if force nil org-publish-use-timestamps-flag)))
  841. (if (not project)
  842. (error "File %s is not part of any known project" (buffer-file-name)))
  843. ;; FIXME: force is not used here?
  844. (org-publish project))))
  845. ;;; Index generation
  846. (defun org-publish-aux-preprocess ()
  847. "Find index entries and write them to an .orgx file."
  848. (let ((case-fold-search t)
  849. entry index target)
  850. (goto-char (point-min))
  851. (while
  852. (and
  853. (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t)
  854. (> (match-end 1) (match-beginning 1)))
  855. (setq entry (match-string 1))
  856. (when (eq org-export-current-backend 'latex)
  857. (replace-match (format "\\index{%s}" entry) t t))
  858. (save-excursion
  859. (ignore-errors (org-back-to-heading t))
  860. (setq target (get-text-property (point) 'target))
  861. (setq target (or (cdr (assoc target org-export-preferred-target-alist))
  862. (cdr (assoc target org-export-id-target-alist))
  863. target ""))
  864. (push (cons entry target) index)))
  865. (with-temp-file
  866. (concat
  867. (file-name-directory org-current-export-file) "."
  868. (file-name-sans-extension
  869. (file-name-nondirectory org-current-export-file)) ".orgx")
  870. (dolist (entry (nreverse index))
  871. (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
  872. (defun org-publish-index-generate-theindex (directory)
  873. "Generate the index from all .orgx files in DIRECTORY."
  874. (require 'find-lisp)
  875. (let* ((fulldir (file-name-as-directory
  876. (expand-file-name directory)))
  877. (full-files (find-lisp-find-files directory "\\.orgx\\'"))
  878. (re (concat "\\`" fulldir))
  879. (files (mapcar (lambda (f) (if (string-match re f)
  880. (substring f (match-end 0))
  881. f))
  882. full-files))
  883. (default-directory directory)
  884. index origfile buf target entry ibuffer
  885. main last-main letter last-letter file sub link tgext)
  886. ;; `files' contains the list of relative file names
  887. (dolist (file files)
  888. (setq origfile
  889. (concat (file-name-directory file)
  890. (substring (file-name-nondirectory file) 1 -1)))
  891. (setq buf (find-file-noselect file))
  892. (with-current-buffer buf
  893. (goto-char (point-min))
  894. (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
  895. (setq target (match-string 1)
  896. entry (match-string 2))
  897. (push (list entry origfile target) index)))
  898. (kill-buffer buf))
  899. (setq index (sort index (lambda (a b) (string< (downcase (car a))
  900. (downcase (car b))))))
  901. (setq ibuffer (find-file-noselect (expand-file-name "theindex.inc" directory)))
  902. (with-current-buffer ibuffer
  903. (erase-buffer)
  904. (insert "* Index\n")
  905. (setq last-letter nil)
  906. (dolist (idx index)
  907. (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
  908. (if (and (stringp target) (string-match "\\S-" target))
  909. (setq tgext (concat "::#" target))
  910. (setq tgext ""))
  911. (setq letter (upcase (substring entry 0 1)))
  912. (when (not (equal letter last-letter))
  913. (insert "** " letter "\n")
  914. (setq last-letter letter))
  915. (if (string-match "!" entry)
  916. (setq main (substring entry 0 (match-beginning 0))
  917. sub (substring entry (match-end 0)))
  918. (setq main nil sub nil last-main nil))
  919. (when (and main (not (equal main last-main)))
  920. (insert " - " main "\n")
  921. (setq last-main main))
  922. (setq link (concat "[[file:" file tgext "]"
  923. "[" (or sub entry) "]]"))
  924. (if (and main sub)
  925. (insert " - " link "\n")
  926. (insert " - " link "\n")))
  927. (save-buffer))
  928. (kill-buffer ibuffer)
  929. ;; Create theindex.org if it doesn't exist already
  930. (let ((index-file (expand-file-name "theindex.org" directory)))
  931. (unless (file-exists-p index-file)
  932. (setq ibuffer (find-file-noselect index-file))
  933. (with-current-buffer ibuffer
  934. (erase-buffer)
  935. (insert "\n\n#+INCLUDE: \"theindex.inc\"\n\n")
  936. (save-buffer))
  937. (kill-buffer ibuffer)))))
  938. ;; Caching functions:
  939. (defun org-publish-write-cache-file (&optional free-cache)
  940. "Write `org-publish-cache' to file.
  941. If FREE-CACHE, empty the cache."
  942. (or org-publish-cache
  943. (error "`org-publish-write-cache-file' called, but no cache present"))
  944. (let ((cache-file (org-publish-cache-get ":cache-file:")))
  945. (or cache-file
  946. (error "Cannot find cache-file name in `org-publish-write-cache-file'"))
  947. (with-temp-file cache-file
  948. (let ((print-level nil)
  949. (print-length nil))
  950. (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
  951. (maphash (lambda (k v)
  952. (insert
  953. (format (concat "(puthash %S "
  954. (if (or (listp v) (symbolp v))
  955. "'" "")
  956. "%S org-publish-cache)\n") k v)))
  957. org-publish-cache)))
  958. (when free-cache (org-publish-reset-cache))))
  959. (defun org-publish-initialize-cache (project-name)
  960. "Initialize the projects cache if not initialized yet and return it."
  961. (or project-name
  962. (error "Cannot initialize `org-publish-cache' without projects name in `org-publish-initialize-cache'"))
  963. (unless (file-exists-p org-publish-timestamp-directory)
  964. (make-directory org-publish-timestamp-directory t))
  965. (if (not (file-directory-p org-publish-timestamp-directory))
  966. (error "Org publish timestamp: %s is not a directory"
  967. org-publish-timestamp-directory))
  968. (unless (and org-publish-cache
  969. (string= (org-publish-cache-get ":project:") project-name))
  970. (let* ((cache-file (concat
  971. (expand-file-name org-publish-timestamp-directory)
  972. project-name
  973. ".cache"))
  974. (cexists (file-exists-p cache-file)))
  975. (when org-publish-cache
  976. (org-publish-reset-cache))
  977. (if cexists
  978. (load-file cache-file)
  979. (setq org-publish-cache
  980. (make-hash-table :test 'equal :weakness nil :size 100))
  981. (org-publish-cache-set ":project:" project-name)
  982. (org-publish-cache-set ":cache-file:" cache-file))
  983. (unless cexists (org-publish-write-cache-file nil))))
  984. org-publish-cache)
  985. (defun org-publish-reset-cache ()
  986. "Empty org-publish-cache and reset it nil."
  987. (message "%s" "Resetting org-publish-cache")
  988. (if (hash-table-p org-publish-cache)
  989. (clrhash org-publish-cache))
  990. (setq org-publish-cache nil))
  991. (defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func base-dir)
  992. "Check the timestamp of the last publishing of FILENAME.
  993. Return `t', if the file needs publishing. The function also
  994. checks if any included files have been more recently published,
  995. so that the file including them will be republished as well."
  996. (or org-publish-cache
  997. (error "`org-publish-cache-file-needs-publishing' called, but no cache present"))
  998. (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func))
  999. (pstamp (org-publish-cache-get key))
  1000. (visiting (find-buffer-visiting filename))
  1001. (case-fold-search t)
  1002. included-files-ctime buf)
  1003. (when (equal (file-name-extension filename) "org")
  1004. (setq buf (find-file (expand-file-name filename)))
  1005. (with-current-buffer buf
  1006. (goto-char (point-min))
  1007. (while (re-search-forward "^#\\+include:[ \t]+\"\\([^\t\n\r\"]*\\)\"[ \t]*.*$" nil t)
  1008. (let* ((included-file (expand-file-name (match-string 1))))
  1009. (add-to-list 'included-files-ctime
  1010. (org-publish-cache-ctime-of-src included-file) t))))
  1011. ;; FIXME don't kill current buffer
  1012. (unless visiting (kill-buffer buf)))
  1013. (if (null pstamp)
  1014. t
  1015. (let ((ctime (org-publish-cache-ctime-of-src filename)))
  1016. (or (< pstamp ctime)
  1017. (when included-files-ctime
  1018. (not (null (delq nil (mapcar (lambda(ct) (< ctime ct))
  1019. included-files-ctime))))))))))
  1020. (defun org-publish-cache-set-file-property (filename property value &optional project-name)
  1021. "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
  1022. Use cache file of PROJECT-NAME. If the entry does not exist, it will be
  1023. created. Return VALUE."
  1024. ;; Evtl. load the requested cache file:
  1025. (if project-name (org-publish-initialize-cache project-name))
  1026. (let ((pl (org-publish-cache-get filename)))
  1027. (if pl
  1028. (progn
  1029. (plist-put pl property value)
  1030. value)
  1031. (org-publish-cache-get-file-property
  1032. filename property value nil project-name))))
  1033. (defun org-publish-cache-get-file-property
  1034. (filename property &optional default no-create project-name)
  1035. "Return the value for a PROPERTY of file FILENAME in publishing cache.
  1036. Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
  1037. DEFAULT, if the value does not yet exist.
  1038. If the entry will be created, unless NO-CREATE is not nil."
  1039. ;; Evtl. load the requested cache file:
  1040. (if project-name (org-publish-initialize-cache project-name))
  1041. (let ((pl (org-publish-cache-get filename))
  1042. (retval nil))
  1043. (if pl
  1044. (if (plist-member pl property)
  1045. (setq retval (plist-get pl property))
  1046. (setq retval default))
  1047. ;; no pl yet:
  1048. (unless no-create
  1049. (org-publish-cache-set filename (list property default)))
  1050. (setq retval default))
  1051. retval))
  1052. (defun org-publish-cache-get (key)
  1053. "Return the value stored in `org-publish-cache' for key KEY.
  1054. Returns nil, if no value or nil is found, or the cache does not
  1055. exist."
  1056. (or org-publish-cache
  1057. (error "`org-publish-cache-get' called, but no cache present"))
  1058. (gethash key org-publish-cache))
  1059. (defun org-publish-cache-set (key value)
  1060. "Store KEY VALUE pair in `org-publish-cache'.
  1061. Returns value on success, else nil."
  1062. (or org-publish-cache
  1063. (error "`org-publish-cache-set' called, but no cache present"))
  1064. (puthash key value org-publish-cache))
  1065. (defun org-publish-cache-ctime-of-src (file)
  1066. "Get the ctime of filename F as an integer."
  1067. (let ((attr (file-attributes
  1068. (expand-file-name (or (file-symlink-p file) file)
  1069. (file-name-directory file)))))
  1070. (+ (lsh (car (nth 5 attr)) 16)
  1071. (cadr (nth 5 attr)))))
  1072. (provide 'org-publish)
  1073. ;; Local variables:
  1074. ;; generated-autoload-file: "org-loaddefs.el"
  1075. ;; End:
  1076. ;;; org-publish.el ends here