org-publish.el 47 KB

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