ox-publish.el 49 KB

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