org-publish.el 20 KB

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