ox-publish.el 50 KB

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