ox-publish.el 49 KB

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