org-publish.el 40 KB

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