Make sure that altering the tex template files will change tex_hash

This commit is contained in:
Grant Sanderson
2018-08-11 19:38:59 -07:00
parent abf0f9dc00
commit fe92169970
4 changed files with 19 additions and 14 deletions

View File

@ -6,34 +6,34 @@ from constants import TEX_DIR
from constants import TEX_TEXT_TO_REPLACE
def tex_hash(expression, template_tex_file):
id_str = str(expression + template_tex_file)
def tex_hash(expression, template_tex_file_body):
id_str = str(expression + template_tex_file_body)
hasher = hashlib.sha256()
hasher.update(id_str.encode())
# Truncating at 16 bytes for cleanliness
return hasher.hexdigest()[:16]
def tex_to_svg_file(expression, template_tex_file):
tex_file = generate_tex_file(expression, template_tex_file)
def tex_to_svg_file(expression, template_tex_file_body):
tex_file = generate_tex_file(expression, template_tex_file_body)
dvi_file = tex_to_dvi(tex_file)
return dvi_to_svg(dvi_file)
def generate_tex_file(expression, template_tex_file):
def generate_tex_file(expression, template_tex_file_body):
result = os.path.join(
TEX_DIR,
tex_hash(expression, template_tex_file)
tex_hash(expression, template_tex_file_body)
) + ".tex"
if not os.path.exists(result):
print("Writing \"%s\" to %s" % (
"".join(expression), result
))
with open(template_tex_file, "r") as infile:
body = infile.read()
body = body.replace(TEX_TEXT_TO_REPLACE, expression)
new_body = template_tex_file_body.replace(
TEX_TEXT_TO_REPLACE, expression
)
with open(result, "w") as outfile:
outfile.write(body)
outfile.write(new_body)
return result