ox-publish.el 46 KB

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