emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob 29f2cfef81e957a1604d176494c3400a51b799c3 6593 bytes (raw)
name: contrib/lisp/org-static-mathjax.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
 
;;; org-static-mathjax.el --- Muse-like tags in Org-mode
;;
;; Author: Jan Böker <jan dot boecker at jboecker dot de>

;; This elisp code integrates Static MathJax into the
;; HTML export process of Org-mode.
;;
;; The supporting files for this package are in contrib/scripts/staticmathjax
;; Please read the README.org file in that directory for more information.

;; To use it, evaluate it on startup, add the following to your .emacs:

;; (require 'org-static-mathjax)
;;
;; You will then have to customize the following two variables:
;; - org-static-mathjax-app-ini-path
;; - org-static-mathjax-local-mathjax-path
;;
;; If xulrunner is not in your $PATH, you will also need to customize
;; org-static-mathjax-xulrunner-path.
;;
;; If everything is setup correctly, you can trigger Static MathJax on
;; export to HTML by adding the following line to your Org file:
;; #+StaticMathJax: embed-fonts:nil output-file-name:"embedded-math.html"
;;
;; You can omit either argument.
;; embed-fonts defaults to nil. If you do not specify output-file-name,
;; the exported file is overwritten with the static version.
;;
;; If embed-fonts is non-nil, the fonts are embedded directly into the
;; output file using data: URIs.
;;
;; output-file-name specifies the file name of the static version. You
;; can use any arbitrary lisp form here, for example:
;; output-file-name:(concat (file-name-sans-extension buffer-file-name) "-static.html")
;;
;; The StaticMathJax XULRunner application expects a UTF-8 encoded
;; input file. If the static version displays random characters instead
;; of your math, add the following line at the top of your Org file:
;; -*- coding: utf-8; -*-
;;
;; License: GPL v2 or later

(defcustom org-static-mathjax-app-ini-path
  (or (expand-file-name
       "../scripts/staticmatchjax/application.ini"
       (file-name-directory (or load-file-name buffer-file-name)))
      "")
  "Path to \"application.ini\" of the Static MathJax XULRunner application.
If you have extracted StaticMathJax to e.g. ~/.local/staticmathjax, set
this to ~/.local/staticmathjax/application.ini"
  :type 'string)

(defcustom org-static-mathjax-xulrunner-path
  "xulrunner"
  "Path to your xulrunner binary"
  :type 'string)

(defcustom org-static-mathjax-local-mathjax-path
  ""
  "Extract the MathJax zip file somewhere on your local
hard drive and specify the path here.

The directory has to be writeable, as org-static-mathjax
creates a temporary file there during export."
  :type 'string)

(defvar org-static-mathjax-debug
  nil
  "If non-nil, org-static-mathjax will print some debug messages")

(defun org-static-mathjax-hook-installer ()
  "Installs org-static-mathjax-process in after-save-hook.

Sets the following buffer-local variables for org-static-mathjax-process to pick up:
org-static-mathjax-mathjax-path: The path to MathJax.js as used by Org HTML export
org-static-mathjax-options:      The string given with #+STATICMATHJAX: in the file"
  (let ((static-mathjax-option-string (plist-get opt-plist :static-mathjax)))
	(if static-mathjax-option-string
		(progn (set (make-local-variable 'org-static-mathjax-options) static-mathjax-option-string)
			   (set (make-local-variable 'org-static-mathjax-mathjax-path)
					(nth 1 (assq 'path org-export-html-mathjax-options)))
			   (let ((mathjax-options (plist-get opt-plist :mathjax)))
				 (if mathjax-options
					 (if (string-match "\\<path:" mathjax-options)
						 (set 'org-static-mathjax-mathjax-path
							  (car (read-from-string
									(substring mathjax-options (match-end 0))))))))
			   (add-hook 'after-save-hook
						 'org-static-mathjax-process
						 nil t)))))


(defun org-static-mathjax-process ()
  (save-excursion
	; some sanity checking
	(if (or (string= org-static-mathjax-app-ini-path "")
			(not (file-exists-p org-static-mathjax-app-ini-path)))
		(error "Static MathJax: You must customize org-static-mathjax-app-ini-path!"))
	(if (or (string= org-static-mathjax-local-mathjax-path "")
			(not (file-exists-p org-static-mathjax-local-mathjax-path)))
		(error "Static MathJax: You must customize org-static-mathjax-local-mathjax-path!"))

	; define variables
	(let* ((options org-static-mathjax-options)
		   (output-file-name buffer-file-name)
		   (input-file-name (let ((temporary-file-directory (file-name-directory org-static-mathjax-local-mathjax-path)))
							  (make-temp-file "org-static-mathjax-" nil ".html")))
		   (html-code (buffer-string))
		   (mathjax-oldpath (concat "src=\"" org-static-mathjax-mathjax-path))
		   (mathjax-newpath (concat "src=\"" org-static-mathjax-local-mathjax-path))
		   embed-fonts)
	  ; read file-local options
	  (mapc
	   (lambda (symbol)
		 (if (string-match (concat "\\<" (symbol-name symbol) ":") options)
			 (set symbol (eval (car (read-from-string
									 (substring options (match-end 0))))))))
	   '(embed-fonts output-file-name))

	  ; debug
	  (when org-static-mathjax-debug
		(message "output file name, embed-fonts")
		(print output-file-name)
		(print embed-fonts))

	  ; open (temporary) input file, copy contents there, replace MathJax path with local installation
	  (with-temp-buffer
		(insert html-code)
		(goto-char 1)
		(replace-regexp mathjax-oldpath mathjax-newpath)
		(write-file input-file-name))

	  ; prepare argument list for call-process
	  (let ((call-process-args (list org-static-mathjax-xulrunner-path
									 nil nil nil
									 org-static-mathjax-app-ini-path
									 input-file-name
									 output-file-name)))
		; if fonts are embedded, just append the --embed-fonts flag
		(if embed-fonts
			(add-to-list 'call-process-args "--embed-fonts" t))
		; if fonts are not embedded, the XULRunner app must replace all references
		; to the font files with the real location (Firefox inserts file:// URLs there,
		; because we are using a local MathJax installation here)
		(if (not embed-fonts)
			(progn
			  (add-to-list 'call-process-args "--final-mathjax-url" t)
			  (add-to-list 'call-process-args
						   (file-name-directory org-static-mathjax-mathjax-path)
						   t)))

		; debug
		(when org-static-mathjax-debug
		  (print call-process-args))
		; call it
		(apply 'call-process call-process-args)
		; delete our temporary input file
		(kill-buffer)
		(delete-file input-file-name)
		(let ((backup-file (concat input-file-name "~")))
		  (if (file-exists-p backup-file)
			  (delete-file backup-file)))))))

(add-to-list 'org-export-inbuffer-options-extra
'("STATICMATHJAX" :static-mathjax))

(add-hook 'org-export-html-final-hook 'org-static-mathjax-hook-installer)


(provide 'org-static-mathjax)

debug log:

solving 29f2cfe ...
found 29f2cfe in https://git.savannah.gnu.org/cgit/emacs/org-mode.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).