org-publish.el 45 KB

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