ox-publish.el 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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. ;; 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-publish nil
  47. "Options for publishing a set of files."
  48. :tag "Org Publishing"
  49. :group 'org)
  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 "25.2"
  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. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  268. ;;; Timestamp-related functions
  269. (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
  270. "Return path to timestamp file for filename FILENAME."
  271. (setq filename (concat filename "::" (or pub-dir "") "::"
  272. (format "%s" (or pub-func ""))))
  273. (concat "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
  274. (defun org-publish-needed-p
  275. (filename &optional pub-dir pub-func _true-pub-dir base-dir)
  276. "Non-nil if FILENAME should be published in PUB-DIR using PUB-FUNC.
  277. TRUE-PUB-DIR is where the file will truly end up. Currently we
  278. are not using this - maybe it can eventually be used to check if
  279. the file is present at the target location, and how old it is.
  280. Right now we cannot do this, because we do not know under what
  281. file name the file will be stored - the publishing function can
  282. still decide about that independently."
  283. (let ((rtn (if (not org-publish-use-timestamps-flag) t
  284. (org-publish-cache-file-needs-publishing
  285. filename pub-dir pub-func base-dir))))
  286. (if rtn (message "Publishing file %s using `%s'" filename pub-func)
  287. (when org-publish-list-skipped-files
  288. (message "Skipping unmodified file %s" filename)))
  289. rtn))
  290. (defun org-publish-update-timestamp
  291. (filename &optional pub-dir pub-func _base-dir)
  292. "Update publishing timestamp for file FILENAME.
  293. If there is no timestamp, create one."
  294. (let ((key (org-publish-timestamp-filename filename pub-dir pub-func))
  295. (stamp (org-publish-cache-ctime-of-src filename)))
  296. (org-publish-cache-set key stamp)))
  297. (defun org-publish-remove-all-timestamps ()
  298. "Remove all files in the timestamp directory."
  299. (let ((dir org-publish-timestamp-directory))
  300. (when (and (file-exists-p dir) (file-directory-p dir))
  301. (mapc #'delete-file (directory-files dir 'full "[^.]\\'"))
  302. (org-publish-reset-cache))))
  303. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  304. ;;; Getting project information out of `org-publish-project-alist'
  305. (defun org-publish-property (property project &optional default)
  306. "Return value PROPERTY, as à symbol, in PROJECT.
  307. DEFAULT is returned when PROPERTY is not actually set in PROJECT
  308. definition."
  309. (let ((properties (cdr project)))
  310. (if (plist-member properties property)
  311. (plist-get properties property)
  312. default)))
  313. (defun org-publish--expand-file-name (file project)
  314. "Return full file name for FILE in PROJECT.
  315. When FILE is a relative file name, it is expanded according to
  316. project base directory."
  317. (if (file-name-absolute-p file) file
  318. (expand-file-name file (org-publish-property :base-directory project))))
  319. (defun org-publish-expand-projects (projects-alist)
  320. "Expand projects in PROJECTS-ALIST.
  321. This splices all the components into the list."
  322. (let ((rest projects-alist) rtn p components)
  323. (while (setq p (pop rest))
  324. (if (setq components (plist-get (cdr p) :components))
  325. (setq rest (append
  326. (mapcar (lambda (x) (assoc x org-publish-project-alist))
  327. components)
  328. rest))
  329. (push p rtn)))
  330. (nreverse (delete-dups (delq nil rtn)))))
  331. (defun org-publish-get-base-files (project)
  332. "Return a list of all files in PROJECT."
  333. (let* ((base-dir (file-name-as-directory
  334. (org-publish-property :base-directory project)))
  335. (extension (or (org-publish-property :base-extension project) "org"))
  336. (match (and (not (eq extension 'any))
  337. (concat "^[^\\.].*\\.\\(" extension "\\)$")))
  338. (base-files
  339. (cl-remove-if #'file-directory-p
  340. (if (org-publish-property :recursive project)
  341. (directory-files-recursively base-dir match)
  342. (directory-files base-dir t match t)))))
  343. (org-uniquify
  344. (append
  345. ;; Files from BASE-DIR. Apply exclusion filter before adding
  346. ;; included files.
  347. (let ((exclude-regexp (org-publish-property :exclude project)))
  348. (if exclude-regexp
  349. (cl-remove-if
  350. (lambda (f)
  351. ;; Match against relative names, yet BASE-DIR file
  352. ;; names are absolute.
  353. (string-match exclude-regexp
  354. (file-relative-name f base-dir)))
  355. base-files)
  356. base-files))
  357. ;; Sitemap file.
  358. (and (org-publish-property :auto-sitemap project)
  359. (list (expand-file-name
  360. (or (org-publish-property :sitemap-filename project)
  361. "sitemap.org")
  362. base-dir)))
  363. ;; Included files.
  364. (mapcar (lambda (f) (expand-file-name f base-dir))
  365. (org-publish-property :include project))))))
  366. (defun org-publish-get-project-from-filename (filename &optional up)
  367. "Return the project that FILENAME belongs to."
  368. (let* ((filename (expand-file-name filename))
  369. project-name)
  370. (catch 'p-found
  371. (dolist (prj org-publish-project-alist)
  372. (unless (plist-get (cdr prj) :components)
  373. ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
  374. (let* ((r (plist-get (cdr prj) :recursive))
  375. (b (expand-file-name (file-name-as-directory
  376. (plist-get (cdr prj) :base-directory))))
  377. (x (or (plist-get (cdr prj) :base-extension) "org"))
  378. (e (plist-get (cdr prj) :exclude))
  379. (i (plist-get (cdr prj) :include))
  380. (xm (concat "\\`" b
  381. (if r ".+" "[^/]+")
  382. (and (not (eq x 'any))
  383. (format "\\.\\(%s\\)\\'" x)))))
  384. (when
  385. (or (and i
  386. (member filename
  387. (dolist (file i) (expand-file-name file b))))
  388. (and (not (and e (string-match e filename)))
  389. (string-match xm filename)))
  390. (setq project-name (car prj))
  391. (throw 'p-found project-name))))))
  392. (when up
  393. (dolist (prj org-publish-project-alist)
  394. (if (member project-name (plist-get (cdr prj) :components))
  395. (setq project-name (car prj)))))
  396. (assoc project-name org-publish-project-alist)))
  397. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  398. ;;; Tools for publishing functions in back-ends
  399. (defun org-publish-org-to (backend filename extension plist &optional pub-dir)
  400. "Publish an Org file to a specified back-end.
  401. BACKEND is a symbol representing the back-end used for
  402. transcoding. FILENAME is the filename of the Org file to be
  403. published. EXTENSION is the extension used for the output
  404. string, with the leading dot. PLIST is the property list for the
  405. given project.
  406. Optional argument PUB-DIR, when non-nil is the publishing
  407. directory.
  408. Return output file name."
  409. (unless (or (not pub-dir) (file-exists-p pub-dir)) (make-directory pub-dir t))
  410. ;; Check if a buffer visiting FILENAME is already open.
  411. (let* ((org-inhibit-startup t)
  412. (visiting (find-buffer-visiting filename))
  413. (work-buffer (or visiting (find-file-noselect filename))))
  414. (unwind-protect
  415. (with-current-buffer work-buffer
  416. (let ((output (org-export-output-file-name extension nil pub-dir)))
  417. (org-export-to-file backend output
  418. nil nil nil (plist-get plist :body-only)
  419. ;; Add `org-publish--store-crossrefs' and
  420. ;; `org-publish-collect-index' to final output filters.
  421. ;; The latter isn't dependent on `:makeindex', since we
  422. ;; want to keep it up-to-date in cache anyway.
  423. (org-combine-plists
  424. plist
  425. `(:crossrefs
  426. ,(org-publish-cache-get-file-property
  427. (expand-file-name filename) :crossrefs nil t)
  428. :filter-final-output
  429. (org-publish--store-crossrefs
  430. org-publish-collect-index
  431. ,@(plist-get plist :filter-final-output)))))))
  432. ;; Remove opened buffer in the process.
  433. (unless visiting (kill-buffer work-buffer)))))
  434. (defun org-publish-attachment (_plist filename pub-dir)
  435. "Publish a file with no transformation of any kind.
  436. FILENAME is the filename of the Org file to be published. PLIST
  437. is the property list for the given project. PUB-DIR is the
  438. publishing directory.
  439. Return output file name."
  440. (unless (file-directory-p pub-dir)
  441. (make-directory pub-dir t))
  442. (let ((output (expand-file-name (file-name-nondirectory filename) pub-dir)))
  443. (or (equal (expand-file-name (file-name-directory filename))
  444. (file-name-as-directory (expand-file-name pub-dir)))
  445. (copy-file filename output t))
  446. ;; Return file name.
  447. output))
  448. ;;; Publishing files, sets of files
  449. (defun org-publish-file (filename &optional project no-cache)
  450. "Publish file FILENAME from PROJECT.
  451. If NO-CACHE is not nil, do not initialize `org-publish-cache'.
  452. This is needed, since this function is used to publish single
  453. files, when entire projects are published (see
  454. `org-publish-projects')."
  455. (let* ((project
  456. (or project
  457. (or (org-publish-get-project-from-filename filename)
  458. (error "File %s not part of any known project"
  459. (abbreviate-file-name filename)))))
  460. (plist (cdr project))
  461. (ftname (expand-file-name filename))
  462. (publishing-function
  463. (let ((fun (org-publish-property :publishing-function project)))
  464. (cond ((null fun) (error "No publishing function chosen"))
  465. ((listp fun) fun)
  466. (t (list fun)))))
  467. (base-dir
  468. (file-name-as-directory
  469. (expand-file-name
  470. (or (org-publish-property :base-directory project)
  471. (error "Project %s does not have :base-directory defined"
  472. (car project))))))
  473. (pub-dir
  474. (file-name-as-directory
  475. (file-truename
  476. (or (org-publish-property :publishing-directory project)
  477. (error "Project %s does not have :publishing-directory defined"
  478. (car project))))))
  479. tmp-pub-dir)
  480. (unless no-cache (org-publish-initialize-cache (car project)))
  481. (setq tmp-pub-dir
  482. (file-name-directory
  483. (concat pub-dir
  484. (and (string-match (regexp-quote base-dir) ftname)
  485. (substring ftname (match-end 0))))))
  486. ;; Allow chain of publishing functions.
  487. (dolist (f publishing-function)
  488. (when (org-publish-needed-p filename pub-dir f tmp-pub-dir base-dir)
  489. (let ((output (funcall f plist filename tmp-pub-dir)))
  490. (org-publish-update-timestamp filename pub-dir f base-dir)
  491. (run-hook-with-args 'org-publish-after-publishing-hook
  492. filename
  493. output))))
  494. ;; Make sure to write cache to file after successfully publishing
  495. ;; a file, so as to minimize impact of a publishing failure.
  496. (org-publish-write-cache-file)))
  497. (defun org-publish-projects (projects)
  498. "Publish all files belonging to the PROJECTS alist.
  499. If `:auto-sitemap' is set, publish the sitemap too. If
  500. `:makeindex' is set, also produce a file \"theindex.org\"."
  501. (dolist (project (org-publish-expand-projects projects))
  502. (let ((plist (cdr project)))
  503. (let ((fun (org-publish-property :preparation-function project)))
  504. (cond
  505. ((consp fun) (dolist (f fun) (funcall f plist)))
  506. ((functionp fun) (funcall fun plist))))
  507. ;; Each project uses its own cache file.
  508. (org-publish-initialize-cache (car project))
  509. (when (org-publish-property :auto-sitemap project)
  510. (let ((sitemap-filename
  511. (or (org-publish-property :sitemap-filename project)
  512. "sitemap.org")))
  513. (org-publish-sitemap project sitemap-filename)))
  514. ;; Publish all files from PROJECT except "theindex.org". Its
  515. ;; publishing will be deferred until "theindex.inc" is
  516. ;; populated.
  517. (let ((theindex (expand-file-name
  518. "theindex.org"
  519. (org-publish-property :base-directory project))))
  520. (dolist (file (org-publish-get-base-files project))
  521. (unless (equal file theindex) (org-publish-file file project t)))
  522. ;; Populate "theindex.inc", if needed, and publish
  523. ;; "theindex.org".
  524. (when (org-publish-property :makeindex project)
  525. (org-publish-index-generate-theindex
  526. project (org-publish-property :base-directory project))
  527. (org-publish-file theindex project t)))
  528. (let ((fun (org-publish-property :completion-function project)))
  529. (cond
  530. ((consp fun) (dolist (f fun) (funcall f plist)))
  531. ((functionp fun) (funcall fun plist)))))
  532. (org-publish-write-cache-file)))
  533. ;;; Site map generation
  534. (defun org-publish--sitemap-files-to-lisp (files project style format-entry)
  535. "Represent FILES as a parsed plain list.
  536. FILES is the list of files in the site map. PROJECT is the
  537. current project. STYLE determines is either `list' or `tree'.
  538. FORMAT-ENTRY is a function called on each file which should
  539. return a string. Return value is a list as returned by
  540. `org-list-to-lisp'."
  541. (let ((root (expand-file-name
  542. (file-name-as-directory
  543. (org-publish-property :base-directory project)))))
  544. (pcase style
  545. (`list
  546. (cons 'unordered
  547. (mapcar
  548. (lambda (f)
  549. (list (funcall format-entry
  550. (file-relative-name f root)
  551. style
  552. project)))
  553. files)))
  554. (`tree
  555. (letrec ((files-only (cl-remove-if #'directory-name-p files))
  556. (directories (cl-remove-if-not #'directory-name-p files))
  557. (subtree-to-list
  558. (lambda (dir)
  559. (cons 'unordered
  560. (nconc
  561. ;; Files in DIR.
  562. (mapcar
  563. (lambda (f)
  564. (list (funcall format-entry
  565. (file-relative-name f root)
  566. style
  567. project)))
  568. (cl-remove-if-not
  569. (lambda (f) (string= dir (file-name-directory f)))
  570. files-only))
  571. ;; Direct sub-directories.
  572. (mapcar
  573. (lambda (sub)
  574. (list (funcall format-entry
  575. (file-relative-name sub root)
  576. style
  577. project)
  578. (funcall subtree-to-list sub)))
  579. (cl-remove-if-not
  580. (lambda (f)
  581. (string=
  582. dir
  583. ;; Parent directory.
  584. (file-name-directory (directory-file-name f))))
  585. directories)))))))
  586. (funcall subtree-to-list root)))
  587. (_ (user-error "Unknown site-map style: `%s'" style)))))
  588. (defun org-publish-sitemap (project &optional sitemap-filename)
  589. "Create a sitemap of pages in set defined by PROJECT.
  590. Optionally set the filename of the sitemap with SITEMAP-FILENAME.
  591. Default for SITEMAP-FILENAME is `sitemap.org'."
  592. (let* ((root (expand-file-name
  593. (file-name-as-directory
  594. (org-publish-property :base-directory project))))
  595. (sitemap-filename (concat root (or sitemap-filename "sitemap.org")))
  596. (title (or (org-publish-property :sitemap-title project)
  597. (concat "Sitemap for project " (car project))))
  598. (style (or (org-publish-property :sitemap-style project)
  599. 'tree))
  600. (sitemap-builder (or (org-publish-property :sitemap-function project)
  601. #'org-publish-sitemap-default))
  602. (format-entry (or (org-publish-property :sitemap-format-entry project)
  603. #'org-publish-sitemap-default-entry))
  604. (sort-folders
  605. (org-publish-property :sitemap-sort-folders project
  606. org-publish-sitemap-sort-folders))
  607. (sort-files
  608. (org-publish-property :sitemap-sort-files project
  609. org-publish-sitemap-sort-files))
  610. (ignore-case
  611. (org-publish-property :sitemap-ignore-case project
  612. org-publish-sitemap-sort-ignore-case))
  613. (org-file-p (lambda (f) (equal "org" (file-name-extension f))))
  614. (sort-predicate
  615. (lambda (a b)
  616. (let ((retval t))
  617. ;; First we sort files:
  618. (pcase sort-files
  619. (`alphabetically
  620. (let ((A (if (funcall org-file-p a)
  621. (concat (file-name-directory a)
  622. (org-publish-find-title a project))
  623. a))
  624. (B (if (funcall org-file-p b)
  625. (concat (file-name-directory b)
  626. (org-publish-find-title b project))
  627. b)))
  628. (setq retval
  629. (if ignore-case
  630. (not (string-lessp (upcase B) (upcase A)))
  631. (not (string-lessp B A))))))
  632. ((or `anti-chronologically `chronologically)
  633. (let* ((adate (org-publish-find-date a project))
  634. (bdate (org-publish-find-date b project))
  635. (A (+ (lsh (car adate) 16) (cadr adate)))
  636. (B (+ (lsh (car bdate) 16) (cadr bdate))))
  637. (setq retval
  638. (if (eq sort-files 'chronologically)
  639. (<= A B)
  640. (>= A B)))))
  641. (`nil nil)
  642. (_ (user-error "Invalid sort value %s" sort-files)))
  643. ;; Directory-wise wins:
  644. (when (memq sort-folders '(first last))
  645. ;; a is directory, b not:
  646. (cond
  647. ((and (file-directory-p a) (not (file-directory-p b)))
  648. (setq retval (eq sort-folders 'first)))
  649. ;; a is not a directory, but b is:
  650. ((and (not (file-directory-p a)) (file-directory-p b))
  651. (setq retval (eq sort-folders 'last)))))
  652. retval))))
  653. (message "Generating sitemap for %s" title)
  654. (with-temp-file sitemap-filename
  655. (insert
  656. (let ((files (remove sitemap-filename
  657. (org-publish-get-base-files project))))
  658. ;; Add directories, if applicable.
  659. (unless (and (eq style 'list) (eq sort-folders 'ignore))
  660. (setq files
  661. (nconc (remove root (org-uniquify
  662. (mapcar #'file-name-directory files)))
  663. files)))
  664. ;; Eventually sort all entries.
  665. (when (or sort-files (not (memq sort-folders 'ignore)))
  666. (setq files (sort files sort-predicate)))
  667. (funcall sitemap-builder
  668. title
  669. (org-publish--sitemap-files-to-lisp
  670. files project style format-entry)))))))
  671. (defun org-publish-find-property (file property project &optional backend)
  672. "Find the PROPERTY of FILE in project.
  673. PROPERTY is a keyword referring to an export option, as defined
  674. in `org-export-options-alist' or in export back-ends. In the
  675. latter case, optional argument BACKEND has to be set to the
  676. back-end where the option is defined, e.g.,
  677. (org-publish-find-property file :subtitle 'latex)
  678. Return value may be a string or a list, depending on the type of
  679. PROPERTY, i.e. \"behavior\" parameter from `org-export-options-alist'."
  680. (let ((file (org-publish--expand-file-name file project)))
  681. (when (and (file-readable-p file) (not (directory-name-p file)))
  682. (let* ((org-inhibit-startup t)
  683. (visiting (find-buffer-visiting file))
  684. (buffer (or visiting (find-file-noselect file))))
  685. (unwind-protect
  686. (plist-get (with-current-buffer buffer
  687. (if (not visiting) (org-export-get-environment backend)
  688. ;; Protect local variables in open buffers.
  689. (org-export-with-buffer-copy
  690. (org-export-get-environment backend))))
  691. property)
  692. (unless visiting (kill-buffer buffer)))))))
  693. (defun org-publish-find-title (file project)
  694. "Find the title of FILE in PROJECT."
  695. (let ((file (org-publish--expand-file-name file project)))
  696. (or (org-publish-cache-get-file-property file :title nil t)
  697. (let* ((parsed-title (org-publish-find-property file :title project))
  698. (title
  699. (if parsed-title
  700. ;; Remove property so that the return value is
  701. ;; cache-able (i.e., it can be `read' back).
  702. (org-no-properties
  703. (org-element-interpret-data parsed-title))
  704. (file-name-nondirectory (file-name-sans-extension file)))))
  705. (org-publish-cache-set-file-property file :title title)
  706. title))))
  707. (defun org-publish-find-date (file project)
  708. "Find the date of FILE in PROJECT.
  709. This function assumes FILE is either a directory or an Org file.
  710. If FILE is an Org file and provides a DATE keyword use it. In
  711. any other case use the file system's modification time. Return
  712. time in `current-time' format."
  713. (let ((file (org-publish--expand-file-name file project)))
  714. (if (file-directory-p file) (nth 5 (file-attributes file))
  715. (let ((date (org-publish-find-property file :date project)))
  716. ;; DATE is a secondary string. If it contains a time-stamp,
  717. ;; convert it to internal format. Otherwise, use FILE
  718. ;; modification time.
  719. (cond ((let ((ts (and (consp date) (assq 'timestamp date))))
  720. (and ts
  721. (let ((value (org-element-interpret-data ts)))
  722. (and (org-string-nw-p value)
  723. (org-time-string-to-time value))))))
  724. ((file-exists-p file) (nth 5 (file-attributes file)))
  725. (t (error "No such file: \"%s\"" file)))))))
  726. (defun org-publish-sitemap-default-entry (entry style project)
  727. "Default format for site map ENTRY, as a string.
  728. ENTRY is a file name. STYLE is the style of the sitemap.
  729. PROJECT is the current project."
  730. (cond ((not (directory-name-p entry))
  731. (format "[[file:%s][%s]]"
  732. entry
  733. (org-publish-find-title entry project)))
  734. ((eq style 'tree)
  735. ;; Return only last subdir.
  736. (file-name-nondirectory (directory-file-name entry)))
  737. (t entry)))
  738. (defun org-publish-sitemap-default (title list)
  739. "Default site map, as a string.
  740. TITLE is the the title of the site map. LIST is an internal
  741. representation for the files to include, as returned by
  742. `org-list-to-lisp'. PROJECT is the current project."
  743. (concat "#+TITLE: " title "\n\n"
  744. (org-list-to-org list)))
  745. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  746. ;;; Interactive publishing functions
  747. ;;;###autoload
  748. (defalias 'org-publish-project 'org-publish)
  749. ;;;###autoload
  750. (defun org-publish (project &optional force async)
  751. "Publish PROJECT.
  752. PROJECT is either a project name, as a string, or a project
  753. alist (see `org-publish-project-alist' variable).
  754. When optional argument FORCE is non-nil, force publishing all
  755. files in PROJECT. With a non-nil optional argument ASYNC,
  756. publishing will be done asynchronously, in another process."
  757. (interactive
  758. (list (assoc (completing-read "Publish project: "
  759. org-publish-project-alist nil t)
  760. org-publish-project-alist)
  761. current-prefix-arg))
  762. (let ((project (if (not (stringp project)) project
  763. ;; If this function is called in batch mode,
  764. ;; PROJECT is still a string here.
  765. (assoc project org-publish-project-alist))))
  766. (cond
  767. ((not project))
  768. (async
  769. (org-export-async-start (lambda (_) nil)
  770. `(let ((org-publish-use-timestamps-flag
  771. ,(and (not force) org-publish-use-timestamps-flag)))
  772. ;; Expand components right now as external process may not
  773. ;; be aware of complete `org-publish-project-alist'.
  774. (org-publish-projects
  775. ',(org-publish-expand-projects (list project))))))
  776. (t (save-window-excursion
  777. (let ((org-publish-use-timestamps-flag
  778. (and (not force) org-publish-use-timestamps-flag)))
  779. (org-publish-projects (list project))))))))
  780. ;;;###autoload
  781. (defun org-publish-all (&optional force async)
  782. "Publish all projects.
  783. With prefix argument FORCE, remove all files in the timestamp
  784. directory and force publishing all projects. With a non-nil
  785. optional argument ASYNC, publishing will be done asynchronously,
  786. in another process."
  787. (interactive "P")
  788. (if async
  789. (org-export-async-start (lambda (_) nil)
  790. `(progn
  791. (when ',force (org-publish-remove-all-timestamps))
  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. (when force (org-publish-remove-all-timestamps))
  796. (save-window-excursion
  797. (let ((org-publish-use-timestamps-flag
  798. (if force nil org-publish-use-timestamps-flag)))
  799. (org-publish-projects org-publish-project-alist)))))
  800. ;;;###autoload
  801. (defun org-publish-current-file (&optional force async)
  802. "Publish the current file.
  803. With prefix argument FORCE, force publish the file. When
  804. optional argument ASYNC is non-nil, publishing will be done
  805. asynchronously, in another process."
  806. (interactive "P")
  807. (let ((file (buffer-file-name (buffer-base-buffer))))
  808. (if async
  809. (org-export-async-start (lambda (_) nil)
  810. `(let ((org-publish-use-timestamps-flag
  811. (if ',force nil ,org-publish-use-timestamps-flag)))
  812. (org-publish-file ,file)))
  813. (save-window-excursion
  814. (let ((org-publish-use-timestamps-flag
  815. (if force nil org-publish-use-timestamps-flag)))
  816. (org-publish-file file))))))
  817. ;;;###autoload
  818. (defun org-publish-current-project (&optional force async)
  819. "Publish the project associated with the current file.
  820. With a prefix argument, force publishing of all files in
  821. the project."
  822. (interactive "P")
  823. (save-window-excursion
  824. (let ((project (org-publish-get-project-from-filename
  825. (buffer-file-name (buffer-base-buffer)) 'up)))
  826. (if project (org-publish project force async)
  827. (error "File %s is not part of any known project"
  828. (buffer-file-name (buffer-base-buffer)))))))
  829. ;;; Index generation
  830. (defun org-publish-collect-index (output _backend info)
  831. "Update index for a file in cache.
  832. OUTPUT is the output from transcoding current file. BACKEND is
  833. the back-end that was used for transcoding. INFO is a plist
  834. containing publishing and export options.
  835. The index relative to current file is stored as an alist. An
  836. association has the following shape: (TERM FILE-NAME PARENT),
  837. where TERM is the indexed term, as a string, FILE-NAME is the
  838. original full path of the file where the term in encountered, and
  839. PARENT is a reference to the headline, if any, containing the
  840. original index keyword. When non-nil, this reference is a cons
  841. cell. Its CAR is a symbol among `id', `custom-id' and `name' and
  842. its CDR is a string."
  843. (let ((file (plist-get info :input-file)))
  844. (org-publish-cache-set-file-property
  845. file :index
  846. (delete-dups
  847. (org-element-map (plist-get info :parse-tree) 'keyword
  848. (lambda (k)
  849. (when (equal (org-element-property :key k) "INDEX")
  850. (let ((parent (org-export-get-parent-headline k)))
  851. (list (org-element-property :value k)
  852. file
  853. (cond
  854. ((not parent) nil)
  855. ((let ((id (org-element-property :ID parent)))
  856. (and id (cons 'id id))))
  857. ((let ((id (org-element-property :CUSTOM_ID parent)))
  858. (and id (cons 'custom-id id))))
  859. (t (cons 'name
  860. ;; Remove statistics cookie.
  861. (replace-regexp-in-string
  862. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  863. (org-element-property :raw-value parent)))))))))
  864. info))))
  865. ;; Return output unchanged.
  866. output)
  867. (defun org-publish-index-generate-theindex (project directory)
  868. "Retrieve full index from cache and build \"theindex.org\".
  869. PROJECT is the project the index relates to. DIRECTORY is the
  870. publishing directory."
  871. (let ((all-files (org-publish-get-base-files project))
  872. full-index)
  873. ;; Compile full index and sort it alphabetically.
  874. (dolist (file all-files
  875. (setq full-index
  876. (sort (nreverse full-index)
  877. (lambda (a b) (string< (downcase (car a))
  878. (downcase (car b)))))))
  879. (let ((index (org-publish-cache-get-file-property file :index)))
  880. (dolist (term index)
  881. (unless (member term full-index) (push term full-index)))))
  882. ;; Write "theindex.inc" in DIRECTORY.
  883. (with-temp-file (expand-file-name "theindex.inc" directory)
  884. (let ((current-letter nil) (last-entry nil))
  885. (dolist (idx full-index)
  886. (let* ((entry (org-split-string (car idx) "!"))
  887. (letter (upcase (substring (car entry) 0 1)))
  888. ;; Transform file into a path relative to publishing
  889. ;; directory.
  890. (file (file-relative-name
  891. (nth 1 idx)
  892. (plist-get (cdr project) :base-directory))))
  893. ;; Check if another letter has to be inserted.
  894. (unless (string= letter current-letter)
  895. (insert (format "* %s\n" letter)))
  896. ;; Compute the first difference between last entry and
  897. ;; current one: it tells the level at which new items
  898. ;; should be added.
  899. (let* ((rank
  900. (if (equal entry last-entry) (1- (length entry))
  901. (cl-loop for n from 0 to (length entry)
  902. unless (equal (nth n entry) (nth n last-entry))
  903. return n)))
  904. (len (length (nthcdr rank entry))))
  905. ;; For each term after the first difference, create
  906. ;; a new sub-list with the term as body. Moreover,
  907. ;; linkify the last term.
  908. (dotimes (n len)
  909. (insert
  910. (concat
  911. (make-string (* (+ rank n) 2) ?\s) " - "
  912. (if (not (= (1- len) n)) (nth (+ rank n) entry)
  913. ;; Last term: Link it to TARGET, if possible.
  914. (let ((target (nth 2 idx)))
  915. (format
  916. "[[%s][%s]]"
  917. ;; Destination.
  918. (pcase (car target)
  919. (`nil (format "file:%s" file))
  920. (`id (format "id:%s" (cdr target)))
  921. (`custom-id (format "file:%s::#%s" file (cdr target)))
  922. (_ (format "file:%s::*%s" file (cdr target))))
  923. ;; Description.
  924. (car (last entry)))))
  925. "\n"))))
  926. (setq current-letter letter last-entry entry))))
  927. ;; Create "theindex.org", if it doesn't exist yet, and provide
  928. ;; a default index file.
  929. (let ((index.org (expand-file-name "theindex.org" directory)))
  930. (unless (file-exists-p index.org)
  931. (with-temp-file index.org
  932. (insert "#+TITLE: Index\n\n#+INCLUDE: \"theindex.inc\"\n\n")))))))
  933. ;;; External Fuzzy Links Resolution
  934. ;;
  935. ;; This part implements tools to resolve [[file.org::*Some headline]]
  936. ;; links, where "file.org" belongs to the current project.
  937. (defun org-publish--store-crossrefs (output _backend info)
  938. "Store cross-references for current published file.
  939. OUPUT is the produced output, as a string. BACKEND is the export
  940. back-end used, as a symbol. INFO is the final export state, as
  941. a plist.
  942. This function is meant to be used as a final output filter. See
  943. `org-publish-org-to'."
  944. (org-publish-cache-set-file-property
  945. (plist-get info :input-file) :crossrefs
  946. ;; Update `:crossrefs' so as to remove unused references and search
  947. ;; cells. Actually used references are extracted from
  948. ;; `:internal-references', with references as strings removed. See
  949. ;; `org-export-get-reference' for details.
  950. (cl-remove-if (lambda (pair) (stringp (car pair)))
  951. (plist-get info :internal-references)))
  952. ;; Return output unchanged.
  953. output)
  954. (defun org-publish-resolve-external-link (search file)
  955. "Return reference for element matching string SEARCH in FILE.
  956. Return value is an internal reference, as a string.
  957. This function allows resolving external links with a search
  958. option, e.g.,
  959. [[file.org::*heading][description]]
  960. [[file.org::#custom-id][description]]
  961. [[file.org::fuzzy][description]]
  962. It only makes sense to use this if export back-end builds
  963. references with `org-export-get-reference'."
  964. (if (not org-publish-cache)
  965. (progn
  966. (message "Reference %S in file %S cannot be resolved without publishing"
  967. search
  968. file)
  969. "MissingReference")
  970. (let* ((filename (expand-file-name file))
  971. (crossrefs
  972. (org-publish-cache-get-file-property filename :crossrefs nil t))
  973. (cells (org-export-string-to-search-cell search)))
  974. (or
  975. ;; Look for reference associated to search cells triggered by
  976. ;; LINK. It can match when targeted file has been published
  977. ;; already.
  978. (let ((known (cdr (cl-some (lambda (c) (assoc c crossrefs)) cells))))
  979. (and known (org-export-format-reference known)))
  980. ;; Search cell is unknown so far. Generate a new internal
  981. ;; reference that will be used when the targeted file will be
  982. ;; published.
  983. (let ((new (org-export-new-reference crossrefs)))
  984. (dolist (cell cells) (push (cons cell new) crossrefs))
  985. (org-publish-cache-set-file-property filename :crossrefs crossrefs)
  986. (org-export-format-reference new))))))
  987. ;;; Caching functions
  988. (defun org-publish-write-cache-file (&optional free-cache)
  989. "Write `org-publish-cache' to file.
  990. If FREE-CACHE, empty the cache."
  991. (unless org-publish-cache
  992. (error "`org-publish-write-cache-file' called, but no cache present"))
  993. (let ((cache-file (org-publish-cache-get ":cache-file:")))
  994. (unless cache-file
  995. (error "Cannot find cache-file name in `org-publish-write-cache-file'"))
  996. (with-temp-file cache-file
  997. (let (print-level print-length)
  998. (insert "(setq org-publish-cache \
  999. \(make-hash-table :test 'equal :weakness nil :size 100))\n")
  1000. (maphash (lambda (k v)
  1001. (insert
  1002. (format "(puthash %S %s%S org-publish-cache)\n"
  1003. k (if (or (listp v) (symbolp v)) "'" "") v)))
  1004. org-publish-cache)))
  1005. (when free-cache (org-publish-reset-cache))))
  1006. (defun org-publish-initialize-cache (project-name)
  1007. "Initialize the projects cache if not initialized yet and return it."
  1008. (unless project-name
  1009. (error "Cannot initialize `org-publish-cache' without projects name in \
  1010. `org-publish-initialize-cache'"))
  1011. (unless (file-exists-p org-publish-timestamp-directory)
  1012. (make-directory org-publish-timestamp-directory t))
  1013. (unless (file-directory-p org-publish-timestamp-directory)
  1014. (error "Org publish timestamp: %s is not a directory"
  1015. org-publish-timestamp-directory))
  1016. (unless (and org-publish-cache
  1017. (string= (org-publish-cache-get ":project:") project-name))
  1018. (let* ((cache-file
  1019. (concat
  1020. (expand-file-name org-publish-timestamp-directory)
  1021. project-name ".cache"))
  1022. (cexists (file-exists-p cache-file)))
  1023. (when org-publish-cache (org-publish-reset-cache))
  1024. (if cexists (load-file cache-file)
  1025. (setq org-publish-cache
  1026. (make-hash-table :test 'equal :weakness nil :size 100))
  1027. (org-publish-cache-set ":project:" project-name)
  1028. (org-publish-cache-set ":cache-file:" cache-file))
  1029. (unless cexists (org-publish-write-cache-file nil))))
  1030. org-publish-cache)
  1031. (defun org-publish-reset-cache ()
  1032. "Empty org-publish-cache and reset it nil."
  1033. (message "%s" "Resetting org-publish-cache")
  1034. (when (hash-table-p org-publish-cache)
  1035. (clrhash org-publish-cache))
  1036. (setq org-publish-cache nil))
  1037. (defun org-publish-cache-file-needs-publishing
  1038. (filename &optional pub-dir pub-func _base-dir)
  1039. "Check the timestamp of the last publishing of FILENAME.
  1040. Return non-nil if the file needs publishing. Also check if
  1041. any included files have been more recently published, so that
  1042. the file including them will be republished as well."
  1043. (unless org-publish-cache
  1044. (error
  1045. "`org-publish-cache-file-needs-publishing' called, but no cache present"))
  1046. (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func))
  1047. (pstamp (org-publish-cache-get key))
  1048. (org-inhibit-startup t)
  1049. included-files-ctime)
  1050. (when (equal (file-name-extension filename) "org")
  1051. (let ((visiting (find-buffer-visiting filename))
  1052. (buf (find-file-noselect filename))
  1053. (case-fold-search t))
  1054. (unwind-protect
  1055. (with-current-buffer buf
  1056. (goto-char (point-min))
  1057. (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t)
  1058. (let* ((element (org-element-at-point))
  1059. (included-file
  1060. (and (eq (org-element-type element) 'keyword)
  1061. (let ((value (org-element-property :value element)))
  1062. (and value
  1063. (string-match
  1064. "\\`\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)"
  1065. value)
  1066. (let ((m (match-string 1 value)))
  1067. (org-unbracket-string
  1068. "\"" "\""
  1069. ;; Ignore search suffix.
  1070. (if (string-match "::.*?\"?\\'" m)
  1071. (substring m 0 (match-beginning 0))
  1072. m))))))))
  1073. (when included-file
  1074. (push (org-publish-cache-ctime-of-src
  1075. (expand-file-name included-file))
  1076. included-files-ctime)))))
  1077. (unless visiting (kill-buffer buf)))))
  1078. (or (null pstamp)
  1079. (let ((ctime (org-publish-cache-ctime-of-src filename)))
  1080. (or (< pstamp ctime)
  1081. (cl-some (lambda (ct) (< ctime ct)) included-files-ctime))))))
  1082. (defun org-publish-cache-set-file-property
  1083. (filename property value &optional project-name)
  1084. "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
  1085. Use cache file of PROJECT-NAME. If the entry does not exist, it
  1086. will be created. Return VALUE."
  1087. ;; Evtl. load the requested cache file:
  1088. (if project-name (org-publish-initialize-cache project-name))
  1089. (let ((pl (org-publish-cache-get filename)))
  1090. (if pl (progn (plist-put pl property value) value)
  1091. (org-publish-cache-get-file-property
  1092. filename property value nil project-name))))
  1093. (defun org-publish-cache-get-file-property
  1094. (filename property &optional default no-create project-name)
  1095. "Return the value for a PROPERTY of file FILENAME in publishing cache.
  1096. Use cache file of PROJECT-NAME. Return the value of that PROPERTY,
  1097. or DEFAULT, if the value does not yet exist. Create the entry,
  1098. if necessary, unless NO-CREATE is non-nil."
  1099. ;; Evtl. load the requested cache file:
  1100. (if project-name (org-publish-initialize-cache project-name))
  1101. (let ((pl (org-publish-cache-get filename)) retval)
  1102. (if pl
  1103. (if (plist-member pl property)
  1104. (setq retval (plist-get pl property))
  1105. (setq retval default))
  1106. ;; no pl yet:
  1107. (unless no-create
  1108. (org-publish-cache-set filename (list property default)))
  1109. (setq retval default))
  1110. retval))
  1111. (defun org-publish-cache-get (key)
  1112. "Return the value stored in `org-publish-cache' for key KEY.
  1113. Return nil, if no value or nil is found. Raise an error if the
  1114. cache does not exist."
  1115. (unless org-publish-cache
  1116. (error "`org-publish-cache-get' called, but no cache present"))
  1117. (gethash key org-publish-cache))
  1118. (defun org-publish-cache-set (key value)
  1119. "Store KEY VALUE pair in `org-publish-cache'.
  1120. Returns value on success, else nil. Raise an error if the cache
  1121. does not exist."
  1122. (unless org-publish-cache
  1123. (error "`org-publish-cache-set' called, but no cache present"))
  1124. (puthash key value org-publish-cache))
  1125. (defun org-publish-cache-ctime-of-src (file)
  1126. "Get the ctime of FILE as an integer."
  1127. (let ((attr (file-attributes
  1128. (expand-file-name (or (file-symlink-p file) file)
  1129. (file-name-directory file)))))
  1130. (if (not attr) (error "No such file: \"%s\"" file)
  1131. (+ (lsh (car (nth 5 attr)) 16)
  1132. (cadr (nth 5 attr))))))
  1133. (provide 'ox-publish)
  1134. ;; Local variables:
  1135. ;; generated-autoload-file: "org-loaddefs.el"
  1136. ;; End:
  1137. ;;; ox-publish.el ends here