ox-publish.el 49 KB

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