ox-publish.el 45 KB

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