ox-publish.el 50 KB

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