From d0d332df4a836ab037cd0c90f54d728abc33d9a0 Mon Sep 17 00:00:00 2001 From: Tor Erlend Fjelde Date: Sun, 28 May 2023 02:22:23 +0100 Subject: [PATCH 1/6] added suppport for specification of MathJax config and version, both 2 and 3, while maintaining backwards compat --- ox-reveal.el | 137 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 133 insertions(+), 4 deletions(-) diff --git a/ox-reveal.el b/ox-reveal.el index 3bd9192..57a6bee 100644 --- a/ox-reveal.el +++ b/ox-reveal.el @@ -87,6 +87,8 @@ (:reveal-default-slide-background-opacity "REVEAL_DEFAULT_SLIDE_BACKGROUND_OPACITY" nil nil t) (:reveal-default-slide-background-transition "REVEAL_DEFAULT_SLIDE_BACKGROUND_TRANSITION" nil nil t) (:reveal-mathjax-url "REVEAL_MATHJAX_URL" nil org-reveal-mathjax-url t) + (:reveal-mathjax-version "REVEAL_MATHJAX_VERSION" nil org-reveal-mathjax-version t) + (:reveal-mathjax-config "REVEAL_MATHJAX_CONFIG" nil org-reveal-mathjax-config t) (:reveal-preamble "REVEAL_PREAMBLE" nil org-reveal-preamble t) (:reveal-head-preamble "REVEAL_HEAD_PREAMBLE" nil org-reveal-head-preamble newline) (:reveal-postamble "REVEAL_POSTAMBLE" nil org-reveal-postamble t) @@ -225,9 +227,57 @@ embedded into Reveal.initialize()." :group 'org-export-reveal :type 'string) +(defcustom org-reveal-mathjax-version + nil + "Default MathJax version. + +If not specified, the version is automatically detected from +the url (determined by `org-reveal-mathjax-url' or the buffer `REVEAL_MATHJAX_URL')." + :group 'org-export-reveal + :type 'string) + +(defcustom org-reveal-mathjax2-version + "2.7.5" + "Default MathJax v2 version." + :group 'org-export-reveal + :type 'string) + +(defcustom org-reveal-mathjax3-version + "3.0.0" + "Default MathJax v3 version." + :group 'org-export-reveal + :type 'string) + (defcustom org-reveal-mathjax-url - "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML" - "Default MathJax URL." + nil + "Default MathJax URL. + +If specified, this overrides `org-reveal-mathjax2-url' and +`org-reveal-mathjax3-url'." + :group 'org-export-reveal + :type 'string) + +(defcustom org-reveal-mathjax2-url + "https://cdnjs.cloudflare.com/ajax/libs/mathjax/%version/MathJax.js?config=TeX-AMS-MML_HTMLorMML" + "Default MathJax v2 URL." + :group 'org-export-reveal + :type 'string) + +(defcustom org-reveal-mathjax3-url + "https://cdn.jsdelivr.net/npm/mathjax@%version/es5/tex-svg.js" + "Default MathJax v3 URL." + :group 'org-export-reveal + :type 'string) + +(defcustom org-reveal-mathjax2-config + nil + "Default MathJax v2 config." + :group 'org-export-reveal + :type 'string) + +(defcustom org-reveal-mathjax3-config + nil + "Default MathJax v3 config." :group 'org-export-reveal :type 'string) @@ -691,12 +741,91 @@ using custom variable `org-reveal-root'." " root-path))))) +(defun org-reveal--mathjax-major-version (version-string) + "Return the major version number of MathJax from VERSION-STRING. + +Expects VERSION-STRING to be of the form X.Y.Z." + ;; Ensure that the version string is of the form X.Y.Z. + ;; Otherwise, throw an error. + (save-match-data + (unless (string-match "\\([0-9]+\\.[0-9]+\\.[0-9]+\\)" version-string) + (error "MathJax version is not of the form X.Y.Z: %s" version-string))) + (string-to-number (car (split-string version-string "\\.")))) + +(defun org-reveal--mathjax-determine-version (info) + "Determine the MathJax version from INFO. + +The precedence is as follows: +1. If `:reveal-mathjax-version' is specified, use it. +2. If `:reveal-mathjax-url' is specified, try to extract the version + number from it. +3. Otherwise, assume MathJax 2. Warn the user about this." + (let ((version-string (plist-get info :reveal-mathjax-version))) + (if version-string + version-string + (let ((url (plist-get info :reveal-mathjax-url))) + (if url + ;; Try too match a version number of the form X.Y.Z in the URL. + (save-match-data + (if (string-match "\\([0-9]+\\.[0-9]+\\.[0-9]+\\)" url) + (match-string 1 mathjax-url) + ;; Otherwise, assume MathJax 2. Warn the user about this. + org-reveal-mathjax2-version)) + ;; Otherwise, assume MathJax 2. Warn the user about this. + org-reveal-mathjax2-version))))) + +(defun org-reveal--mathjax-url-from-info (info version) + "Return the MathJax URL from INFO and VERSION. + +The precedence is as follows: +1. If the user has specified a MathJax URL (key `:reveal-mathjax-url' in INFO), + use that. +2. Otherwise, if VERSION is ≥3, use the URL specified by `:reveal-mathjax3-url'. +3. Otherwise, if VERSION is ≥2, use the URL specified by `:reveal-mathjax2-url'." + (let ((url (plist-get :reveal-mathjax-url info)) + (url2 (plist-get :reveal-mathjax2-url info)) + (url3 (plist-get :reveal-mathjax3-url info)) + (major-version (org-reveal--mathjax-major-version version))) + (if url + ;; If the user has specified a MathJax URL, use that. + url + ;; Otherwise, we determine the MathJax version from the URL. + (cond + ((eq major-version 3) (org-fill-template (plist-get info :reveal-mathjax3-url) `(("version" . ,version)))) + ((eq major-version 2) (org-fill-template (plist-get info :reveal-mathjax2-url) `(("version" . ,version)))) + (t (error "Unable to resolve URL for MathJax version: %s" version)))))) + +(defun org-reveal--mathjax2-make-config-script (config) + "Return the MathJax configuration string from CONFIG." + (format "" config)) + +(defun org-reveal--mathjax3-make-config-script (config) + "Return the MathJax configuration string from CONFIG." + (format "" config)) + (defun org-reveal-mathjax-scripts (info) "Return the HTML contents for declaring MathJax scripts" (if (plist-get info :reveal-mathjax) ;; MathJax enabled. - (format "\n" - (plist-get info :reveal-mathjax-url)))) + (let* ((mathjax-version (org-reveal--mathjax-determine-version info)) + (mathjax-major-version (org-reveal--mathjax-major-version mathjax-version)) + (mathjax-config (cond + ((plist-get info :reveal-mathjax-config) (plist-get info :reveal-mathjax-config)) + ((= mathjax-major-version 3) (plist-get info :reveal-mathjax3-config)) + ((= mathjax-major-version 2) (plist-get info :reveal-mathjax2-config)) + (t (error "Unsupported MathJax version: %s" mathjax-version)))) + (load-script (format "\n" + (org-reveal--mathjax-url-from-info info mathjax-version)))) + (if mathjax-config + (cond + ((= mathjax-major-version 3) + (concat (org-reveal--mathjax3-make-config-script mathjax-config) "\n" load-script)) + ((= mathjax-major-version 2) + ;; Here we put the MathJax config after the load-script. + (concat load-script "\n" (org-reveal--mathjax2-make-config-script mathjax-config)))) + ;; No MathJax config file specified + load-script + )))) (defun org-reveal--get-plugins (info) (let ((buffer-plugins (condition-case e From 58fa1305162dbce74b3e8e5a1fc66fef0d207111 Mon Sep 17 00:00:00 2001 From: Tor Erlend Fjelde Date: Sun, 28 May 2023 02:42:34 +0100 Subject: [PATCH 2/6] removed redundant options from :options-alist --- ox-reveal.el | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/ox-reveal.el b/ox-reveal.el index 5ead17e..d076041 100644 --- a/ox-reveal.el +++ b/ox-reveal.el @@ -269,6 +269,10 @@ If specified, this overrides `org-reveal-mathjax2-url' and :group 'org-export-reveal :type 'string) +(defvar org-reveal-mathjax-config + nil + "Default MathJax config.") + (defcustom org-reveal-mathjax2-config nil "Default MathJax v2 config." @@ -785,19 +789,17 @@ The precedence is as follows: The precedence is as follows: 1. If the user has specified a MathJax URL (key `:reveal-mathjax-url' in INFO), use that. -2. Otherwise, if VERSION is ≥3, use the URL specified by `:reveal-mathjax3-url'. -3. Otherwise, if VERSION is ≥2, use the URL specified by `:reveal-mathjax2-url'." +2. Otherwise, if VERSION is ≥3, use the URL specified by `org-reveal-mathjax3-url'. +3. Otherwise, if VERSION is ≥2, use the URL specified by `org-reveal-mathjax2-url'." (let ((url (plist-get :reveal-mathjax-url info)) - (url2 (plist-get :reveal-mathjax2-url info)) - (url3 (plist-get :reveal-mathjax3-url info)) (major-version (org-reveal--mathjax-major-version version))) (if url ;; If the user has specified a MathJax URL, use that. url ;; Otherwise, we determine the MathJax version from the URL. (cond - ((eq major-version 3) (org-fill-template (plist-get info :reveal-mathjax3-url) `(("version" . ,version)))) - ((eq major-version 2) (org-fill-template (plist-get info :reveal-mathjax2-url) `(("version" . ,version)))) + ((eq major-version 3) (org-fill-template org-reveal-mathjax3-url `(("version" . ,version)))) + ((eq major-version 2) (org-fill-template org-reveal-mathjax2-url `(("version" . ,version)))) (t (error "Unable to resolve URL for MathJax version: %s" version)))))) (defun org-reveal--mathjax2-make-config-script (config) @@ -816,8 +818,8 @@ The precedence is as follows: (mathjax-major-version (org-reveal--mathjax-major-version mathjax-version)) (mathjax-config (cond ((plist-get info :reveal-mathjax-config) (plist-get info :reveal-mathjax-config)) - ((= mathjax-major-version 3) (plist-get info :reveal-mathjax3-config)) - ((= mathjax-major-version 2) (plist-get info :reveal-mathjax2-config)) + ((= mathjax-major-version 3) org-reveal-mathjax3-config) + ((= mathjax-major-version 2) org-reveal-mathjax2-config) (t (error "Unsupported MathJax version: %s" mathjax-version)))) (load-script (format "\n" (org-reveal--mathjax-url-from-info info mathjax-version)))) From 14f6e3dd0dbc5520904b06df52704fca17afe6b5 Mon Sep 17 00:00:00 2001 From: Tor Erlend Fjelde Date: Sun, 28 May 2023 02:43:52 +0100 Subject: [PATCH 3/6] removed unnecessary comments --- ox-reveal.el | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ox-reveal.el b/ox-reveal.el index d076041..8786677 100644 --- a/ox-reveal.el +++ b/ox-reveal.el @@ -768,7 +768,7 @@ The precedence is as follows: 1. If `:reveal-mathjax-version' is specified, use it. 2. If `:reveal-mathjax-url' is specified, try to extract the version number from it. -3. Otherwise, assume MathJax 2. Warn the user about this." +3. Otherwise, assume MathJax 2." (let ((version-string (plist-get info :reveal-mathjax-version))) (if version-string version-string @@ -778,9 +778,9 @@ The precedence is as follows: (save-match-data (if (string-match "\\([0-9]+\\.[0-9]+\\.[0-9]+\\)" url) (match-string 1 mathjax-url) - ;; Otherwise, assume MathJax 2. Warn the user about this. + ;; Otherwise, assume MathJax 2. org-reveal-mathjax2-version)) - ;; Otherwise, assume MathJax 2. Warn the user about this. + ;; Otherwise, assume MathJax 2. org-reveal-mathjax2-version))))) (defun org-reveal--mathjax-url-from-info (info version) From 631a93607709ea454ce53dd0f00317e707fd390b Mon Sep 17 00:00:00 2001 From: Tor Erlend Fjelde Date: Sun, 28 May 2023 11:10:11 +0100 Subject: [PATCH 4/6] allow user to decide whether or not we should do automatic extraction of version from url --- ox-reveal.el | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ox-reveal.el b/ox-reveal.el index 8786677..e2bd4f6 100644 --- a/ox-reveal.el +++ b/ox-reveal.el @@ -269,6 +269,12 @@ If specified, this overrides `org-reveal-mathjax2-url' and :group 'org-export-reveal :type 'string) +(defcustom org-reveal-extract-mathjax-version-from-url + nil + "Non-nil means extract MathJax version from URL." + :group 'org-export-reveal + :type 'boolean) + (defvar org-reveal-mathjax-config nil "Default MathJax config.") @@ -767,13 +773,13 @@ Expects VERSION-STRING to be of the form X.Y.Z." The precedence is as follows: 1. If `:reveal-mathjax-version' is specified, use it. 2. If `:reveal-mathjax-url' is specified, try to extract the version - number from it. + number from it if `org-reveal-extract-mathjax-version-from-url' is non-nil. 3. Otherwise, assume MathJax 2." (let ((version-string (plist-get info :reveal-mathjax-version))) (if version-string version-string (let ((url (plist-get info :reveal-mathjax-url))) - (if url + (if (and url org-reveal-extract-mathjax-version-from-url) ;; Try too match a version number of the form X.Y.Z in the URL. (save-match-data (if (string-match "\\([0-9]+\\.[0-9]+\\.[0-9]+\\)" url) From 36d9449a82f0428378fc78899099fd809992d2e7 Mon Sep 17 00:00:00 2001 From: Tor Erlend Fjelde Date: Sun, 28 May 2023 11:26:18 +0100 Subject: [PATCH 5/6] insert mathjax config before loading also for MathJax v2 --- ox-reveal.el | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ox-reveal.el b/ox-reveal.el index e2bd4f6..44f81aa 100644 --- a/ox-reveal.el +++ b/ox-reveal.el @@ -834,8 +834,7 @@ The precedence is as follows: ((= mathjax-major-version 3) (concat (org-reveal--mathjax3-make-config-script mathjax-config) "\n" load-script)) ((= mathjax-major-version 2) - ;; Here we put the MathJax config after the load-script. - (concat load-script "\n" (org-reveal--mathjax2-make-config-script mathjax-config)))) + (concat (org-reveal--mathjax2-make-config-script mathjax-config) "\n" load-script))) ;; No MathJax config file specified load-script )))) From 85ae84af70f84a29e2d0c315becb7ce0f2dc628a Mon Sep 17 00:00:00 2001 From: Tor Erlend Fjelde Date: Sun, 28 May 2023 11:36:58 +0100 Subject: [PATCH 6/6] added newline to mathjax config v2 --- ox-reveal.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ox-reveal.el b/ox-reveal.el index 44f81aa..1ffc884 100644 --- a/ox-reveal.el +++ b/ox-reveal.el @@ -810,7 +810,7 @@ The precedence is as follows: (defun org-reveal--mathjax2-make-config-script (config) "Return the MathJax configuration string from CONFIG." - (format "" config)) + (format "" config)) (defun org-reveal--mathjax3-make-config-script (config) "Return the MathJax configuration string from CONFIG."