ox-publish.el 48 KB

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