org-e-publish.el 43 KB

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