org-e-publish.el 45 KB

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