ox-publish.el 50 KB

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