ox-publish.el 46 KB

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