offline-tex-editor 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- offline_tex_editor/__init__.py +2 -0
- offline_tex_editor/__main__.py +3 -0
- offline_tex_editor/gitapi.py +250 -0
- offline_tex_editor/index.html +132 -0
- offline_tex_editor/latexlog.py +205 -0
- offline_tex_editor/project.py +403 -0
- offline_tex_editor/server.py +923 -0
- offline_tex_editor/static/app.css +231 -0
- offline_tex_editor/static/app.js +72 -0
- offline_tex_editor/static/compile.js +83 -0
- offline_tex_editor/static/complete.js +159 -0
- offline_tex_editor/static/diffview.js +72 -0
- offline_tex_editor/static/editor.js +230 -0
- offline_tex_editor/static/files.js +181 -0
- offline_tex_editor/static/git.js +120 -0
- offline_tex_editor/static/package.json +5 -0
- offline_tex_editor/static/panels.js +83 -0
- offline_tex_editor/static/pdfview.js +254 -0
- offline_tex_editor/static/textindex.js +45 -0
- offline_tex_editor/static/tools.js +72 -0
- offline_tex_editor/static/util.js +57 -0
- offline_tex_editor/vendor/README.md +29 -0
- offline_tex_editor/vendor/codemirror/LICENSE +21 -0
- offline_tex_editor/vendor/codemirror/addon/dialog/dialog.min.css +1 -0
- offline_tex_editor/vendor/codemirror/addon/dialog/dialog.min.js +1 -0
- offline_tex_editor/vendor/codemirror/addon/edit/matchbrackets.min.js +1 -0
- offline_tex_editor/vendor/codemirror/addon/search/jump-to-line.min.js +1 -0
- offline_tex_editor/vendor/codemirror/addon/search/search.min.js +1 -0
- offline_tex_editor/vendor/codemirror/addon/search/searchcursor.min.js +1 -0
- offline_tex_editor/vendor/codemirror/addon/selection/active-line.min.js +1 -0
- offline_tex_editor/vendor/codemirror/codemirror.min.css +1 -0
- offline_tex_editor/vendor/codemirror/codemirror.min.js +1 -0
- offline_tex_editor/vendor/codemirror/mode/markdown/markdown.min.js +1 -0
- offline_tex_editor/vendor/codemirror/mode/python/python.min.js +1 -0
- offline_tex_editor/vendor/codemirror/mode/stex/stex.min.js +1 -0
- offline_tex_editor/vendor/pdfjs/LICENSE +177 -0
- offline_tex_editor/vendor/pdfjs/pdf.min.mjs +21 -0
- offline_tex_editor/vendor/pdfjs/pdf.worker.min.mjs +21 -0
- offline_tex_editor-0.2.0.dist-info/METADATA +193 -0
- offline_tex_editor-0.2.0.dist-info/RECORD +44 -0
- offline_tex_editor-0.2.0.dist-info/WHEEL +5 -0
- offline_tex_editor-0.2.0.dist-info/entry_points.txt +2 -0
- offline_tex_editor-0.2.0.dist-info/licenses/LICENSE +21 -0
- offline_tex_editor-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,923 @@
|
|
|
1
|
+
"""Offline TeX Editor: a local, Overleaf-style LaTeX editor. Edit in the browser, compile with latexmk, preview the
|
|
2
|
+
PDF with SyncTeX both ways, search, and use git. Standard library only; serves on 127.0.0.1 by default.
|
|
3
|
+
|
|
4
|
+
offline-tex-editor [--root PROJECT_DIR] [--main FILE] [--engine auto] [--port N] [--open]
|
|
5
|
+
|
|
6
|
+
Compiles with latexmk into <root>/build/<jobname>.pdf with -synctex=1, and -f so a PDF is still produced when there
|
|
7
|
+
are errors.
|
|
8
|
+
"""
|
|
9
|
+
import argparse
|
|
10
|
+
import base64
|
|
11
|
+
import difflib
|
|
12
|
+
import gzip
|
|
13
|
+
import json
|
|
14
|
+
import mimetypes
|
|
15
|
+
import os
|
|
16
|
+
import re
|
|
17
|
+
import shutil
|
|
18
|
+
import signal
|
|
19
|
+
import socket
|
|
20
|
+
import subprocess
|
|
21
|
+
import threading
|
|
22
|
+
import time
|
|
23
|
+
import webbrowser
|
|
24
|
+
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
25
|
+
from pathlib import Path
|
|
26
|
+
from urllib.parse import parse_qs, urlparse, urlsplit
|
|
27
|
+
|
|
28
|
+
from . import __version__, gitapi, latexlog, project
|
|
29
|
+
|
|
30
|
+
HERE = Path(__file__).resolve().parent
|
|
31
|
+
STATIC = {'static': HERE / 'static', 'vendor': HERE / 'vendor'}
|
|
32
|
+
TYPES = {'.js': 'text/javascript', '.mjs': 'text/javascript', '.css': 'text/css', '.svg': 'image/svg+xml',
|
|
33
|
+
'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp',
|
|
34
|
+
'.pdf': 'application/pdf', '.eps': 'application/postscript', '.tif': 'image/tiff', '.tiff': 'image/tiff',
|
|
35
|
+
'.bmp': 'image/bmp'}
|
|
36
|
+
WILDCARD_HOSTS = {'0.0.0.0', '::', ''}
|
|
37
|
+
DEFAULT_PORT = 8765
|
|
38
|
+
MAX_BODY = 64 * 1024 * 1024
|
|
39
|
+
PAGE_CSP = ("default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; "
|
|
40
|
+
"font-src 'self' data: blob:; connect-src 'self'; worker-src 'self' blob:; object-src 'none'; "
|
|
41
|
+
"base-uri 'none'; form-action 'none'; frame-ancestors 'none'")
|
|
42
|
+
RAW_CSP = "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; sandbox; frame-ancestors 'none'"
|
|
43
|
+
# Long log lines, so file names and messages are not wrapped at 79 characters (TeX Live reads these from the env).
|
|
44
|
+
TEX_ENV = dict(os.environ, max_print_line='1000', error_line='254', half_error_line='238')
|
|
45
|
+
|
|
46
|
+
ROOT = None
|
|
47
|
+
MAIN = 'main.tex'
|
|
48
|
+
JOB = 'main'
|
|
49
|
+
ENGINE = 'auto'
|
|
50
|
+
SHELL_ESCAPE = False
|
|
51
|
+
TIMEOUT = 600
|
|
52
|
+
ALLOWED_HOSTS = {'localhost', '127.0.0.1', '::1'}
|
|
53
|
+
ANY_HOST = False
|
|
54
|
+
settings = {'draft': False, 'only': None}
|
|
55
|
+
compile_lock = threading.Lock()
|
|
56
|
+
git_lock = threading.Lock()
|
|
57
|
+
fs_lock = threading.Lock()
|
|
58
|
+
proc_state = {'proc': None, 'stop': False, 'force_next': False, 'last_sig': None}
|
|
59
|
+
last_compile = {}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class UserError(Exception):
|
|
63
|
+
"""A request the user can fix: answered with 400 and the message."""
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class Conflict(Exception):
|
|
67
|
+
"""Answered with 409 and the payload."""
|
|
68
|
+
|
|
69
|
+
def __init__(self, payload):
|
|
70
|
+
super().__init__(payload.get('error', 'conflict'))
|
|
71
|
+
self.payload = payload
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def reset_compile_state():
|
|
75
|
+
last_compile.clear()
|
|
76
|
+
last_compile.update(success=None, started=None, finished=None, seconds=None, errors=[], warnings=[], log_tail='',
|
|
77
|
+
running=False, stopped=False, engine=None, engine_source=None)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
reset_compile_state()
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def configure(root, main=None, jobname=None, host='127.0.0.1', engine='auto', shell_escape=False, timeout=600):
|
|
84
|
+
"""Set the project and options. Called by main() and by the tests. Raises ValueError for a bad --root/--main."""
|
|
85
|
+
global ROOT, MAIN, JOB, ENGINE, SHELL_ESCAPE, TIMEOUT, ALLOWED_HOSTS, ANY_HOST
|
|
86
|
+
ROOT = Path(root).resolve()
|
|
87
|
+
if not ROOT.is_dir():
|
|
88
|
+
raise ValueError(f'{root} is not a directory')
|
|
89
|
+
MAIN = project.resolve_main(ROOT, main)
|
|
90
|
+
JOB = jobname or Path(MAIN).stem
|
|
91
|
+
ENGINE, SHELL_ESCAPE, TIMEOUT = engine, shell_escape, timeout
|
|
92
|
+
ALLOWED_HOSTS = {'localhost', '127.0.0.1', '::1', host.lower().strip('[]')}
|
|
93
|
+
ANY_HOST = host in WILDCARD_HOSTS
|
|
94
|
+
settings.update(draft=False, only=None)
|
|
95
|
+
proc_state.update(proc=None, stop=False, force_next=False, last_sig=None)
|
|
96
|
+
reset_compile_state()
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
# ---------- paths ----------
|
|
100
|
+
|
|
101
|
+
def rel_of(p):
|
|
102
|
+
return p.relative_to(ROOT).as_posix()
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def safe(rel):
|
|
106
|
+
"""Resolve a path inside ROOT; refuse anything outside it."""
|
|
107
|
+
p = (ROOT / rel).resolve()
|
|
108
|
+
if p != ROOT and ROOT not in p.parents:
|
|
109
|
+
raise PermissionError(f'{rel} is outside the project')
|
|
110
|
+
return p
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def editable(rel):
|
|
114
|
+
"""A path the editor may open or save: inside ROOT, an editable type, and not inside .git."""
|
|
115
|
+
p = safe(rel)
|
|
116
|
+
if not project.is_editable(p) or '.git' in p.relative_to(ROOT).parts:
|
|
117
|
+
raise PermissionError(f'{rel} is not an editable file')
|
|
118
|
+
return p
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def asset(rel):
|
|
122
|
+
p = safe(rel)
|
|
123
|
+
if p.suffix.lower() not in project.ASSETS:
|
|
124
|
+
raise PermissionError(f'{rel} is not an image or PDF')
|
|
125
|
+
return p
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def fs_target(rel):
|
|
129
|
+
"""A path file operations may create, rename or delete: inside ROOT, not ROOT, not in build/ or .git/."""
|
|
130
|
+
p = safe(rel)
|
|
131
|
+
if p == ROOT or project.skipped(p.relative_to(ROOT)):
|
|
132
|
+
raise PermissionError(f'{rel}: not allowed here')
|
|
133
|
+
return p
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def hostname(netloc):
|
|
137
|
+
try:
|
|
138
|
+
return urlsplit('//' + netloc).hostname
|
|
139
|
+
except ValueError:
|
|
140
|
+
return None
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def list_files():
|
|
144
|
+
return project.list_files(ROOT)[0]
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
# ---------- compiling ----------
|
|
148
|
+
|
|
149
|
+
def effective_engine():
|
|
150
|
+
"""(engine or None to let the project's latexmkrc decide, where the choice comes from)."""
|
|
151
|
+
if ENGINE != 'auto':
|
|
152
|
+
return ENGINE, '--engine'
|
|
153
|
+
try:
|
|
154
|
+
magic = project.engine_from_magic(project.read_text(ROOT / MAIN, 65536))
|
|
155
|
+
except OSError:
|
|
156
|
+
magic = None
|
|
157
|
+
if magic:
|
|
158
|
+
return magic, '% !TEX program'
|
|
159
|
+
if project.rc_sets_pdf_mode(ROOT):
|
|
160
|
+
return None, 'latexmkrc'
|
|
161
|
+
return 'pdflatex', 'default'
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def latexmk_cmd(engine, force=False):
|
|
165
|
+
cmd = ['latexmk']
|
|
166
|
+
if force:
|
|
167
|
+
cmd.append('-g')
|
|
168
|
+
if engine:
|
|
169
|
+
cmd.append({'pdflatex': '-pdf', 'xelatex': '-xelatex', 'lualatex': '-lualatex'}[engine])
|
|
170
|
+
cmd += ['-f', '-interaction=nonstopmode', '-file-line-error', '-synctex=1', '-outdir=build', f'-jobname={JOB}']
|
|
171
|
+
if SHELL_ESCAPE:
|
|
172
|
+
cmd.append('-shell-escape')
|
|
173
|
+
pretex = ''
|
|
174
|
+
if settings['draft']:
|
|
175
|
+
pretex += r'\PassOptionsToPackage{draft}{graphicx}'
|
|
176
|
+
if settings['only']:
|
|
177
|
+
pretex += r'\includeonly{%s}' % settings['only']
|
|
178
|
+
if pretex:
|
|
179
|
+
cmd.append('-usepretex=' + pretex)
|
|
180
|
+
cmd.append(MAIN)
|
|
181
|
+
return cmd
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def kill_tree(proc):
|
|
185
|
+
"""Stop latexmk and the programs it started (it runs in its own process group)."""
|
|
186
|
+
if proc.poll() is not None:
|
|
187
|
+
return
|
|
188
|
+
if os.name == 'nt':
|
|
189
|
+
subprocess.run(['taskkill', '/T', '/F', '/PID', str(proc.pid)], capture_output=True)
|
|
190
|
+
return
|
|
191
|
+
try:
|
|
192
|
+
os.killpg(proc.pid, signal.SIGTERM)
|
|
193
|
+
proc.wait(3)
|
|
194
|
+
except ProcessLookupError:
|
|
195
|
+
pass
|
|
196
|
+
except subprocess.TimeoutExpired:
|
|
197
|
+
os.killpg(proc.pid, signal.SIGKILL)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def mirror_dirs():
|
|
201
|
+
"""Create build/<dir> for each source folder. TeX cannot create folders, so with an output directory,
|
|
202
|
+
\\include{chapters/one} fails ("I can't write on file chapters/one.aux") unless build/chapters exists."""
|
|
203
|
+
for p, rel in project.walk(ROOT):
|
|
204
|
+
if p.suffix == '.tex' and '/' in rel:
|
|
205
|
+
(ROOT / 'build' / rel).parent.mkdir(parents=True, exist_ok=True)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def run_latexmk(cmd):
|
|
209
|
+
"""(exit code, output, why it ended early: None, 'timeout', 'stopped' or 'missing')."""
|
|
210
|
+
group = {'creationflags': subprocess.CREATE_NEW_PROCESS_GROUP} if os.name == 'nt' else {'start_new_session': True}
|
|
211
|
+
try:
|
|
212
|
+
proc = subprocess.Popen(cmd, cwd=ROOT, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
|
|
213
|
+
encoding='utf-8', errors='replace', env=TEX_ENV, **group)
|
|
214
|
+
except FileNotFoundError:
|
|
215
|
+
return -1, 'latexmk not found on PATH. Install a TeX distribution that includes it (TeX Live does).', 'missing'
|
|
216
|
+
proc_state['proc'] = proc
|
|
217
|
+
try:
|
|
218
|
+
try:
|
|
219
|
+
out, _ = proc.communicate(timeout=TIMEOUT)
|
|
220
|
+
why = 'stopped' if proc_state['stop'] else None
|
|
221
|
+
except subprocess.TimeoutExpired:
|
|
222
|
+
kill_tree(proc)
|
|
223
|
+
out, _ = proc.communicate()
|
|
224
|
+
out, why = out + f'\nlatexmk timed out after {TIMEOUT} s (see --timeout)', 'timeout'
|
|
225
|
+
finally:
|
|
226
|
+
proc_state['proc'] = None
|
|
227
|
+
return proc.returncode, out, why
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def stop_compile():
|
|
231
|
+
proc = proc_state['proc']
|
|
232
|
+
if proc is None:
|
|
233
|
+
return {'stopped': False}
|
|
234
|
+
proc_state['stop'] = True
|
|
235
|
+
kill_tree(proc)
|
|
236
|
+
return {'stopped': True}
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def bib_index():
|
|
240
|
+
return {e['key']: (e['file'], e['line']) for e in project.bib_entries(ROOT, list_files())}
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def bib_file(name):
|
|
244
|
+
"""A .bib name from a BibTeX/biber log, as a project path (they may give a bare file name)."""
|
|
245
|
+
if not name:
|
|
246
|
+
return None
|
|
247
|
+
try:
|
|
248
|
+
p = safe(name)
|
|
249
|
+
if p.is_file():
|
|
250
|
+
return rel_of(p)
|
|
251
|
+
except PermissionError:
|
|
252
|
+
pass
|
|
253
|
+
base = Path(name).name
|
|
254
|
+
return next((f for f in list_files() if Path(f).name == base), name)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def read_logs():
|
|
258
|
+
log_path, blg_path = ROOT / 'build' / f'{JOB}.log', ROOT / 'build' / f'{JOB}.blg'
|
|
259
|
+
index = None
|
|
260
|
+
|
|
261
|
+
def find_key(key):
|
|
262
|
+
nonlocal index
|
|
263
|
+
index = bib_index() if index is None else index
|
|
264
|
+
return index.get(key)
|
|
265
|
+
|
|
266
|
+
errors, warnings = latexlog.parse_log(project.read_text(log_path) if log_path.exists() else '', ROOT, find_key)
|
|
267
|
+
if blg_path.exists():
|
|
268
|
+
berr, bwarn, _ = latexlog.parse_blg(project.read_text(blg_path))
|
|
269
|
+
for item in berr + bwarn:
|
|
270
|
+
item['file'] = bib_file(item['file'])
|
|
271
|
+
errors += berr
|
|
272
|
+
warnings += bwarn
|
|
273
|
+
return errors, warnings
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def run_compile():
|
|
277
|
+
with compile_lock:
|
|
278
|
+
last_compile.update(running=True, started=time.time(), stopped=False)
|
|
279
|
+
proc_state['stop'] = False
|
|
280
|
+
t0 = time.time()
|
|
281
|
+
engine, source = effective_engine()
|
|
282
|
+
sig = (MAIN, JOB, engine, settings['draft'], settings['only'], SHELL_ESCAPE)
|
|
283
|
+
force = (proc_state['force_next'] or proc_state['last_sig'] not in (None, sig)
|
|
284
|
+
or not (ROOT / 'build' / f'{JOB}.synctex.gz').exists())
|
|
285
|
+
cmd = latexmk_cmd(engine, force)
|
|
286
|
+
try:
|
|
287
|
+
errors, warnings = [], []
|
|
288
|
+
mirror_dirs()
|
|
289
|
+
code, out, why = run_latexmk(cmd)
|
|
290
|
+
if why != 'missing':
|
|
291
|
+
errors, warnings = read_logs()
|
|
292
|
+
try:
|
|
293
|
+
rotate_snapshots()
|
|
294
|
+
except Exception as e: # the change view is a convenience; never fail the compile over it
|
|
295
|
+
print(f'snapshot failed: {e!r}', flush=True)
|
|
296
|
+
except BaseException:
|
|
297
|
+
last_compile['running'] = False # never leave the UI showing "compiling…" forever
|
|
298
|
+
raise
|
|
299
|
+
proc_state.update(last_sig=sig, force_next=why in ('timeout', 'stopped'))
|
|
300
|
+
last_compile.update(success=(code == 0 and not errors and why is None), finished=time.time(),
|
|
301
|
+
seconds=round(time.time() - t0, 1), errors=errors, warnings=warnings,
|
|
302
|
+
log_tail='\n'.join(out.splitlines()[-80:]), running=False, stopped=why == 'stopped',
|
|
303
|
+
engine=engine, engine_source=source, main=MAIN)
|
|
304
|
+
return dict(last_compile)
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def pdf_info():
|
|
308
|
+
"""(mtime, complete) of the PDF, or (None, False). A PDF still being written has no %%EOF at its end yet."""
|
|
309
|
+
pdf = ROOT / 'build' / f'{JOB}.pdf'
|
|
310
|
+
try:
|
|
311
|
+
size, mtime = pdf.stat().st_size, pdf.stat().st_mtime
|
|
312
|
+
with open(pdf, 'rb') as f:
|
|
313
|
+
f.seek(max(0, size - 2048))
|
|
314
|
+
return mtime, b'%%EOF' in f.read()
|
|
315
|
+
except OSError:
|
|
316
|
+
return None, False
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def settings_view():
|
|
320
|
+
engine, source = effective_engine()
|
|
321
|
+
entries, includes = project.outline(ROOT, MAIN)
|
|
322
|
+
return {'main': MAIN, 'job': JOB, 'draft': settings['draft'], 'only': settings['only'], 'engine': engine,
|
|
323
|
+
'engine_source': source, 'shell_escape': SHELL_ESCAPE, 'timeout': TIMEOUT,
|
|
324
|
+
'roots': project.find_roots(ROOT, list_files()), 'includes': includes,
|
|
325
|
+
'tools': {t: shutil.which(t) is not None for t in ('latexmk', 'synctex', 'texcount', 'git', 'biber')}}
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def set_main(rel):
|
|
329
|
+
global MAIN, JOB
|
|
330
|
+
p = editable(rel)
|
|
331
|
+
if p.suffix != '.tex' or not project.is_root(project.read_text(p, 65536)):
|
|
332
|
+
raise UserError(f'{rel} is not a LaTeX document (no \\documentclass)')
|
|
333
|
+
MAIN, JOB = rel_of(p), p.stem
|
|
334
|
+
settings['only'] = None
|
|
335
|
+
reset_compile_state()
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
def update_settings(b):
|
|
339
|
+
if 'main' in b and b['main'] != MAIN:
|
|
340
|
+
set_main(b['main'])
|
|
341
|
+
if 'draft' in b:
|
|
342
|
+
settings['draft'] = bool(b['draft'])
|
|
343
|
+
if 'only' in b:
|
|
344
|
+
only = b['only'] or None
|
|
345
|
+
if only is not None and only not in project.outline(ROOT, MAIN)[1]:
|
|
346
|
+
raise UserError(f'{only} is not \\include\'d by {MAIN}')
|
|
347
|
+
settings['only'] = only
|
|
348
|
+
return settings_view()
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
# ---------- changes between compiles ----------
|
|
352
|
+
# After each compile the source files it read are snapshotted to build/.editor-snapshots/<job>-last.json. When the
|
|
353
|
+
# sources differ from the previous snapshot, that one becomes <job>-prev.json, so /api/changes always shows the most
|
|
354
|
+
# recent compile that changed something.
|
|
355
|
+
|
|
356
|
+
def snap_file(name):
|
|
357
|
+
return ROOT / 'build' / '.editor-snapshots' / f'{JOB}-{name}.json'
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def compile_inputs():
|
|
361
|
+
"""Project files the last compile read: INPUT lines of the .fls, plus the .bib databases named in the .blg."""
|
|
362
|
+
rels = set()
|
|
363
|
+
fls = ROOT / 'build' / f'{JOB}.fls'
|
|
364
|
+
if fls.exists():
|
|
365
|
+
for line in project.read_text(fls).splitlines():
|
|
366
|
+
if line.startswith('INPUT '):
|
|
367
|
+
rels.add(line[6:].strip())
|
|
368
|
+
blg = ROOT / 'build' / f'{JOB}.blg'
|
|
369
|
+
if blg.exists():
|
|
370
|
+
rels.update(latexlog.parse_blg(project.read_text(blg))[2])
|
|
371
|
+
out = set()
|
|
372
|
+
for r in rels:
|
|
373
|
+
try:
|
|
374
|
+
p = safe(r)
|
|
375
|
+
except PermissionError:
|
|
376
|
+
continue
|
|
377
|
+
if p.is_file() and project.is_editable(p) and not project.skipped(p.relative_to(ROOT)):
|
|
378
|
+
out.add(rel_of(p))
|
|
379
|
+
return sorted(out) or [f for f in list_files() if f.endswith(('.tex', '.bib'))]
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
def load_snapshot(name):
|
|
383
|
+
p = snap_file(name)
|
|
384
|
+
return json.loads(p.read_text(encoding='utf-8')) if p.exists() else None
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def rotate_snapshots():
|
|
388
|
+
cur = {'time': time.time(), 'files': {r: project.read_text(ROOT / r) for r in compile_inputs()}}
|
|
389
|
+
last = load_snapshot('last')
|
|
390
|
+
if last and last['files'] == cur['files']:
|
|
391
|
+
return
|
|
392
|
+
snap_file('last').parent.mkdir(parents=True, exist_ok=True)
|
|
393
|
+
if last:
|
|
394
|
+
snap_file('prev').write_text(json.dumps(last), encoding='utf-8')
|
|
395
|
+
snap_file('last').write_text(json.dumps(cur), encoding='utf-8')
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
TOKEN = re.compile(r'\s+|\w+|[^\w\s]')
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
def word_diff(a, b):
|
|
402
|
+
"""Segments [kind, text] (kind eq/add/del) turning a into b, word by word; a near-total rewrite is shown whole."""
|
|
403
|
+
ta, tb = TOKEN.findall(a), TOKEN.findall(b)
|
|
404
|
+
sm = difflib.SequenceMatcher(None, ta, tb, autojunk=False)
|
|
405
|
+
if sm.ratio() < 0.3:
|
|
406
|
+
return [['del', a], ['add', b]]
|
|
407
|
+
segs = []
|
|
408
|
+
for tag, i1, i2, j1, j2 in sm.get_opcodes():
|
|
409
|
+
parts = [['eq', ''.join(ta[i1:i2])]] if tag == 'equal' else \
|
|
410
|
+
[['del', ''.join(ta[i1:i2])], ['add', ''.join(tb[j1:j2])]]
|
|
411
|
+
for kind, text in parts:
|
|
412
|
+
if not text:
|
|
413
|
+
continue
|
|
414
|
+
if segs and segs[-1][0] == kind:
|
|
415
|
+
segs[-1][1] += text
|
|
416
|
+
else:
|
|
417
|
+
segs.append([kind, text])
|
|
418
|
+
return segs
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
def file_hunks(a, b, context=2):
|
|
422
|
+
al, bl = a.splitlines(), b.splitlines()
|
|
423
|
+
hunks = []
|
|
424
|
+
for group in difflib.SequenceMatcher(None, al, bl, autojunk=False).get_grouped_opcodes(context):
|
|
425
|
+
segs = []
|
|
426
|
+
for tag, i1, i2, j1, j2 in group:
|
|
427
|
+
old, new = '\n'.join(al[i1:i2]) + '\n', '\n'.join(bl[j1:j2]) + '\n'
|
|
428
|
+
if tag == 'equal':
|
|
429
|
+
segs.append(['eq', old])
|
|
430
|
+
elif tag == 'delete':
|
|
431
|
+
segs.append(['del', old])
|
|
432
|
+
elif tag == 'insert':
|
|
433
|
+
segs.append(['add', new])
|
|
434
|
+
else:
|
|
435
|
+
segs.extend(word_diff(old, new))
|
|
436
|
+
hunks.append({'line': group[0][3] + 1, 'segs': segs})
|
|
437
|
+
return hunks
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
def changes():
|
|
441
|
+
prev, last = load_snapshot('prev'), load_snapshot('last')
|
|
442
|
+
if not prev or not last:
|
|
443
|
+
return {'baseline': False, 'files': []}
|
|
444
|
+
files = []
|
|
445
|
+
for rel in sorted(set(prev['files']) | set(last['files'])):
|
|
446
|
+
a, b = prev['files'].get(rel), last['files'].get(rel)
|
|
447
|
+
if a == b:
|
|
448
|
+
continue
|
|
449
|
+
status = 'added' if a is None else 'deleted' if b is None else 'modified'
|
|
450
|
+
files.append({'path': rel, 'status': status, 'hunks': file_hunks(a or '', b or '')})
|
|
451
|
+
return {'baseline': True, 'from': prev['time'], 'to': last['time'], 'files': files}
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
# ---------- SyncTeX ----------
|
|
455
|
+
|
|
456
|
+
_synctex_cache = {'key': None, 'names': {}}
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
def synctex_names():
|
|
460
|
+
"""Input file names exactly as the .synctex.gz records them, by resolved path. Forward search must use the
|
|
461
|
+
recorded name, which may be relative, start with ./ or contain spaces."""
|
|
462
|
+
f = ROOT / 'build' / f'{JOB}.synctex.gz'
|
|
463
|
+
try:
|
|
464
|
+
key = (f, f.stat().st_mtime)
|
|
465
|
+
except OSError:
|
|
466
|
+
return {}
|
|
467
|
+
if _synctex_cache['key'] != key:
|
|
468
|
+
names = {}
|
|
469
|
+
with gzip.open(f, 'rt', encoding='utf-8', errors='replace') as fh:
|
|
470
|
+
for line in fh:
|
|
471
|
+
if line.startswith('Input:'):
|
|
472
|
+
name = line.split(':', 2)[2].rstrip('\n')
|
|
473
|
+
p = Path(name)
|
|
474
|
+
try:
|
|
475
|
+
names.setdefault((p if p.is_absolute() else ROOT / p).resolve(), name)
|
|
476
|
+
except OSError:
|
|
477
|
+
pass
|
|
478
|
+
_synctex_cache.update(key=key, names=names)
|
|
479
|
+
return _synctex_cache['names']
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
def synctex(args):
|
|
483
|
+
proc = subprocess.run(['synctex', *args], cwd=ROOT / 'build', capture_output=True, text=True, encoding='utf-8',
|
|
484
|
+
errors='replace', timeout=20)
|
|
485
|
+
return proc.stdout
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
def synctex_view(rel, line, column=0):
|
|
489
|
+
"""PDF position of a source line. Lines with no typeset material (blank, comments, \\begin) have no record, so
|
|
490
|
+
nearby lines are tried, nearest first."""
|
|
491
|
+
src = safe(rel)
|
|
492
|
+
name = synctex_names().get(src, str(src))
|
|
493
|
+
for delta in (0, 1, -1, 2, -2, 3, -3, 4, 5, 6, 8, 10):
|
|
494
|
+
ln = line + delta
|
|
495
|
+
if ln < 1:
|
|
496
|
+
continue
|
|
497
|
+
out = synctex(['view', '-i', f'{ln}:{column}:{name}', '-o', f'{JOB}.pdf'])
|
|
498
|
+
pages = re.findall(r'^Page:(\d+)$', out, re.M)
|
|
499
|
+
if pages:
|
|
500
|
+
ys = re.findall(r'^y:([\d.]+)$', out, re.M)
|
|
501
|
+
xs = re.findall(r'^x:([\d.]+)$', out, re.M)
|
|
502
|
+
return {'page': int(pages[0]), 'x': float(xs[0]) if xs else 0, 'y': float(ys[0]) if ys else 0,
|
|
503
|
+
'line': ln}
|
|
504
|
+
raise FileNotFoundError('no PDF location for that line (compile first?)')
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
def synctex_edit(page, x, y):
|
|
508
|
+
out = synctex(['edit', '-o', f'{int(page)}:{float(x):.2f}:{float(y):.2f}:{JOB}.pdf'])
|
|
509
|
+
f = re.search(r'^Input:(.*)$', out, re.M)
|
|
510
|
+
ln = re.search(r'^Line:(-?\d+)$', out, re.M)
|
|
511
|
+
if not f:
|
|
512
|
+
raise FileNotFoundError('no source location for that spot')
|
|
513
|
+
p = Path(f.group(1))
|
|
514
|
+
p = (p if p.is_absolute() else ROOT / p).resolve()
|
|
515
|
+
if ROOT not in p.parents:
|
|
516
|
+
raise FileNotFoundError(f'that spot comes from {f.group(1)}, outside the project')
|
|
517
|
+
return {'path': rel_of(p), 'line': max(1, int(ln.group(1))) if ln else 1}
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
# ---------- other tools ----------
|
|
521
|
+
|
|
522
|
+
WORDCOUNT_FIELDS = {'Words in text': 'text', 'Words in headers': 'headers',
|
|
523
|
+
'Words outside text (captions, etc.)': 'outside', 'Number of headers': 'n_headers',
|
|
524
|
+
'Number of floats/tables/figures': 'floats', 'Number of math inlines': 'math_inline',
|
|
525
|
+
'Number of math displayed': 'math_display'}
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
def word_count():
|
|
529
|
+
if shutil.which('texcount') is None:
|
|
530
|
+
raise UserError('texcount not found. It comes with TeX Live (on Debian/Ubuntu: texlive-extra-utils).')
|
|
531
|
+
p = subprocess.run(['texcount', '-inc', '-total', '-utf8', '-q', MAIN], cwd=ROOT, capture_output=True, text=True,
|
|
532
|
+
encoding='utf-8', errors='replace', timeout=60)
|
|
533
|
+
out = {}
|
|
534
|
+
for line in p.stdout.splitlines():
|
|
535
|
+
k, _, v = line.partition(':')
|
|
536
|
+
if k.strip() in WORDCOUNT_FIELDS and v.strip().isdigit():
|
|
537
|
+
out[WORDCOUNT_FIELDS[k.strip()]] = int(v)
|
|
538
|
+
if 'text' not in out:
|
|
539
|
+
raise UserError('texcount failed: ' + (p.stderr or p.stdout).strip()[:300])
|
|
540
|
+
return out
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
# ---------- file operations ----------
|
|
544
|
+
|
|
545
|
+
def fs_new(b):
|
|
546
|
+
p = fs_target(b['path'])
|
|
547
|
+
with fs_lock:
|
|
548
|
+
if p.exists():
|
|
549
|
+
raise UserError(f"{b['path']} already exists")
|
|
550
|
+
if b.get('kind') == 'folder':
|
|
551
|
+
p.mkdir(parents=True)
|
|
552
|
+
else:
|
|
553
|
+
if not project.is_editable(p):
|
|
554
|
+
raise UserError('a new file needs an editable extension such as .tex or .bib')
|
|
555
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
556
|
+
p.write_bytes(b'')
|
|
557
|
+
return {'path': rel_of(p)}
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
def fs_rename(b):
|
|
561
|
+
global MAIN
|
|
562
|
+
a, dst = fs_target(b['from']), fs_target(b['to'])
|
|
563
|
+
with fs_lock:
|
|
564
|
+
if not a.exists():
|
|
565
|
+
raise FileNotFoundError(b['from'])
|
|
566
|
+
if dst.exists():
|
|
567
|
+
raise UserError(f"{b['to']} already exists")
|
|
568
|
+
if a.is_file() and not (project.is_editable(dst) or dst.suffix.lower() in project.ASSETS):
|
|
569
|
+
raise UserError('keep an editable or image extension')
|
|
570
|
+
if a.is_dir() and a in dst.parents:
|
|
571
|
+
raise UserError('cannot move a folder into itself')
|
|
572
|
+
dst.parent.mkdir(parents=True, exist_ok=True)
|
|
573
|
+
main = ROOT / MAIN
|
|
574
|
+
a.rename(dst)
|
|
575
|
+
if main == a or a in main.parents: # keep compiling the same document
|
|
576
|
+
MAIN = rel_of(dst / main.relative_to(a)) if main != a else rel_of(dst)
|
|
577
|
+
return {'path': rel_of(dst), 'main': MAIN}
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
def fs_delete(b):
|
|
581
|
+
p = fs_target(b['path'])
|
|
582
|
+
with fs_lock:
|
|
583
|
+
if not p.exists():
|
|
584
|
+
raise FileNotFoundError(b['path'])
|
|
585
|
+
main = ROOT / MAIN
|
|
586
|
+
if p == main or p in main.parents:
|
|
587
|
+
raise UserError('this is the main document (or its folder); choose another main document first')
|
|
588
|
+
# Not deleted: moved to build/.editor-trash/<time>/, so a mistake can be undone.
|
|
589
|
+
base = ROOT / 'build' / '.editor-trash' / time.strftime('%Y%m%d-%H%M%S')
|
|
590
|
+
dest, n = base / rel_of(p), 1
|
|
591
|
+
while dest.exists():
|
|
592
|
+
dest, n = base.with_name(f'{base.name}-{n}') / rel_of(p), n + 1
|
|
593
|
+
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
594
|
+
shutil.move(str(p), str(dest))
|
|
595
|
+
return {'trash': rel_of(dest)}
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
def fs_upload(b):
|
|
599
|
+
p = fs_target(b['path'])
|
|
600
|
+
if not (project.is_editable(p) or p.suffix.lower() in project.ASSETS):
|
|
601
|
+
raise UserError(f"{b['path']}: only images, PDFs and editable files can be added")
|
|
602
|
+
try:
|
|
603
|
+
data = base64.b64decode(b['data'], validate=True)
|
|
604
|
+
except ValueError:
|
|
605
|
+
raise UserError('bad file data')
|
|
606
|
+
with fs_lock:
|
|
607
|
+
if p.exists() and not b.get('replace'):
|
|
608
|
+
raise Conflict({'error': f"{b['path']} already exists", 'exists': True})
|
|
609
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
610
|
+
p.write_bytes(data)
|
|
611
|
+
return {'path': rel_of(p), 'size': len(data)}
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
# ---------- search ----------
|
|
615
|
+
|
|
616
|
+
def query_rx(d):
|
|
617
|
+
try:
|
|
618
|
+
return project.compile_query(d['q'], str(d.get('regex')) in ('1', 'True', 'true'),
|
|
619
|
+
str(d.get('case')) in ('1', 'True', 'true'),
|
|
620
|
+
str(d.get('word')) in ('1', 'True', 'true'))
|
|
621
|
+
except re.error as e:
|
|
622
|
+
raise UserError(f'bad regular expression: {e}')
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
def h_search(q):
|
|
626
|
+
if not q.get('q'):
|
|
627
|
+
return {'matches': [], 'truncated': False}
|
|
628
|
+
matches, truncated = project.search(ROOT, list_files(), query_rx(q))
|
|
629
|
+
return {'matches': matches, 'truncated': truncated}
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
def h_replace(b):
|
|
633
|
+
if not b.get('q'):
|
|
634
|
+
raise UserError('nothing to replace')
|
|
635
|
+
files = list_files()
|
|
636
|
+
if b.get('paths'):
|
|
637
|
+
files = [f for f in files if f in set(b['paths'])]
|
|
638
|
+
regex = bool(b.get('regex'))
|
|
639
|
+
try:
|
|
640
|
+
counts = project.replace(ROOT, files, query_rx(b), b.get('replacement', ''), regex)
|
|
641
|
+
except re.error as e:
|
|
642
|
+
raise UserError(f'bad replacement: {e}')
|
|
643
|
+
except UnicodeEncodeError as e:
|
|
644
|
+
raise UserError(f'cannot save: the replacement has characters the file\'s encoding cannot hold ({e.reason})')
|
|
645
|
+
return {'files': counts, 'replaced': sum(counts.values())}
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
# ---------- HTTP ----------
|
|
649
|
+
|
|
650
|
+
def h_file(q):
|
|
651
|
+
p = editable(q['path'])
|
|
652
|
+
text, enc, eol = project.read_source(p)
|
|
653
|
+
return {'path': q['path'], 'content': text, 'mtime': p.stat().st_mtime, 'encoding': enc, 'eol': eol}
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
def h_save(b):
|
|
657
|
+
p = editable(b['path'])
|
|
658
|
+
on_disk = p.stat().st_mtime if p.exists() else None
|
|
659
|
+
if b.get('mtime') and on_disk and abs(on_disk - b['mtime']) > 1e-6 and not b.get('force'):
|
|
660
|
+
raise Conflict({'error': 'changed on disk since you opened it', 'mtime': on_disk})
|
|
661
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
662
|
+
try:
|
|
663
|
+
project.write_source(p, b['content'], b.get('encoding', 'utf-8'))
|
|
664
|
+
except UnicodeEncodeError as e:
|
|
665
|
+
raise UserError(f'cannot save as {b.get("encoding")}: {e.reason}. Remove the characters it cannot hold.')
|
|
666
|
+
return {'mtime': p.stat().st_mtime}
|
|
667
|
+
|
|
668
|
+
|
|
669
|
+
def h_status(q):
|
|
670
|
+
mtime, complete = pdf_info()
|
|
671
|
+
return dict(last_compile, pdf_mtime=mtime, pdf_complete=complete, main=MAIN, job=JOB, draft=settings['draft'],
|
|
672
|
+
only=settings['only'])
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
def h_compile(b):
|
|
676
|
+
if compile_lock.locked():
|
|
677
|
+
raise Conflict({'error': 'a compile is already running'})
|
|
678
|
+
return run_compile()
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
def h_outline(q):
|
|
682
|
+
entries, includes = project.outline(ROOT, MAIN)
|
|
683
|
+
return {'main': MAIN, 'entries': entries, 'includes': includes}
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
def h_labels(q):
|
|
687
|
+
files = list_files()
|
|
688
|
+
return {'labels': project.labels(ROOT, files), 'bib': project.bib_entries(ROOT, files),
|
|
689
|
+
'commands': project.commands(ROOT, files)}
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
GET_ROUTES = {
|
|
693
|
+
'/api/files': lambda q: dict(zip(('files', 'assets'), project.list_files(ROOT)), dirs=project.list_dirs(ROOT),
|
|
694
|
+
root=str(ROOT), main=MAIN),
|
|
695
|
+
'/api/file': h_file,
|
|
696
|
+
'/api/mtime': lambda q: {'mtime': (lambda p: p.stat().st_mtime if p.exists() else None)(editable(q['path']))},
|
|
697
|
+
'/api/status': h_status,
|
|
698
|
+
'/api/settings': lambda q: settings_view(),
|
|
699
|
+
'/api/changes': lambda q: changes(),
|
|
700
|
+
'/api/synctex/edit': lambda q: synctex_edit(q['page'], q['x'], q['y']),
|
|
701
|
+
'/api/synctex/view': lambda q: synctex_view(q['path'], int(q['line']), int(q.get('column', 0))),
|
|
702
|
+
'/api/search': h_search,
|
|
703
|
+
'/api/outline': h_outline,
|
|
704
|
+
'/api/labels': h_labels,
|
|
705
|
+
'/api/wordcount': lambda q: word_count(),
|
|
706
|
+
'/api/git/status': lambda q: gitapi.status(ROOT),
|
|
707
|
+
'/api/git/diff': lambda q: gitapi.diff(ROOT, q['path'], q.get('staged') == '1'),
|
|
708
|
+
'/api/git/log': lambda q: gitapi.log(ROOT),
|
|
709
|
+
'/api/git/show': lambda q: gitapi.show(ROOT, q['rev']),
|
|
710
|
+
}
|
|
711
|
+
POST_ROUTES = {
|
|
712
|
+
'/api/file': h_save,
|
|
713
|
+
'/api/compile': h_compile,
|
|
714
|
+
'/api/compile/stop': lambda b: stop_compile(),
|
|
715
|
+
'/api/settings': update_settings,
|
|
716
|
+
'/api/replace': h_replace,
|
|
717
|
+
'/api/fs/new': fs_new,
|
|
718
|
+
'/api/fs/rename': fs_rename,
|
|
719
|
+
'/api/fs/delete': fs_delete,
|
|
720
|
+
'/api/fs/upload': fs_upload,
|
|
721
|
+
}
|
|
722
|
+
GIT_OPS = {
|
|
723
|
+
'stage': lambda b: gitapi.stage(ROOT, b.get('paths', [])),
|
|
724
|
+
'unstage': lambda b: gitapi.unstage(ROOT, b.get('paths', [])),
|
|
725
|
+
'commit': lambda b: gitapi.commit(ROOT, b.get('message', ''), bool(b.get('amend'))),
|
|
726
|
+
'push': lambda b: gitapi.push(ROOT),
|
|
727
|
+
'pull': lambda b: gitapi.pull(ROOT, b.get('mode', 'ff')),
|
|
728
|
+
'fetch': lambda b: gitapi.fetch(ROOT),
|
|
729
|
+
'abort': lambda b: gitapi.abort(ROOT),
|
|
730
|
+
'continue': lambda b: gitapi.resume(ROOT),
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
class Handler(BaseHTTPRequestHandler):
|
|
735
|
+
server_version = f'OfflineTeXEditor/{__version__}'
|
|
736
|
+
|
|
737
|
+
def log_message(self, fmt, *a):
|
|
738
|
+
pass
|
|
739
|
+
|
|
740
|
+
def send(self, code, body, ctype='application/json', csp="frame-ancestors 'none'"):
|
|
741
|
+
data = body if isinstance(body, bytes) else (json.dumps(body) if ctype == 'application/json' else body).encode()
|
|
742
|
+
self.send_response(code)
|
|
743
|
+
self.send_header('Content-Type', ctype)
|
|
744
|
+
self.send_header('Content-Length', str(len(data)))
|
|
745
|
+
self.send_header('Cache-Control', 'no-store')
|
|
746
|
+
self.send_header('X-Content-Type-Options', 'nosniff')
|
|
747
|
+
self.send_header('X-Frame-Options', 'DENY') # no clickjacking the Push button from another site
|
|
748
|
+
self.send_header('Content-Security-Policy', csp)
|
|
749
|
+
self.send_header('Referrer-Policy', 'no-referrer')
|
|
750
|
+
self.end_headers()
|
|
751
|
+
self.wfile.write(data)
|
|
752
|
+
|
|
753
|
+
def body(self):
|
|
754
|
+
n = int(self.headers.get('Content-Length', 0))
|
|
755
|
+
if n > MAX_BODY:
|
|
756
|
+
raise UserError(f'request too large (over {MAX_BODY // 2**20} MB)')
|
|
757
|
+
return json.loads(self.rfile.read(n) or b'{}')
|
|
758
|
+
|
|
759
|
+
def refused(self, post):
|
|
760
|
+
"""Why this request must be refused, or None.
|
|
761
|
+
|
|
762
|
+
The server has no login, so it must only answer the editor page itself. A Host header naming another site
|
|
763
|
+
means DNS rebinding; an Origin from another site, or a POST that is not JSON (which a cross-site page can send
|
|
764
|
+
without a CORS preflight), means cross-site request forgery.
|
|
765
|
+
"""
|
|
766
|
+
host = self.headers.get('Host', '')
|
|
767
|
+
if not ANY_HOST and hostname(host) not in ALLOWED_HOSTS:
|
|
768
|
+
return f'unexpected Host header {host!r}'
|
|
769
|
+
if post:
|
|
770
|
+
origin = self.headers.get('Origin')
|
|
771
|
+
if origin is not None and urlsplit(origin).netloc != host:
|
|
772
|
+
return f'cross-origin request from {origin!r} refused'
|
|
773
|
+
if self.headers.get_content_type() != 'application/json':
|
|
774
|
+
return 'Content-Type must be application/json'
|
|
775
|
+
return None
|
|
776
|
+
|
|
777
|
+
def fail(self, e):
|
|
778
|
+
if isinstance(e, PermissionError):
|
|
779
|
+
return self.send(403, {'error': str(e)})
|
|
780
|
+
if isinstance(e, Conflict):
|
|
781
|
+
return self.send(409, e.payload)
|
|
782
|
+
if isinstance(e, KeyError):
|
|
783
|
+
return self.send(400, {'error': f'missing parameter {e}'})
|
|
784
|
+
if isinstance(e, (UserError, gitapi.GitError, ValueError)):
|
|
785
|
+
return self.send(400, {'error': str(e)})
|
|
786
|
+
if isinstance(e, FileNotFoundError):
|
|
787
|
+
return self.send(404, {'error': str(e)})
|
|
788
|
+
if isinstance(e, subprocess.TimeoutExpired):
|
|
789
|
+
return self.send(504, {'error': f'{e.cmd[0]} timed out'})
|
|
790
|
+
return self.send(500, {'error': repr(e)})
|
|
791
|
+
|
|
792
|
+
def do_GET(self):
|
|
793
|
+
why = self.refused(post=False)
|
|
794
|
+
if why:
|
|
795
|
+
return self.send(403, {'error': why})
|
|
796
|
+
u = urlparse(self.path)
|
|
797
|
+
q = {k: v[0] for k, v in parse_qs(u.query).items()}
|
|
798
|
+
try:
|
|
799
|
+
if u.path in ('/', '/index.html'):
|
|
800
|
+
return self.send(200, (HERE / 'index.html').read_bytes(), 'text/html; charset=utf-8', PAGE_CSP)
|
|
801
|
+
if u.path == '/pdf':
|
|
802
|
+
mtime, complete = pdf_info()
|
|
803
|
+
if mtime is None:
|
|
804
|
+
return self.send(404, {'error': 'no PDF yet; compile first'})
|
|
805
|
+
if not complete:
|
|
806
|
+
return self.send(409, {'error': 'the PDF is still being written'})
|
|
807
|
+
return self.send(200, (ROOT / 'build' / f'{JOB}.pdf').read_bytes(), 'application/pdf')
|
|
808
|
+
if u.path == '/api/raw':
|
|
809
|
+
p = asset(q['path'])
|
|
810
|
+
return self.send(200, p.read_bytes(), TYPES.get(p.suffix.lower(), 'application/octet-stream'),
|
|
811
|
+
RAW_CSP)
|
|
812
|
+
route = GET_ROUTES.get(u.path)
|
|
813
|
+
if route:
|
|
814
|
+
return self.send(200, route(q))
|
|
815
|
+
top, _, rest = u.path.lstrip('/').partition('/')
|
|
816
|
+
if top in STATIC and rest:
|
|
817
|
+
p = (STATIC[top] / rest).resolve()
|
|
818
|
+
if STATIC[top] in p.parents and p.is_file():
|
|
819
|
+
ctype = TYPES.get(p.suffix.lower()) or mimetypes.guess_type(p.name)[0] or 'application/octet-stream'
|
|
820
|
+
return self.send(200, p.read_bytes(), ctype)
|
|
821
|
+
return self.send(404, {'error': 'not found'})
|
|
822
|
+
except Exception as e: # report, don't crash the server
|
|
823
|
+
return self.fail(e)
|
|
824
|
+
|
|
825
|
+
def do_POST(self):
|
|
826
|
+
why = self.refused(post=True)
|
|
827
|
+
if why:
|
|
828
|
+
return self.send(403, {'error': why})
|
|
829
|
+
u = urlparse(self.path)
|
|
830
|
+
try:
|
|
831
|
+
b = self.body()
|
|
832
|
+
route = POST_ROUTES.get(u.path)
|
|
833
|
+
if route:
|
|
834
|
+
return self.send(200, route(b))
|
|
835
|
+
if u.path.startswith('/api/git/'):
|
|
836
|
+
op = GIT_OPS.get(u.path.rsplit('/', 1)[1])
|
|
837
|
+
if op is None:
|
|
838
|
+
return self.send(404, {'error': 'unknown git operation'})
|
|
839
|
+
with git_lock:
|
|
840
|
+
return self.send(200, op(b))
|
|
841
|
+
return self.send(404, {'error': 'not found'})
|
|
842
|
+
except Exception as e:
|
|
843
|
+
return self.fail(e)
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
class Server(ThreadingHTTPServer):
|
|
847
|
+
daemon_threads = True
|
|
848
|
+
# On Windows SO_REUSEADDR lets two servers share a port, which would hide that the port is busy.
|
|
849
|
+
allow_reuse_address = os.name != 'nt'
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
def make_server(host, port):
|
|
853
|
+
"""Listen on host:port, or on the first free port from 8765 when port is None."""
|
|
854
|
+
cls = Server
|
|
855
|
+
if ':' in host:
|
|
856
|
+
cls = type('Server6', (Server,), {'address_family': socket.AF_INET6})
|
|
857
|
+
ports = [port] if port else list(range(DEFAULT_PORT, DEFAULT_PORT + 20))
|
|
858
|
+
err = None
|
|
859
|
+
for p in ports:
|
|
860
|
+
try:
|
|
861
|
+
return cls((host, p), Handler)
|
|
862
|
+
except OSError as e:
|
|
863
|
+
err = e
|
|
864
|
+
if port:
|
|
865
|
+
raise SystemExit(f'cannot listen on {host}:{port}: {err.strerror or err}')
|
|
866
|
+
raise SystemExit(f'no free port between {ports[0]} and {ports[-1]} on {host}; pass --port')
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
def main(argv=None):
|
|
870
|
+
ap = argparse.ArgumentParser(prog='offline-tex-editor', description=__doc__,
|
|
871
|
+
formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
872
|
+
ap.add_argument('--root', type=Path, default=Path.cwd(), help='LaTeX project directory (default: current directory)')
|
|
873
|
+
ap.add_argument('--main', help='main .tex file, relative to --root (default: main.tex, or the document found)')
|
|
874
|
+
ap.add_argument('--jobname', help='latexmk job name, i.e. the PDF name in build/ (default: stem of --main)')
|
|
875
|
+
ap.add_argument('--engine', choices=['auto', *project.ENGINES], default='auto',
|
|
876
|
+
help='TeX engine (default auto: "%% !TEX program" in the main file, else the project '
|
|
877
|
+
'latexmkrc, else pdflatex)')
|
|
878
|
+
ap.add_argument('--shell-escape', action='store_true',
|
|
879
|
+
help='let the document run external programs (needed by some packages, e.g. svg); only for '
|
|
880
|
+
'projects you trust')
|
|
881
|
+
ap.add_argument('--timeout', type=int, default=600, help='stop a compile after this many seconds (default 600)')
|
|
882
|
+
ap.add_argument('--host', default='127.0.0.1', help='bind address (default: 127.0.0.1, local only)')
|
|
883
|
+
ap.add_argument('--port', type=int, help=f'port (default: the first free one from {DEFAULT_PORT})')
|
|
884
|
+
ap.add_argument('--open', action='store_true', help='open the editor in the default browser')
|
|
885
|
+
ap.add_argument('--no-initial-compile', action='store_true')
|
|
886
|
+
ap.add_argument('--version', action='version', version=f'%(prog)s {__version__}')
|
|
887
|
+
a = ap.parse_args(argv)
|
|
888
|
+
try:
|
|
889
|
+
configure(a.root, a.main, a.jobname, a.host, a.engine, a.shell_escape, a.timeout)
|
|
890
|
+
except ValueError as e:
|
|
891
|
+
raise SystemExit(str(e))
|
|
892
|
+
missing = [t for t in ('latexmk', 'synctex') if shutil.which(t) is None]
|
|
893
|
+
if missing:
|
|
894
|
+
print(f'warning: {" and ".join(missing)} not found on PATH; install a TeX distribution such as TeX Live',
|
|
895
|
+
flush=True)
|
|
896
|
+
if ANY_HOST:
|
|
897
|
+
print('warning: listening on all interfaces with no authentication; anyone who can reach this port can '
|
|
898
|
+
'edit files and push with your git credentials', flush=True)
|
|
899
|
+
if SHELL_ESCAPE:
|
|
900
|
+
print('warning: --shell-escape lets this project run programs on your computer when it compiles', flush=True)
|
|
901
|
+
srv = make_server(a.host, a.port)
|
|
902
|
+
port = srv.server_address[1]
|
|
903
|
+
shown = 'localhost' if a.host in WILDCARD_HOSTS else (f'[{a.host}]' if ':' in a.host else a.host)
|
|
904
|
+
url = f'http://{shown}:{port}/'
|
|
905
|
+
if not a.no_initial_compile:
|
|
906
|
+
threading.Thread(target=run_compile, daemon=True).start()
|
|
907
|
+
engine, source = effective_engine()
|
|
908
|
+
print(f'Editing {ROOT / MAIN} -> build/{JOB}.pdf ({engine or "engine from latexmkrc"}, {source})\n'
|
|
909
|
+
f'Open {url} (Ctrl+C to stop)', flush=True)
|
|
910
|
+
if a.open:
|
|
911
|
+
threading.Timer(0.3, webbrowser.open, [url]).start()
|
|
912
|
+
try:
|
|
913
|
+
srv.serve_forever()
|
|
914
|
+
except KeyboardInterrupt:
|
|
915
|
+
pass
|
|
916
|
+
finally:
|
|
917
|
+
proc = proc_state['proc']
|
|
918
|
+
if proc is not None:
|
|
919
|
+
kill_tree(proc)
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
if __name__ == '__main__':
|
|
923
|
+
main()
|