This repository was archived by the owner on Aug 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathingraph.py
More file actions
executable file
·72 lines (65 loc) · 2.78 KB
/
Copy pathingraph.py
File metadata and controls
executable file
·72 lines (65 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
import os
from flask import Flask, request, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = 'uploads/sources'
ALLOWED_EXTENSIONS = set(['tex'])
RESOURCE_FOLDER = 'resources'
RELALG_FRAGMENT_KEY = 'is_relalg_fragment'
ENVELOPE_PLACEHOLDER = '% INSERT_CONTENT_HERE'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
is_relalg_fragment = RELALG_FRAGMENT_KEY in request.form and request.form.get(RELALG_FRAGMENT_KEY)=='1'
if is_relalg_fragment:
with open(os.path.join(RESOURCE_FOLDER, 'relalg-envelope.tex'), 'r') as envelope_file:
envelope = envelope_file.read()
standalone_doc = envelope.replace(ENVELOPE_PLACEHOLDER, file.read().decode("utf-8"))
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'w') as standalone_doc_file:
standalone_doc_file.write(standalone_doc)
else:
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',
filename=filename))
return '''
<!doctype html>
<html>
<head>
<title>ingraph-tex-converter</title>
</head>
<body>
<h1>ingraph-tex-converter</h1>
<form method="post" enctype="multipart/form-data">
<p><input type="file" name="file">
<input type="submit" value="Upload">
</p>
<p>
<input type="checkbox" id="''' + RELALG_FRAGMENT_KEY + '''" name="''' + RELALG_FRAGMENT_KEY + '''" value="1" > <label for="''' +RELALG_FRAGMENT_KEY + '''">Relalg TeX fragment only.</label>
</p>
</form>
</body>
</html>
'''
@app.route('/uploads/<filename>')
def uploaded_file(filename):
filename = secure_filename(filename)
pdf_filename = filename.rsplit(".", 1)[0] + ".pdf"
os.system("bash -c 'cd " + app.config['UPLOAD_FOLDER'] + " && latexmk -xelatex -quiet " + filename + "'")
return send_from_directory(app.config['UPLOAD_FOLDER'], pdf_filename)