org-publish.el 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  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)
  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)
  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)
  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-attachment (plist filename pub-dir)
  566. "Publish a file with no transformation of any kind.
  567. See `org-publish-org-to' to the list of arguments."
  568. ;; make sure eshell/cp code is loaded
  569. (unless (file-directory-p pub-dir)
  570. (make-directory pub-dir t))
  571. (or (equal (expand-file-name (file-name-directory filename))
  572. (file-name-as-directory (expand-file-name pub-dir)))
  573. (copy-file filename
  574. (expand-file-name (file-name-nondirectory filename) pub-dir)
  575. t)))
  576. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  577. ;;; Publishing files, sets of files, and indices
  578. (defun org-publish-file (filename &optional project no-cache)
  579. "Publish file FILENAME from PROJECT.
  580. If NO-CACHE is not nil, do not initialize org-publish-cache and
  581. write it to disk. This is needed, since this function is used to
  582. publish single files, when entire projects are published.
  583. See `org-publish-projects'."
  584. (let* ((project
  585. (or project
  586. (or (org-publish-get-project-from-filename filename)
  587. (error "File %s not part of any known project"
  588. (abbreviate-file-name filename)))))
  589. (project-plist (cdr project))
  590. (ftname (expand-file-name filename))
  591. (publishing-function
  592. (or (plist-get project-plist :publishing-function)
  593. 'org-publish-org-to-html))
  594. (base-dir
  595. (file-name-as-directory
  596. (expand-file-name
  597. (or (plist-get project-plist :base-directory)
  598. (error "Project %s does not have :base-directory defined"
  599. (car project))))))
  600. (pub-dir
  601. (file-name-as-directory
  602. (file-truename
  603. (or (eval (plist-get project-plist :publishing-directory))
  604. (error "Project %s does not have :publishing-directory defined"
  605. (car project))))))
  606. tmp-pub-dir)
  607. (unless no-cache
  608. (org-publish-initialize-cache (car project)))
  609. (setq tmp-pub-dir
  610. (file-name-directory
  611. (concat pub-dir
  612. (and (string-match (regexp-quote base-dir) ftname)
  613. (substring ftname (match-end 0))))))
  614. (if (listp publishing-function)
  615. ;; allow chain of publishing functions
  616. (mapc (lambda (f)
  617. (when (org-publish-needed-p filename pub-dir f tmp-pub-dir)
  618. (funcall f project-plist filename tmp-pub-dir)
  619. (org-publish-update-timestamp filename pub-dir f)))
  620. publishing-function)
  621. (when (org-publish-needed-p filename pub-dir publishing-function
  622. tmp-pub-dir)
  623. (funcall publishing-function project-plist filename tmp-pub-dir)
  624. (org-publish-update-timestamp
  625. filename pub-dir publishing-function)))
  626. (unless no-cache (org-publish-write-cache-file))))
  627. (defun org-publish-projects (projects)
  628. "Publish all files belonging to the PROJECTS alist.
  629. If :auto-sitemap is set, publish the sitemap too.
  630. If :makeindex is set, also produce a file theindex.org."
  631. (mapc
  632. (lambda (project)
  633. ;; Each project uses its own cache file:
  634. (org-publish-initialize-cache (car project))
  635. (let*
  636. ((project-plist (cdr project))
  637. (exclude-regexp (plist-get project-plist :exclude))
  638. (sitemap-p (plist-get project-plist :auto-sitemap))
  639. (sitemap-filename (or (plist-get project-plist :sitemap-filename)
  640. "sitemap.org"))
  641. (sitemap-function (or (plist-get project-plist :sitemap-function)
  642. 'org-publish-org-sitemap))
  643. (org-sitemap-date-format (or (plist-get project-plist :sitemap-date-format)
  644. org-publish-sitemap-date-format))
  645. (org-sitemap-file-entry-format (or (plist-get project-plist :sitemap-file-entry-format)
  646. org-publish-sitemap-file-entry-format))
  647. (preparation-function (plist-get project-plist :preparation-function))
  648. (completion-function (plist-get project-plist :completion-function))
  649. (files (org-publish-get-base-files project exclude-regexp)) file)
  650. (when preparation-function (run-hooks 'preparation-function))
  651. (if sitemap-p (funcall sitemap-function project sitemap-filename))
  652. (while (setq file (pop files))
  653. (org-publish-file file project t))
  654. (when (plist-get project-plist :makeindex)
  655. (org-publish-index-generate-theindex
  656. (plist-get project-plist :base-directory))
  657. (org-publish-file (expand-file-name
  658. "theindex.org"
  659. (plist-get project-plist :base-directory))
  660. project t))
  661. (when completion-function (run-hooks 'completion-function))
  662. (org-publish-write-cache-file)))
  663. (org-publish-expand-projects projects)))
  664. (defun org-publish-org-sitemap (project &optional sitemap-filename)
  665. "Create a sitemap of pages in set defined by PROJECT.
  666. Optionally set the filename of the sitemap with SITEMAP-FILENAME.
  667. Default for SITEMAP-FILENAME is 'sitemap.org'."
  668. (let* ((project-plist (cdr project))
  669. (dir (file-name-as-directory
  670. (plist-get project-plist :base-directory)))
  671. (localdir (file-name-directory dir))
  672. (indent-str (make-string 2 ?\ ))
  673. (exclude-regexp (plist-get project-plist :exclude))
  674. (files (nreverse (org-publish-get-base-files project exclude-regexp)))
  675. (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
  676. (sitemap-title (or (plist-get project-plist :sitemap-title)
  677. (concat "Sitemap for project " (car project))))
  678. (sitemap-style (or (plist-get project-plist :sitemap-style)
  679. 'tree))
  680. (sitemap-sans-extension (plist-get project-plist :sitemap-sans-extension))
  681. (visiting (find-buffer-visiting sitemap-filename))
  682. (ifn (file-name-nondirectory sitemap-filename))
  683. file sitemap-buffer)
  684. (with-current-buffer (setq sitemap-buffer
  685. (or visiting (find-file sitemap-filename)))
  686. (erase-buffer)
  687. (insert (concat "#+TITLE: " sitemap-title "\n\n"))
  688. (while (setq file (pop files))
  689. (let ((fn (file-name-nondirectory file))
  690. (link (file-relative-name file dir))
  691. (oldlocal localdir))
  692. (when sitemap-sans-extension
  693. (setq link (file-name-sans-extension link)))
  694. ;; sitemap shouldn't list itself
  695. (unless (equal (file-truename sitemap-filename)
  696. (file-truename file))
  697. (if (eq sitemap-style 'list)
  698. (message "Generating list-style sitemap for %s" sitemap-title)
  699. (message "Generating tree-style sitemap for %s" sitemap-title)
  700. (setq localdir (concat (file-name-as-directory dir)
  701. (file-name-directory link)))
  702. (unless (string= localdir oldlocal)
  703. (if (string= localdir dir)
  704. (setq indent-str (make-string 2 ?\ ))
  705. (let ((subdirs
  706. (split-string
  707. (directory-file-name
  708. (file-name-directory
  709. (file-relative-name localdir dir))) "/"))
  710. (subdir "")
  711. (old-subdirs (split-string
  712. (file-relative-name oldlocal dir) "/")))
  713. (setq indent-str (make-string 2 ?\ ))
  714. (while (string= (car old-subdirs) (car subdirs))
  715. (setq indent-str (concat indent-str (make-string 2 ?\ )))
  716. (pop old-subdirs)
  717. (pop subdirs))
  718. (dolist (d subdirs)
  719. (setq subdir (concat subdir d "/"))
  720. (insert (concat indent-str " + " d "\n"))
  721. (setq indent-str (make-string
  722. (+ (length indent-str) 2) ?\ )))))))
  723. ;; This is common to 'flat and 'tree
  724. (let ((entry
  725. (org-publish-format-file-entry org-sitemap-file-entry-format
  726. file project-plist))
  727. (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
  728. (cond ((string-match-p regexp entry)
  729. (string-match regexp entry)
  730. (insert (concat indent-str " + " (match-string 1 entry)
  731. "[[file:" link "]["
  732. (match-string 2 entry)
  733. "]]" (match-string 3 entry) "\n")))
  734. (t
  735. (insert (concat indent-str " + [[file:" link "]["
  736. entry
  737. "]]\n"))))))))
  738. (save-buffer))
  739. (or visiting (kill-buffer sitemap-buffer))))
  740. (defun org-publish-format-file-entry (fmt file project-plist)
  741. (format-spec fmt
  742. `((?t . ,(org-publish-find-title file t))
  743. (?d . ,(format-time-string org-sitemap-date-format
  744. (org-publish-find-date file)))
  745. (?a . ,(or (plist-get project-plist :author) user-full-name)))))
  746. (defun org-publish-find-title (file &optional reset)
  747. "Find the title of FILE in project."
  748. (or
  749. (and (not reset) (org-publish-cache-get-file-property file :title nil t))
  750. (let* ((visiting (find-buffer-visiting file))
  751. (buffer (or visiting (find-file-noselect file)))
  752. title)
  753. (with-current-buffer buffer
  754. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  755. (org-infile-export-plist))))
  756. (setq title
  757. (or (plist-get opt-plist :title)
  758. (and (not
  759. (plist-get opt-plist :skip-before-1st-heading))
  760. (org-export-grab-title-from-buffer))
  761. (file-name-nondirectory (file-name-sans-extension file))))))
  762. (unless visiting
  763. (kill-buffer buffer))
  764. (org-publish-cache-set-file-property file :title title)
  765. title)))
  766. (defun org-publish-find-date (file)
  767. "Find the date of FILE in project.
  768. If FILE provides a #+date keyword use it else use the file
  769. system's modification time.
  770. It returns time in `current-time' format."
  771. (let ((visiting (find-buffer-visiting file)))
  772. (save-excursion
  773. (org-pop-to-buffer-same-window (or visiting (find-file-noselect file nil t)))
  774. (let* ((plist (org-infile-export-plist))
  775. (date (plist-get plist :date)))
  776. (unless visiting
  777. (kill-buffer (current-buffer)))
  778. (if date
  779. (org-time-string-to-time date)
  780. (when (file-exists-p file)
  781. (nth 5 (file-attributes file))))))))
  782. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  783. ;;; Interactive publishing functions
  784. ;;;###autoload
  785. (defalias 'org-publish-project 'org-publish)
  786. ;;;###autoload
  787. (defun org-publish (project &optional force)
  788. "Publish PROJECT."
  789. (interactive
  790. (list
  791. (assoc (org-icompleting-read
  792. "Publish project: "
  793. org-publish-project-alist nil t)
  794. org-publish-project-alist)
  795. current-prefix-arg))
  796. (setq org-publish-initial-buffer (current-buffer))
  797. (save-window-excursion
  798. (let* ((org-publish-use-timestamps-flag
  799. (if force nil org-publish-use-timestamps-flag)))
  800. (org-publish-projects
  801. (if (stringp project)
  802. ;; If this function is called in batch mode,
  803. ;; project is still a string here.
  804. (list (assoc project org-publish-project-alist))
  805. (list project))))))
  806. ;;;###autoload
  807. (defun org-publish-all (&optional force)
  808. "Publish all projects.
  809. With prefix argument, remove all files in the timestamp
  810. directory and force publishing all files."
  811. (interactive "P")
  812. (when force
  813. (org-publish-remove-all-timestamps))
  814. (save-window-excursion
  815. (let ((org-publish-use-timestamps-flag
  816. (if force nil org-publish-use-timestamps-flag)))
  817. (org-publish-projects org-publish-project-alist))))
  818. ;;;###autoload
  819. (defun org-publish-current-file (&optional force)
  820. "Publish the current file.
  821. With prefix argument, force publish the file."
  822. (interactive "P")
  823. (save-window-excursion
  824. (let ((org-publish-use-timestamps-flag
  825. (if force nil org-publish-use-timestamps-flag)))
  826. (org-publish-file (buffer-file-name)))))
  827. ;;;###autoload
  828. (defun org-publish-current-project (&optional force)
  829. "Publish the project associated with the current file.
  830. With a prefix argument, force publishing of all files in
  831. the project."
  832. (interactive "P")
  833. (save-window-excursion
  834. (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up))
  835. (org-publish-use-timestamps-flag
  836. (if force nil org-publish-use-timestamps-flag)))
  837. (if (not project)
  838. (error "File %s is not part of any known project" (buffer-file-name)))
  839. ;; FIXME: force is not used here?
  840. (org-publish project))))
  841. ;;; Index generation
  842. (defun org-publish-aux-preprocess ()
  843. "Find index entries and write them to an .orgx file."
  844. (let ((case-fold-search t)
  845. entry index target)
  846. (goto-char (point-min))
  847. (while
  848. (and
  849. (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t)
  850. (> (match-end 1) (match-beginning 1)))
  851. (setq entry (match-string 1))
  852. (when (eq org-export-current-backend 'latex)
  853. (replace-match (format "\\index{%s}" entry) t t))
  854. (save-excursion
  855. (ignore-errors (org-back-to-heading t))
  856. (setq target (get-text-property (point) 'target))
  857. (setq target (or (cdr (assoc target org-export-preferred-target-alist))
  858. (cdr (assoc target org-export-id-target-alist))
  859. target ""))
  860. (push (cons entry target) index)))
  861. (with-temp-file
  862. (concat
  863. (file-name-directory org-current-export-file) "."
  864. (file-name-sans-extension
  865. (file-name-nondirectory org-current-export-file)) ".orgx")
  866. (dolist (entry (nreverse index))
  867. (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
  868. (defun org-publish-index-generate-theindex (directory)
  869. "Generate the index from all .orgx files in DIRECTORY."
  870. (require 'find-lisp)
  871. (let* ((fulldir (file-name-as-directory
  872. (expand-file-name directory)))
  873. (full-files (find-lisp-find-files directory "\\.orgx\\'"))
  874. (re (concat "\\`" fulldir))
  875. (files (mapcar (lambda (f) (if (string-match re f)
  876. (substring f (match-end 0))
  877. f))
  878. full-files))
  879. (default-directory directory)
  880. index origfile buf target entry ibuffer
  881. main last-main letter last-letter file sub link tgext)
  882. ;; `files' contains the list of relative file names
  883. (dolist (file files)
  884. (setq origfile
  885. (concat (file-name-directory file)
  886. (substring (file-name-nondirectory file) 1 -1)))
  887. (setq buf (find-file-noselect file))
  888. (with-current-buffer buf
  889. (goto-char (point-min))
  890. (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
  891. (setq target (match-string 1)
  892. entry (match-string 2))
  893. (push (list entry origfile target) index)))
  894. (kill-buffer buf))
  895. (setq index (sort index (lambda (a b) (string< (downcase (car a))
  896. (downcase (car b))))))
  897. (setq ibuffer (find-file-noselect (expand-file-name "theindex.inc" directory)))
  898. (with-current-buffer ibuffer
  899. (erase-buffer)
  900. (insert "* Index\n")
  901. (setq last-letter nil)
  902. (dolist (idx index)
  903. (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
  904. (if (and (stringp target) (string-match "\\S-" target))
  905. (setq tgext (concat "::#" target))
  906. (setq tgext ""))
  907. (setq letter (upcase (substring entry 0 1)))
  908. (when (not (equal letter last-letter))
  909. (insert "** " letter "\n")
  910. (setq last-letter letter))
  911. (if (string-match "!" entry)
  912. (setq main (substring entry 0 (match-beginning 0))
  913. sub (substring entry (match-end 0)))
  914. (setq main nil sub nil last-main nil))
  915. (when (and main (not (equal main last-main)))
  916. (insert " - " main "\n")
  917. (setq last-main main))
  918. (setq link (concat "[[file:" file tgext "]"
  919. "[" (or sub entry) "]]"))
  920. (if (and main sub)
  921. (insert " - " link "\n")
  922. (insert " - " link "\n")))
  923. (save-buffer))
  924. (kill-buffer ibuffer)
  925. ;; Create theindex.org if it doesn't exist already
  926. (let ((index-file (expand-file-name "theindex.org" directory)))
  927. (unless (file-exists-p index-file)
  928. (setq ibuffer (find-file-noselect index-file))
  929. (with-current-buffer ibuffer
  930. (erase-buffer)
  931. (insert "\n\n#+INCLUDE: \"theindex.inc\"\n\n")
  932. (save-buffer))
  933. (kill-buffer ibuffer)))))
  934. ;; Caching functions:
  935. (defun org-publish-write-cache-file (&optional free-cache)
  936. "Write `org-publish-cache' to file.
  937. If FREE-CACHE, empty the cache."
  938. (unless org-publish-cache
  939. (error "%s" "`org-publish-write-cache-file' called, but no cache present"))
  940. (let ((cache-file (org-publish-cache-get ":cache-file:")))
  941. (unless cache-file
  942. (error
  943. "%s" "Cannot find cache-file name in `org-publish-write-cache-file'"))
  944. (with-temp-file cache-file
  945. (let ((print-level nil)
  946. (print-length nil))
  947. (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
  948. (maphash (lambda (k v)
  949. (insert
  950. (format (concat "(puthash %S "
  951. (if (or (listp v) (symbolp v))
  952. "'" "")
  953. "%S org-publish-cache)\n") k v)))
  954. org-publish-cache)))
  955. (when free-cache (org-publish-reset-cache))))
  956. (defun org-publish-initialize-cache (project-name)
  957. "Initialize the projects cache if not initialized yet and return it."
  958. (unless project-name
  959. (error "%s%s" "Cannot initialize `org-publish-cache' without projects name"
  960. " in `org-publish-initialize-cache'"))
  961. (unless (file-exists-p org-publish-timestamp-directory)
  962. (make-directory org-publish-timestamp-directory t))
  963. (if (not (file-directory-p org-publish-timestamp-directory))
  964. (error "Org publish timestamp: %s is not a directory"
  965. org-publish-timestamp-directory))
  966. (unless (and org-publish-cache
  967. (string= (org-publish-cache-get ":project:") project-name))
  968. (let* ((cache-file (concat
  969. (expand-file-name org-publish-timestamp-directory)
  970. project-name
  971. ".cache"))
  972. (cexists (file-exists-p cache-file)))
  973. (when org-publish-cache
  974. (org-publish-reset-cache))
  975. (if cexists
  976. (load-file cache-file)
  977. (setq org-publish-cache
  978. (make-hash-table :test 'equal :weakness nil :size 100))
  979. (org-publish-cache-set ":project:" project-name)
  980. (org-publish-cache-set ":cache-file:" cache-file))
  981. (unless cexists (org-publish-write-cache-file nil))))
  982. org-publish-cache)
  983. (defun org-publish-reset-cache ()
  984. "Empty org-publish-cache and reset it nil."
  985. (message "%s" "Resetting org-publish-cache")
  986. (if (hash-table-p org-publish-cache)
  987. (clrhash org-publish-cache))
  988. (setq org-publish-cache nil))
  989. (defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func)
  990. "Check the timestamp of the last publishing of FILENAME.
  991. Return `t', if the file needs publishing. The function also
  992. checks if any included files have been more recently published,
  993. so that the file including them will be republished as well."
  994. (unless org-publish-cache
  995. (error "%s" "`org-publish-cache-file-needs-publishing' called, but no cache present"))
  996. (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func))
  997. (pstamp (org-publish-cache-get key))
  998. (visiting (find-buffer-visiting filename))
  999. included-files-ctime buf)
  1000. (when (equal (file-name-extension filename) "org")
  1001. (setq buf (find-file (expand-file-name filename)))
  1002. (with-current-buffer buf
  1003. (goto-char (point-min))
  1004. (while (re-search-forward "^#\\+INCLUDE:[ \t]+\"?\\([^ \t\n\r\"]*\\)\"?[ \t]*.*$" nil t)
  1005. (let* ((included-file (expand-file-name (match-string 1))))
  1006. (add-to-list 'included-files-ctime
  1007. (org-publish-cache-ctime-of-src included-file) t))))
  1008. ;; FIXME don't kill current buffer
  1009. (unless visiting (kill-buffer buf)))
  1010. (if (null pstamp)
  1011. t
  1012. (let ((ctime (org-publish-cache-ctime-of-src filename)))
  1013. (or (< pstamp ctime)
  1014. (when included-files-ctime
  1015. (not (null (delq nil (mapcar (lambda(ct) (< ctime ct))
  1016. included-files-ctime))))))))))
  1017. (defun org-publish-cache-set-file-property (filename property value &optional project-name)
  1018. "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
  1019. Use cache file of PROJECT-NAME. If the entry does not exist, it will be
  1020. created. Return VALUE."
  1021. ;; Evtl. load the requested cache file:
  1022. (if project-name (org-publish-initialize-cache project-name))
  1023. (let ((pl (org-publish-cache-get filename)))
  1024. (if pl
  1025. (progn
  1026. (plist-put pl property value)
  1027. value)
  1028. (org-publish-cache-get-file-property
  1029. filename property value nil project-name))))
  1030. (defun org-publish-cache-get-file-property
  1031. (filename property &optional default no-create project-name)
  1032. "Return the value for a PROPERTY of file FILENAME in publishing cache.
  1033. Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
  1034. DEFAULT, if the value does not yet exist.
  1035. If the entry will be created, unless NO-CREATE is not nil."
  1036. ;; Evtl. load the requested cache file:
  1037. (if project-name (org-publish-initialize-cache project-name))
  1038. (let ((pl (org-publish-cache-get filename))
  1039. (retval nil))
  1040. (if pl
  1041. (if (plist-member pl property)
  1042. (setq retval (plist-get pl property))
  1043. (setq retval default))
  1044. ;; no pl yet:
  1045. (unless no-create
  1046. (org-publish-cache-set filename (list property default)))
  1047. (setq retval default))
  1048. retval))
  1049. (defun org-publish-cache-get (key)
  1050. "Return the value stored in `org-publish-cache' for key KEY.
  1051. Returns nil, if no value or nil is found, or the cache does not
  1052. exist."
  1053. (unless org-publish-cache
  1054. (error "%s" "`org-publish-cache-get' called, but no cache present"))
  1055. (gethash key org-publish-cache))
  1056. (defun org-publish-cache-set (key value)
  1057. "Store KEY VALUE pair in `org-publish-cache'.
  1058. Returns value on success, else nil."
  1059. (unless org-publish-cache
  1060. (error "%s" "`org-publish-cache-set' called, but no cache present"))
  1061. (puthash key value org-publish-cache))
  1062. (defun org-publish-cache-ctime-of-src (filename)
  1063. "Get the FILENAME ctime as an integer."
  1064. (let* ((symlink-maybe (or (file-symlink-p filename) filename))
  1065. (src-attr (file-attributes (if (file-name-absolute-p symlink-maybe)
  1066. symlink-maybe
  1067. (expand-file-name
  1068. symlink-maybe
  1069. (file-name-directory filename))))))
  1070. (+
  1071. (lsh (car (nth 5 src-attr)) 16)
  1072. (cadr (nth 5 src-attr)))))
  1073. (provide 'org-publish)
  1074. ;;; org-publish.el ends here