ox-publish.el 47 KB

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