org-publish.el 22 KB

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