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.
Files changed (44) hide show
  1. offline_tex_editor/__init__.py +2 -0
  2. offline_tex_editor/__main__.py +3 -0
  3. offline_tex_editor/gitapi.py +250 -0
  4. offline_tex_editor/index.html +132 -0
  5. offline_tex_editor/latexlog.py +205 -0
  6. offline_tex_editor/project.py +403 -0
  7. offline_tex_editor/server.py +923 -0
  8. offline_tex_editor/static/app.css +231 -0
  9. offline_tex_editor/static/app.js +72 -0
  10. offline_tex_editor/static/compile.js +83 -0
  11. offline_tex_editor/static/complete.js +159 -0
  12. offline_tex_editor/static/diffview.js +72 -0
  13. offline_tex_editor/static/editor.js +230 -0
  14. offline_tex_editor/static/files.js +181 -0
  15. offline_tex_editor/static/git.js +120 -0
  16. offline_tex_editor/static/package.json +5 -0
  17. offline_tex_editor/static/panels.js +83 -0
  18. offline_tex_editor/static/pdfview.js +254 -0
  19. offline_tex_editor/static/textindex.js +45 -0
  20. offline_tex_editor/static/tools.js +72 -0
  21. offline_tex_editor/static/util.js +57 -0
  22. offline_tex_editor/vendor/README.md +29 -0
  23. offline_tex_editor/vendor/codemirror/LICENSE +21 -0
  24. offline_tex_editor/vendor/codemirror/addon/dialog/dialog.min.css +1 -0
  25. offline_tex_editor/vendor/codemirror/addon/dialog/dialog.min.js +1 -0
  26. offline_tex_editor/vendor/codemirror/addon/edit/matchbrackets.min.js +1 -0
  27. offline_tex_editor/vendor/codemirror/addon/search/jump-to-line.min.js +1 -0
  28. offline_tex_editor/vendor/codemirror/addon/search/search.min.js +1 -0
  29. offline_tex_editor/vendor/codemirror/addon/search/searchcursor.min.js +1 -0
  30. offline_tex_editor/vendor/codemirror/addon/selection/active-line.min.js +1 -0
  31. offline_tex_editor/vendor/codemirror/codemirror.min.css +1 -0
  32. offline_tex_editor/vendor/codemirror/codemirror.min.js +1 -0
  33. offline_tex_editor/vendor/codemirror/mode/markdown/markdown.min.js +1 -0
  34. offline_tex_editor/vendor/codemirror/mode/python/python.min.js +1 -0
  35. offline_tex_editor/vendor/codemirror/mode/stex/stex.min.js +1 -0
  36. offline_tex_editor/vendor/pdfjs/LICENSE +177 -0
  37. offline_tex_editor/vendor/pdfjs/pdf.min.mjs +21 -0
  38. offline_tex_editor/vendor/pdfjs/pdf.worker.min.mjs +21 -0
  39. offline_tex_editor-0.2.0.dist-info/METADATA +193 -0
  40. offline_tex_editor-0.2.0.dist-info/RECORD +44 -0
  41. offline_tex_editor-0.2.0.dist-info/WHEEL +5 -0
  42. offline_tex_editor-0.2.0.dist-info/entry_points.txt +2 -0
  43. offline_tex_editor-0.2.0.dist-info/licenses/LICENSE +21 -0
  44. offline_tex_editor-0.2.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,403 @@
