ox-publish.el 49 KB

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