org-publish.el 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  1. ;;; org-publish.el --- publish related org-mode files as a website
  2. ;; Copyright (C) 2006, 2007, 2008, 2009, 2010
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: David O'Toole <dto@gnu.org>
  5. ;; Maintainer: Carsten Dominik <carsten DOT dominik AT gmail DOT com>
  6. ;; Keywords: hypermedia, outlines, wp
  7. ;; Version: 7.6
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; This program allow configurable publishing of related sets of
  22. ;; Org-mode files as a complete website.
  23. ;;
  24. ;; org-publish.el can do the following:
  25. ;;
  26. ;; + Publish all one's org-files to HTML or PDF
  27. ;; + Upload HTML, images, attachments and other files to a web server
  28. ;; + Exclude selected private pages from publishing
  29. ;; + Publish a clickable sitemap of pages
  30. ;; + Manage local timestamps for publishing only changed files
  31. ;; + Accept plugin functions to extend range of publishable content
  32. ;;
  33. ;; Documentation for publishing is in the manual.
  34. ;;; Code:
  35. (defun org-publish-sanitize-plist (plist)
  36. (mapcar (lambda (x)
  37. (or (cdr (assq x '((:index-filename . :sitemap-filename)
  38. (:index-title . :sitemap-title)
  39. (:index-function . :sitemap-function)
  40. (:index-style . :sitemap-style)
  41. (:auto-index . :auto-sitemap))))
  42. x))
  43. plist))
  44. (eval-when-compile
  45. (require 'cl))
  46. (require 'org)
  47. (require 'org-exp)
  48. (require 'format-spec)
  49. (eval-and-compile
  50. (unless (fboundp 'declare-function)
  51. (defmacro declare-function (fn file &optional arglist fileonly))))
  52. (defgroup org-publish nil
  53. "Options for publishing a set of Org-mode and related files."
  54. :tag "Org Publishing"
  55. :group 'org)
  56. (defcustom org-publish-project-alist nil
  57. "Association list to control publishing behavior.
  58. Each element of the alist is a publishing 'project.' The CAR of
  59. each element is a string, uniquely identifying the project. The
  60. CDR of each element is in one of the following forms:
  61. 1. A well-formed property list with an even number of elements, alternating
  62. keys and values, specifying parameters for the publishing process.
  63. (:property value :property value ... )
  64. 2. A meta-project definition, specifying of a list of sub-projects:
  65. (:components (\"project-1\" \"project-2\" ...))
  66. When the CDR of an element of org-publish-project-alist is in
  67. this second form, the elements of the list after :components are
  68. taken to be components of the project, which group together files
  69. requiring different publishing options. When you publish such a
  70. project with \\[org-publish], the components all publish.
  71. When a property is given a value in org-publish-project-alist, its
  72. setting overrides the value of the corresponding user variable
  73. \(if any) during publishing. However, options set within a file
  74. override everything.
  75. Most properties are optional, but some should always be set:
  76. :base-directory Directory containing publishing source files
  77. :base-extension Extension (without the dot!) of source files.
  78. This can be a regular expression. If not given,
  79. \"org\" will be used as default extension.
  80. :publishing-directory Directory (possibly remote) where output
  81. files will be published
  82. The :exclude property may be used to prevent certain files from
  83. being published. Its value may be a string or regexp matching
  84. file names you don't want to be published.
  85. The :include property may be used to include extra files. Its
  86. value may be a list of filenames to include. The filenames are
  87. considered relative to the base directory.
  88. When both :include and :exclude properties are given values, the
  89. exclusion step happens first.
  90. One special property controls which back-end function to use for
  91. publishing files in the project. This can be used to extend the
  92. set of file types publishable by org-publish, as well as the set
  93. of output formats.
  94. :publishing-function Function to publish file. The default is
  95. `org-publish-org-to-html', but other
  96. values are possible. May also be a
  97. list of functions, in which case
  98. each function in the list is invoked
  99. in turn.
  100. Another property allows you to insert code that prepares a
  101. project for publishing. For example, you could call GNU Make on a
  102. certain makefile, to ensure published files are built up to date.
  103. :preparation-function Function to be called before publishing
  104. this project. This may also be a list
  105. of functions.
  106. :completion-function Function to be called after publishing
  107. this project. This may also be a list
  108. of functions.
  109. Some properties control details of the Org publishing process,
  110. and are equivalent to the corresponding user variables listed in
  111. the right column. See the documentation for those variables to
  112. learn more about their use and default values.
  113. :language `org-export-default-language'
  114. :headline-levels `org-export-headline-levels'
  115. :section-numbers `org-export-with-section-numbers'
  116. :table-of-contents `org-export-with-toc'
  117. :emphasize `org-export-with-emphasize'
  118. :sub-superscript `org-export-with-sub-superscripts'
  119. :TeX-macros `org-export-with-TeX-macros'
  120. :fixed-width `org-export-with-fixed-width'
  121. :tables `org-export-with-tables'
  122. :table-auto-headline `org-export-highlight-first-table-line'
  123. :style `org-export-html-style'
  124. :convert-org-links `org-export-html-link-org-files-as-html'
  125. :inline-images `org-export-html-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. :type 'symbol)
  208. (defcustom org-publish-sitemap-sort-folders 'first
  209. "A symbol, denoting if folders are sorted first in sitemaps.
  210. Possible values are `first', `last', and nil.
  211. If `first', folders will be sorted before files.
  212. If `last', folders are sorted to the end after the files.
  213. Any other value will not mix files and folders.
  214. You can overwrite this default per project in your
  215. `org-publish-project-alist', using `:sitemap-sort-folders'."
  216. :group 'org-publish
  217. :type 'symbol)
  218. (defcustom org-publish-sitemap-sort-ignore-case nil
  219. "Sort sitemaps case insensitively by default?
  220. You can overwrite this default per project in your
  221. `org-publish-project-alist', using `:sitemap-ignore-case'."
  222. :group 'org-publish
  223. :type 'boolean)
  224. (defcustom org-publish-sitemap-date-format "%Y-%m-%d"
  225. "Format for `format-time-string' which is used to print a date
  226. in the sitemap."
  227. :group 'org-publish
  228. :type 'string)
  229. (defcustom org-publish-sitemap-file-entry-format "%t"
  230. "How a sitemap file entry is formated.
  231. You could use brackets to delimit on what part the link will be.
  232. %t is the title.
  233. %a is the author.
  234. %d is the date formated using `org-publish-sitemap-date-format'."
  235. :group 'org-publish
  236. :type 'string)
  237. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  238. ;;; Timestamp-related functions
  239. (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
  240. "Return path to timestamp file for filename FILENAME."
  241. (setq filename (concat filename "::" (or pub-dir "") "::"
  242. (format "%s" (or pub-func ""))))
  243. (concat "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
  244. (defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir)
  245. "Return t if FILENAME should be published in PUB-DIR using PUB-FUNC.
  246. TRUE-PUB-DIR is where the file will truly end up. Currently we are not using
  247. this - maybe it can eventually be used to check if the file is present at
  248. the target location, and how old it is. Right now we cannot do this, because
  249. we do not know under what file name the file will be stored - the publishing
  250. function can still decide about that independently."
  251. (let ((rtn
  252. (if org-publish-use-timestamps-flag
  253. (org-publish-cache-file-needs-publishing
  254. filename pub-dir pub-func)
  255. ;; don't use timestamps, always return t
  256. t)))
  257. (if rtn
  258. (message "Publishing file %s using `%s'" filename pub-func)
  259. (when org-publish-list-skipped-files
  260. (message "Skipping unmodified file %s" filename)))
  261. rtn))
  262. (defun org-publish-update-timestamp (filename &optional pub-dir pub-func)
  263. "Update publishing timestamp for file FILENAME.
  264. If there is no timestamp, create one."
  265. (let ((key (org-publish-timestamp-filename filename pub-dir pub-func))
  266. (stamp (org-publish-cache-ctime-of-src filename)))
  267. (org-publish-cache-set key stamp)))
  268. (defun org-publish-remove-all-timestamps ()
  269. "Remove all files in the timestamp directory."
  270. (let ((dir org-publish-timestamp-directory)
  271. files)
  272. (when (and (file-exists-p dir)
  273. (file-directory-p dir))
  274. (mapc 'delete-file (directory-files dir 'full "[^.]\\'"))
  275. (org-publish-reset-cache))))
  276. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  277. ;;;
  278. (defvar org-publish-initial-buffer nil
  279. "The buffer `org-publish' has been called from.")
  280. (defvar org-publish-temp-files nil
  281. "Temporary list of files to be published.")
  282. ;; Here, so you find the variable right before it's used the first time:
  283. (defvar org-publish-cache nil
  284. "This will cache timestamps and titles for files in publishing projects.
  285. Blocks could hash sha1 values here.")
  286. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  287. ;;; Compatibility aliases
  288. ;; Delete-dups is not in Emacs <22
  289. (if (fboundp 'delete-dups)
  290. (defalias 'org-publish-delete-dups 'delete-dups)
  291. (defun org-publish-delete-dups (list)
  292. "Destructively remove `equal' duplicates from LIST.
  293. Store the result in LIST and return it. LIST must be a proper list.
  294. Of several `equal' occurrences of an element in LIST, the first
  295. one is kept.
  296. This is a compatibility function for Emacsen without `delete-dups'."
  297. ;; Code from `subr.el' in Emacs 22:
  298. (let ((tail list))
  299. (while tail
  300. (setcdr tail (delete (car tail) (cdr tail)))
  301. (setq tail (cdr tail))))
  302. list))
  303. (declare-function org-publish-delete-dups "org-publish" (list))
  304. (declare-function find-lisp-find-files "find-lisp" (directory regexp))
  305. (declare-function org-pop-to-buffer-same-window
  306. "org-compat" (&optional buffer-or-name norecord label))
  307. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  308. ;;; Getting project information out of org-publish-project-alist
  309. (defun org-publish-expand-projects (projects-alist)
  310. "Expand projects in PROJECTS-ALIST.
  311. This splices all the components into the list."
  312. (let ((rest projects-alist) rtn p components)
  313. (while (setq p (pop rest))
  314. (if (setq components (plist-get (cdr p) :components))
  315. (setq rest (append
  316. (mapcar (lambda (x) (assoc x org-publish-project-alist))
  317. components)
  318. rest))
  319. (push p rtn)))
  320. (nreverse (org-publish-delete-dups (delq nil rtn)))))
  321. (defvar sitemap-sort-files)
  322. (defvar sitemap-sort-folders)
  323. (defvar sitemap-ignore-case)
  324. (defvar sitemap-requested)
  325. (defvar sitemap-date-format)
  326. (defvar sitemap-file-entry-format)
  327. (defun org-publish-compare-directory-files (a b)
  328. "Predicate for `sort', that sorts folders and files for sitemap."
  329. (let ((retval t))
  330. (when (or sitemap-sort-files sitemap-sort-folders)
  331. ;; First we sort files:
  332. (when sitemap-sort-files
  333. (cond ((equal sitemap-sort-files 'alphabetically)
  334. (let* ((adir (file-directory-p a))
  335. (aorg (and (string-match "\\.org$" a) (not adir)))
  336. (bdir (file-directory-p b))
  337. (borg (and (string-match "\\.org$" b) (not bdir)))
  338. (A (if aorg
  339. (concat (file-name-directory a)
  340. (org-publish-find-title a)) a))
  341. (B (if borg
  342. (concat (file-name-directory b)
  343. (org-publish-find-title b)) b)))
  344. (setq retval (if sitemap-ignore-case
  345. (not (string-lessp (upcase B) (upcase A)))
  346. (not (string-lessp B A))))))
  347. ((or (equal sitemap-sort-files 'chronologically)
  348. (equal sitemap-sort-files 'anti-chronologically))
  349. (let* ((adate (org-publish-find-date a))
  350. (bdate (org-publish-find-date b))
  351. (A (+ (lsh (car adate) 16) (cadr adate)))
  352. (B (+ (lsh (car bdate) 16) (cadr bdate))))
  353. (setq retval (if (equal sitemap-sort-files 'chronologically)
  354. (<= A B)
  355. (>= A B)))))))
  356. ;; Directory-wise wins:
  357. (when sitemap-sort-folders
  358. ;; a is directory, b not:
  359. (cond
  360. ((and (file-directory-p a) (not (file-directory-p b)))
  361. (setq retval (equal sitemap-sort-folders 'first)))
  362. ;; a is not a directory, but b is:
  363. ((and (not (file-directory-p a)) (file-directory-p b))
  364. (setq retval (equal sitemap-sort-folders 'last))))))
  365. retval))
  366. (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
  367. "Set `org-publish-temp-files' with files from BASE-DIR directory.
  368. If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
  369. non-nil, restrict this list to the files matching the regexp
  370. MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
  371. SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
  372. matching the regexp SKIP-DIR when recursing through BASE-DIR."
  373. (mapc (lambda (f)
  374. (let ((fd-p (file-directory-p f))
  375. (fnd (file-name-nondirectory f)))
  376. (if (and fd-p recurse
  377. (not (string-match "^\\.+$" fnd))
  378. (if skip-dir (not (string-match skip-dir fnd)) t))
  379. (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
  380. (unless (or fd-p ;; this is a directory
  381. (and skip-file (string-match skip-file fnd))
  382. (not (file-exists-p (file-truename f)))
  383. (not (string-match match fnd)))
  384. (pushnew f org-publish-temp-files)))))
  385. (if sitemap-requested
  386. (sort (directory-files base-dir t (unless recurse match))
  387. 'org-publish-compare-directory-files)
  388. (directory-files base-dir t (unless recurse match)))))
  389. (defun org-publish-get-base-files (project &optional exclude-regexp)
  390. "Return a list of all files in PROJECT.
  391. If EXCLUDE-REGEXP is set, this will be used to filter out
  392. matching filenames."
  393. (let* ((project-plist (cdr project))
  394. (base-dir (file-name-as-directory
  395. (plist-get project-plist :base-directory)))
  396. (include-list (plist-get project-plist :include))
  397. (recurse (plist-get project-plist :recursive))
  398. (extension (or (plist-get project-plist :base-extension) "org"))
  399. ;; sitemap-... variables are dynamically scoped for
  400. ;; org-publish-compare-directory-files:
  401. (sitemap-requested
  402. (plist-get project-plist :auto-sitemap))
  403. (sitemap-filename
  404. (or (plist-get project-plist :sitemap-filename)
  405. "sitemap.org"))
  406. (sitemap-sort-folders
  407. (if (plist-member project-plist :sitemap-sort-folders)
  408. (plist-get project-plist :sitemap-sort-folders)
  409. org-publish-sitemap-sort-folders))
  410. (sitemap-sort-files
  411. (cond ((plist-member project-plist :sitemap-sort-files)
  412. (plist-get project-plist :sitemap-sort-files))
  413. ;; For backward compatibility:
  414. ((plist-member project-plist :sitemap-alphabetically)
  415. (if (plist-get project-plist :sitemap-alphabetically)
  416. 'alphabetically nil))
  417. (t org-publish-sitemap-sort-files)))
  418. (sitemap-ignore-case
  419. (if (plist-member project-plist :sitemap-ignore-case)
  420. (plist-get project-plist :sitemap-ignore-case)
  421. org-publish-sitemap-sort-ignore-case))
  422. (match (if (eq extension 'any)
  423. "^[^\\.]"
  424. (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
  425. ;; Make sure sitemap-sort-folders' has an accepted value
  426. (unless (memq sitemap-sort-folders '(first last))
  427. (setq sitemap-sort-folders nil))
  428. (setq org-publish-temp-files nil)
  429. (if sitemap-requested
  430. (pushnew (expand-file-name (concat base-dir sitemap-filename))
  431. org-publish-temp-files))
  432. (org-publish-get-base-files-1 base-dir recurse match
  433. ;; FIXME distinguish exclude regexp
  434. ;; for skip-file and skip-dir?
  435. exclude-regexp exclude-regexp)
  436. (mapc (lambda (f)
  437. (pushnew
  438. (expand-file-name (concat base-dir f))
  439. org-publish-temp-files))
  440. include-list)
  441. org-publish-temp-files))
  442. (defun org-publish-get-project-from-filename (filename &optional up)
  443. "Return the project that FILENAME belongs to."
  444. (let* ((filename (expand-file-name filename))
  445. project-name)
  446. (catch 'p-found
  447. (dolist (prj org-publish-project-alist)
  448. (unless (plist-get (cdr prj) :components)
  449. ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
  450. (let* ((r (plist-get (cdr prj) :recursive))
  451. (b (expand-file-name (file-name-as-directory
  452. (plist-get (cdr prj) :base-directory))))
  453. (x (or (plist-get (cdr prj) :base-extension) "org"))
  454. (e (plist-get (cdr prj) :exclude))
  455. (i (plist-get (cdr prj) :include))
  456. (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
  457. (when
  458. (or
  459. (and
  460. i (member filename
  461. (mapcar
  462. (lambda (file) (expand-file-name file b))
  463. i)))
  464. (and
  465. (not (and e (string-match e filename)))
  466. (string-match xm filename)))
  467. (setq project-name (car prj))
  468. (throw 'p-found project-name))))))
  469. (when up
  470. (dolist (prj org-publish-project-alist)
  471. (if (member project-name (plist-get (cdr prj) :components))
  472. (setq project-name (car prj)))))
  473. (assoc project-name org-publish-project-alist)))
  474. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  475. ;;; Pluggable publishing back-end functions
  476. (defun org-publish-org-to (format plist filename pub-dir)
  477. "Publish an org file to FORMAT.
  478. PLIST is the property list for the given project.
  479. FILENAME is the filename of the org file to be published.
  480. PUB-DIR is the publishing directory."
  481. (require 'org)
  482. (unless (file-exists-p pub-dir)
  483. (make-directory pub-dir t))
  484. (let ((visiting (find-buffer-visiting filename)))
  485. (save-excursion
  486. (org-pop-to-buffer-same-window (or visiting (find-file filename)))
  487. (let* ((plist (cons :buffer-will-be-killed (cons t plist)))
  488. (init-buf (current-buffer))
  489. (init-point (point))
  490. (init-buf-string (buffer-string))
  491. export-buf-or-file)
  492. ;; run hooks before exporting
  493. (run-hooks 'org-publish-before-export-hook)
  494. ;; export the possibly modified buffer
  495. (setq export-buf-or-file
  496. (funcall (intern (concat "org-export-as-" format))
  497. (plist-get plist :headline-levels)
  498. nil plist nil
  499. (plist-get plist :body-only)
  500. pub-dir))
  501. (when (and (bufferp export-buf-or-file)
  502. (buffer-live-p export-buf-or-file))
  503. (set-buffer export-buf-or-file)
  504. ;; run hooks after export and save export
  505. (progn (run-hooks 'org-publish-after-export-hook)
  506. (if (buffer-modified-p) (save-buffer)))
  507. (kill-buffer export-buf-or-file))
  508. ;; maybe restore buffer's content
  509. (set-buffer init-buf)
  510. (when (buffer-modified-p init-buf)
  511. (erase-buffer)
  512. (insert init-buf-string)
  513. (save-buffer)
  514. (goto-char init-point))
  515. (unless visiting
  516. (kill-buffer init-buf))))))
  517. (defmacro org-publish-with-aux-preprocess-maybe (&rest body)
  518. "Execute BODY with a modified hook to preprocess for index."
  519. `(let ((org-export-preprocess-after-headline-targets-hook
  520. (if (plist-get project-plist :makeindex)
  521. (cons 'org-publish-aux-preprocess
  522. org-export-preprocess-after-headline-targets-hook)
  523. org-export-preprocess-after-headline-targets-hook)))
  524. ,@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 (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 it's 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. (sitemap-date-format (or (plist-get project-plist :sitemap-date-format)
  639. org-publish-sitemap-date-format))
  640. (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.inc
  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 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 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 (file-name-sans-extension org-current-export-file) ".orgx")
  858. (dolist (entry (nreverse index))
  859. (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
  860. (defun org-publish-index-generate-theindex.inc (directory)
  861. "Generate the index from all .orgx files in the current directory and below."
  862. (require 'find-lisp)
  863. (let* ((fulldir (file-name-as-directory
  864. (expand-file-name directory)))
  865. (full-files (find-lisp-find-files directory "\\.orgx\\'"))
  866. (re (concat "\\`" fulldir))
  867. (files (mapcar (lambda (f) (if (string-match re f)
  868. (substring f (match-end 0))
  869. f))
  870. full-files))
  871. (default-directory directory)
  872. index origfile buf target entry ibuffer
  873. main last-main letter last-letter file sub link tgext)
  874. ;; `files' contains the list of relative file names
  875. (dolist (file files)
  876. (setq origfile (substring file 0 -1))
  877. (setq buf (find-file-noselect file))
  878. (with-current-buffer buf
  879. (goto-char (point-min))
  880. (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
  881. (setq target (match-string 1)
  882. entry (match-string 2))
  883. (push (list entry origfile target) index)))
  884. (kill-buffer buf))
  885. (setq index (sort index (lambda (a b) (string< (downcase (car a))
  886. (downcase (car b))))))
  887. (setq ibuffer (find-file-noselect (expand-file-name "theindex.inc" directory)))
  888. (with-current-buffer ibuffer
  889. (erase-buffer)
  890. (insert "* Index\n")
  891. (setq last-letter nil)
  892. (dolist (idx index)
  893. (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
  894. (if (and (stringp target) (string-match "\\S-" target))
  895. (setq tgext (concat "::#" target))
  896. (setq tgext ""))
  897. (setq letter (upcase (substring entry 0 1)))
  898. (when (not (equal letter last-letter))
  899. (insert "** " letter "\n")
  900. (setq last-letter letter))
  901. (if (string-match "!" entry)
  902. (setq main (substring entry 0 (match-beginning 0))
  903. sub (substring entry (match-end 0)))
  904. (setq main nil sub nil last-main nil))
  905. (when (and main (not (equal main last-main)))
  906. (insert " - " main "\n")
  907. (setq last-main main))
  908. (setq link (concat "[[file:" file tgext "]"
  909. "[" (or sub entry) "]]"))
  910. (if (and main sub)
  911. (insert " - " link "\n")
  912. (insert " - " link "\n")))
  913. (save-buffer))
  914. (kill-buffer ibuffer)
  915. (let ((index-file (expand-file-name "theindex.org" directory)))
  916. (unless (file-exists-p index-file)
  917. (setq ibuffer (find-file-noselect index-file))
  918. (with-current-buffer ibuffer
  919. (erase-buffer)
  920. (insert "\n\n#+include: \"theindex.inc\"\n\n")
  921. (save-buffer))
  922. (kill-buffer ibuffer)))))
  923. ;; Caching functions:
  924. (defun org-publish-write-cache-file (&optional free-cache)
  925. "Write `org-publish-cache' to file.
  926. If FREE-CACHE, empty the cache."
  927. (unless org-publish-cache
  928. (error "%s" "`org-publish-write-cache-file' called, but no cache present"))
  929. (let ((cache-file (org-publish-cache-get ":cache-file:")))
  930. (unless cache-file
  931. (error
  932. "%s" "Cannot find cache-file name in `org-publish-write-cache-file'"))
  933. (with-temp-file cache-file
  934. (let ((print-level nil)
  935. (print-length nil))
  936. (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
  937. (maphash (lambda (k v)
  938. (insert
  939. (format (concat "(puthash %S "
  940. (if (or (listp v) (symbolp v))
  941. "'" "")
  942. "%S org-publish-cache)\n") k v)))
  943. org-publish-cache)))
  944. (when free-cache (org-publish-reset-cache))))
  945. (defun org-publish-initialize-cache (project-name)
  946. "Initialize the projects cache if not initialized yet and return it."
  947. (unless project-name
  948. (error "%s%s" "Cannot initialize `org-publish-cache' without projects name"
  949. " in `org-publish-initialize-cache'"))
  950. (unless (file-exists-p org-publish-timestamp-directory)
  951. (make-directory org-publish-timestamp-directory t))
  952. (if (not (file-directory-p org-publish-timestamp-directory))
  953. (error "Org publish timestamp: %s is not a directory"
  954. org-publish-timestamp-directory))
  955. (unless (and org-publish-cache
  956. (string= (org-publish-cache-get ":project:") project-name))
  957. (let* ((cache-file (concat
  958. (expand-file-name org-publish-timestamp-directory)
  959. project-name
  960. ".cache"))
  961. (cexists (file-exists-p cache-file)))
  962. (when org-publish-cache
  963. (org-publish-reset-cache))
  964. (if cexists
  965. (load-file cache-file)
  966. (setq org-publish-cache
  967. (make-hash-table :test 'equal :weakness nil :size 100))
  968. (org-publish-cache-set ":project:" project-name)
  969. (org-publish-cache-set ":cache-file:" cache-file))
  970. (unless cexists (org-publish-write-cache-file nil))))
  971. org-publish-cache)
  972. (defun org-publish-reset-cache ()
  973. "Empty org-publish-cache and reset it nil."
  974. (message "%s" "Resetting org-publish-cache")
  975. (if (hash-table-p org-publish-cache)
  976. (clrhash org-publish-cache))
  977. (setq org-publish-cache nil))
  978. (defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func)
  979. "Check the timestamp of the last publishing of FILENAME.
  980. Return `t', if the file needs publishing. The function also
  981. checks if any included files have been more recently published,
  982. so that the file including them will be republished as well."
  983. (unless org-publish-cache
  984. (error "%s" "`org-publish-cache-file-needs-publishing' called, but no cache present"))
  985. (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func))
  986. (pstamp (org-publish-cache-get key))
  987. included-files-ctime)
  988. (with-temp-buffer
  989. (when (equal (file-name-extension filename) "org")
  990. (find-file (expand-file-name filename))
  991. (goto-char (point-min))
  992. (while (re-search-forward "^#\\+INCLUDE:[ \t]+\"?\\([^ \t\"]*\\)\"?[ \t]*.*$" nil t)
  993. (let* ((included-file (expand-file-name (match-string 1))))
  994. (add-to-list 'included-files-ctime
  995. (org-publish-cache-ctime-of-src included-file) t)))))
  996. (if (null pstamp)
  997. t
  998. (let ((ctime (org-publish-cache-ctime-of-src filename)))
  999. (or (< pstamp ctime)
  1000. (when included-files-ctime
  1001. (not (null (delq nil (mapcar (lambda(ct) (< ctime ct))
  1002. included-files-ctime))))))))))
  1003. (defun org-publish-cache-set-file-property (filename property value &optional project-name)
  1004. "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
  1005. Use cache file of PROJECT-NAME. If the entry does not exist, it will be
  1006. created. Return VALUE."
  1007. ;; Evtl. load the requested cache file:
  1008. (if project-name (org-publish-initialize-cache project-name))
  1009. (let ((pl (org-publish-cache-get filename)))
  1010. (if pl
  1011. (progn
  1012. (plist-put pl property value)
  1013. value)
  1014. (org-publish-cache-get-file-property
  1015. filename property value nil project-name))))
  1016. (defun org-publish-cache-get-file-property
  1017. (filename property &optional default no-create project-name)
  1018. "Return the value for a PROPERTY of file FILENAME in publishing cache.
  1019. Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
  1020. DEFAULT, if the value does not yet exist.
  1021. If the entry will be created, unless NO-CREATE is not nil."
  1022. ;; Evtl. load the requested cache file:
  1023. (if project-name (org-publish-initialize-cache project-name))
  1024. (let ((pl (org-publish-cache-get filename))
  1025. (retval nil))
  1026. (if pl
  1027. (if (plist-member pl property)
  1028. (setq retval (plist-get pl property))
  1029. (setq retval default))
  1030. ;; no pl yet:
  1031. (unless no-create
  1032. (org-publish-cache-set filename (list property default)))
  1033. (setq retval default))
  1034. retval))
  1035. (defun org-publish-cache-get (key)
  1036. "Return the value stored in `org-publish-cache' for key KEY.
  1037. Returns nil, if no value or nil is found, or the cache does not
  1038. exist."
  1039. (unless org-publish-cache
  1040. (error "%s" "`org-publish-cache-get' called, but no cache present"))
  1041. (gethash key org-publish-cache))
  1042. (defun org-publish-cache-set (key value)
  1043. "Store KEY VALUE pair in `org-publish-cache'.
  1044. Returns value on success, else nil."
  1045. (unless org-publish-cache
  1046. (error "%s" "`org-publish-cache-set' called, but no cache present"))
  1047. (puthash key value org-publish-cache))
  1048. (defun org-publish-cache-ctime-of-src (filename)
  1049. "Get the FILENAME ctime as an integer."
  1050. (let* ((symlink-maybe (or (file-symlink-p filename) filename))
  1051. (src-attr (file-attributes (if (file-name-absolute-p symlink-maybe)
  1052. symlink-maybe
  1053. (expand-file-name
  1054. symlink-maybe
  1055. (file-name-directory filename))))))
  1056. (+
  1057. (lsh (car (nth 5 src-attr)) 16)
  1058. (cadr (nth 5 src-attr)))))
  1059. (provide 'org-publish)
  1060. ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
  1061. ;;; org-publish.el ends here