ox-publish.el 48 KB

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