add option to avoid building latex

This commit is contained in:
Damien Elmes 2008-12-24 15:48:54 +09:00
parent 35ef8e6fa9
commit b161d6e485

View file

@ -33,17 +33,19 @@ tmpdir = tempfile.mkdtemp(prefix="anki-latex")
if sys.platform == "darwin": if sys.platform == "darwin":
os.environ['PATH'] += ":/usr/texbin" os.environ['PATH'] += ":/usr/texbin"
def renderLatex(deck, text): def renderLatex(deck, text, build=True):
"Convert TEXT with embedded latex tags to image links." "Convert TEXT with embedded latex tags to image links."
for match in regexps['standard'].finditer(text): for match in regexps['standard'].finditer(text):
text = text.replace(match.group(), imgLink(deck, match.group(1))) text = text.replace(match.group(), imgLink(deck, match.group(1),
build))
for match in regexps['expression'].finditer(text): for match in regexps['expression'].finditer(text):
text = text.replace(match.group(), imgLink( text = text.replace(match.group(), imgLink(
deck, "$" + match.group(1) + "$")) deck, "$" + match.group(1) + "$", build))
for match in regexps['math'].finditer(text): for match in regexps['math'].finditer(text):
text = text.replace(match.group(), imgLink( text = text.replace(match.group(), imgLink(
deck, deck,
"\\begin{displaymath}" + match.group(1) + "\\end{displaymath}")) "\\begin{displaymath}" + match.group(1) + "\\end{displaymath}",
build))
return text return text
def stripLatex(text): def stripLatex(text):
@ -137,22 +139,22 @@ def buildImg(deck, latex):
finally: finally:
os.chdir(oldcwd) os.chdir(oldcwd)
def imageForLatex(deck, latex): def imageForLatex(deck, latex, build=True):
"Return an image that represents 'latex', building if necessary." "Return an image that represents 'latex', building if necessary."
imageFile = latexImgFile(deck, latex) imageFile = latexImgFile(deck, latex)
if imageFile: if imageFile:
path = latexImgPath(deck, imageFile) path = latexImgPath(deck, imageFile)
ok = True ok = True
if not imageFile or not os.path.exists(path): if build and (not imageFile or not os.path.exists(path)):
(ok, imageFile) = buildImg(deck, latex) (ok, imageFile) = buildImg(deck, latex)
if not ok: if not ok:
return (False, imageFile) return (False, imageFile)
return (True, imageFile) return (True, imageFile)
def imgLink(deck, latex): def imgLink(deck, latex, build=True):
"Parse LATEX and return a HTML image representing the output." "Parse LATEX and return a HTML image representing the output."
latex = mungeLatex(latex) latex = mungeLatex(latex)
(ok, img) = imageForLatex(deck, latex) (ok, img) = imageForLatex(deck, latex, build)
if ok: if ok:
return '<img src="%s">' % img return '<img src="%s">' % img
else: else: