ox-publish.el 49 KB

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