org-publish.el 44 KB

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