mirror of
https://github.com/ankitects/anki.git
synced 2025-11-15 00:57:13 -05:00
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:
parent
90a5a48d6c
commit
4990b2f8eb
4 changed files with 24 additions and 20 deletions
|
|
@ -187,15 +187,21 @@ class Template:
|
||||||
reg = clozeReg
|
reg = clozeReg
|
||||||
if not re.search(reg%ord, txt):
|
if not re.search(reg%ord, txt):
|
||||||
return ""
|
return ""
|
||||||
|
mathjax = False
|
||||||
|
if '\[' in txt or '\(' in txt:
|
||||||
|
mathjax = True
|
||||||
def repl(m):
|
def repl(m):
|
||||||
# replace chosen cloze with type
|
# replace chosen cloze with type
|
||||||
if type == "q":
|
if type == "q":
|
||||||
if m.group(3):
|
if m.group(3):
|
||||||
return "<span class=cloze>[%s]</span>" % m.group(3)
|
buf = "[%s]" % m.group(3)
|
||||||
else:
|
else:
|
||||||
return "<span class=cloze>[...]</span>"
|
buf = "[...]"
|
||||||
else:
|
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)
|
txt = re.sub(reg%ord, repl, txt)
|
||||||
# and display other clozes normally
|
# and display other clozes normally
|
||||||
return re.sub(reg%"\d+", "\\1", txt)
|
return re.sub(reg%"\d+", "\\1", txt)
|
||||||
|
|
|
||||||
|
|
@ -185,6 +185,8 @@ class Editor:
|
||||||
("Ctrl+T, T", self.insertLatex),
|
("Ctrl+T, T", self.insertLatex),
|
||||||
("Ctrl+T, E", self.insertLatexEqn),
|
("Ctrl+T, E", self.insertLatexEqn),
|
||||||
("Ctrl+T, M", self.insertLatexMathEnv),
|
("Ctrl+T, M", self.insertLatexMathEnv),
|
||||||
|
("Ctrl+M, M", self.insertMathjaxInline),
|
||||||
|
("Ctrl+M, E", self.insertMathjaxBlock),
|
||||||
("Ctrl+Shift+X", self.onHtmlEdit),
|
("Ctrl+Shift+X", self.onHtmlEdit),
|
||||||
("Ctrl+Shift+T", self.onFocusTags)
|
("Ctrl+Shift+T", self.onFocusTags)
|
||||||
]
|
]
|
||||||
|
|
@ -654,6 +656,10 @@ to a cloze type first, via Edit>Change Note Type."""))
|
||||||
|
|
||||||
def onAdvanced(self):
|
def onAdvanced(self):
|
||||||
m = QMenu(self.mw)
|
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 = m.addAction(_("LaTeX"))
|
||||||
a.triggered.connect(self.insertLatex)
|
a.triggered.connect(self.insertLatex)
|
||||||
a = m.addAction(_("LaTeX equation"))
|
a = m.addAction(_("LaTeX equation"))
|
||||||
|
|
@ -676,6 +682,12 @@ to a cloze type first, via Edit>Change Note Type."""))
|
||||||
def insertLatexMathEnv(self):
|
def insertLatexMathEnv(self):
|
||||||
self.web.eval("wrap('[$$]', '[/$$]');")
|
self.web.eval("wrap('[$$]', '[/$$]');")
|
||||||
|
|
||||||
|
def insertMathjaxInline(self):
|
||||||
|
self.web.eval("wrap('\\\\(', '\\\\)');")
|
||||||
|
|
||||||
|
def insertMathjaxBlock(self):
|
||||||
|
self.web.eval("wrap('\\\\[', '\\\\]');")
|
||||||
|
|
||||||
# Links from HTML
|
# Links from HTML
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,9 @@ window.MathJax = {
|
||||||
TeX: {
|
TeX: {
|
||||||
extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js", "mhchem.js"]
|
extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js", "mhchem.js"]
|
||||||
},
|
},
|
||||||
|
tex2jax: {
|
||||||
|
displayMath: [ ["\\[","\\]"] ],
|
||||||
|
},
|
||||||
messageStyle: "none",
|
messageStyle: "none",
|
||||||
skipStartupTypeset: true,
|
skipStartupTypeset: true,
|
||||||
showMathMenu: false,
|
showMathMenu: false,
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,6 @@ function _updateQA(html, fadeTime, onupdate, onshown) {
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
qa.text("Invalid HTML on card: "+err);
|
qa.text("Invalid HTML on card: "+err);
|
||||||
}
|
}
|
||||||
_removeStylingFromMathjaxCloze();
|
|
||||||
_runHook(onUpdateHook);
|
_runHook(onUpdateHook);
|
||||||
|
|
||||||
// don't allow drags of images, which cause them to be deleted
|
// don't allow drags of images, which cause them to be deleted
|
||||||
|
|
@ -106,19 +105,3 @@ function _typeAnsPress() {
|
||||||
pycmd("ans");
|
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);
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue