ox-publish.el 50 KB

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