org-publish.el 41 KB

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