fix reporting of latex errors; catch some bad commands

This commit is contained in:
Damien Elmes 2011-10-29 15:12:57 +09:00
parent 6911ae839e
commit ed7367d67f
5 changed files with 26 additions and 9 deletions

View file

@ -74,6 +74,10 @@ def _buildImg(deck, latex, fname, model):
latex = (model["latexPre"] + "\n" +
latex + "\n" +
model["latexPost"])
# it's only really secure if run in a jail, but these are the most common
for bad in ("write18", "\\readline", "\\input", "\\include", "\\catcode",
"\\openout", "\\write", "\\loop", "\\def", "\\shipout"):
assert bad not in latex
# write into a temp file
log = open(namedtmp("latex_log.txt"), "w")
texfile = file(namedtmp("tmp.tex"), "w")
@ -101,7 +105,7 @@ def _buildImg(deck, latex, fname, model):
def _errMsg(type):
msg = (_("Error executing %s.") % type) + "<br>"
try:
log = open(namedtmp("latex_log.txt")).read()
log = open(namedtmp("latex_log.txt", rm=False)).read()
if not log:
raise Exception()
msg += "<small><pre>" + cgi.escape(log) + "</pre></small>"

View file

@ -9,6 +9,7 @@ from anki.utils import checksum, intTime, namedtmp, isWin
from anki.lang import _
from anki.db import DB
from anki.consts import *
from anki.latex import mungeQA
class MediaManager(object):
@ -83,8 +84,13 @@ If the same name exists, compare checksums."""
# String manipulation
##########################################################################
def mediaFiles(self, string, includeRemote=False):
def files(self, mid, string, includeRemote=False):
l = []
# convert latex first
model = self.deck.models.get(mid)
string = mungeQA(string, None, None, model, None, self.deck)
print string
# extract filenames
for reg in self.regexps:
for (full, fname) in re.findall(reg, string):
isLocal = not re.match("(https?|ftp)://", fname.lower())

View file

@ -22,7 +22,7 @@ defaultModel = {
'latexPre': """\
\\documentclass[12pt]{article}
\\special{papersize=3in,5in}
\\usepackage[utf8x]{inputenc}
% \\usepackage[utf8x]{inputenc} % uncomment this for foreign characters
\\usepackage{amssymb,amsmath}
\\pagestyle{empty}
\\setlength{\\parindent}{0in}

View file

@ -461,6 +461,11 @@ order by ordinal""", mid)):
new = fld
# rewrite reference in template
t[key] = t[key].replace(all, "{{{%s}}}" % new)
regexps = deck.media.regexps + (
r"(\[latex\](.+?)\[/latex\])",
r"(\[\$\](.+?)\[/\$\])",
r"(\[\$\$\](.+?)\[/\$\$\])")
# process each model
for m in deck.models.all():
state = dict(mflds={}, fields=0)
for t in m['tmpls']:
@ -469,7 +474,8 @@ order by ordinal""", mid)):
rewriteRef('qfmt')
for match in re.findall(r, t['afmt']):
rewriteRef('afmt')
if state['fields']:
deck.models.save(m)
# Upgrading deck
######################################################################

View file

@ -242,13 +242,14 @@ def tmpfile(prefix="", suffix=""):
os.close(fd)
return name
def namedtmp(name):
def namedtmp(name, rm=True):
"Return tmpdir+name. Deletes any existing file."
path = os.path.join(tmpdir(), name)
try:
os.unlink(path)
except (OSError, IOError):
pass
if rm:
try:
os.unlink(path)
except (OSError, IOError):
pass
return path
# Cmd invocation