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