org-e-publish.el 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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-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. (defun org-e-publish-org-to-html (plist filename pub-dir)
  493. "Publish an org file to HTML.
  494. FILENAME is the filename of the Org file to be published. PLIST
  495. is the property list for the given project. PUB-DIR is the
  496. publishing directory.
  497. Return output file name."
  498. (org-e-publish-org-to 'e-html filename "html" plist pub-dir))
  499. ;; TODO: Not implemented yet.
  500. ;; (defun org-e-publish-org-to-org (plist filename pub-dir)
  501. ;; "Publish an org file to HTML.
  502. ;;
  503. ;; FILENAME is the filename of the Org file to be published. PLIST
  504. ;; is the property list for the given project. PUB-DIR is the
  505. ;; publishing directory.
  506. ;;
  507. ;; Return output file name."
  508. ;; (org-e-publish-org-to "org" plist filename pub-dir))
  509. (defun org-e-publish-org-to-ascii (plist filename pub-dir)
  510. "Publish an Org file to ASCII.
  511. FILENAME is the filename of the Org file to be published. PLIST
  512. is the property list for the given project. PUB-DIR is the
  513. publishing directory.
  514. Return output file name."
  515. (org-e-publish-org-to
  516. 'e-ascii filename ".txt" `(:ascii-charset ascii ,@plist) pub-dir))
  517. (defun org-e-publish-org-to-latin1 (plist filename pub-dir)
  518. "Publish an Org file to Latin-1.
  519. FILENAME is the filename of the Org file to be published. PLIST
  520. is the property list for the given project. PUB-DIR is the
  521. publishing directory.
  522. Return output file name."
  523. (org-e-publish-org-to
  524. 'e-ascii filename ".txt" `(:ascii-charset latin1 ,@plist) pub-dir))
  525. (defun org-e-publish-org-to-utf8 (plist filename pub-dir)
  526. "Publish an org file to UTF-8.
  527. FILENAME is the filename of the Org file to be published. PLIST
  528. is the property list for the given project. PUB-DIR is the
  529. publishing directory.
  530. Return output file name."
  531. (org-e-publish-org-to
  532. 'e-ascii filename ".txt" `(:ascii-charset utf-8 ,@plist) pub-dir))
  533. (defun org-e-publish-attachment (plist filename pub-dir)
  534. "Publish a file with no transformation of any kind.
  535. FILENAME is the filename of the Org file to be published. PLIST
  536. is the property list for the given project. PUB-DIR is the
  537. publishing directory.
  538. Return output file name."
  539. (unless (file-directory-p pub-dir)
  540. (make-directory pub-dir t))
  541. (or (equal (expand-file-name (file-name-directory filename))
  542. (file-name-as-directory (expand-file-name pub-dir)))
  543. (copy-file filename
  544. (expand-file-name (file-name-nondirectory filename) pub-dir)
  545. t)))
  546. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  547. ;;; Publishing files, sets of files, and indices
  548. (defun org-e-publish-file (filename &optional project no-cache)
  549. "Publish file FILENAME from PROJECT.
  550. If NO-CACHE is not nil, do not initialize org-e-publish-cache and
  551. write it to disk. This is needed, since this function is used to
  552. publish single files, when entire projects are published.
  553. See `org-e-publish-projects'."
  554. (let* ((project
  555. (or project
  556. (or (org-e-publish-get-project-from-filename filename)
  557. (error "File %s not part of any known project"
  558. (abbreviate-file-name filename)))))
  559. (project-plist (cdr project))
  560. (ftname (expand-file-name filename))
  561. (publishing-function
  562. (or (plist-get project-plist :publishing-function)
  563. 'org-e-publish-org-to-ascii))
  564. (base-dir
  565. (file-name-as-directory
  566. (expand-file-name
  567. (or (plist-get project-plist :base-directory)
  568. (error "Project %s does not have :base-directory defined"
  569. (car project))))))
  570. (pub-dir
  571. (file-name-as-directory
  572. (file-truename
  573. (or (eval (plist-get project-plist :publishing-directory))
  574. (error "Project %s does not have :publishing-directory defined"
  575. (car project))))))
  576. tmp-pub-dir)
  577. (unless no-cache (org-e-publish-initialize-cache (car project)))
  578. (setq tmp-pub-dir
  579. (file-name-directory
  580. (concat pub-dir
  581. (and (string-match (regexp-quote base-dir) ftname)
  582. (substring ftname (match-end 0))))))
  583. (if (listp publishing-function)
  584. ;; allow chain of publishing functions
  585. (mapc (lambda (f)
  586. (when (org-e-publish-needed-p filename pub-dir f tmp-pub-dir)
  587. (funcall f project-plist filename tmp-pub-dir)
  588. (org-e-publish-update-timestamp filename pub-dir f)))
  589. publishing-function)
  590. (when (org-e-publish-needed-p filename pub-dir publishing-function
  591. tmp-pub-dir)
  592. (funcall publishing-function project-plist filename tmp-pub-dir)
  593. (org-e-publish-update-timestamp
  594. filename pub-dir publishing-function)))
  595. (unless no-cache (org-e-publish-write-cache-file))))
  596. (defun org-e-publish-projects (projects)
  597. "Publish all files belonging to the PROJECTS alist.
  598. If `:auto-sitemap' is set, publish the sitemap too. If
  599. `:makeindex' is set, also produce a file theindex.org."
  600. (mapc
  601. (lambda (project)
  602. ;; Each project uses its own cache file:
  603. (org-e-publish-initialize-cache (car project))
  604. (let* ((project-plist (cdr project))
  605. (exclude-regexp (plist-get project-plist :exclude))
  606. (sitemap-p (plist-get project-plist :auto-sitemap))
  607. (sitemap-filename (or (plist-get project-plist :sitemap-filename)
  608. "sitemap.org"))
  609. (sitemap-function (or (plist-get project-plist :sitemap-function)
  610. 'org-e-publish-org-sitemap))
  611. (org-sitemap-date-format
  612. (or (plist-get project-plist :sitemap-date-format)
  613. org-e-publish-sitemap-date-format))
  614. (org-sitemap-file-entry-format
  615. (or (plist-get project-plist :sitemap-file-entry-format)
  616. org-e-publish-sitemap-file-entry-format))
  617. (preparation-function
  618. (plist-get project-plist :preparation-function))
  619. (completion-function (plist-get project-plist :completion-function))
  620. (files (org-e-publish-get-base-files project exclude-regexp)) file)
  621. (when preparation-function (run-hooks 'preparation-function))
  622. (if sitemap-p (funcall sitemap-function project sitemap-filename))
  623. (dolist (file files) (org-e-publish-file file project t))
  624. (when (plist-get project-plist :makeindex)
  625. (org-e-publish-index-generate-theindex
  626. project (plist-get project-plist :base-directory))
  627. (org-e-publish-file
  628. (expand-file-name
  629. "theindex.org" (plist-get project-plist :base-directory))
  630. project t))
  631. (when completion-function (run-hooks 'completion-function))
  632. (org-e-publish-write-cache-file)))
  633. (org-e-publish-expand-projects projects)))
  634. (defun org-e-publish-org-sitemap (project &optional sitemap-filename)
  635. "Create a sitemap of pages in set defined by PROJECT.
  636. Optionally set the filename of the sitemap with SITEMAP-FILENAME.
  637. Default for SITEMAP-FILENAME is 'sitemap.org'."
  638. (let* ((project-plist (cdr project))
  639. (dir (file-name-as-directory
  640. (plist-get project-plist :base-directory)))
  641. (localdir (file-name-directory dir))
  642. (indent-str (make-string 2 ?\ ))
  643. (exclude-regexp (plist-get project-plist :exclude))
  644. (files (nreverse
  645. (org-e-publish-get-base-files project exclude-regexp)))
  646. (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
  647. (sitemap-title (or (plist-get project-plist :sitemap-title)
  648. (concat "Sitemap for project " (car project))))
  649. (sitemap-style (or (plist-get project-plist :sitemap-style)
  650. 'tree))
  651. (sitemap-sans-extension
  652. (plist-get project-plist :sitemap-sans-extension))
  653. (visiting (find-buffer-visiting sitemap-filename))
  654. (ifn (file-name-nondirectory sitemap-filename))
  655. file sitemap-buffer)
  656. (with-current-buffer (setq sitemap-buffer
  657. (or visiting (find-file sitemap-filename)))
  658. (erase-buffer)
  659. (insert (concat "#+TITLE: " sitemap-title "\n\n"))
  660. (while (setq file (pop files))
  661. (let ((fn (file-name-nondirectory file))
  662. (link (file-relative-name file dir))
  663. (oldlocal localdir))
  664. (when sitemap-sans-extension
  665. (setq link (file-name-sans-extension link)))
  666. ;; sitemap shouldn't list itself
  667. (unless (equal (file-truename sitemap-filename)
  668. (file-truename file))
  669. (if (eq sitemap-style 'list)
  670. (message "Generating list-style sitemap for %s" sitemap-title)
  671. (message "Generating tree-style sitemap for %s" sitemap-title)
  672. (setq localdir (concat (file-name-as-directory dir)
  673. (file-name-directory link)))
  674. (unless (string= localdir oldlocal)
  675. (if (string= localdir dir)
  676. (setq indent-str (make-string 2 ?\ ))
  677. (let ((subdirs
  678. (split-string
  679. (directory-file-name
  680. (file-name-directory
  681. (file-relative-name localdir dir))) "/"))
  682. (subdir "")
  683. (old-subdirs (split-string
  684. (file-relative-name oldlocal dir) "/")))
  685. (setq indent-str (make-string 2 ?\ ))
  686. (while (string= (car old-subdirs) (car subdirs))
  687. (setq indent-str (concat indent-str (make-string 2 ?\ )))
  688. (pop old-subdirs)
  689. (pop subdirs))
  690. (dolist (d subdirs)
  691. (setq subdir (concat subdir d "/"))
  692. (insert (concat indent-str " + " d "\n"))
  693. (setq indent-str (make-string
  694. (+ (length indent-str) 2) ?\ )))))))
  695. ;; This is common to 'flat and 'tree
  696. (let ((entry
  697. (org-e-publish-format-file-entry
  698. org-sitemap-file-entry-format file project-plist))
  699. (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
  700. (cond ((string-match-p regexp entry)
  701. (string-match regexp entry)
  702. (insert (concat indent-str " + " (match-string 1 entry)
  703. "[[file:" link "]["
  704. (match-string 2 entry)
  705. "]]" (match-string 3 entry) "\n")))
  706. (t
  707. (insert (concat indent-str " + [[file:" link "]["
  708. entry
  709. "]]\n"))))))))
  710. (save-buffer))
  711. (or visiting (kill-buffer sitemap-buffer))))
  712. (defun org-e-publish-format-file-entry (fmt file project-plist)
  713. (format-spec fmt
  714. `((?t . ,(org-e-publish-find-title file t))
  715. (?d . ,(format-time-string org-sitemap-date-format
  716. (org-e-publish-find-date file)))
  717. (?a . ,(or (plist-get project-plist :author) user-full-name)))))
  718. (defun org-e-publish-find-title (file &optional reset)
  719. "Find the title of FILE in project."
  720. (or
  721. (and (not reset) (org-e-publish-cache-get-file-property file :title nil t))
  722. (let* ((visiting (find-buffer-visiting file))
  723. (buffer (or visiting (find-file-noselect file)))
  724. title)
  725. (with-current-buffer buffer
  726. (org-mode)
  727. (setq title
  728. (or (plist-get (org-export-get-environment) :title)
  729. (file-name-nondirectory (file-name-sans-extension file)))))
  730. (unless visiting (kill-buffer buffer))
  731. (org-e-publish-cache-set-file-property file :title title)
  732. title)))
  733. (defun org-e-publish-find-date (file)
  734. "Find the date of FILE in project.
  735. If FILE provides a #+date keyword use it else use the file
  736. system's modification time.
  737. It returns time in `current-time' format."
  738. (let* ((visiting (find-buffer-visiting file))
  739. (file-buf (or visiting (find-file-noselect file nil)))
  740. (date (plist-get
  741. (with-current-buffer file-buf
  742. (org-mode)
  743. (org-export-get-inbuffer-options))
  744. :date)))
  745. (unless visiting (kill-buffer file-buf))
  746. (if date (org-time-string-to-time date)
  747. (when (file-exists-p file)
  748. (nth 5 (file-attributes file))))))
  749. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  750. ;;; Interactive publishing functions
  751. ;;;###autoload
  752. (defalias 'org-e-publish-project 'org-e-publish)
  753. ;;;###autoload
  754. (defun org-e-publish (project &optional force)
  755. "Publish PROJECT."
  756. (interactive
  757. (list
  758. (assoc (org-icompleting-read
  759. "Publish project: "
  760. org-e-publish-project-alist nil t)
  761. org-e-publish-project-alist)
  762. current-prefix-arg))
  763. (setq org-e-publish-initial-buffer (current-buffer))
  764. (save-window-excursion
  765. (let* ((org-e-publish-use-timestamps-flag
  766. (if force nil org-e-publish-use-timestamps-flag)))
  767. (org-e-publish-projects
  768. (if (stringp project)
  769. ;; If this function is called in batch mode, project is
  770. ;; still a string here.
  771. (list (assoc project org-e-publish-project-alist))
  772. (list project))))))
  773. ;;;###autoload
  774. (defun org-e-publish-all (&optional force)
  775. "Publish all projects.
  776. With prefix argument, remove all files in the timestamp
  777. directory and force publishing all files."
  778. (interactive "P")
  779. (when force (org-e-publish-remove-all-timestamps))
  780. (save-window-excursion
  781. (let ((org-e-publish-use-timestamps-flag
  782. (if force nil org-e-publish-use-timestamps-flag)))
  783. (org-e-publish-projects org-e-publish-project-alist))))
  784. ;;;###autoload
  785. (defun org-e-publish-current-file (&optional force)
  786. "Publish the current file.
  787. With prefix argument, force publish the file."
  788. (interactive "P")
  789. (save-window-excursion
  790. (let ((org-e-publish-use-timestamps-flag
  791. (if force nil org-e-publish-use-timestamps-flag)))
  792. (org-e-publish-file (buffer-file-name (buffer-base-buffer))))))
  793. ;;;###autoload
  794. (defun org-e-publish-current-project (&optional force)
  795. "Publish the project associated with the current file.
  796. With a prefix argument, force publishing of all files in
  797. the project."
  798. (interactive "P")
  799. (save-window-excursion
  800. (let ((project (org-e-publish-get-project-from-filename
  801. (buffer-file-name (buffer-base-buffer)) 'up))
  802. (org-e-publish-use-timestamps-flag
  803. (if force nil org-e-publish-use-timestamps-flag)))
  804. (if project (org-e-publish project)
  805. (error "File %s is not part of any known project"
  806. (buffer-file-name (buffer-base-buffer)))))))
  807. ;;; Index generation
  808. (defun org-e-publish-collect-index (tree backend info)
  809. "Update index for a file with TREE in cache.
  810. BACKEND is the back-end being used for transcoding. INFO is
  811. a plist containing publishing options.
  812. The index relative to current file is stored as an alist. An
  813. association has the following shape: \(TERM FILE-NAME PARENT),
  814. where TERM is the indexed term, as a string, FILE-NAME is the
  815. original full path of the file where the term in encountered, and
  816. PARENT is the headline element containing the original index
  817. keyword."
  818. (org-e-publish-cache-set-file-property
  819. (plist-get info :input-file) :index
  820. (delete-dups
  821. (org-element-map
  822. tree 'keyword
  823. (lambda (k)
  824. (when (string= (downcase (org-element-property :key k))
  825. "index")
  826. (let ((index (org-element-property :value k))
  827. (parent (org-export-get-parent-headline k info)))
  828. (list index (plist-get info :input-file) parent))))
  829. info)))
  830. ;; Return parse-tree to avoid altering output.
  831. tree)
  832. (defun org-e-publish-index-generate-theindex (project directory)
  833. "Retrieve full index from cache and build \"theindex.org\".
  834. PROJECT is the project the index relates to. DIRECTORY is the
  835. publishing directory."
  836. (let ((all-files (org-e-publish-get-base-files
  837. project (plist-get (cdr project) :exclude)))
  838. full-index)
  839. ;; Compile full index.
  840. (mapc
  841. (lambda (file)
  842. (let ((index (org-e-publish-cache-get-file-property file :index)))
  843. (dolist (term index)
  844. (unless (member term full-index) (push term full-index)))))
  845. all-files)
  846. ;; Sort it alphabetically.
  847. (setq full-index
  848. (sort full-index (lambda (a b) (string< (downcase (car a))
  849. (downcase (car b))))))
  850. ;; Fill "theindex.org".
  851. (with-temp-buffer
  852. (insert "#+TITLE: Index\n#+OPTIONS: num:nil author:nil\n")
  853. (let ((current-letter nil) (last-entry nil))
  854. (dolist (idx full-index)
  855. (let* ((entry (org-split-string (car idx) "!"))
  856. (letter (upcase (substring (car entry) 0 1)))
  857. ;; Transform file into a path relative to publishing
  858. ;; directory.
  859. (file (file-relative-name
  860. (nth 1 idx)
  861. (plist-get (cdr project) :base-directory))))
  862. ;; Check if another letter has to be inserted.
  863. (unless (string= letter current-letter)
  864. (insert (format "* %s\n" letter)))
  865. ;; Compute the first difference between last entry and
  866. ;; current one: it tells the level at which new items
  867. ;; should be added.
  868. (let* ((rank (loop for n from 0 to (length entry)
  869. unless (equal (nth n entry) (nth n last-entry))
  870. return n))
  871. (len (length (nthcdr rank entry))))
  872. ;; For each term after the first difference, create
  873. ;; a new sub-list with the term as body. Moreover,
  874. ;; linkify the last term.
  875. (dotimes (n len)
  876. (insert
  877. (concat
  878. (make-string (* (+ rank n) 2) ? ) " - "
  879. (if (not (= (1- len) n)) (nth (+ rank n) entry)
  880. ;; Last term: Link it to TARGET, if possible.
  881. (let ((target (nth 2 idx)))
  882. (format
  883. "[[%s][%s]]"
  884. ;; Destination.
  885. (cond
  886. ((not target) (format "file:%s" file))
  887. ((let ((id (org-element-property :id target)))
  888. (and id (format "id:%s" id))))
  889. ((let ((id (org-element-property :custom-id target)))
  890. (and id (format "file:%s::#%s" file id))))
  891. (t (format "file:%s::*%s" file
  892. (org-element-property :raw-value target))))
  893. ;; Description.
  894. (car (last entry)))))
  895. "\n"))))
  896. (setq current-letter letter last-entry entry))))
  897. ;; Write index.
  898. (write-file (expand-file-name "theindex.org" directory)))))
  899. ;;; Caching functions
  900. (defun org-e-publish-write-cache-file (&optional free-cache)
  901. "Write `org-e-publish-cache' to file.
  902. If FREE-CACHE, empty the cache."
  903. (unless org-e-publish-cache
  904. (error "`org-e-publish-write-cache-file' called, but no cache present"))
  905. (let ((cache-file (org-e-publish-cache-get ":cache-file:")))
  906. (unless cache-file
  907. (error "Cannot find cache-file name in `org-e-publish-write-cache-file'"))
  908. (with-temp-file cache-file
  909. (let (print-level print-length)
  910. (insert "(setq org-e-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
  911. (maphash (lambda (k v)
  912. (insert
  913. (format (concat "(puthash %S "
  914. (if (or (listp v) (symbolp v))
  915. "'" "")
  916. "%S org-e-publish-cache)\n") k v)))
  917. org-e-publish-cache)))
  918. (when free-cache (org-e-publish-reset-cache))))
  919. (defun org-e-publish-initialize-cache (project-name)
  920. "Initialize the projects cache if not initialized yet and return it."
  921. (unless project-name
  922. (error "%s%s" "Cannot initialize `org-e-publish-cache' without projects name"
  923. " in `org-e-publish-initialize-cache'"))
  924. (unless (file-exists-p org-e-publish-timestamp-directory)
  925. (make-directory org-e-publish-timestamp-directory t))
  926. (unless (file-directory-p org-e-publish-timestamp-directory)
  927. (error "Org publish timestamp: %s is not a directory"
  928. org-e-publish-timestamp-directory))
  929. (unless (and org-e-publish-cache
  930. (string= (org-e-publish-cache-get ":project:") project-name))
  931. (let* ((cache-file
  932. (concat
  933. (expand-file-name org-e-publish-timestamp-directory)
  934. project-name ".cache"))
  935. (cexists (file-exists-p cache-file)))
  936. (when org-e-publish-cache (org-e-publish-reset-cache))
  937. (if cexists (load-file cache-file)
  938. (setq org-e-publish-cache
  939. (make-hash-table :test 'equal :weakness nil :size 100))
  940. (org-e-publish-cache-set ":project:" project-name)
  941. (org-e-publish-cache-set ":cache-file:" cache-file))
  942. (unless cexists (org-e-publish-write-cache-file nil))))
  943. org-e-publish-cache)
  944. (defun org-e-publish-reset-cache ()
  945. "Empty org-e-publish-cache and reset it nil."
  946. (message "%s" "Resetting org-e-publish-cache")
  947. (when (hash-table-p org-e-publish-cache)
  948. (clrhash org-e-publish-cache))
  949. (setq org-e-publish-cache nil))
  950. (defun org-e-publish-cache-file-needs-publishing
  951. (filename &optional pub-dir pub-func)
  952. "Check the timestamp of the last publishing of FILENAME.
  953. Return `t', if the file needs publishing. The function also
  954. checks if any included files have been more recently published,
  955. so that the file including them will be republished as well."
  956. (unless org-e-publish-cache
  957. (error
  958. "`org-e-publish-cache-file-needs-publishing' called, but no cache present"))
  959. (let* ((key (org-e-publish-timestamp-filename filename pub-dir pub-func))
  960. (pstamp (org-e-publish-cache-get key))
  961. (visiting (find-buffer-visiting filename))
  962. included-files-ctime buf)
  963. (when (equal (file-name-extension filename) "org")
  964. (setq buf (find-file (expand-file-name filename)))
  965. (with-current-buffer buf
  966. (goto-char (point-min))
  967. (while (re-search-forward
  968. "^#\\+INCLUDE:[ \t]+\"?\\([^ \t\n\r\"]*\\)\"?[ \t]*.*$" nil t)
  969. (let* ((included-file (expand-file-name (match-string 1))))
  970. (add-to-list 'included-files-ctime
  971. (org-e-publish-cache-ctime-of-src included-file) t))))
  972. ;; FIXME: don't kill current buffer.
  973. (unless visiting (kill-buffer buf)))
  974. (if (null pstamp)
  975. t
  976. (let ((ctime (org-e-publish-cache-ctime-of-src filename)))
  977. (or (< pstamp ctime)
  978. (when included-files-ctime
  979. (not (null (delq nil (mapcar (lambda(ct) (< ctime ct))
  980. included-files-ctime))))))))))
  981. (defun org-e-publish-cache-set-file-property
  982. (filename property value &optional project-name)
  983. "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
  984. Use cache file of PROJECT-NAME. If the entry does not exist, it
  985. will be created. Return VALUE."
  986. ;; Evtl. load the requested cache file:
  987. (if project-name (org-e-publish-initialize-cache project-name))
  988. (let ((pl (org-e-publish-cache-get filename)))
  989. (if pl (progn (plist-put pl property value) value)
  990. (org-e-publish-cache-get-file-property
  991. filename property value nil project-name))))
  992. (defun org-e-publish-cache-get-file-property
  993. (filename property &optional default no-create project-name)
  994. "Return the value for a PROPERTY of file FILENAME in publishing cache.
  995. Use cache file of PROJECT-NAME. Return the value of that PROPERTY
  996. or DEFAULT, if the value does not yet exist. If the entry will
  997. be created, unless NO-CREATE is not nil."
  998. ;; Evtl. load the requested cache file:
  999. (if project-name (org-e-publish-initialize-cache project-name))
  1000. (let ((pl (org-e-publish-cache-get filename)) retval)
  1001. (if pl
  1002. (if (plist-member pl property)
  1003. (setq retval (plist-get pl property))
  1004. (setq retval default))
  1005. ;; no pl yet:
  1006. (unless no-create
  1007. (org-e-publish-cache-set filename (list property default)))
  1008. (setq retval default))
  1009. retval))
  1010. (defun org-e-publish-cache-get (key)
  1011. "Return the value stored in `org-e-publish-cache' for key KEY.
  1012. Returns nil, if no value or nil is found, or the cache does not
  1013. exist."
  1014. (unless org-e-publish-cache
  1015. (error "`org-e-publish-cache-get' called, but no cache present"))
  1016. (gethash key org-e-publish-cache))
  1017. (defun org-e-publish-cache-set (key value)
  1018. "Store KEY VALUE pair in `org-e-publish-cache'.
  1019. Returns value on success, else nil."
  1020. (unless org-e-publish-cache
  1021. (error "`org-e-publish-cache-set' called, but no cache present"))
  1022. (puthash key value org-e-publish-cache))
  1023. (defun org-e-publish-cache-ctime-of-src (filename)
  1024. "Get the FILENAME ctime as an integer."
  1025. (let* ((symlink-maybe (or (file-symlink-p filename) filename))
  1026. (src-attr
  1027. (file-attributes
  1028. (if (file-name-absolute-p symlink-maybe) symlink-maybe
  1029. (expand-file-name symlink-maybe (file-name-directory filename))))))
  1030. (+ (lsh (car (nth 5 src-attr)) 16)
  1031. (cadr (nth 5 src-attr)))))
  1032. (provide 'org-e-publish)
  1033. ;;; org-e-publish.el ends here