ox-publish.el 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. ;;; ox-publish.el --- Publish Related Org Mode Files as a Website
  2. ;; Copyright (C) 2006-2013 Free Software Foundation, Inc.
  3. ;; Author: David O'Toole <dto@gnu.org>
  4. ;; Maintainer: Carsten Dominik <carsten DOT dominik AT gmail DOT com>
  5. ;; Keywords: hypermedia, outlines, wp
  6. ;; This file is not part of GNU Emacs.
  7. ;;
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This program allow configurable publishing of related sets of
  20. ;; Org mode files as a complete website.
  21. ;;
  22. ;; ox-publish.el can do the following:
  23. ;;
  24. ;; + Publish all one's Org files to a given export back-end
  25. ;; + Upload HTML, images, attachments and other files to a web server
  26. ;; + Exclude selected private pages from publishing
  27. ;; + Publish a clickable sitemap of pages
  28. ;; + Manage local timestamps for publishing only changed files
  29. ;; + Accept plugin functions to extend range of publishable content
  30. ;;
  31. ;; Documentation for publishing is in the manual.
  32. ;;; Code:
  33. (eval-when-compile (require 'cl))
  34. (require 'format-spec)
  35. (require 'ox)
  36. ;;; Variables
  37. (defvar org-publish-temp-files nil
  38. "Temporary list of files to be published.")
  39. ;; Here, so you find the variable right before it's used the first time:
  40. (defvar org-publish-cache nil
  41. "This will cache timestamps and titles for files in publishing projects.
  42. Blocks could hash sha1 values here.")
  43. (defgroup org-publish nil
  44. "Options for publishing a set of Org-mode and related files."
  45. :tag "Org Publishing"
  46. :group 'org)
  47. (defcustom org-publish-project-alist nil
  48. "Association list to control publishing behavior.
  49. Each element of the alist is a publishing 'project.' The CAR of
  50. each element is a string, uniquely identifying the project. The
  51. CDR of each element is in one of the following forms:
  52. 1. A well-formed property list with an even number of elements,
  53. alternating keys and values, specifying parameters for the
  54. publishing process.
  55. \(:property value :property value ... )
  56. 2. A meta-project definition, specifying of a list of
  57. sub-projects:
  58. \(:components (\"project-1\" \"project-2\" ...))
  59. When the CDR of an element of org-publish-project-alist is in
  60. this second form, the elements of the list after `:components'
  61. are taken to be components of the project, which group together
  62. files requiring different publishing options. When you publish
  63. such a project with \\[org-publish], the components all publish.
  64. When a property is given a value in `org-publish-project-alist',
  65. its setting overrides the value of the corresponding user
  66. variable (if any) during publishing. However, options set within
  67. a file override everything.
  68. Most properties are optional, but some should always be set:
  69. `:base-directory'
  70. Directory containing publishing source files.
  71. `:base-extension'
  72. Extension (without the dot!) of source files. This can be
  73. a regular expression. If not given, \"org\" will be used as
  74. default extension.
  75. `:publishing-directory'
  76. Directory (possibly remote) where output files will be
  77. published.
  78. The `:exclude' property may be used to prevent certain files from
  79. being published. Its value may be a string or regexp matching
  80. file names you don't want to be published.
  81. The `:include' property may be used to include extra files. Its
  82. value may be a list of filenames to include. The filenames are
  83. considered relative to the base directory.
  84. When both `:include' and `:exclude' properties are given values,
  85. the exclusion step happens first.
  86. One special property controls which back-end function to use for
  87. publishing files in the project. This can be used to extend the
  88. set of file types publishable by `org-publish', as well as the
  89. set of output formats.
  90. `:publishing-function'
  91. Function to publish file. Each back-end may define its
  92. own (i.e. `org-latex-publish-to-pdf',
  93. `org-html-publish-to-html'). May be a list of functions, in
  94. which case each function in the list is invoked in turn.
  95. Another property allows you to insert code that prepares
  96. a project for publishing. For example, you could call GNU Make
  97. on a certain makefile, to ensure published files are built up to
  98. date.
  99. `:preparation-function'
  100. Function to be called before publishing this project. This
  101. may also be a list of functions.
  102. `:completion-function'
  103. Function to be called after publishing this project. This
  104. may also be a list of functions.
  105. Some properties control details of the Org publishing process,
  106. and are equivalent to the corresponding user variables listed in
  107. the right column. Back-end specific properties may also be
  108. included. See the back-end documentation for more information.
  109. :author `user-full-name'
  110. :creator `org-export-creator-string'
  111. :email `user-mail-address'
  112. :exclude-tags `org-export-exclude-tags'
  113. :headline-levels `org-export-headline-levels'
  114. :language `org-export-default-language'
  115. :preserve-breaks `org-export-preserve-breaks'
  116. :section-numbers `org-export-with-section-numbers'
  117. :select-tags `org-export-select-tags'
  118. :time-stamp-file `org-export-time-stamp-file'
  119. :with-archived-trees `org-export-with-archived-trees'
  120. :with-author `org-export-with-author'
  121. :with-creator `org-export-with-creator'
  122. :with-date `org-export-with-date'
  123. :with-drawers `org-export-with-drawers'
  124. :with-email `org-export-with-email'
  125. :with-emphasize `org-export-with-emphasize'
  126. :with-entities `org-export-with-entities'
  127. :with-fixed-width `org-export-with-fixed-width'
  128. :with-footnotes `org-export-with-footnotes'
  129. :with-inlinetasks `org-export-with-inlinetasks'
  130. :with-latex `org-export-with-latex'
  131. :with-priority `org-export-with-priority'
  132. :with-smart-quotes `org-export-with-smart-quotes'
  133. :with-special-strings `org-export-with-special-strings'
  134. :with-statistics-cookies' `org-export-with-statistics-cookies'
  135. :with-sub-superscript `org-export-with-sub-superscripts'
  136. :with-toc `org-export-with-toc'
  137. :with-tables `org-export-with-tables'
  138. :with-tags `org-export-with-tags'
  139. :with-tasks `org-export-with-tasks'
  140. :with-timestamps `org-export-with-timestamps'
  141. :with-todo-keywords `org-export-with-todo-keywords'
  142. The following properties may be used to control publishing of
  143. a site-map of files or summary page for a given project.
  144. `:auto-sitemap'
  145. Whether to publish a site-map during
  146. `org-publish-current-project' or `org-publish-all'.
  147. `:sitemap-filename'
  148. Filename for output of sitemap. Defaults to \"sitemap.org\".
  149. `:sitemap-title'
  150. Title of site-map page. Defaults to name of file.
  151. `:sitemap-function'
  152. Plugin function to use for generation of site-map. Defaults
  153. to `org-publish-org-sitemap', which generates a plain list of
  154. links to all files in the project.
  155. `:sitemap-style'
  156. Can be `list' (site-map is just an itemized list of the
  157. titles of the files involved) or `tree' (the directory
  158. structure of the source files is reflected in the site-map).
  159. Defaults to `tree'.
  160. `:sitemap-sans-extension'
  161. Remove extension from site-map's file-names. Useful to have
  162. cool URIs (see http://www.w3.org/Provider/Style/URI).
  163. Defaults to nil.
  164. If you create a site-map file, adjust the sorting like this:
  165. `:sitemap-sort-folders'
  166. Where folders should appear in the site-map. Set this to
  167. `first' (default) or `last' to display folders first or last,
  168. respectively. Any other value will mix files and folders.
  169. `:sitemap-sort-files'
  170. The site map is normally sorted alphabetically. You can
  171. change this behaviour setting this to `anti-chronologically',
  172. `chronologically', or nil.
  173. `:sitemap-ignore-case'
  174. Should sorting be case-sensitive? Default nil.
  175. The following property control the creation of a concept index.
  176. `:makeindex'
  177. Create a concept index. The file containing the index has to
  178. be called \"theindex.org\". If it doesn't exist in the
  179. project, it will be generated. Contents of the index are
  180. stored in the file \"theindex.inc\", which can be included in
  181. \"theindex.org\".
  182. Other properties affecting publication.
  183. `:body-only'
  184. Set this to t to publish only the body of the documents."
  185. :group 'org-export-publish
  186. :type 'alist)
  187. (defcustom org-publish-use-timestamps-flag t
  188. "Non-nil means use timestamp checking to publish only changed files.
  189. When nil, do no timestamp checking and always publish all files."
  190. :group 'org-export-publish
  191. :type 'boolean)
  192. (defcustom org-publish-timestamp-directory
  193. (convert-standard-filename "~/.org-timestamps/")
  194. "Name of directory in which to store publishing timestamps."
  195. :group 'org-export-publish
  196. :type 'directory)
  197. (defcustom org-publish-list-skipped-files t
  198. "Non-nil means show message about files *not* published."
  199. :group 'org-export-publish
  200. :type 'boolean)
  201. (defcustom org-publish-sitemap-sort-files 'alphabetically
  202. "Method to sort files in site-maps.
  203. Possible values are `alphabetically', `chronologically',
  204. `anti-chronologically' and nil.
  205. If `alphabetically', files will be sorted alphabetically. If
  206. `chronologically', files will be sorted with older modification
  207. time first. If `anti-chronologically', files will be sorted with
  208. newer modification time first. nil won't sort files.
  209. You can overwrite this default per project in your
  210. `org-publish-project-alist', using `:sitemap-sort-files'."
  211. :group 'org-export-publish
  212. :type 'symbol)
  213. (defcustom org-publish-sitemap-sort-folders 'first
  214. "A symbol, denoting if folders are sorted first in sitemaps.
  215. Possible values are `first', `last', and nil.
  216. If `first', folders will be sorted before files.
  217. If `last', folders are sorted to the end after the files.
  218. Any other value will not mix files and folders.
  219. You can overwrite this default per project in your
  220. `org-publish-project-alist', using `:sitemap-sort-folders'."
  221. :group 'org-export-publish
  222. :type 'symbol)
  223. (defcustom org-publish-sitemap-sort-ignore-case nil
  224. "Non-nil when site-map sorting should ignore case.
  225. You can overwrite this default per project in your
  226. `org-publish-project-alist', using `:sitemap-ignore-case'."
  227. :group 'org-export-publish
  228. :type 'boolean)
  229. (defcustom org-publish-sitemap-date-format "%Y-%m-%d"
  230. "Format for `format-time-string' which is used to print a date
  231. in the sitemap."
  232. :group 'org-export-publish
  233. :type 'string)
  234. (defcustom org-publish-sitemap-file-entry-format "%t"
  235. "Format string for site-map file entry.
  236. You could use brackets to delimit on what part the link will be.
  237. %t is the title.
  238. %a is the author.
  239. %d is the date formatted using `org-publish-sitemap-date-format'."
  240. :group 'org-export-publish
  241. :type 'string)
  242. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  243. ;;; Timestamp-related functions
  244. (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
  245. "Return path to timestamp file for filename FILENAME."
  246. (setq filename (concat filename "::" (or pub-dir "") "::"
  247. (format "%s" (or pub-func ""))))
  248. (concat "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
  249. (defun org-publish-needed-p
  250. (filename &optional pub-dir pub-func true-pub-dir base-dir)
  251. "Non-nil if FILENAME should be published in PUB-DIR using PUB-FUNC.
  252. TRUE-PUB-DIR is where the file will truly end up. Currently we
  253. are not using this - maybe it can eventually be used to check if
  254. the file is present at the target location, and how old it is.
  255. Right now we cannot do this, because we do not know under what
  256. file name the file will be stored - the publishing function can
  257. still decide about that independently."
  258. (let ((rtn (if (not org-publish-use-timestamps-flag) t
  259. (org-publish-cache-file-needs-publishing
  260. filename pub-dir pub-func base-dir))))
  261. (if rtn (message "Publishing file %s using `%s'" filename pub-func)
  262. (when org-publish-list-skipped-files
  263. (message "Skipping unmodified file %s" filename)))
  264. rtn))
  265. (defun org-publish-update-timestamp
  266. (filename &optional pub-dir pub-func base-dir)
  267. "Update publishing timestamp for file FILENAME.
  268. If there is no timestamp, create one."
  269. (let ((key (org-publish-timestamp-filename filename pub-dir pub-func))
  270. (stamp (org-publish-cache-ctime-of-src filename)))
  271. (org-publish-cache-set key stamp)))
  272. (defun org-publish-remove-all-timestamps ()
  273. "Remove all files in the timestamp directory."
  274. (let ((dir org-publish-timestamp-directory)
  275. files)
  276. (when (and (file-exists-p dir) (file-directory-p dir))
  277. (mapc 'delete-file (directory-files dir 'full "[^.]\\'"))
  278. (org-publish-reset-cache))))
  279. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  280. ;;; Getting project information out of `org-publish-project-alist'
  281. (defun org-publish-expand-projects (projects-alist)
  282. "Expand projects in PROJECTS-ALIST.
  283. This splices all the components into the list."
  284. (let ((rest projects-alist) rtn p components)
  285. (while (setq p (pop rest))
  286. (if (setq components (plist-get (cdr p) :components))
  287. (setq rest (append
  288. (mapcar (lambda (x) (assoc x org-publish-project-alist))
  289. components)
  290. rest))
  291. (push p rtn)))
  292. (nreverse (delete-dups (delq nil rtn)))))
  293. (defvar org-sitemap-sort-files)
  294. (defvar org-sitemap-sort-folders)
  295. (defvar org-sitemap-ignore-case)
  296. (defvar org-sitemap-requested)
  297. (defvar org-sitemap-date-format)
  298. (defvar org-sitemap-file-entry-format)
  299. (defun org-publish-compare-directory-files (a b)
  300. "Predicate for `sort', that sorts folders and files for sitemap."
  301. (let ((retval t))
  302. (when (or org-sitemap-sort-files org-sitemap-sort-folders)
  303. ;; First we sort files:
  304. (when org-sitemap-sort-files
  305. (case org-sitemap-sort-files
  306. (alphabetically
  307. (let* ((adir (file-directory-p a))
  308. (aorg (and (string-match "\\.org$" a) (not adir)))
  309. (bdir (file-directory-p b))
  310. (borg (and (string-match "\\.org$" b) (not bdir)))
  311. (A (if aorg (concat (file-name-directory a)
  312. (org-publish-find-title a)) a))
  313. (B (if borg (concat (file-name-directory b)
  314. (org-publish-find-title b)) b)))
  315. (setq retval (if org-sitemap-ignore-case
  316. (not (string-lessp (upcase B) (upcase A)))
  317. (not (string-lessp B A))))))
  318. ((anti-chronologically chronologically)
  319. (let* ((adate (org-publish-find-date a))
  320. (bdate (org-publish-find-date b))
  321. (A (+ (lsh (car adate) 16) (cadr adate)))
  322. (B (+ (lsh (car bdate) 16) (cadr bdate))))
  323. (setq retval
  324. (if (eq org-sitemap-sort-files 'chronologically) (<= A B)
  325. (>= A B)))))))
  326. ;; Directory-wise wins:
  327. (when org-sitemap-sort-folders
  328. ;; a is directory, b not:
  329. (cond
  330. ((and (file-directory-p a) (not (file-directory-p b)))
  331. (setq retval (equal org-sitemap-sort-folders 'first)))
  332. ;; a is not a directory, but b is:
  333. ((and (not (file-directory-p a)) (file-directory-p b))
  334. (setq retval (equal org-sitemap-sort-folders 'last))))))
  335. retval))
  336. (defun org-publish-get-base-files-1
  337. (base-dir &optional recurse match skip-file skip-dir)
  338. "Set `org-publish-temp-files' with files from BASE-DIR directory.
  339. If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
  340. non-nil, restrict this list to the files matching the regexp
  341. MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
  342. SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
  343. matching the regexp SKIP-DIR when recursing through BASE-DIR."
  344. (mapc (lambda (f)
  345. (let ((fd-p (file-directory-p f))
  346. (fnd (file-name-nondirectory f)))
  347. (if (and fd-p recurse
  348. (not (string-match "^\\.+$" fnd))
  349. (if skip-dir (not (string-match skip-dir fnd)) t))
  350. (org-publish-get-base-files-1
  351. f recurse match skip-file skip-dir)
  352. (unless (or fd-p ;; this is a directory
  353. (and skip-file (string-match skip-file fnd))
  354. (not (file-exists-p (file-truename f)))
  355. (not (string-match match fnd)))
  356. (pushnew f org-publish-temp-files)))))
  357. (if org-sitemap-requested
  358. (sort (directory-files base-dir t (unless recurse match))
  359. 'org-publish-compare-directory-files)
  360. (directory-files base-dir t (unless recurse match)))))
  361. (defun org-publish-get-base-files (project &optional exclude-regexp)
  362. "Return a list of all files in PROJECT.
  363. If EXCLUDE-REGEXP is set, this will be used to filter out
  364. matching filenames."
  365. (let* ((project-plist (cdr project))
  366. (base-dir (file-name-as-directory
  367. (plist-get project-plist :base-directory)))
  368. (include-list (plist-get project-plist :include))
  369. (recurse (plist-get project-plist :recursive))
  370. (extension (or (plist-get project-plist :base-extension) "org"))
  371. ;; sitemap-... variables are dynamically scoped for
  372. ;; org-publish-compare-directory-files:
  373. (org-sitemap-requested
  374. (plist-get project-plist :auto-sitemap))
  375. (sitemap-filename
  376. (or (plist-get project-plist :sitemap-filename) "sitemap.org"))
  377. (org-sitemap-sort-folders
  378. (if (plist-member project-plist :sitemap-sort-folders)
  379. (plist-get project-plist :sitemap-sort-folders)
  380. org-publish-sitemap-sort-folders))
  381. (org-sitemap-sort-files
  382. (cond ((plist-member project-plist :sitemap-sort-files)
  383. (plist-get project-plist :sitemap-sort-files))
  384. ;; For backward compatibility:
  385. ((plist-member project-plist :sitemap-alphabetically)
  386. (if (plist-get project-plist :sitemap-alphabetically)
  387. 'alphabetically nil))
  388. (t org-publish-sitemap-sort-files)))
  389. (org-sitemap-ignore-case
  390. (if (plist-member project-plist :sitemap-ignore-case)
  391. (plist-get project-plist :sitemap-ignore-case)
  392. org-publish-sitemap-sort-ignore-case))
  393. (match (if (eq extension 'any) "^[^\\.]"
  394. (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
  395. ;; Make sure `org-sitemap-sort-folders' has an accepted value
  396. (unless (memq org-sitemap-sort-folders '(first last))
  397. (setq org-sitemap-sort-folders nil))
  398. (setq org-publish-temp-files nil)
  399. (if org-sitemap-requested
  400. (pushnew (expand-file-name (concat base-dir sitemap-filename))
  401. org-publish-temp-files))
  402. (org-publish-get-base-files-1 base-dir recurse match
  403. ;; FIXME distinguish exclude regexp
  404. ;; for skip-file and skip-dir?
  405. exclude-regexp exclude-regexp)
  406. (mapc (lambda (f)
  407. (pushnew
  408. (expand-file-name (concat base-dir f))
  409. org-publish-temp-files))
  410. include-list)
  411. org-publish-temp-files))
  412. (defun org-publish-get-project-from-filename (filename &optional up)
  413. "Return the project that FILENAME belongs to."
  414. (let* ((filename (expand-file-name filename))
  415. project-name)
  416. (catch 'p-found
  417. (dolist (prj org-publish-project-alist)
  418. (unless (plist-get (cdr prj) :components)
  419. ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
  420. (let* ((r (plist-get (cdr prj) :recursive))
  421. (b (expand-file-name (file-name-as-directory
  422. (plist-get (cdr prj) :base-directory))))
  423. (x (or (plist-get (cdr prj) :base-extension) "org"))
  424. (e (plist-get (cdr prj) :exclude))
  425. (i (plist-get (cdr prj) :include))
  426. (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
  427. (when
  428. (or (and i
  429. (member filename
  430. (mapcar (lambda (file)
  431. (expand-file-name file b))
  432. i)))
  433. (and (not (and e (string-match e filename)))
  434. (string-match xm filename)))
  435. (setq project-name (car prj))
  436. (throw 'p-found project-name))))))
  437. (when up
  438. (dolist (prj org-publish-project-alist)
  439. (if (member project-name (plist-get (cdr prj) :components))
  440. (setq project-name (car prj)))))
  441. (assoc project-name org-publish-project-alist)))
  442. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  443. ;;; Tools for publishing functions in back-ends
  444. (defun org-publish-org-to (backend filename extension plist &optional pub-dir)
  445. "Publish an Org file to a specified back-end.
  446. BACKEND is a symbol representing the back-end used for
  447. transcoding. FILENAME is the filename of the Org file to be
  448. published. EXTENSION is the extension used for the output
  449. string, with the leading dot. PLIST is the property list for the
  450. given project.
  451. Optional argument PUB-DIR, when non-nil is the publishing
  452. directory.
  453. Return output file name."
  454. (unless (or (not pub-dir) (file-exists-p pub-dir)) (make-directory pub-dir t))
  455. ;; Check if a buffer visiting FILENAME is already open.
  456. (let* ((visitingp (find-buffer-visiting filename))
  457. (work-buffer (or visitingp (find-file-noselect filename))))
  458. (prog1 (with-current-buffer work-buffer
  459. (let ((output-file
  460. (org-export-output-file-name extension nil pub-dir))
  461. (body-p (plist-get plist :body-only)))
  462. (org-export-to-file
  463. backend output-file nil nil body-p
  464. ;; Install `org-publish-collect-index' in parse tree
  465. ;; filters. It isn't dependent on `:makeindex', since
  466. ;; we want to keep it up-to-date in cache anyway.
  467. (org-combine-plists
  468. plist `(:filter-parse-tree
  469. ,(cons 'org-publish-collect-index
  470. (plist-get plist :filter-parse-tree)))))))
  471. ;; Remove opened buffer in the process.
  472. (unless visitingp (kill-buffer work-buffer)))))
  473. (defvar project-plist)
  474. (defun org-publish-attachment (plist filename pub-dir)
  475. "Publish a file with no transformation of any kind.
  476. FILENAME is the filename of the Org file to be published. PLIST
  477. is the property list for the given project. PUB-DIR is the
  478. publishing directory.
  479. Return output file name."
  480. (unless (file-directory-p pub-dir)
  481. (make-directory pub-dir t))
  482. (or (equal (expand-file-name (file-name-directory filename))
  483. (file-name-as-directory (expand-file-name pub-dir)))
  484. (copy-file filename
  485. (expand-file-name (file-name-nondirectory filename) pub-dir)
  486. t)))
  487. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  488. ;;; Publishing files, sets of files, and indices
  489. (defun org-publish-file (filename &optional project no-cache)
  490. "Publish file FILENAME from PROJECT.
  491. If NO-CACHE is not nil, do not initialize org-publish-cache and
  492. write it to disk. This is needed, since this function is used to
  493. publish single files, when entire projects are published.
  494. See `org-publish-projects'."
  495. (let* ((project
  496. (or project
  497. (or (org-publish-get-project-from-filename filename)
  498. (error "File %s not part of any known project"
  499. (abbreviate-file-name filename)))))
  500. (project-plist (cdr project))
  501. (ftname (expand-file-name filename))
  502. (publishing-function
  503. (or (plist-get project-plist :publishing-function)
  504. (error "No publishing function chosen")))
  505. (base-dir
  506. (file-name-as-directory
  507. (expand-file-name
  508. (or (plist-get project-plist :base-directory)
  509. (error "Project %s does not have :base-directory defined"
  510. (car project))))))
  511. (pub-dir
  512. (file-name-as-directory
  513. (file-truename
  514. (or (eval (plist-get project-plist :publishing-directory))
  515. (error "Project %s does not have :publishing-directory defined"
  516. (car project))))))
  517. tmp-pub-dir)
  518. (unless no-cache (org-publish-initialize-cache (car project)))
  519. (setq tmp-pub-dir
  520. (file-name-directory
  521. (concat pub-dir
  522. (and (string-match (regexp-quote base-dir) ftname)
  523. (substring ftname (match-end 0))))))
  524. (if (listp publishing-function)
  525. ;; allow chain of publishing functions
  526. (mapc (lambda (f)
  527. (when (org-publish-needed-p
  528. filename pub-dir f tmp-pub-dir base-dir)
  529. (funcall f project-plist filename tmp-pub-dir)
  530. (org-publish-update-timestamp filename pub-dir f base-dir)))
  531. publishing-function)
  532. (when (org-publish-needed-p
  533. filename pub-dir publishing-function tmp-pub-dir base-dir)
  534. (funcall publishing-function project-plist filename tmp-pub-dir)
  535. (org-publish-update-timestamp
  536. filename pub-dir publishing-function base-dir)))
  537. (unless no-cache (org-publish-write-cache-file))))
  538. (defun org-publish-projects (projects)
  539. "Publish all files belonging to the PROJECTS alist.
  540. If `:auto-sitemap' is set, publish the sitemap too. If
  541. `:makeindex' is set, also produce a file theindex.org."
  542. (mapc
  543. (lambda (project)
  544. ;; Each project uses its own cache file:
  545. (org-publish-initialize-cache (car project))
  546. (let* ((project-plist (cdr project))
  547. (exclude-regexp (plist-get project-plist :exclude))
  548. (sitemap-p (plist-get project-plist :auto-sitemap))
  549. (sitemap-filename (or (plist-get project-plist :sitemap-filename)
  550. "sitemap.org"))
  551. (sitemap-function (or (plist-get project-plist :sitemap-function)
  552. 'org-publish-org-sitemap))
  553. (org-sitemap-date-format
  554. (or (plist-get project-plist :sitemap-date-format)
  555. org-publish-sitemap-date-format))
  556. (org-sitemap-file-entry-format
  557. (or (plist-get project-plist :sitemap-file-entry-format)
  558. org-publish-sitemap-file-entry-format))
  559. (preparation-function
  560. (plist-get project-plist :preparation-function))
  561. (completion-function (plist-get project-plist :completion-function))
  562. (files (org-publish-get-base-files project exclude-regexp))
  563. (theindex
  564. (expand-file-name "theindex.org"
  565. (plist-get project-plist :base-directory))))
  566. (when preparation-function (run-hooks 'preparation-function))
  567. (if sitemap-p (funcall sitemap-function project sitemap-filename))
  568. ;; Publish all files from PROJECT excepted "theindex.org". Its
  569. ;; publishing will be deferred until "theindex.inc" is
  570. ;; populated.
  571. (dolist (file files)
  572. (unless (equal file theindex)
  573. (org-publish-file file project t)))
  574. ;; Populate "theindex.inc", if needed, and publish
  575. ;; "theindex.org".
  576. (when (plist-get project-plist :makeindex)
  577. (org-publish-index-generate-theindex
  578. project (plist-get project-plist :base-directory))
  579. (org-publish-file theindex project t))
  580. (when completion-function (run-hooks 'completion-function))
  581. (org-publish-write-cache-file)))
  582. (org-publish-expand-projects projects)))
  583. (defun org-publish-org-sitemap (project &optional sitemap-filename)
  584. "Create a sitemap of pages in set defined by PROJECT.
  585. Optionally set the filename of the sitemap with SITEMAP-FILENAME.
  586. Default for SITEMAP-FILENAME is 'sitemap.org'."
  587. (let* ((project-plist (cdr project))
  588. (dir (file-name-as-directory
  589. (plist-get project-plist :base-directory)))
  590. (localdir (file-name-directory dir))
  591. (indent-str (make-string 2 ?\ ))
  592. (exclude-regexp (plist-get project-plist :exclude))
  593. (files (nreverse
  594. (org-publish-get-base-files project exclude-regexp)))
  595. (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
  596. (sitemap-title (or (plist-get project-plist :sitemap-title)
  597. (concat "Sitemap for project " (car project))))
  598. (sitemap-style (or (plist-get project-plist :sitemap-style)
  599. 'tree))
  600. (sitemap-sans-extension
  601. (plist-get project-plist :sitemap-sans-extension))
  602. (visiting (find-buffer-visiting sitemap-filename))
  603. (ifn (file-name-nondirectory sitemap-filename))
  604. file sitemap-buffer)
  605. (with-current-buffer (setq sitemap-buffer
  606. (or visiting (find-file sitemap-filename)))
  607. (erase-buffer)
  608. (insert (concat "#+TITLE: " sitemap-title "\n\n"))
  609. (while (setq file (pop files))
  610. (let ((fn (file-name-nondirectory file))
  611. (link (file-relative-name file dir))
  612. (oldlocal localdir))
  613. (when sitemap-sans-extension
  614. (setq link (file-name-sans-extension link)))
  615. ;; sitemap shouldn't list itself
  616. (unless (equal (file-truename sitemap-filename)
  617. (file-truename file))
  618. (if (eq sitemap-style 'list)
  619. (message "Generating list-style sitemap for %s" sitemap-title)
  620. (message "Generating tree-style sitemap for %s" sitemap-title)
  621. (setq localdir (concat (file-name-as-directory dir)
  622. (file-name-directory link)))
  623. (unless (string= localdir oldlocal)
  624. (if (string= localdir dir)
  625. (setq indent-str (make-string 2 ?\ ))
  626. (let ((subdirs
  627. (split-string
  628. (directory-file-name
  629. (file-name-directory
  630. (file-relative-name localdir dir))) "/"))
  631. (subdir "")
  632. (old-subdirs (split-string
  633. (file-relative-name oldlocal dir) "/")))
  634. (setq indent-str (make-string 2 ?\ ))
  635. (while (string= (car old-subdirs) (car subdirs))
  636. (setq indent-str (concat indent-str (make-string 2 ?\ )))
  637. (pop old-subdirs)
  638. (pop subdirs))
  639. (dolist (d subdirs)
  640. (setq subdir (concat subdir d "/"))
  641. (insert (concat indent-str " + " d "\n"))
  642. (setq indent-str (make-string
  643. (+ (length indent-str) 2) ?\ )))))))
  644. ;; This is common to 'flat and 'tree
  645. (let ((entry
  646. (org-publish-format-file-entry
  647. org-sitemap-file-entry-format file project-plist))
  648. (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
  649. (cond ((string-match-p regexp entry)
  650. (string-match regexp entry)
  651. (insert (concat indent-str " + " (match-string 1 entry)
  652. "[[file:" link "]["
  653. (match-string 2 entry)
  654. "]]" (match-string 3 entry) "\n")))
  655. (t
  656. (insert (concat indent-str " + [[file:" link "]["
  657. entry
  658. "]]\n"))))))))
  659. (save-buffer))
  660. (or visiting (kill-buffer sitemap-buffer))))
  661. (defun org-publish-format-file-entry (fmt file project-plist)
  662. (format-spec fmt
  663. `((?t . ,(org-publish-find-title file t))
  664. (?d . ,(format-time-string org-sitemap-date-format
  665. (org-publish-find-date file)))
  666. (?a . ,(or (plist-get project-plist :author) user-full-name)))))
  667. (defun org-publish-find-title (file &optional reset)
  668. "Find the title of FILE in project."
  669. (or
  670. (and (not reset) (org-publish-cache-get-file-property file :title nil t))
  671. (let* ((visiting (find-buffer-visiting file))
  672. (buffer (or visiting (find-file-noselect file)))
  673. title)
  674. (with-current-buffer buffer
  675. (org-mode)
  676. (setq title
  677. (or (org-element-interpret-data
  678. (plist-get (org-export-get-environment) :title))
  679. (file-name-nondirectory (file-name-sans-extension file)))))
  680. (unless visiting (kill-buffer buffer))
  681. (org-publish-cache-set-file-property file :title title)
  682. title)))
  683. (defun org-publish-find-date (file)
  684. "Find the date of FILE in project.
  685. If FILE provides a #+date keyword use it else use the file
  686. system's modification time.
  687. It returns time in `current-time' format."
  688. (let* ((visiting (find-buffer-visiting file))
  689. (file-buf (or visiting (find-file-noselect file nil)))
  690. (date (plist-get
  691. (with-current-buffer file-buf
  692. (org-mode)
  693. (org-export--get-inbuffer-options))
  694. :date)))
  695. (unless visiting (kill-buffer file-buf))
  696. (if date (org-time-string-to-time date)
  697. (when (file-exists-p file)
  698. (nth 5 (file-attributes file))))))
  699. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  700. ;;; Interactive publishing functions
  701. ;;;###autoload
  702. (defalias 'org-publish-project 'org-publish)
  703. ;;;###autoload
  704. (defun org-publish (project &optional force async)
  705. "Publish PROJECT.
  706. PROJECT is either a project name, as a string, or a project
  707. alist (see `org-publish-project-alist' variable).
  708. When optional argument FORCE is non-nil, force publishing all
  709. files in PROJECT. With a non-nil optional argument ASYNC,
  710. publishing will be done asynchronously, in another process."
  711. (interactive
  712. (list
  713. (assoc (org-icompleting-read
  714. "Publish project: "
  715. org-publish-project-alist nil t)
  716. org-publish-project-alist)
  717. current-prefix-arg))
  718. (let ((project-alist (if (not (stringp project)) (list project)
  719. ;; If this function is called in batch mode,
  720. ;; project is still a string here.
  721. (list (assoc project org-publish-project-alist)))))
  722. (if async
  723. (org-export-async-start 'ignore
  724. `(let ((org-publish-use-timestamps-flag
  725. (if ',force nil ,org-publish-use-timestamps-flag)))
  726. (org-publish-projects ',project-alist)))
  727. (save-window-excursion
  728. (let* ((org-publish-use-timestamps-flag
  729. (if force nil org-publish-use-timestamps-flag)))
  730. (org-publish-projects project-alist))))))
  731. ;;;###autoload
  732. (defun org-publish-all (&optional force async)
  733. "Publish all projects.
  734. With prefix argument FORCE, remove all files in the timestamp
  735. directory and force publishing all projects. With a non-nil
  736. optional argument ASYNC, publishing will be done asynchronously,
  737. in another process."
  738. (interactive "P")
  739. (if async
  740. (org-export-async-start 'ignore
  741. `(when ',force (org-publish-remove-all-timestamps))
  742. `(let ((org-publish-use-timestamps-flag
  743. (if ',force nil ,org-publish-use-timestamps-flag)))
  744. (org-publish-projects ',org-publish-project-alist)))
  745. (when force (org-publish-remove-all-timestamps))
  746. (save-window-excursion
  747. (let ((org-publish-use-timestamps-flag
  748. (if force nil org-publish-use-timestamps-flag)))
  749. (org-publish-projects org-publish-project-alist)))))
  750. ;;;###autoload
  751. (defun org-publish-current-file (&optional force async)
  752. "Publish the current file.
  753. With prefix argument FORCE, force publish the file. When
  754. optional argument ASYNC is non-nil, publishing will be done
  755. asynchronously, in another process."
  756. (interactive "P")
  757. (let ((file (buffer-file-name (buffer-base-buffer))))
  758. (if async
  759. (org-export-async-start 'ignore
  760. `(let ((org-publish-use-timestamps-flag
  761. (if ',force nil ,org-publish-use-timestamps-flag)))
  762. (org-publish-file ,file)))
  763. (save-window-excursion
  764. (let ((org-publish-use-timestamps-flag
  765. (if force nil org-publish-use-timestamps-flag)))
  766. (org-publish-file file))))))
  767. ;;;###autoload
  768. (defun org-publish-current-project (&optional force async)
  769. "Publish the project associated with the current file.
  770. With a prefix argument, force publishing of all files in
  771. the project."
  772. (interactive "P")
  773. (save-window-excursion
  774. (let ((project (org-publish-get-project-from-filename
  775. (buffer-file-name (buffer-base-buffer)) 'up)))
  776. (if project (org-publish project force async)
  777. (error "File %s is not part of any known project"
  778. (buffer-file-name (buffer-base-buffer)))))))
  779. ;;; Index generation
  780. (defun org-publish-collect-index (tree backend info)
  781. "Update index for a file with TREE in cache.
  782. BACKEND is the back-end being used for transcoding. INFO is
  783. a plist containing publishing options.
  784. The index relative to current file is stored as an alist. An
  785. association has the following shape: (TERM FILE-NAME PARENT),
  786. where TERM is the indexed term, as a string, FILE-NAME is the
  787. original full path of the file where the term in encountered, and
  788. PARENT is a reference to the headline, if any, containing the
  789. original index keyword. When non-nil, this reference is a cons
  790. cell. Its CAR is a symbol among `id', `custom-id' and `name' and
  791. its CDR is a string."
  792. (let ((file (plist-get info :input-file)))
  793. (org-publish-cache-set-file-property
  794. file :index
  795. (delete-dups
  796. (org-element-map tree 'keyword
  797. (lambda (k)
  798. (when (equal (upcase (org-element-property :key k)) "INDEX")
  799. (let ((parent (org-export-get-parent-headline k)))
  800. (list (org-element-property :value k)
  801. file
  802. (cond
  803. ((not parent) nil)
  804. ((let ((id (org-element-property :ID parent)))
  805. (and id (cons 'id id))))
  806. ((let ((id (org-element-property :CUSTOM_ID parent)))
  807. (and id (cons 'custom-id id))))
  808. (t (cons 'name
  809. (org-element-property :raw-value parent))))))))
  810. info))))
  811. ;; Return parse-tree to avoid altering output.
  812. tree)
  813. (defun org-publish-index-generate-theindex (project directory)
  814. "Retrieve full index from cache and build \"theindex.org\".
  815. PROJECT is the project the index relates to. DIRECTORY is the
  816. publishing directory."
  817. (let ((all-files (org-publish-get-base-files
  818. project (plist-get (cdr project) :exclude)))
  819. full-index)
  820. ;; Compile full index and sort it alphabetically.
  821. (dolist (file all-files
  822. (setq full-index
  823. (sort (nreverse full-index)
  824. (lambda (a b) (string< (downcase (car a))
  825. (downcase (car b)))))))
  826. (let ((index (org-publish-cache-get-file-property file :index)))
  827. (dolist (term index)
  828. (unless (member term full-index) (push term full-index)))))
  829. ;; Write "theindex.inc" in DIRECTORY.
  830. (with-temp-file (expand-file-name "theindex.inc" directory)
  831. (insert "* Index\n")
  832. (let ((current-letter nil) (last-entry nil))
  833. (dolist (idx full-index)
  834. (let* ((entry (org-split-string (car idx) "!"))
  835. (letter (upcase (substring (car entry) 0 1)))
  836. ;; Transform file into a path relative to publishing
  837. ;; directory.
  838. (file (file-relative-name
  839. (nth 1 idx)
  840. (plist-get (cdr project) :base-directory))))
  841. ;; Check if another letter has to be inserted.
  842. (unless (string= letter current-letter)
  843. (insert (format "** %s\n" letter)))
  844. ;; Compute the first difference between last entry and
  845. ;; current one: it tells the level at which new items
  846. ;; should be added.
  847. (let* ((rank (if (equal entry last-entry) (1- (length entry))
  848. (loop for n from 0 to (length entry)
  849. unless (equal (nth n entry) (nth n last-entry))
  850. return n)))
  851. (len (length (nthcdr rank entry))))
  852. ;; For each term after the first difference, create
  853. ;; a new sub-list with the term as body. Moreover,
  854. ;; linkify the last term.
  855. (dotimes (n len)
  856. (insert
  857. (concat
  858. (make-string (* (+ rank n) 2) ? ) " - "
  859. (if (not (= (1- len) n)) (nth (+ rank n) entry)
  860. ;; Last term: Link it to TARGET, if possible.
  861. (let ((target (nth 2 idx)))
  862. (format
  863. "[[%s][%s]]"
  864. ;; Destination.
  865. (case (car target)
  866. ('nil (format "file:%s" file))
  867. (id (format "id:%s" (cdr target)))
  868. (custom-id (format "file:%s::#%s" file (cdr target)))
  869. (otherwise (format "file:%s::*%s" file (cdr target))))
  870. ;; Description.
  871. (car (last entry)))))
  872. "\n"))))
  873. (setq current-letter letter last-entry entry))))
  874. ;; Write "theindex.org" if it doesn't exist. The combination
  875. ;; "theindex.inc" and conditional "theindex.org" allows for
  876. ;; a greater flexibility for the user, since he can provide its
  877. ;; own "theindex.org", inserting "theindex.inc" wherever he
  878. ;; wants.
  879. (let ((index.org (expand-file-name "theindex.org" directory)))
  880. (unless (file-exists-p index.org)
  881. (with-temp-file index.org
  882. (insert "\n\n#+INCLUDE: \"theindex.inc\"\n\n")))))))
  883. ;;; Caching functions
  884. (defun org-publish-write-cache-file (&optional free-cache)
  885. "Write `org-publish-cache' to file.
  886. If FREE-CACHE, empty the cache."
  887. (unless org-publish-cache
  888. (error "`org-publish-write-cache-file' called, but no cache present"))
  889. (let ((cache-file (org-publish-cache-get ":cache-file:")))
  890. (unless cache-file
  891. (error "Cannot find cache-file name in `org-publish-write-cache-file'"))
  892. (with-temp-file cache-file
  893. (let (print-level print-length)
  894. (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
  895. (maphash (lambda (k v)
  896. (insert
  897. (format (concat "(puthash %S "
  898. (if (or (listp v) (symbolp v))
  899. "'" "")
  900. "%S org-publish-cache)\n") k v)))
  901. org-publish-cache)))
  902. (when free-cache (org-publish-reset-cache))))
  903. (defun org-publish-initialize-cache (project-name)
  904. "Initialize the projects cache if not initialized yet and return it."
  905. (unless project-name
  906. (error "Cannot initialize `org-publish-cache' without projects name in `org-publish-initialize-cache'"))
  907. (unless (file-exists-p org-publish-timestamp-directory)
  908. (make-directory org-publish-timestamp-directory t))
  909. (unless (file-directory-p org-publish-timestamp-directory)
  910. (error "Org publish timestamp: %s is not a directory"
  911. org-publish-timestamp-directory))
  912. (unless (and org-publish-cache
  913. (string= (org-publish-cache-get ":project:") project-name))
  914. (let* ((cache-file
  915. (concat
  916. (expand-file-name org-publish-timestamp-directory)
  917. project-name ".cache"))
  918. (cexists (file-exists-p cache-file)))
  919. (when org-publish-cache (org-publish-reset-cache))
  920. (if cexists (load-file cache-file)
  921. (setq org-publish-cache
  922. (make-hash-table :test 'equal :weakness nil :size 100))
  923. (org-publish-cache-set ":project:" project-name)
  924. (org-publish-cache-set ":cache-file:" cache-file))
  925. (unless cexists (org-publish-write-cache-file nil))))
  926. org-publish-cache)
  927. (defun org-publish-reset-cache ()
  928. "Empty org-publish-cache and reset it nil."
  929. (message "%s" "Resetting org-publish-cache")
  930. (when (hash-table-p org-publish-cache)
  931. (clrhash org-publish-cache))
  932. (setq org-publish-cache nil))
  933. (defun org-publish-cache-file-needs-publishing
  934. (filename &optional pub-dir pub-func base-dir)
  935. "Check the timestamp of the last publishing of FILENAME.
  936. Non-nil if the file needs publishing. The function also checks
  937. if any included files have been more recently published, so that
  938. the file including them will be republished as well."
  939. (unless org-publish-cache
  940. (error
  941. "`org-publish-cache-file-needs-publishing' called, but no cache present"))
  942. (let* ((case-fold-search t)
  943. (key (org-publish-timestamp-filename filename pub-dir pub-func))
  944. (pstamp (org-publish-cache-get key))
  945. (visiting (find-buffer-visiting filename))
  946. included-files-ctime buf)
  947. (when (equal (file-name-extension filename) "org")
  948. (setq buf (find-file (expand-file-name filename)))
  949. (with-current-buffer buf
  950. (goto-char (point-min))
  951. (while (re-search-forward
  952. "^#\\+INCLUDE:[ \t]+\"\\([^\t\n\r\"]*\\)\"[ \t]*.*$" nil t)
  953. (let* ((included-file (expand-file-name (match-string 1))))
  954. (add-to-list 'included-files-ctime
  955. (org-publish-cache-ctime-of-src included-file) t))))
  956. (unless visiting (kill-buffer buf)))
  957. (if (null pstamp) t
  958. (let ((ctime (org-publish-cache-ctime-of-src filename)))
  959. (or (< pstamp ctime)
  960. (when included-files-ctime
  961. (not (null (delq nil (mapcar (lambda(ct) (< ctime ct))
  962. included-files-ctime))))))))))
  963. (defun org-publish-cache-set-file-property
  964. (filename property value &optional project-name)
  965. "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
  966. Use cache file of PROJECT-NAME. If the entry does not exist, it
  967. will be created. Return VALUE."
  968. ;; Evtl. load the requested cache file:
  969. (if project-name (org-publish-initialize-cache project-name))
  970. (let ((pl (org-publish-cache-get filename)))
  971. (if pl (progn (plist-put pl property value) value)
  972. (org-publish-cache-get-file-property
  973. filename property value nil project-name))))
  974. (defun org-publish-cache-get-file-property
  975. (filename property &optional default no-create project-name)
  976. "Return the value for a PROPERTY of file FILENAME in publishing cache.
  977. Use cache file of PROJECT-NAME. Return the value of that PROPERTY
  978. or DEFAULT, if the value does not yet exist. If the entry will
  979. be created, unless NO-CREATE is not nil."
  980. ;; Evtl. load the requested cache file:
  981. (if project-name (org-publish-initialize-cache project-name))
  982. (let ((pl (org-publish-cache-get filename)) retval)
  983. (if pl
  984. (if (plist-member pl property)
  985. (setq retval (plist-get pl property))
  986. (setq retval default))
  987. ;; no pl yet:
  988. (unless no-create
  989. (org-publish-cache-set filename (list property default)))
  990. (setq retval default))
  991. retval))
  992. (defun org-publish-cache-get (key)
  993. "Return the value stored in `org-publish-cache' for key KEY.
  994. Returns nil, if no value or nil is found, or the cache does not
  995. exist."
  996. (unless org-publish-cache
  997. (error "`org-publish-cache-get' called, but no cache present"))
  998. (gethash key org-publish-cache))
  999. (defun org-publish-cache-set (key value)
  1000. "Store KEY VALUE pair in `org-publish-cache'.
  1001. Returns value on success, else nil."
  1002. (unless org-publish-cache
  1003. (error "`org-publish-cache-set' called, but no cache present"))
  1004. (puthash key value org-publish-cache))
  1005. (defun org-publish-cache-ctime-of-src (file)
  1006. "Get the ctime of FILE as an integer."
  1007. (let ((attr (file-attributes
  1008. (expand-file-name (or (file-symlink-p file) file)
  1009. (file-name-directory file)))))
  1010. (+ (lsh (car (nth 5 attr)) 16)
  1011. (cadr (nth 5 attr)))))
  1012. (provide 'ox-publish)
  1013. ;; Local variables:
  1014. ;; generated-autoload-file: "org-loaddefs.el"
  1015. ;; End:
  1016. ;;; ox-publish.el ends here