ox-publish.el 51 KB

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