From 4990b2f8ebeb7c28e136236294249e0d1fa38fa0 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 8 Sep 2017 19:20:37 +1000 Subject: [PATCH] mathjax tweaks - drop support for $$ in favour of separate opening and closing tags - add shortcuts to add mathjax - don't highlight any clozes in blue if field contains mathjax chars --- anki/template/template.py | 12 +++++++++--- aqt/editor.py | 12 ++++++++++++ web/mathjax/conf.js | 3 +++ web/reviewer.js | 17 ----------------- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/anki/template/template.py b/anki/template/template.py index 89451eec9..654e6e3d4 100644 --- a/anki/template/template.py +++ b/anki/template/template.py @@ -187,15 +187,21 @@ class Template: reg = clozeReg if not re.search(reg%ord, txt): return "" + mathjax = False + if '\[' in txt or '\(' in txt: + mathjax = True def repl(m): # replace chosen cloze with type if type == "q": if m.group(3): - return "[%s]" % m.group(3) + buf = "[%s]" % m.group(3) else: - return "[...]" + buf = "[...]" else: - return "%s" % m.group(1) + buf = m.group(1) + if not mathjax: + buf = "[%s]" % buf + return buf txt = re.sub(reg%ord, repl, txt) # and display other clozes normally return re.sub(reg%"\d+", "\\1", txt) diff --git a/aqt/editor.py b/aqt/editor.py index f54740fbe..0dc275422 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -185,6 +185,8 @@ class Editor: ("Ctrl+T, T", self.insertLatex), ("Ctrl+T, E", self.insertLatexEqn), ("Ctrl+T, M", self.insertLatexMathEnv), + ("Ctrl+M, M", self.insertMathjaxInline), + ("Ctrl+M, E", self.insertMathjaxBlock), ("Ctrl+Shift+X", self.onHtmlEdit), ("Ctrl+Shift+T", self.onFocusTags) ] @@ -654,6 +656,10 @@ to a cloze type first, via Edit>Change Note Type.""")) def onAdvanced(self): m = QMenu(self.mw) + a = m.addAction(_("MathJax inline")) + a.triggered.connect(self.insertMathjaxInline) + a = m.addAction(_("MathJax block")) + a.triggered.connect(self.insertMathjaxBlock) a = m.addAction(_("LaTeX")) a.triggered.connect(self.insertLatex) a = m.addAction(_("LaTeX equation")) @@ -676,6 +682,12 @@ to a cloze type first, via Edit>Change Note Type.""")) def insertLatexMathEnv(self): self.web.eval("wrap('[$$]', '[/$$]');") + def insertMathjaxInline(self): + self.web.eval("wrap('\\\\(', '\\\\)');") + + def insertMathjaxBlock(self): + self.web.eval("wrap('\\\\[', '\\\\]');") + # Links from HTML ###################################################################### diff --git a/web/mathjax/conf.js b/web/mathjax/conf.js index 7bed3c279..cea3bf9bd 100644 --- a/web/mathjax/conf.js +++ b/web/mathjax/conf.js @@ -4,6 +4,9 @@ window.MathJax = { TeX: { extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js", "mhchem.js"] }, + tex2jax: { + displayMath: [ ["\\[","\\]"] ], + }, messageStyle: "none", skipStartupTypeset: true, showMathMenu: false, diff --git a/web/reviewer.js b/web/reviewer.js index 448bb3e4c..866ef435d 100644 --- a/web/reviewer.js +++ b/web/reviewer.js @@ -26,7 +26,6 @@ function _updateQA(html, fadeTime, onupdate, onshown) { } catch(err) { qa.text("Invalid HTML on card: "+err); } - _removeStylingFromMathjaxCloze(); _runHook(onUpdateHook); // don't allow drags of images, which cause them to be deleted @@ -106,19 +105,3 @@ function _typeAnsPress() { pycmd("ans"); } } - -function _removeStylingFromMathjaxCloze() { - $(".cloze").each(function (i) { - if (_clozeIsInsideMathjax(this)) { - this.outerHTML = this.innerHTML; - } - }); -} - -function _clozeIsInsideMathjax(node) { - if (!node.previousSibling || node.previousSibling.nodeType !== 3) { - return; - } - // look for mathjax opening in previous text - return /\\\(|\$\$/.test(node.previousSibling.textContent); -} \ No newline at end of file