ox-publish.el 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. ;;; ox-publish.el --- Publish Related Org Mode Files as a Website
  2. ;; Copyright (C) 2006-2014 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-publish-sitemap-sort-files)
  295. (defvar org-publish-sitemap-sort-folders)
  296. (defvar org-publish-sitemap-ignore-case)
  297. (defvar org-publish-sitemap-requested)
  298. (defvar org-publish-sitemap-date-format)
  299. (defvar org-publish-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-publish-sitemap-sort-files org-publish-sitemap-sort-folders)
  304. ;; First we sort files:
  305. (when org-publish-sitemap-sort-files
  306. (case org-publish-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-publish-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-publish-sitemap-sort-files 'chronologically) (<= A B)
  326. (>= A B)))))))
  327. ;; Directory-wise wins:
  328. (when org-publish-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-publish-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-publish-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-publish-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-publish-sitemap-requested
  381. (plist-get project-plist :auto-sitemap))
  382. (sitemap-filename
  383. (or (plist-get project-plist :sitemap-filename) "sitemap.org"))
  384. (org-publish-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-publish-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-publish-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-publish-sitemap-sort-folders' has an accepted
  403. ;; value.
  404. (unless (memq org-publish-sitemap-sort-folders '(first last))
  405. (setq org-publish-sitemap-sort-folders nil))
  406. (setq org-publish-temp-files nil)
  407. (if org-publish-sitemap-requested
  408. (pushnew (expand-file-name (concat base-dir sitemap-filename))
  409. org-publish-temp-files))
  410. (org-publish-get-base-files-1 base-dir recurse match
  411. ;; FIXME distinguish exclude regexp
  412. ;; for skip-file and skip-dir?
  413. exclude-regexp exclude-regexp)
  414. (mapc (lambda (f)
  415. (pushnew
  416. (expand-file-name (concat base-dir f))
  417. org-publish-temp-files))
  418. include-list)
  419. org-publish-temp-files))
  420. (defun org-publish-get-project-from-filename (filename &optional up)
  421. "Return the project that FILENAME belongs to."
  422. (let* ((filename (expand-file-name filename))
  423. project-name)
  424. (catch 'p-found
  425. (dolist (prj org-publish-project-alist)
  426. (unless (plist-get (cdr prj) :components)
  427. ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
  428. (let* ((r (plist-get (cdr prj) :recursive))
  429. (b (expand-file-name (file-name-as-directory
  430. (plist-get (cdr prj) :base-directory))))
  431. (x (or (plist-get (cdr prj) :base-extension) "org"))
  432. (e (plist-get (cdr prj) :exclude))
  433. (i (plist-get (cdr prj) :include))
  434. (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
  435. (when
  436. (or (and i
  437. (member filename
  438. (mapcar (lambda (file)
  439. (expand-file-name file b))
  440. i)))
  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-numbering' 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-numbering
  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. (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-publish-sitemap-date-format
  566. (or (plist-get project-plist :sitemap-date-format)
  567. org-publish-sitemap-date-format))
  568. (org-publish-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-publish-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
  677. fmt
  678. `((?t . ,(org-publish-find-title file t))
  679. (?d . ,(format-time-string org-publish-sitemap-date-format
  680. (org-publish-find-date file)))
  681. (?a . ,(or (plist-get project-plist :author) user-full-name)))))
  682. (defun org-publish-find-title (file &optional reset)
  683. "Find the title of FILE in project."
  684. (or
  685. (and (not reset) (org-publish-cache-get-file-property file :title nil t))
  686. (let* ((org-inhibit-startup t)
  687. (visiting (find-buffer-visiting file))
  688. (buffer (or visiting (find-file-noselect file))))
  689. (with-current-buffer buffer
  690. (org-mode)
  691. (let ((title
  692. (let ((property (plist-get (org-export-get-environment) :title)))
  693. (if property (org-element-interpret-data property)
  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. `(progn
  769. (when ',force (org-publish-remove-all-timestamps))
  770. (let ((org-publish-use-timestamps-flag
  771. (if ',force nil ,org-publish-use-timestamps-flag)))
  772. (org-publish-projects ',org-publish-project-alist))))
  773. (when force (org-publish-remove-all-timestamps))
  774. (save-window-excursion
  775. (let ((org-publish-use-timestamps-flag
  776. (if force nil org-publish-use-timestamps-flag)))
  777. (org-publish-projects org-publish-project-alist)))))
  778. ;;;###autoload
  779. (defun org-publish-current-file (&optional force async)
  780. "Publish the current file.
  781. With prefix argument FORCE, force publish the file. When
  782. optional argument ASYNC is non-nil, publishing will be done
  783. asynchronously, in another process."
  784. (interactive "P")
  785. (let ((file (buffer-file-name (buffer-base-buffer))))
  786. (if async
  787. (org-export-async-start 'ignore
  788. `(let ((org-publish-use-timestamps-flag
  789. (if ',force nil ,org-publish-use-timestamps-flag)))
  790. (org-publish-file ,file)))
  791. (save-window-excursion
  792. (let ((org-publish-use-timestamps-flag
  793. (if force nil org-publish-use-timestamps-flag)))
  794. (org-publish-file file))))))
  795. ;;;###autoload
  796. (defun org-publish-current-project (&optional force async)
  797. "Publish the project associated with the current file.
  798. With a prefix argument, force publishing of all files in
  799. the project."
  800. (interactive "P")
  801. (save-window-excursion
  802. (let ((project (org-publish-get-project-from-filename
  803. (buffer-file-name (buffer-base-buffer)) 'up)))
  804. (if project (org-publish project force async)
  805. (error "File %s is not part of any known project"
  806. (buffer-file-name (buffer-base-buffer)))))))
  807. ;;; Index generation
  808. (defun org-publish-collect-index (output backend info)
  809. "Update index for a file in cache.
  810. OUTPUT is the output from transcoding current file. BACKEND is
  811. the back-end that was used for transcoding. INFO is a plist
  812. containing publishing and export options.
  813. The index relative to current file is stored as an alist. An
  814. association has the following shape: (TERM FILE-NAME PARENT),
  815. where TERM is the indexed term, as a string, FILE-NAME is the
  816. original full path of the file where the term in encountered, and
  817. PARENT is a reference to the headline, if any, containing the
  818. original index keyword. When non-nil, this reference is a cons
  819. cell. Its CAR is a symbol among `id', `custom-id' and `name' and
  820. its CDR is a string."
  821. (let ((file (plist-get info :input-file)))
  822. (org-publish-cache-set-file-property
  823. file :index
  824. (delete-dups
  825. (org-element-map (plist-get info :parse-tree) 'keyword
  826. (lambda (k)
  827. (when (equal (org-element-property :key k) "INDEX")
  828. (let ((parent (org-export-get-parent-headline k)))
  829. (list (org-element-property :value k)
  830. file
  831. (cond
  832. ((not parent) nil)
  833. ((let ((id (org-element-property :ID parent)))
  834. (and id (cons 'id id))))
  835. ((let ((id (org-element-property :CUSTOM_ID parent)))
  836. (and id (cons 'custom-id id))))
  837. (t (cons 'name
  838. ;; Remove statistics cookie.
  839. (replace-regexp-in-string
  840. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  841. (org-element-property :raw-value parent)))))))))
  842. info))))
  843. ;; Return output unchanged.
  844. output)
  845. (defun org-publish-index-generate-theindex (project directory)
  846. "Retrieve full index from cache and build \"theindex.org\".
  847. PROJECT is the project the index relates to. DIRECTORY is the
  848. publishing directory."
  849. (let ((all-files (org-publish-get-base-files
  850. project (plist-get (cdr project) :exclude)))
  851. full-index)
  852. ;; Compile full index and sort it alphabetically.
  853. (dolist (file all-files
  854. (setq full-index
  855. (sort (nreverse full-index)
  856. (lambda (a b) (string< (downcase (car a))
  857. (downcase (car b)))))))
  858. (let ((index (org-publish-cache-get-file-property file :index)))
  859. (dolist (term index)
  860. (unless (member term full-index) (push term full-index)))))
  861. ;; Write "theindex.inc" in DIRECTORY.
  862. (with-temp-file (expand-file-name "theindex.inc" directory)
  863. (let ((current-letter nil) (last-entry nil))
  864. (dolist (idx full-index)
  865. (let* ((entry (org-split-string (car idx) "!"))
  866. (letter (upcase (substring (car entry) 0 1)))
  867. ;; Transform file into a path relative to publishing
  868. ;; directory.
  869. (file (file-relative-name
  870. (nth 1 idx)
  871. (plist-get (cdr project) :base-directory))))
  872. ;; Check if another letter has to be inserted.
  873. (unless (string= letter current-letter)
  874. (insert (format "* %s\n" letter)))
  875. ;; Compute the first difference between last entry and
  876. ;; current one: it tells the level at which new items
  877. ;; should be added.
  878. (let* ((rank (if (equal entry last-entry) (1- (length entry))
  879. (loop for n from 0 to (length entry)
  880. unless (equal (nth n entry) (nth n last-entry))
  881. return n)))
  882. (len (length (nthcdr rank entry))))
  883. ;; For each term after the first difference, create
  884. ;; a new sub-list with the term as body. Moreover,
  885. ;; linkify the last term.
  886. (dotimes (n len)
  887. (insert
  888. (concat
  889. (make-string (* (+ rank n) 2) ? ) " - "
  890. (if (not (= (1- len) n)) (nth (+ rank n) entry)
  891. ;; Last term: Link it to TARGET, if possible.
  892. (let ((target (nth 2 idx)))
  893. (format
  894. "[[%s][%s]]"
  895. ;; Destination.
  896. (case (car target)
  897. ('nil (format "file:%s" file))
  898. (id (format "id:%s" (cdr target)))
  899. (custom-id (format "file:%s::#%s" file (cdr target)))
  900. (otherwise (format "file:%s::*%s" file (cdr target))))
  901. ;; Description.
  902. (car (last entry)))))
  903. "\n"))))
  904. (setq current-letter letter last-entry entry))))
  905. ;; Create "theindex.org", if it doesn't exist yet, and provide
  906. ;; a default index file.
  907. (let ((index.org (expand-file-name "theindex.org" directory)))
  908. (unless (file-exists-p index.org)
  909. (with-temp-file index.org
  910. (insert "#+TITLE: Index\n\n#+INCLUDE: \"theindex.inc\"\n\n")))))))
  911. ;;; External Fuzzy Links Resolution
  912. ;;
  913. ;; This part implements tools to resolve [[file.org::*Some headline]]
  914. ;; links, where "file.org" belongs to the current project.
  915. (defun org-publish-collect-numbering (output backend info)
  916. (org-publish-cache-set-file-property
  917. (plist-get info :input-file) :numbering
  918. (mapcar (lambda (entry)
  919. (cons (org-split-string
  920. (replace-regexp-in-string
  921. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  922. (org-element-property :raw-value (car entry))))
  923. (cdr entry)))
  924. (plist-get info :headline-numbering)))
  925. ;; Return output unchanged.
  926. output)
  927. (defun org-publish-resolve-external-fuzzy-link (file fuzzy)
  928. "Return numbering for headline matching FUZZY search in FILE.
  929. Return value is a list of numbers, or nil. This function allows
  930. to resolve external fuzzy links like:
  931. [[file.org::*fuzzy][description]]"
  932. (when org-publish-cache
  933. (cdr (assoc (org-split-string
  934. (if (eq (aref fuzzy 0) ?*) (substring fuzzy 1) fuzzy))
  935. (org-publish-cache-get-file-property
  936. (expand-file-name file) :numbering nil t)))))
  937. ;;; Caching functions
  938. (defun org-publish-write-cache-file (&optional free-cache)
  939. "Write `org-publish-cache' to file.
  940. If FREE-CACHE, empty the cache."
  941. (unless org-publish-cache
  942. (error "`org-publish-write-cache-file' called, but no cache present"))
  943. (let ((cache-file (org-publish-cache-get ":cache-file:")))
  944. (unless cache-file
  945. (error "Cannot find cache-file name in `org-publish-write-cache-file'"))
  946. (with-temp-file cache-file
  947. (let (print-level print-length)
  948. (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
  949. (maphash (lambda (k v)
  950. (insert
  951. (format (concat "(puthash %S "
  952. (if (or (listp v) (symbolp v))
  953. "'" "")
  954. "%S org-publish-cache)\n") k v)))
  955. org-publish-cache)))
  956. (when free-cache (org-publish-reset-cache))))
  957. (defun org-publish-initialize-cache (project-name)
  958. "Initialize the projects cache if not initialized yet and return it."
  959. (unless project-name
  960. (error "Cannot initialize `org-publish-cache' without projects name in `org-publish-initialize-cache'"))
  961. (unless (file-exists-p org-publish-timestamp-directory)
  962. (make-directory org-publish-timestamp-directory t))
  963. (unless (file-directory-p org-publish-timestamp-directory)
  964. (error "Org publish timestamp: %s is not a directory"
  965. org-publish-timestamp-directory))
  966. (unless (and org-publish-cache
  967. (string= (org-publish-cache-get ":project:") project-name))
  968. (let* ((cache-file
  969. (concat
  970. (expand-file-name org-publish-timestamp-directory)
  971. project-name ".cache"))
  972. (cexists (file-exists-p cache-file)))
  973. (when org-publish-cache (org-publish-reset-cache))
  974. (if cexists (load-file cache-file)
  975. (setq org-publish-cache
  976. (make-hash-table :test 'equal :weakness nil :size 100))
  977. (org-publish-cache-set ":project:" project-name)
  978. (org-publish-cache-set ":cache-file:" cache-file))
  979. (unless cexists (org-publish-write-cache-file nil))))
  980. org-publish-cache)
  981. (defun org-publish-reset-cache ()
  982. "Empty org-publish-cache and reset it nil."
  983. (message "%s" "Resetting org-publish-cache")
  984. (when (hash-table-p org-publish-cache)
  985. (clrhash org-publish-cache))
  986. (setq org-publish-cache nil))
  987. (defun org-publish-cache-file-needs-publishing
  988. (filename &optional pub-dir pub-func base-dir)
  989. "Check the timestamp of the last publishing of FILENAME.
  990. Return non-nil if the file needs publishing. Also check if
  991. any included files have been more recently published, so that
  992. the file including them will be republished as well."
  993. (unless org-publish-cache
  994. (error
  995. "`org-publish-cache-file-needs-publishing' called, but no cache present"))
  996. (let* ((case-fold-search t)
  997. (key (org-publish-timestamp-filename filename pub-dir pub-func))
  998. (pstamp (org-publish-cache-get key))
  999. (org-inhibit-startup t)
  1000. (visiting (find-buffer-visiting filename))
  1001. included-files-ctime buf)
  1002. (when (equal (file-name-extension filename) "org")
  1003. (setq buf (find-file (expand-file-name filename)))
  1004. (with-current-buffer buf
  1005. (goto-char (point-min))
  1006. (while (re-search-forward
  1007. "^#\\+INCLUDE:[ \t]+\"\\([^\t\n\r\"]*\\)\"[ \t]*.*$" nil t)
  1008. (let* ((included-file (expand-file-name (match-string 1))))
  1009. (add-to-list 'included-files-ctime
  1010. (org-publish-cache-ctime-of-src included-file) t))))
  1011. (unless visiting (kill-buffer buf)))
  1012. (if (null pstamp) t
  1013. (let ((ctime (org-publish-cache-ctime-of-src filename)))
  1014. (or (< pstamp ctime)
  1015. (when included-files-ctime
  1016. (not (null (delq nil (mapcar (lambda(ct) (< ctime ct))
  1017. included-files-ctime))))))))))
  1018. (defun org-publish-cache-set-file-property
  1019. (filename property value &optional project-name)
  1020. "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
  1021. Use cache file of PROJECT-NAME. If the entry does not exist, it
  1022. will be created. Return VALUE."
  1023. ;; Evtl. load the requested cache file:
  1024. (if project-name (org-publish-initialize-cache project-name))
  1025. (let ((pl (org-publish-cache-get filename)))
  1026. (if pl (progn (plist-put pl property value) value)
  1027. (org-publish-cache-get-file-property
  1028. filename property value nil project-name))))
  1029. (defun org-publish-cache-get-file-property
  1030. (filename property &optional default no-create project-name)
  1031. "Return the value for a PROPERTY of file FILENAME in publishing cache.
  1032. Use cache file of PROJECT-NAME. Return the value of that PROPERTY
  1033. or DEFAULT, if the value does not yet exist. If the entry will
  1034. be created, unless NO-CREATE is not nil."
  1035. ;; Evtl. load the requested cache file:
  1036. (if project-name (org-publish-initialize-cache project-name))
  1037. (let ((pl (org-publish-cache-get filename)) retval)
  1038. (if pl
  1039. (if (plist-member pl property)
  1040. (setq retval (plist-get pl property))
  1041. (setq retval default))
  1042. ;; no pl yet:
  1043. (unless no-create
  1044. (org-publish-cache-set filename (list property default)))
  1045. (setq retval default))
  1046. retval))
  1047. (defun org-publish-cache-get (key)
  1048. "Return the value stored in `org-publish-cache' for key KEY.
  1049. Returns nil, if no value or nil is found, or the cache does not
  1050. exist."
  1051. (unless org-publish-cache
  1052. (error "`org-publish-cache-get' called, but no cache present"))
  1053. (gethash key org-publish-cache))
  1054. (defun org-publish-cache-set (key value)
  1055. "Store KEY VALUE pair in `org-publish-cache'.
  1056. Returns value on success, else nil."
  1057. (unless org-publish-cache
  1058. (error "`org-publish-cache-set' called, but no cache present"))
  1059. (puthash key value org-publish-cache))
  1060. (defun org-publish-cache-ctime-of-src (file)
  1061. "Get the ctime of FILE as an integer."
  1062. (let ((attr (file-attributes
  1063. (expand-file-name (or (file-symlink-p file) file)
  1064. (file-name-directory file)))))
  1065. (if (not attr) (error "No such file: \"%s\"" file)
  1066. (+ (lsh (car (nth 5 attr)) 16)
  1067. (cadr (nth 5 attr))))))
  1068. (provide 'ox-publish)
  1069. ;; Local variables:
  1070. ;; generated-autoload-file: "org-loaddefs.el"
  1071. ;; End:
  1072. ;;; ox-publish.el ends here