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
This commit is contained in:
Damien Elmes 2017-09-08 19:20:37 +10:00
parent 90a5a48d6c
commit 4990b2f8eb
4 changed files with 24 additions and 20 deletions

View file

@ -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 "<span class=cloze>[%s]</span>" % m.group(3)
buf = "[%s]" % m.group(3)
else:
return "<span class=cloze>[...]</span>"
buf = "[...]"
else:
return "<span class=cloze>%s</span>" % m.group(1)
buf = m.group(1)
if not mathjax:
buf = "<span class=cloze>[%s]</span>" % buf
return buf
txt = re.sub(reg%ord, repl, txt)
# and display other clozes normally
return re.sub(reg%"\d+", "\\1", txt)

View file

@ -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
######################################################################

View file

@ -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,

View file

@ -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);
}