org-e-publish.el 44 KB

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