ox-publish.el 47 KB

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