org-static-mathjax.el 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. ;;; org-static-mathjax.el --- Muse-like tags in Org-mode
  2. ;;
  3. ;; Author: Jan Böker <jan dot boecker at jboecker dot de>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This elisp code integrates Static MathJax into the
  17. ;; HTML export process of Org-mode.
  18. ;;
  19. ;; The supporting files for this package are in contrib/scripts/staticmathjax
  20. ;; Please read the README.org file in that directory for more information.
  21. ;; To use it, evaluate it on startup, add the following to your .emacs:
  22. ;; (require 'org-static-mathjax)
  23. ;;
  24. ;; You will then have to customize the following two variables:
  25. ;; - org-static-mathjax-app-ini-path
  26. ;; - org-static-mathjax-local-mathjax-path
  27. ;;
  28. ;; If xulrunner is not in your $PATH, you will also need to customize
  29. ;; org-static-mathjax-xulrunner-path.
  30. ;;
  31. ;; If everything is setup correctly, you can trigger Static MathJax on
  32. ;; export to HTML by adding the following line to your Org file:
  33. ;; #+StaticMathJax: embed-fonts:nil output-file-name:"embedded-math.html"
  34. ;;
  35. ;; You can omit either argument.
  36. ;; embed-fonts defaults to nil. If you do not specify output-file-name,
  37. ;; the exported file is overwritten with the static version.
  38. ;;
  39. ;; If embed-fonts is non-nil, the fonts are embedded directly into the
  40. ;; output file using data: URIs.
  41. ;;
  42. ;; output-file-name specifies the file name of the static version. You
  43. ;; can use any arbitrary lisp form here, for example:
  44. ;; output-file-name:(concat (file-name-sans-extension buffer-file-name) "-static.html")
  45. ;;
  46. ;; The StaticMathJax XULRunner application expects a UTF-8 encoded
  47. ;; input file. If the static version displays random characters instead
  48. ;; of your math, add the following line at the top of your Org file:
  49. ;; -*- coding: utf-8; -*-
  50. ;;
  51. ;;; Code:
  52. (defcustom org-static-mathjax-app-ini-path
  53. (or (expand-file-name
  54. "../scripts/staticmatchjax/application.ini"
  55. (file-name-directory (or load-file-name buffer-file-name)))
  56. "")
  57. "Path to \"application.ini\" of the Static MathJax XULRunner application.
  58. If you have extracted StaticMathJax to e.g. ~/.local/staticmathjax, set
  59. this to ~/.local/staticmathjax/application.ini"
  60. :type 'string)
  61. (defcustom org-static-mathjax-xulrunner-path
  62. "xulrunner"
  63. "Path to your xulrunner binary"
  64. :type 'string)
  65. (defcustom org-static-mathjax-local-mathjax-path
  66. ""
  67. "Extract the MathJax zip file somewhere on your local
  68. hard drive and specify the path here.
  69. The directory has to be writeable, as org-static-mathjax
  70. creates a temporary file there during export."
  71. :type 'string)
  72. (defvar org-static-mathjax-debug
  73. nil
  74. "If non-nil, org-static-mathjax will print some debug messages")
  75. (defun org-static-mathjax-hook-installer ()
  76. "Installs org-static-mathjax-process in after-save-hook.
  77. Sets the following buffer-local variables for org-static-mathjax-process to pick up:
  78. org-static-mathjax-mathjax-path: The path to MathJax.js as used by Org HTML export
  79. org-static-mathjax-options: The string given with #+STATICMATHJAX: in the file"
  80. (let ((static-mathjax-option-string (plist-get opt-plist :static-mathjax)))
  81. (if static-mathjax-option-string
  82. (progn (set (make-local-variable 'org-static-mathjax-options) static-mathjax-option-string)
  83. (set (make-local-variable 'org-static-mathjax-mathjax-path)
  84. (nth 1 (assq 'path org-export-html-mathjax-options)))
  85. (let ((mathjax-options (plist-get opt-plist :mathjax)))
  86. (if mathjax-options
  87. (if (string-match "\\<path:" mathjax-options)
  88. (set 'org-static-mathjax-mathjax-path
  89. (car (read-from-string
  90. (substring mathjax-options (match-end 0))))))))
  91. (add-hook 'after-save-hook
  92. 'org-static-mathjax-process
  93. nil t)))))
  94. (defun org-static-mathjax-process ()
  95. (save-excursion
  96. ; some sanity checking
  97. (if (or (string= org-static-mathjax-app-ini-path "")
  98. (not (file-exists-p org-static-mathjax-app-ini-path)))
  99. (error "Static MathJax: You must customize org-static-mathjax-app-ini-path!"))
  100. (if (or (string= org-static-mathjax-local-mathjax-path "")
  101. (not (file-exists-p org-static-mathjax-local-mathjax-path)))
  102. (error "Static MathJax: You must customize org-static-mathjax-local-mathjax-path!"))
  103. ; define variables
  104. (let* ((options org-static-mathjax-options)
  105. (output-file-name buffer-file-name)
  106. (input-file-name (let ((temporary-file-directory (file-name-directory org-static-mathjax-local-mathjax-path)))
  107. (make-temp-file "org-static-mathjax-" nil ".html")))
  108. (html-code (buffer-string))
  109. (mathjax-oldpath (concat "src=\"" org-static-mathjax-mathjax-path))
  110. (mathjax-newpath (concat "src=\"" org-static-mathjax-local-mathjax-path))
  111. embed-fonts)
  112. ; read file-local options
  113. (mapc
  114. (lambda (symbol)
  115. (if (string-match (concat "\\<" (symbol-name symbol) ":") options)
  116. (set symbol (eval (car (read-from-string
  117. (substring options (match-end 0))))))))
  118. '(embed-fonts output-file-name))
  119. ; debug
  120. (when org-static-mathjax-debug
  121. (message "output file name, embed-fonts")
  122. (print output-file-name)
  123. (print embed-fonts))
  124. ; open (temporary) input file, copy contents there, replace MathJax path with local installation
  125. (with-temp-buffer
  126. (insert html-code)
  127. (goto-char 1)
  128. (replace-regexp mathjax-oldpath mathjax-newpath)
  129. (write-file input-file-name))
  130. ; prepare argument list for call-process
  131. (let ((call-process-args (list org-static-mathjax-xulrunner-path
  132. nil nil nil
  133. org-static-mathjax-app-ini-path
  134. input-file-name
  135. output-file-name)))
  136. ; if fonts are embedded, just append the --embed-fonts flag
  137. (if embed-fonts
  138. (add-to-list 'call-process-args "--embed-fonts" t))
  139. ; if fonts are not embedded, the XULRunner app must replace all references
  140. ; to the font files with the real location (Firefox inserts file:// URLs there,
  141. ; because we are using a local MathJax installation here)
  142. (if (not embed-fonts)
  143. (progn
  144. (add-to-list 'call-process-args "--final-mathjax-url" t)
  145. (add-to-list 'call-process-args
  146. (file-name-directory org-static-mathjax-mathjax-path)
  147. t)))
  148. ; debug
  149. (when org-static-mathjax-debug
  150. (print call-process-args))
  151. ; call it
  152. (apply 'call-process call-process-args)
  153. ; delete our temporary input file
  154. (kill-buffer)
  155. (delete-file input-file-name)
  156. (let ((backup-file (concat input-file-name "~")))
  157. (if (file-exists-p backup-file)
  158. (delete-file backup-file)))))))
  159. (add-to-list 'org-export-inbuffer-options-extra
  160. '("STATICMATHJAX" :static-mathjax))
  161. (add-hook 'org-export-html-final-hook 'org-static-mathjax-hook-installer)
  162. (provide 'org-static-mathjax)