1
+ """Reading a LaTeX project's sources: files, root documents, magic comments, the outline, labels, bibliography keys
2
+ and user commands, and project-wide search and replace. These functions take the project directory and trust the
3
+ paths they are given; the server checks paths before calling them.
4
+ """
5
+ import os
6
+ import re
7
+ from pathlib import Path
8
+
9
+ EDITABLE = {'.tex', '.bib', '.sty', '.cls', '.bst', '.md', '.txt', '.csv', '.py', '.sh', '.tikz', '.bbx', '.cbx',
10
+ '.lbx', '.dtx', '.ins', '.ltx', '.cfg', '.def', '.clo'}
11
+ EDITABLE_NAMES = {'.latexmkrc', 'latexmkrc'}
12
+ ASSETS = {'.png', '.jpg', '.jpeg', '.gif', '.pdf', '.eps', '.svg', '.webp', '.tif', '.tiff', '.bmp'}
13
+ SKIP_DIRS = {'build', '.git', '.venv', 'venv', '__pycache__', 'node_modules', '.editor-trash'}
14
+ ENGINES = ('pdflatex', 'xelatex', 'lualatex')
15
+ ENGINE_ALIASES = {'pdftex': 'pdflatex', 'latex': 'pdflatex', 'xetex': 'xelatex', 'luatex': 'lualatex',
16
+ 'xelatexmk': 'xelatex', 'lualatexmk': 'lualatex', 'pdflatexmk': 'pdflatex'}
17
+ MAX_SCAN = 4 * 1024 * 1024 # skip larger files when scanning or searching
18
+
19
+
20
+ def is_editable(p):
21
+ return p.suffix.lower() in EDITABLE or p.name in EDITABLE_NAMES
22
+
23
+
24
+ def skipped(rel):
25
+ return any(part in SKIP_DIRS for part in Path(rel).parts)
26
+
27
+
28
+ def _walk(root):
29
+ """(folder, its subfolders, its files) like os.walk, never entering build/, .git/, node_modules/ and the like."""
30
+ for d, dirs, files in os.walk(root):
31
+ dirs[:] = sorted(x for x in dirs if x not in SKIP_DIRS)
32
+ yield Path(d), dirs, sorted(files)
33
+
34
+
35
+ def walk(root):
36
+ """(path, POSIX path relative to root) of every project file."""
37
+ for d, _, files in _walk(root):
38
+ for f in files:
39
+ p = d / f
40
+ yield p, p.relative_to(root).as_posix()
41
+
42
+
43
+ def list_dirs(root):
44
+ """Every folder (also empty ones) as POSIX paths relative to root, skipping build/, .git/ and the like."""
45
+ return [(d / x).relative_to(root).as_posix() for d, dirs, _ in _walk(root) for x in dirs]
46
+
47
+
48
+ def list_files(root):
49
+ """(editable sources, assets such as images), as POSIX paths relative to root."""
50
+ files, assets = [], []
51
+ for p, rel in walk(root):
52
+ if is_editable(p):
53
+ files.append(rel)
54
+ elif p.suffix.lower() in ASSETS:
55
+ assets.append(rel)
56
+ return files, assets
57
+
58
+
59
+ # ---------- reading and writing sources ----------
60
+
61
+ def read_source(p):
62
+ """(text, encoding, end of line). UTF-8 when the bytes are valid UTF-8, else Latin-1 (which never fails), so a
63
+ file is saved back in the encoding it came in. Line endings are kept as they are."""
64
+ raw = p.read_bytes()
65
+ try:
66
+ text, enc = raw.decode('utf-8'), 'utf-8'
67
+ except UnicodeDecodeError:
68
+ text, enc = raw.decode('latin-1'), 'latin-1'
69
+ return text, enc, '\r\n' if '\r\n' in text else '\n'
70
+
71
+
72
+ def write_source(p, text, encoding='utf-8'):
73
+ p.write_bytes(text.encode(encoding if encoding in ('utf-8', 'latin-1') else 'utf-8'))
74
+
75
+
76
+ def read_text(p, limit=MAX_SCAN):
77
+ """For scanning only: lenient decoding, universal newlines, at most `limit` bytes."""
78
+ with open(p, 'rb') as f:
79
+ return f.read(limit).decode('utf-8', errors='replace').replace('\r\n', '\n').replace('\r', '\n')
80
+
81
+
82
+ # ---------- LaTeX structure ----------
83
+
84
+ COMMENT = re.compile(r'(?<!\\)%.*')
85
+ MAGIC = re.compile(r'^\s*%\s*!\s*tex\s+(ts-program|program|root|spellcheck|encoding)\s*=\s*(.+?)\s*$', re.I | re.M)
86
+ DOCCLASS = re.compile(r'\\documentclass\s*(?:\[[^\]]*\])?\s*\{([^}]*)\}')
87
+
88
+
89
+ def strip_comment(line):
90
+ return COMMENT.sub('', line)
91
+
92
+
93
+ def magic_comments(text):
94
+ """% !TEX key = value comments in the first 50 lines, keys lower-cased."""
95
+ head = '\n'.join(text.splitlines()[:50])
96
+ return {k.lower(): v for k, v in MAGIC.findall(head)}
97
+
98
+
99
+ def doc_class(text):
100
+ """The document class, or None if the file is not a root document."""
101
+ for line in text.splitlines()[:300]:
102
+ m = DOCCLASS.search(strip_comment(line))
103
+ if m:
104
+ return m.group(1).strip()
105
+ return None
106
+
107
+
108
+ def is_root(text):
109
+ return doc_class(text) not in (None, 'subfiles')
110
+
111
+
112
+ def engine_from_magic(text):
113
+ prog = magic_comments(text).get('program') or magic_comments(text).get('ts-program')
114
+ if not prog:
115
+ return None
116
+ prog = prog.strip().lower()
117
+ prog = ENGINE_ALIASES.get(prog, prog)
118
+ return prog if prog in ENGINES else None
119
+
120
+
121
+ def rc_sets_pdf_mode(root):
122
+ """Whether the project's latexmkrc chooses the engine itself ($pdf_mode)."""
123
+ for name in ('.latexmkrc', 'latexmkrc'):
124
+ p = root / name
125
+ if p.is_file() and re.search(r'^[^#\n]*\$pdf_mode\s*=', read_text(p), re.M):
126
+ return True
127
+ return False
128
+
129
+
130
+ NAME_RANK = ['main', 'thesis', 'paper', 'report', 'article', 'manuscript', 'book', 'document', 'root']
131
+
132
+
133
+ def find_roots(root, files):
134
+ """Root documents (with \\documentclass, not subfiles), best candidates first: top-level files, then common
135
+ names, then shallow paths."""
136
+ found = []
137
+ for rel in files:
138
+ if rel.endswith('.tex'):
139
+ try:
140
+ cls = doc_class(read_text(root / rel, 65536))
141
+ except OSError:
142
+ continue
143
+ if cls not in (None, 'subfiles'):
144
+ stem = Path(rel).stem.lower()
145
+ found.append(((rel.count('/'), cls == 'standalone',
146
+ NAME_RANK.index(stem) if stem in NAME_RANK else len(NAME_RANK), rel), rel))
147
+ return [rel for _, rel in sorted(found)]
148
+
149
+
150
+ def resolve_main(root, main=None):
151
+ """The main file, relative to root (POSIX). Follows "% !TEX root = ..." comments; without `main`, uses main.tex
152
+ or the best root document. Raises ValueError with a message for the user."""
153
+ if main is None:
154
+ if (root / 'main.tex').is_file():
155
+ main = 'main.tex'
156
+ else:
157
+ roots = find_roots(root, list_files(root)[0])
158
+ if not roots:
159
+ raise ValueError(f'no LaTeX document (a .tex file with \\documentclass) in {root}; pass --main')
160
+ main = roots[0]
161
+ p, name = (root / main).resolve(), main
162
+ for _ in range(5):
163
+ if root not in p.parents:
164
+ raise ValueError(f'{name} is outside {root}')
165
+ if not p.is_file():
166
+ raise ValueError(f'no {name} in {root}')
167
+ target = magic_comments(read_text(p, 65536)).get('root')
168
+ if not target:
169
+ break
170
+ # "% !TEX root" is relative to the file that contains it
171
+ p, name = (p.parent / target).resolve(), target
172
+ return p.relative_to(root).as_posix()
173
+
174
+
175
+ def braced(text, start):
176
+ """The contents of the {...} group that opens at text[start - 1]."""
177
+ depth, i = 1, start
178
+ while i < len(text):
179
+ c = text[i]
180
+ if c == '\\':
181
+ i += 2
182
+ continue
183
+ if c == '{':
184
+ depth += 1
185
+ elif c == '}':
186
+ depth -= 1
187
+ if depth == 0:
188
+ return text[start:i]
189
+ i += 1
190
+ return text[start:]
191
+
192
+
193
+ SECTION = re.compile(r'\\(part|chapter|section|subsection|subsubsection|paragraph)\*?\s*(?:\[[^\]]*\]\s*)?\{')
194
+ LEVEL = {'part': 0, 'chapter': 1, 'section': 2, 'subsection': 3, 'subsubsection': 4, 'paragraph': 5}
195
+ INCLUDE = re.compile(r'\\(input|include|subfile|import|subimport|inputfrom|includefrom|subinputfrom|subincludefrom)'
196
+ r'\*?\s*\{([^}]*)\}(?:\s*\{([^}]*)\})?')
197
+
198
+
199
+ def resolve_input(root, here, cmd, a, b):
200
+ """The project file an \\input-like command reads, relative to root, or None."""
201
+ if cmd in ('import', 'inputfrom', 'includefrom'):
202
+ bases, name = [root / a.strip()], b
203
+ elif cmd in ('subimport', 'subinputfrom', 'subincludefrom'):
204
+ bases, name = [here / a.strip()], b
205
+ else: # \input and \include are relative to the main file's folder; also try the including file's folder
206
+ bases, name = [root, here], a
207
+ if name is None:
208
+ return None
209
+ name = name.strip().strip('"')
210
+ for base in bases:
211
+ for cand in (base / name, base / (name + '.tex')):
212
+ try:
213
+ p = cand.resolve()
214
+ except OSError:
215
+ continue
216
+ if p.is_file() and (p == root or root in p.parents):
217
+ return p.relative_to(root).as_posix()
218
+ return None
219
+
220
+
221
+ def outline(root, main):
222
+ """Sectioning commands in document order, following \\input, \\include, \\subfile and \\import from the main
223
+ file: ({'level', 'title', 'file', 'line'} list, list of \\include arguments, as written)."""
224
+ entries, includes, seen = [], [], set()
225
+
226
+ def visit(rel, depth):
227
+ if rel in seen or depth > 20:
228
+ return
229
+ seen.add(rel)
230
+ try:
231
+ text = read_text(root / rel)
232
+ except OSError:
233
+ return
234
+ here = (root / rel).parent
235
+ for n, raw in enumerate(text.splitlines(), 1):
236
+ line = strip_comment(raw)
237
+ if '\\' not in line:
238
+ continue
239
+ if depth == 0 and '\\end{document}' in line:
240
+ break
241
+ events = [(m.start(), 's', m) for m in SECTION.finditer(line)] + \
242
+ [(m.start(), 'i', m) for m in INCLUDE.finditer(line)]
243
+ for _, kind, m in sorted(events, key=lambda e: e[0]):
244
+ if kind == 's':
245
+ title = ' '.join(braced(line, m.end()).split())
246
+ entries.append({'level': LEVEL[m.group(1)], 'title': title, 'file': rel, 'line': n})
247
+ else:
248
+ if m.group(1) == 'include':
249
+ includes.append(m.group(2).strip())
250
+ target = resolve_input(root, here, m.group(1), m.group(2), m.group(3))
251
+ if target and target.endswith('.tex'):
252
+ visit(target, depth + 1)
253
+
254
+ visit(main, 0)
255
+ return entries, list(dict.fromkeys(includes))
256
+
257
+
258
+ LABEL = re.compile(r'\\label\s*\{([^}]+)\}')
259
+ NEWCMD = re.compile(r'\\(?:re|provide)?newcommand\*?\s*\{?\s*\\([A-Za-z@]+)\s*\}?\s*(?:\[(\d)\])?'
260
+ r'|\\DeclareMathOperator\*?\s*\{?\s*\\([A-Za-z@]+)'
261
+ r'|\\def\s*\\([A-Za-z@]+)'
262
+ r'|\\(?:re)?newenvironment\*?\s*\{([A-Za-z@*]+)\}')
263
+
264
+
265
+ def labels(root, files):
266
+ """\\label keys with where they are and a short context (the line, or the line before)."""
267
+ out = []
268
+ for rel in files:
269
+ if not rel.endswith('.tex'):
270
+ continue
271
+ try:
272
+ lines = read_text(root / rel).splitlines()
273
+ except OSError:
274
+ continue
275
+ for n, raw in enumerate(lines, 1):
276
+ line = strip_comment(raw)
277
+ for m in LABEL.finditer(line):
278
+ ctx = (line[:m.start()] + line[m.end():]).strip()
279
+ if not ctx and n > 1:
280
+ ctx = strip_comment(lines[n - 2]).strip()
281
+ out.append({'key': m.group(1).strip(), 'file': rel, 'line': n, 'context': ctx[:100]})
282
+ return out
283
+
284
+
285
+ def commands(root, files):
286
+ """Commands and environments the project defines (\\newcommand, \\DeclareMathOperator, \\def,
287
+ \\newenvironment)."""
288
+ out = {}
289
+ for rel in files:
290
+ if not rel.endswith(('.tex', '.sty', '.cls')):
291
+ continue
292
+ try:
293
+ text = read_text(root / rel)
294
+ except OSError:
295
+ continue
296
+ for n, raw in enumerate(text.splitlines(), 1):
297
+ for m in NEWCMD.finditer(strip_comment(raw)):
298
+ if m.group(5):
299
+ out.setdefault('env:' + m.group(5), {'name': m.group(5), 'env': True, 'args': 0, 'file': rel,
300
+ 'line': n})
301
+ else:
302
+ name = m.group(1) or m.group(3) or m.group(4)
303
+ out.setdefault(name, {'name': name, 'env': False, 'args': int(m.group(2) or 0), 'file': rel,
304
+ 'line': n})
305
+ return list(out.values())
306
+
307
+
308
+ BIBENTRY = re.compile(r'@\s*(\w+)\s*[{(]\s*([^,\s{}()]+)\s*,')
309
+
310
+
311
+ def bib_field(entry, name):
312
+ m = re.search(r'(?<![\w-])' + name + r'\s*=\s*', entry, re.I)
313
+ if not m:
314
+ return ''
315
+ i = m.end()
316
+ if i < len(entry) and entry[i] == '{':
317
+ value = braced(entry, i + 1)
318
+ elif i < len(entry) and entry[i] == '"':
319
+ j = entry.find('"', i + 1)
320
+ value = entry[i + 1:j if j > 0 else len(entry)]
321
+ else:
322
+ value = re.match(r'[^,}\n]*', entry[i:]).group(0)
323
+ return ' '.join(value.replace('{', '').replace('}', '').split())
324
+
325
+
326
+ def bib_entries(root, files):
327
+ """Entries of the project's .bib files: key, type, title, author, year, file and line."""
328
+ out = []
329
+ for rel in files:
330
+ if not rel.endswith('.bib'):
331
+ continue
332
+ try:
333
+ text = read_text(root / rel)
334
+ except OSError:
335
+ continue
336
+ starts = list(BIBENTRY.finditer(text))
337
+ for i, m in enumerate(starts):
338
+ kind = m.group(1).lower()
339
+ if kind in ('string', 'comment', 'preamble'):
340
+ continue
341
+ body = text[m.end():starts[i + 1].start() if i + 1 < len(starts) else len(text)]
342
+ out.append({'key': m.group(2), 'type': kind, 'title': bib_field(body, 'title')[:160],
343
+ 'author': bib_field(body, 'author')[:120], 'year': bib_field(body, 'year')[:10],
344
+ 'file': rel, 'line': text.count('\n', 0, m.start()) + 1})
345
+ return out
346
+
347
+
348
+ # ---------- search and replace ----------
349
+
350
+ def compile_query(q, regex=False, case=False, word=False):
351
+ """A compiled pattern for a search; raises re.error for a bad regular expression."""
352
+ pattern = q if regex else re.escape(q)
353
+ if word:
354
+ pattern = r'(?<!\w)(?:' + pattern + r')(?!\w)'
355
+ return re.compile(pattern, 0 if case else re.IGNORECASE)
356
+
357
+
358
+ def search(root, files, rx, limit=2000):
359
+ """Matches line by line: [{'path', 'line', 'col', 'len', 'text'}], and whether the limit cut the list short."""
360
+ out = []
361
+ for rel in files:
362
+ p = root / rel
363
+ try:
364
+ if p.stat().st_size > MAX_SCAN:
365
+ continue
366
+ lines = read_text(p).split('\n')
367
+ except OSError:
368
+ continue
369
+ for n, line in enumerate(lines, 1):
370
+ for m in rx.finditer(line):
371
+ if m.end() == m.start():
372
+ continue
373
+ out.append({'path': rel, 'line': n, 'col': m.start(), 'len': m.end() - m.start(),
374
+ 'text': line[:400]})
375
+ if len(out) >= limit:
376
+ return out, True
377
+ return out, False
378
+
379
+
380
+ def replace(root, files, rx, replacement, regex=False):
381
+ """Replace every match, line by line, keeping each file's encoding and line endings. Returns {path: count}.
382
+ With regex, the replacement can use \\1 or \\g<name>; otherwise it is literal."""
383
+ repl = replacement if regex else (lambda _m: replacement)
384
+ changed = [] # every file is rewritten in memory first, so a bad replacement template changes nothing
385
+ for rel in files:
386
+ p = root / rel
387
+ try:
388
+ if p.stat().st_size > MAX_SCAN:
389
+ continue
390
+ text, enc, _ = read_source(p)
391
+ except OSError:
392
+ continue
393
+ total, out = 0, []
394
+ for line in text.splitlines(keepends=True):
395
+ body = line.rstrip('\r\n')
396
+ new, n = rx.subn(repl, body)
397
+ total += n
398
+ out.append(new + line[len(body):])
399
+ if total: # encoding can fail too (a Latin-1 file cannot hold every character), so do it before writing
400
+ changed.append((rel, p, ''.join(out).encode(enc), total))
401
+ for _, p, data, _ in changed:
402
+ p.write_bytes(data)
403
+ return {rel: n for rel, _, _, n in changed}