ox-publish.el 48 KB

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