org-publish.el 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. ;;; org-publish.el --- publish related org-mode files as a website
  2. ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
  3. ;; Author: David O'Toole <dto@gnu.org>
  4. ;; Keywords: hypermedia, outlines
  5. ;; Version: 1.80b
  6. ;; This file is part of GNU Emacs.
  7. ;;
  8. ;; GNU Emacs is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  18. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. ;; Boston, MA 02110-1301, USA.
  20. ;;; Commentary:
  21. ;; Requires at least version 4.27 of org.el
  22. ;;
  23. ;; The official org-mode website:
  24. ;; http://staff.science.uva.nl/~dominik/Tools/org/
  25. ;;
  26. ;; Home page for org-publish.el:
  27. ;; http://dto.freeshell.org/notebook/OrgMode.html
  28. ;; This program extends the HTML publishing support of Emacs Org-mode
  29. ;; to allow configurable publishing of related sets of files as a
  30. ;; complete website.
  31. ;;
  32. ;; org-publish.el can do the following:
  33. ;;
  34. ;; + Publish all one's org-files to html
  35. ;; + Upload html, images, attachments and other files to a web server
  36. ;; + Exclude selected private pages from publishing
  37. ;; + Publish a clickable index of pages
  38. ;; + Manage local timestamps, for publishing only changed files
  39. ;; + Accept plugin functions to extend range of publishable content
  40. ;;
  41. ;; Special thanks to the org-mode maintainer Carsten Dominik for his
  42. ;; ideas, enthusiasm, and cooperation.
  43. ;;; Installation:
  44. ;; Put org-publish.el in your load path, byte-compile it, and then add
  45. ;; the following lines to your emacs initialization file:
  46. ;; (autoload 'org-publish "org-publish" nil t)
  47. ;; (autoload 'org-publish "org-publish-all" nil t)
  48. ;; (autoload 'org-publish "org-publish-current-file" nil t)
  49. ;; (autoload 'org-publish "org-publish-current-project" nil t)
  50. ;; NOTE: When org-publish.el is included with org.el, those forms are
  51. ;; already in the file org-install.el, and hence don't need to be put
  52. ;; in your emacs initialization file in this case.
  53. ;;; Usage:
  54. ;;
  55. ;; The program's main configuration variable is
  56. ;; `org-publish-project-alist'. See below for example configurations
  57. ;; with commentary.
  58. ;; The main interactive functions are:
  59. ;;
  60. ;; M-x org-publish
  61. ;; M-x org-publish-all
  62. ;; M-x org-publish-current-file
  63. ;; M-x org-publish-current-project
  64. ;;;; Simple example configuration:
  65. ;; (setq org-publish-project-alist
  66. ;; (list
  67. ;; '("org" . (:base-directory "~/org/"
  68. ;; :base-extension "org"
  69. ;; :publishing-directory "~/public_html"
  70. ;; :with-section-numbers nil
  71. ;; :table-of-contents nil
  72. ;; :style "<link rel=stylesheet href=\"../other/mystyle.css\" type=\"text/css\">")))
  73. ;;;; More complex example configuration:
  74. ;; Imagine your *.org files are kept in ~/org, your images in
  75. ;; ~/images, and stylesheets in ~/other. Now imagine you want to
  76. ;; publish the files through an ssh connection to a remote host, via
  77. ;; Tramp-mode. To maintain relative links from *.org files to /images
  78. ;; and /other, we should replicate the same directory structure in
  79. ;; your web server account's designated html root (in this case,
  80. ;; assumed to be ~/html)
  81. ;; Once you've done created the proper directories, you can adapt the
  82. ;; following example configuration to your specific paths, run M-x
  83. ;; org-publish-all, and it should publish the files to the correct
  84. ;; directories on the web server, transforming the *.org files into
  85. ;; HTML, and leaving other files alone.
  86. ;; (setq org-publish-project-alist
  87. ;; (list
  88. ;; '("orgfiles" :base-directory "~/org/"
  89. ;; :base-extension "org"
  90. ;; :publishing-directory "/ssh:user@host:~/html/notebook/"
  91. ;; :publishing-function org-publish-org-to-html
  92. ;; :exclude "PrivatePage.org" ;; regexp
  93. ;; :headline-levels 3
  94. ;; :with-section-numbers nil
  95. ;; :table-of-contents nil
  96. ;; :style "<link rel=stylesheet href=\"../other/mystyle.css\" type=\"text/css\">"
  97. ;; :auto-preamble t
  98. ;; :auto-postamble nil)
  99. ;;
  100. ;; ("images" :base-directory "~/images/"
  101. ;; :base-extension "jpg\\|gif\\|png"
  102. ;; :publishing-directory "/ssh:user@host:~/html/images/"
  103. ;; :publishing-function org-publish-attachment)
  104. ;;
  105. ;; ("other" :base-directory "~/other/"
  106. ;; :base-extension "css"
  107. ;; :publishing-directory "/ssh:user@host:~/html/other/"
  108. ;; :publishing-function org-publish-attachment)
  109. ;; ("website" :components ("orgfiles" "images" "other"))))
  110. ;; For more information, see the documentation for the variable
  111. ;; `org-publish-project-alist'.
  112. ;; Of course, you don't have to publish to remote directories from
  113. ;; within emacs. You can always just publish to local folders, and
  114. ;; then use the synchronization/upload tool of your choice.
  115. ;;; List of user-visible changes since version 1.27
  116. ;; 1.78: Allow list-valued :publishing-function
  117. ;; 1.77: Added :preparation-function, this allows you to use GNU Make etc.
  118. ;; 1.65: Remove old "composite projects". They're redundant.
  119. ;; 1.64: Allow meta-projects with :components
  120. ;; 1.57: Timestamps flag is now called "org-publish-use-timestamps-flag"
  121. ;; 1.52: Properly set default for :index-filename
  122. ;; 1.48: Composite projects allowed.
  123. ;; :include keyword allowed.
  124. ;; 1.43: Index no longer includes itself in the index.
  125. ;; 1.42: Fix "function definition is void" error
  126. ;; when :publishing-function not set in org-publish-current-file.
  127. ;; 1.41: Fixed bug where index isn't published on first try.
  128. ;; 1.37: Added interactive function "org-publish". Prompts for particular
  129. ;; project name to publish.
  130. ;; 1.34: Added force-publish option to all interactive functions.
  131. ;; 1.32: Fixed "index.org has changed on disk" error during index publishing.
  132. ;; 1.30: Fixed startup error caused by (require 'em-unix)
  133. ;;; Code:
  134. (eval-when-compile
  135. (require 'cl))
  136. (defgroup org-publish nil
  137. "Options for publishing a set of Org-mode and related files."
  138. :tag "Org Publishing"
  139. :group 'org)
  140. (defcustom org-publish-project-alist nil
  141. "Association list to control publishing behavior.
  142. Each element of the alist is a publishing 'project.' The CAR of
  143. each element is a string, uniquely identifying the project. The
  144. CDR of each element is in one of the following forms:
  145. (:property value :property value ... )
  146. OR,
  147. (:components (\"project-1\" \"project-2\" ...))
  148. When the CDR of an element of org-publish-project-alist is in
  149. this second form, the elements of the list after :components are
  150. taken to be components of the project, which group together files
  151. requiring different publishing options. When you publish such a
  152. project with M-x org-publish, the components all publish.
  153. When a property is given a value in org-publish-project-alist, its
  154. setting overrides the value of the corresponding user variable
  155. (if any) during publishing. However, options set within a file
  156. override everything.
  157. Most properties are optional, but some should always be set:
  158. :base-directory Directory containing publishing source files
  159. :base-extension Extension (without the dot!) of source files.
  160. This can be a regular expression.
  161. :publishing-directory Directory (possibly remote) where output
  162. files will be published
  163. The :exclude property may be used to prevent certain files from
  164. being published. Its value may be a string or regexp matching
  165. file names you don't want to be published.
  166. The :include property may be used to include extra files. Its
  167. value may be a list of filenames to include. The filenames are
  168. considered relative to the publishing directory.
  169. When both :include and :exclude properties are given values, the
  170. exclusion step happens first.
  171. One special property controls which back-end function to use for
  172. publishing files in the project. This can be used to extend the
  173. set of file types publishable by org-publish, as well as the set
  174. of output formats.
  175. :publishing-function Function to publish file. The default is
  176. org-publish-org-to-html, but other
  177. values are possible. May also be a
  178. list of functions, in which case
  179. each function in the list is invoked
  180. in turn.
  181. Another property allows you to insert code that prepares a
  182. project for publishing. For example, you could call GNU Make on a
  183. certain makefile, to ensure published files are built up to date.
  184. :preparation-function Function to be called before publishing
  185. this project.
  186. Some properties control details of the Org publishing process,
  187. and are equivalent to the corresponding user variables listed in
  188. the right column. See the documentation for those variables to
  189. learn more about their use and default values.
  190. :language org-export-default-language
  191. :headline-levels org-export-headline-levels
  192. :section-numbers org-export-with-section-numbers
  193. :table-of-contents org-export-with-toc
  194. :emphasize org-export-with-emphasize
  195. :sub-superscript org-export-with-sub-superscripts
  196. :TeX-macros org-export-with-TeX-macros
  197. :fixed-width org-export-with-fixed-width
  198. :tables org-export-with-tables
  199. :table-auto-headline org-export-highlight-first-table-line
  200. :style org-export-html-style
  201. :convert-org-links org-export-html-link-org-files-as-html
  202. :inline-images org-export-html-inline-images
  203. :expand-quoted-html org-export-html-expand
  204. :timestamp org-export-html-with-timestamp
  205. :publishing-directory org-export-publishing-directory
  206. :preamble org-export-html-preamble
  207. :postamble org-export-html-postamble
  208. :auto-preamble org-export-html-auto-preamble
  209. :auto-postamble org-export-html-auto-postamble
  210. :author user-full-name
  211. :email user-mail-address
  212. The following properties may be used to control publishing of an
  213. index of files or summary page for a given project.
  214. :auto-index Whether to publish an index during
  215. org-publish-current-project or org-publish-all.
  216. :index-filename Filename for output of index. Defaults
  217. to 'index.org' (which becomes 'index.html')
  218. :index-title Title of index page. Defaults to name of file.
  219. :index-function Plugin function to use for generation of index.
  220. Defaults to 'org-publish-org-index', which
  221. generates a plain list of links to all files
  222. in the project.
  223. "
  224. :group 'org-publish
  225. :type 'alist)
  226. (defcustom org-publish-use-timestamps-flag t
  227. "When non-nil, use timestamp checking to publish only changed files.
  228. When nil, do no timestamp checking and always publish all
  229. files."
  230. :group 'org-publish
  231. :type 'boolean)
  232. (defcustom org-publish-timestamp-directory "~/.org-timestamps/"
  233. "Name of directory in which to store publishing timestamps."
  234. :group 'org-publish
  235. :type 'string)
  236. ;;;; Timestamp-related functions
  237. (defun org-publish-timestamp-filename (filename)
  238. "Return path to timestamp file for filename FILENAME."
  239. (while (string-match "~\\|/" filename)
  240. (setq filename (replace-match "_" nil t filename)))
  241. (concat org-publish-timestamp-directory filename ".timestamp"))
  242. (defun org-publish-needed-p (filename)
  243. "Check whether file should be published.
  244. If org-publish-use-timestamps-flag is set to nil, this function always
  245. returns t. Otherwise, check the timestamps folder to determine
  246. whether file should be published."
  247. (if org-publish-use-timestamps-flag
  248. (progn
  249. ;;
  250. ;; create folder if needed
  251. (if (not (file-exists-p org-publish-timestamp-directory))
  252. (make-directory org-publish-timestamp-directory)
  253. (if (not (file-directory-p org-publish-timestamp-directory))
  254. (error "org-publish-timestamp-directory must be a directory.")))
  255. ;;
  256. ;; check timestamp. ok if timestamp file doesn't exist
  257. (let* ((timestamp (org-publish-timestamp-filename filename))
  258. (rtn (file-newer-than-file-p filename timestamp)))
  259. (if rtn
  260. ;; handle new timestamps
  261. (if (not (file-exists-p timestamp))
  262. ;; create file
  263. (with-temp-buffer
  264. (make-directory (file-name-directory timestamp) :parents)
  265. (write-file timestamp)
  266. (kill-buffer (current-buffer)))))
  267. rtn))
  268. t))
  269. (defun org-publish-update-timestamp (filename)
  270. "Update publishing timestamp for file FILENAME."
  271. (let ((timestamp (org-publish-timestamp-filename filename)))
  272. ;; Emacs 21 doesn't have set-file-times
  273. (if (fboundp 'set-file-times)
  274. (set-file-times timestamp)
  275. (call-process "touch" nil 0 nil timestamp))))
  276. ;;;; A hash mapping files to project names
  277. (defvar org-publish-files (make-hash-table :test 'equal) "Hash
  278. table mapping file names to project names.")
  279. ;;;; Checking filenames against this hash
  280. (defun org-publish-validate-link (link &optional directory)
  281. (gethash (file-truename (expand-file-name link directory))
  282. org-publish-files))
  283. ;;;; Getting project information out of org-publish-project-alist
  284. (defun org-publish-get-plists (&optional project-name)
  285. "Return a list of property lists for project PROJECT-NAME.
  286. When argument is not given, return all property lists for all projects."
  287. (let ((alist (if project-name
  288. (list (assoc project-name org-publish-project-alist))
  289. org-publish-project-alist))
  290. (project nil)
  291. (plists nil)
  292. (single nil)
  293. (components nil))
  294. ;;
  295. ;;
  296. (while (setq project (pop alist))
  297. ;; what kind of project is it?
  298. (if (setq components (plist-get (cdr project) :components))
  299. ;; meta project. annotate each plist with name of enclosing project
  300. (setq single
  301. (apply 'append
  302. (mapcar 'org-publish-get-plists components)))
  303. ;; normal project
  304. (setq single (list (cdr project))))
  305. ;;
  306. (setq plists (append plists single))
  307. (dolist (p single)
  308. (let* ((exclude (plist-get p :exclude))
  309. (files (org-publish-get-base-files p exclude)))
  310. (dolist (f files)
  311. (puthash (file-truename f) (car project) org-publish-files)))))
  312. plists))
  313. (defun org-publish-get-base-files (plist &optional exclude-regexp)
  314. "Return a list of all files in project defined by PLIST.
  315. If EXCLUDE-REGEXP is set, this will be used to filter out
  316. matching filenames."
  317. (let* ((dir (file-name-as-directory (plist-get plist :base-directory)))
  318. (include-list (plist-get plist :include))
  319. (extension (or (plist-get plist :base-extension) "org"))
  320. (regexp (concat "^[^\\.].*\\.\\(" extension "\\)$"))
  321. (allfiles (directory-files dir t regexp)))
  322. ;;
  323. ;; exclude files
  324. (setq allfiles
  325. (if (not exclude-regexp)
  326. allfiles
  327. (delq nil
  328. (mapcar (lambda (x)
  329. (if (string-match exclude-regexp x) nil x))
  330. allfiles))))
  331. ;;
  332. ;; include extra files
  333. (let ((inc nil))
  334. (while (setq inc (pop include-list))
  335. (setq allfiles (cons (expand-file-name inc dir) allfiles))))
  336. allfiles))
  337. (defun org-publish-get-project-from-filename (filename)
  338. "Figure out which project a given FILENAME belongs to, if any.
  339. Filename should contain full path. Returns name of project, or
  340. nil if not found."
  341. (org-publish-get-plists)
  342. (gethash (file-truename filename) org-publish-files))
  343. (defun org-publish-get-plist-from-filename (filename)
  344. "Return publishing configuration plist for file FILENAME."
  345. (let ((found nil))
  346. (mapc
  347. (lambda (plist)
  348. (let ((files (org-publish-get-base-files plist)))
  349. (if (member (expand-file-name filename) files)
  350. (setq found plist))))
  351. (org-publish-get-plists))
  352. found))
  353. ;;;; Pluggable publishing back-end functions
  354. (defun org-publish-org-to-latex (plist filename)
  355. "Publish an org file to LaTeX."
  356. (org-publish-org-to "latex" plist filename))
  357. (defun org-publish-org-to-html (plist filename)
  358. "Publish an org file to HTML."
  359. (org-publish-org-to "html" plist filename))
  360. (defun org-publish-org-to (format plist filename)
  361. "Publish an org file to FORMAT.
  362. PLIST is the property list for the given project.
  363. FILENAME is the filename of the org file to be published."
  364. (require 'org)
  365. (let* ((arg (plist-get plist :headline-levels)))
  366. (progn
  367. (find-file filename)
  368. (funcall (intern (concat "org-export-as-" format))
  369. arg nil plist)
  370. (kill-buffer (current-buffer)))))
  371. (defun org-publish-attachment (plist filename)
  372. "Publish a file with no transformation of any kind.
  373. PLIST is the property list for the given project.
  374. FILENAME is the filename of the file to be published."
  375. ;; make sure eshell/cp code is loaded
  376. (eval-and-compile
  377. (require 'eshell)
  378. (require 'esh-maint)
  379. (require 'em-unix))
  380. (let ((destination (file-name-as-directory (plist-get plist :publishing-directory))))
  381. (eshell/cp filename destination)))
  382. ;;;; Publishing files, sets of files, and indices
  383. (defun org-publish-file (filename)
  384. "Publish file FILENAME."
  385. (let* ((project-name (org-publish-get-project-from-filename filename))
  386. (plist (org-publish-get-plist-from-filename filename))
  387. (publishing-function (or (plist-get plist :publishing-function) 'org-publish-org-to-html)))
  388. (if (not project-name)
  389. (error "File %s is not part of any known project." filename))
  390. (when (org-publish-needed-p filename)
  391. (if (listp publishing-function)
  392. ;; allow chain of publishing functions
  393. (mapc (lambda (f)
  394. (funcall f plist filename))
  395. publishing-function)
  396. (funcall publishing-function plist filename))
  397. (org-publish-update-timestamp filename))))
  398. (defun org-publish-plist (plist)
  399. "Publish all files in set defined by PLIST.
  400. If :auto-index is set, publish the index too."
  401. (let* ((exclude-regexp (plist-get plist :exclude))
  402. (publishing-function (or (plist-get plist :publishing-function) 'org-publish-org-to-html))
  403. (index-p (plist-get plist :auto-index))
  404. (index-filename (or (plist-get plist :index-filename) "index.org"))
  405. (index-function (or (plist-get plist :index-function) 'org-publish-org-index))
  406. (preparation-function (plist-get plist :preparation-function))
  407. (f nil))
  408. ;;
  409. (when preparation-function
  410. (funcall preparation-function))
  411. (if index-p
  412. (funcall index-function plist index-filename))
  413. (let ((files (org-publish-get-base-files plist exclude-regexp)))
  414. (while (setq f (pop files))
  415. ;; check timestamps
  416. (when (org-publish-needed-p f)
  417. (if (listp publishing-function)
  418. ;; allow chain of publishing functions
  419. (mapc (lambda (func)
  420. (funcall func plist f))
  421. publishing-function)
  422. (funcall publishing-function plist f))
  423. (org-publish-update-timestamp f))))))
  424. (defun org-publish-org-index (plist &optional index-filename)
  425. "Create an index of pages in set defined by PLIST.
  426. Optionally set the filename of the index with INDEX-FILENAME;
  427. default is 'index.org'."
  428. (let* ((dir (file-name-as-directory (plist-get plist :base-directory)))
  429. (exclude-regexp (plist-get plist :exclude))
  430. (files (org-publish-get-base-files plist exclude-regexp))
  431. (index-filename (concat dir (or index-filename "index.org")))
  432. (index-buffer (find-buffer-visiting index-filename))
  433. (ifn (file-name-nondirectory index-filename))
  434. (f nil))
  435. ;;
  436. ;; if buffer is already open, kill it to prevent error message
  437. (if index-buffer
  438. (kill-buffer index-buffer))
  439. (with-temp-buffer
  440. (while (setq f (pop files))
  441. (let ((fn (file-name-nondirectory f)))
  442. (unless (string= fn ifn) ;; index shouldn't index itself
  443. (insert (concat " + [[file:" fn "]["
  444. (file-name-sans-extension fn)
  445. "]]\n")))))
  446. (write-file index-filename)
  447. (kill-buffer (current-buffer)))))
  448. ;(defun org-publish-meta-index (meta-plist &optional index-filename)
  449. ; "Create an index for a metaproject."
  450. ; (let* ((plists (
  451. ;;;; Interactive publishing functions
  452. ;;;###autoload
  453. (defun org-publish (project-name &optional force)
  454. "Publish the project PROJECT-NAME."
  455. (interactive (list (completing-read "Project name: " org-publish-project-alist
  456. nil t)
  457. current-prefix-arg))
  458. (save-window-excursion
  459. (let ((org-publish-use-timestamps-flag (if force nil t))
  460. (plists (org-publish-get-plists project-name)))
  461. (mapcar 'org-publish-plist plists))))
  462. ;;;###autoload
  463. (defun org-publish-current-project (&optional force)
  464. "Publish the project associated with the current file.
  465. With prefix argument, force publishing all files in project."
  466. (interactive "P")
  467. (save-window-excursion
  468. (let* ((project-name (org-publish-get-project-from-filename (buffer-file-name))))
  469. (if (not project-name)
  470. (error "File %s is not part of any known project." (buffer-file-name)))
  471. (org-publish project-name (if force nil t)))))
  472. ;;;###autoload
  473. (defun org-publish-current-file (&optional force)
  474. "Publish the current file.
  475. With prefix argument, force publish the file."
  476. (interactive "P")
  477. (save-window-excursion
  478. (let ((org-publish-use-timestamps-flag
  479. (if force nil t)))
  480. (org-publish-file (buffer-file-name)))))
  481. ;;;###autoload
  482. (defun org-publish-all (&optional force)
  483. "Publish all projects.
  484. With prefix argument, force publish all files."
  485. (interactive "P")
  486. (save-window-excursion
  487. (let ((org-publish-use-timestamps-flag
  488. (if force nil t))
  489. (plists (org-publish-get-plists)))
  490. (mapcar 'org-publish-plist plists))))
  491. (provide 'org-publish)
  492. ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
  493. ;;; org-publish.el ends here