ox-publish.el 49 KB

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