-
Notifications
You must be signed in to change notification settings - Fork 41
/
server.py
157 lines (131 loc) · 5.92 KB
/
server.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from flask import Flask, request, render_template, jsonify, Response, send_file
import os
import os.path
import subprocess
import requests
import distutils.spawn
DEBUG = os.environ.get('DEBUG', 'True') == 'True'
GOOGLE_ANALYTICS = os.environ.get('GOOGLE_ANALYTICS', '')
PRETTIFY_STYLESHEETS_FOLDER = '/static/css/prettify/' # server folder
PRETTIFY_STYLESHEETS = [ x[:-4] for x in os.listdir(os.path.join('static' , 'css' , 'prettify', ''))] # local filesystem folder
DEFAULT_LATEX_PAPER_SIZE = 'a4paper'
FILES_FOLDER = 'files'
if not os.path.exists(FILES_FOLDER):
os.mkdir(FILES_FOLDER)
CSL_FOLDER = 'static' + os.path.sep + 'csl'
CSL_FILES = [ x for x in os.listdir(CSL_FOLDER) if x.endswith('.csl')]
ABBR_FILES = [ x for x in os.listdir(CSL_FOLDER) if x.endswith('.abbr')]
DEFAULT_TEXT_FILE = "HELP.md"
with open(DEFAULT_TEXT_FILE,'r') as f:
DEFAULT_TEXT = f.read()
PANDOC_EXTENSIONS = ['pdf', 'docx', 'epub', 'html']
DOCVERTER_URL = 'http://c.docverter.com/convert'
PDFLATEX_EXISTS = distutils.spawn.find_executable("pdflatex") != None
app = Flask(__name__)
app.config.from_object(__name__)
print " * Overriding default configuration with config.py file"
app.config.from_pyfile('config.py', silent=True)
if app.debug:
print " * Running in debug mode"
mimetypes = {'md':'text/x-markdown', 'bib':'text/x-bibtex','html':'text/html','htm':'text/html','pdf':'application/pdf', 'latex':'application/x-latex', 'docx':'application/vnd.openxmlformats-officedocument.wordprocessingml.document','epub':'application/epub+zip'}
def get_mimetype(extension):
return mimetypes.get(extension, 'application/octet-stream')
def path_to_file(filename):
return FILES_FOLDER + os.path.sep + filename
def just_the_filename(path):
return os.path.splitext(os.path.basename(path))[0]
def save_text_file(filename, content):
filepath = path_to_file(filename)
with open(filepath, 'w') as f:
f.write(content)
return filepath
def pandoc(filename, extension, bibpath):
outname = path_to_file(filename + '.' + extension)
options = ['pandoc', path_to_file(filename + '.md'), '-o', outname]
options += ['--from', 'markdown+tex_math_double_backslash'] # --to inferred from outname
options += ['--standalone'] #--toc
options += ['--variable=geometry:' + DEFAULT_LATEX_PAPER_SIZE]
options += ['--mathjax']
if os.path.exists(bibpath):
options += ['--bibliography=' + bibpath]
if 'CSL_FILES' in app.config and len(app.config['CSL_FILES']) > 0:
csl_file = app.config['CSL_FILES'][0]
options += ['--csl=' + os.path.join(CSL_FOLDER, csl_file)]
if 'ABBR_FILES' in app.config and len(app.config['ABBR_FILES']) > 0:
abbr_file = app.config['ABBR_FILES'][0]
options += ['--citation-abbreviations=' + os.path.join(CSL_FOLDER, abbr_file)]
print ' * Sending command to Pandoc for file', filename, 'with options', options
p = subprocess.Popen(options, stdout=subprocess.PIPE)
stdoutdata, stderrdata = p.communicate()
if stderrdata:
print ' * Command failed:', stderrdata
return False, "Pandoc failed: " + stderrdata
else:
print ' * Command was successful:', stdoutdata
return True, filename + '.' + extension
def docverter(filename, extension, bibpath):
print ' * Sending request to Docverter for file', filename
with open(path_to_file(filename + '.md')) as filestream:
docverter_response = requests.post(app.config['DOCVERTER_URL'], data={
'to': extension,
'from': 'markdown',
'variable': 'geometry:' + app.config['DEFAULT_LATEX_PAPER_SIZE']
},
files={
'input_files[]': filestream
})
if docverter_response.ok:
print ' * Request was successful:', docverter_response.status_code
outname = filename + '.' + extension
with open(path_to_file(outname), 'wb') as fout:
fout.write(docverter_response.content)
return True, outname
else:
print ' * Request failed:', docverter_response.status_code
return False, docverter_response.status_code
@app.route('/save', methods=["POST"])
def save():
content = request.form.get('content', '', type=unicode)
bibtex = request.form.get('bibtex', '', type=str)
extension = request.form.get('extension', 'md', type=str).lower()
filename = request.form.get('filename', 'markx', type=str)
converter = request.form.get('converter', 'pandoc', type=str)
if converter == 'docverter':
converter = docverter
elif converter == 'pandoc':
converter = pandoc
else:
return jsonify(error="Converter named %s not found" % converter)
filename = just_the_filename(filename)
filepath = save_text_file(filename + '.md', content)
bibpath = save_text_file(filename + '.bib', bibtex)
if extension == 'md':
success, result = True, filename + '.md'
elif extension == 'bib':
success, result = True, filename + '.bib'
else:
success, result = converter(filename, extension, bibpath)
if success:
return jsonify(result=result)
else:
return jsonify(error=result)
@app.route('/download/<string:filename>')
def download(filename):
extension = os.path.splitext(filename)[1][1:].strip()
mimetype = get_mimetype(extension)
return send_file(path_to_file(filename), mimetype=mimetype, as_attachment=True, attachment_filename=filename, cache_timeout=0)
@app.route('/view/<string:filename>')
def view(filename):
extension = os.path.splitext(filename)[1][1:].strip()
mimetype = get_mimetype(extension)
return send_file(path_to_file(filename), mimetype=mimetype, as_attachment=False, cache_timeout=0)
@app.route("/")
def index():
return render_template("index.html")
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=app.debug)