org-publish.el 46 KB

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