ox-publish.el 49 KB

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