Add MathJax as an option instead of LaTeX (#40)

This commit is contained in:
analyticd 2019-06-07 05:03:40 -07:00 committed by Lei Tan
parent 805bd238db
commit 06c8ff3e79

View file

@ -112,6 +112,8 @@ See https://apps.ankiweb.net/docs/manual.html#latex-conflicts.")
"8765" "8765"
"The port number AnkiConnect is listening.") "The port number AnkiConnect is listening.")
(defcustom anki-editor-use-math-jax nil
"Use Anki's built in MathJax support instead of LaTeX.")
;;; AnkiConnect ;;; AnkiConnect
@ -221,10 +223,15 @@ The result is the path to the newly stored media file."
;;; Org Export Backend ;;; Org Export Backend
(defconst anki-editor--ox-anki-html-backend (defconst anki-editor--ox-anki-html-backend
(if anki-editor-use-math-jax
(org-export-create-backend
:parent 'html
:transcoders '((latex-fragment . anki-editor--ox-latex-for-mathjax)
(latex-environment . anki-editor--ox-latex-for-mathjax)))
(org-export-create-backend (org-export-create-backend
:parent 'html :parent 'html
:transcoders '((latex-fragment . anki-editor--ox-latex) :transcoders '((latex-fragment . anki-editor--ox-latex)
(latex-environment . anki-editor--ox-latex)))) (latex-environment . anki-editor--ox-latex)))))
(defconst anki-editor--ox-export-ext-plist (defconst anki-editor--ox-export-ext-plist
'(:with-toc nil :anki-editor-mode t)) '(:with-toc nil :anki-editor-mode t))
@ -248,10 +255,29 @@ The result is the path to the newly stored media file."
(when matched (throw 'done latex-code))))) (when matched (throw 'done latex-code)))))
latex-code)) latex-code))
(defun anki-editor--translate-latex-delimiters-to-anki-mathjax-delimiters (latex-code)
(catch 'done
(let ((delimiter-map (list (list (cons (format "^%s" (regexp-quote "$$")) "\\[")
(cons (format "%s$" (regexp-quote "$$")) "\\]"))
(list (cons (format "^%s" (regexp-quote "$")) "\\(")
(cons (format "%s$" (regexp-quote "$")) "\\)"))))
(matched nil))
(save-match-data
(dolist (pair delimiter-map)
(dolist (delimiter pair)
(when (setq matched (string-match (car delimiter) latex-code))
(setq latex-code (replace-match (cdr delimiter) t t latex-code))))
(when matched (throw 'done latex-code)))))
latex-code))
(defun anki-editor--wrap-latex (content) (defun anki-editor--wrap-latex (content)
"Wrap CONTENT with Anki-style latex markers." "Wrap CONTENT with Anki-style latex markers."
(format "<p><div>[latex]</div>%s<div>[/latex]</div></p>" content)) (format "<p><div>[latex]</div>%s<div>[/latex]</div></p>" content))
(defun anki-editor--wrap-latex-for-mathjax (content)
"Wrap CONTENT for Anki's native MathJax support."
(format "<p>%s</p>" content))
(defun anki-editor--wrap-div (content) (defun anki-editor--wrap-div (content)
(format "<div>%s</div>" content)) (format "<div>%s</div>" content))
@ -271,6 +297,22 @@ CONTENTS is nil. INFO is a plist holding contextual information."
(replace-regexp-in-string "}}" "} } " code) (replace-regexp-in-string "}}" "} } " code)
code))) code)))
(defun anki-editor--ox-latex-for-mathjax (latex _contents _info)
"Transcode LATEX from Org to HTML.
CONTENTS is nil. INFO is a plist holding contextual information."
(let ((code (org-remove-indentation (org-element-property :value latex))))
(setq code
(pcase (org-element-type latex)
('latex-fragment (anki-editor--translate-latex-delimiters-to-anki-mathjax-delimiters code))
('latex-environment (anki-editor--wrap-latex-for-mathjax
(mapconcat #'anki-editor--wrap-div
(split-string (org-html-encode-plain-text code) "\n")
"")))))
(if anki-editor-break-consecutive-braces-in-latex
(replace-regexp-in-string "}}" "} } " code)
code)))
(defun anki-editor--ox-html-link (oldfun link desc info) (defun anki-editor--ox-html-link (oldfun link desc info)
"When LINK is a link to local file, transcodes it to html and stores the target file to Anki, otherwise calls OLDFUN for help. "When LINK is a link to local file, transcodes it to html and stores the target file to Anki, otherwise calls OLDFUN for help.
The implementation is borrowed and simplified from ox-html." The implementation is borrowed and simplified from ox-html."