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,2 @@
1
+ """Offline TeX Editor: a local, Overleaf-style LaTeX editor."""
2
+ __version__ = '0.2.0'
@@ -0,0 +1,3 @@
1
+ from .server import main
2
+
3
+ main()
@@ -0,0 +1,250 @@
1
+ """Git operations for the editor: status, diff, stage/unstage, commit, pull (fast-forward, rebase or merge), conflict
2
+ handling, push, log.
3
+
4
+ Every command runs with an argument list (no shell), with `--` before paths, and with GIT_TERMINAL_PROMPT=0 so a
5
+ missing credential fails instead of waiting for input that never comes; GIT_EDITOR=true does the same for commit
6
+ messages. Paths are relative to the repository top level, use forward slashes, and must stay inside it.
7
+ """
8
+ import os
9
+ import subprocess
10
+ from pathlib import Path
11
+
12
+ ENV = dict(os.environ, GIT_TERMINAL_PROMPT='0', GIT_OPTIONAL_LOCKS='0', LC_ALL='C', GIT_EDITOR='true',
13
+ GIT_MERGE_AUTOEDIT='no')
14
+ MAX_DIFF = 400_000
15
+
16
+
17
+ class GitError(Exception):
18
+ pass
19
+
20
+
21
+ def run(cwd, *args, timeout=60, check=True):
22
+ p = subprocess.run(['git', *args], cwd=cwd, capture_output=True, text=True, encoding='utf-8', errors='replace',
23
+ timeout=timeout, env=ENV)
24
+ if check and p.returncode != 0:
25
+ raise GitError((p.stderr or p.stdout).strip() or f'git {args[0]} failed ({p.returncode})')
26
+ return p
27
+
28
+
29
+ def toplevel(root):
30
+ p = run(root, 'rev-parse', '--show-toplevel', check=False)
31
+ return Path(p.stdout.strip()).resolve() if p.returncode == 0 else None
32
+
33
+
34
+ def repo_top(root):
35
+ top = toplevel(root)
36
+ if top is None:
37
+ raise GitError('not a git repository')
38
+ return top
39
+
40
+
41
+ def inside(top, rel):
42
+ p = (top / rel).resolve()
43
+ if p != top and top not in p.parents:
44
+ raise PermissionError(f'{rel} is outside the repository')
45
+ return p.relative_to(top).as_posix()
46
+
47
+
48
+ def git_path(top, name):
49
+ return top / run(top, 'rev-parse', '--git-path', name).stdout.strip()
50
+
51
+
52
+ def repo_state(top):
53
+ """'rebasing', 'merging' or None."""
54
+ if git_path(top, 'rebase-merge').exists() or git_path(top, 'rebase-apply').exists():
55
+ return 'rebasing'
56
+ if git_path(top, 'MERGE_HEAD').exists():
57
+ return 'merging'
58
+ return None
59
+
60
+
61
+ def parse_status(raw):
62
+ """Parse `git status --porcelain=v1 -z -b` into a branch line and file entries."""
63
+ parts = raw.split('\0')
64
+ branch, files, i = '', [], 0
65
+ while i < len(parts):
66
+ e = parts[i]
67
+ i += 1
68
+ if not e:
69
+ continue
70
+ if e.startswith('## '):
71
+ branch = e[3:]
72
+ continue
73
+ x, y, path = e[0], e[1], e[3:]
74
+ orig = None
75
+ if x in 'RC': # rename/copy: the next field is the original path
76
+ orig = parts[i]
77
+ i += 1
78
+ conflict = 'U' in (x, y) or (x, y) in (('A', 'A'), ('D', 'D'))
79
+ files.append({'path': path, 'orig': orig, 'x': x, 'y': y,
80
+ 'staged': x not in ' ?!' and not conflict, 'unstaged': (y != ' ' or x == '?') and not conflict,
81
+ 'untracked': x == '?', 'conflict': conflict})
82
+ return branch, files
83
+
84
+
85
+ def parse_branch(line):
86
+ """'main...origin/main [ahead 1, behind 2]' -> dict."""
87
+ out = {'branch': line, 'upstream': None, 'ahead': 0, 'behind': 0}
88
+ head, _, track = line.partition(' [')
89
+ if '...' in head:
90
+ out['branch'], out['upstream'] = head.split('...', 1)
91
+ else:
92
+ out['branch'] = head
93
+ for part in track.rstrip(']').split(', '):
94
+ if part.startswith('ahead '):
95
+ out['ahead'] = int(part.split()[1])
96
+ elif part.startswith('behind '):
97
+ out['behind'] = int(part.split()[1])
98
+ if out['branch'].startswith('No commits yet on '):
99
+ out['branch'] = out['branch'][len('No commits yet on '):]
100
+ return out
101
+
102
+
103
+ def status(root):
104
+ top = toplevel(root)
105
+ if top is None:
106
+ return {'repo': False}
107
+ raw = run(top, 'status', '--porcelain=v1', '-z', '-b', '--untracked-files=all').stdout
108
+ branch, files = parse_status(raw)
109
+ remotes = run(top, 'remote', check=False).stdout.split()
110
+ prefix = root.relative_to(top).as_posix() if root != top else ''
111
+ return {'repo': True, 'top': str(top), 'prefix': prefix, 'remotes': remotes, 'state': repo_state(top),
112
+ **parse_branch(branch), 'files': files}
113
+
114
+
115
+ def diff(root, rel, staged=False):
116
+ top = repo_top(root)
117
+ rel = inside(top, rel)
118
+ tracked = run(top, 'ls-files', '--error-unmatch', '--', rel, check=False).returncode == 0
119
+ if staged:
120
+ p = run(top, 'diff', '--cached', '--no-color', '--', rel, check=False)
121
+ elif tracked:
122
+ p = run(top, 'diff', '--no-color', '--', rel, check=False)
123
+ else: # untracked: show the whole file as added
124
+ p = run(top, 'diff', '--no-color', '--no-index', '--', os.devnull, rel, check=False)
125
+ text = p.stdout
126
+ if len(text) > MAX_DIFF:
127
+ text = text[:MAX_DIFF] + '\n… diff truncated …\n'
128
+ return {'path': rel, 'staged': staged, 'diff': text or '(no textual changes)'}
129
+
130
+
131
+ def stage(root, paths):
132
+ top = repo_top(root)
133
+ rels = [inside(top, p) for p in paths]
134
+ if rels:
135
+ run(top, 'add', '-A', '--', *rels)
136
+ return status(root)
137
+
138
+
139
+ def unstage(root, paths):
140
+ top = repo_top(root)
141
+ rels = [inside(top, p) for p in paths]
142
+ has_head = run(top, 'rev-parse', '--verify', 'HEAD', check=False).returncode == 0
143
+ if rels:
144
+ if has_head:
145
+ run(top, 'reset', '-q', 'HEAD', '--', *rels)
146
+ else:
147
+ run(top, 'rm', '--cached', '-q', '-r', '--', *rels)
148
+ return status(root)
149
+
150
+
151
+ def commit(root, message, amend=False):
152
+ top = repo_top(root)
153
+ st = status(root)
154
+ if any(f['conflict'] for f in st['files']):
155
+ raise GitError('resolve the conflicts first (edit the files, then mark them resolved)')
156
+ if not message.strip() and not amend and st['state'] != 'merging':
157
+ raise GitError('empty commit message')
158
+ if not amend and st['state'] != 'merging' and not [f for f in st['files'] if f['staged']]:
159
+ raise GitError('nothing staged to commit')
160
+ args = ['commit', '-q', '-m', message] if message.strip() else ['commit', '-q', '--no-edit']
161
+ if amend:
162
+ args.insert(1, '--amend')
163
+ run(top, *args)
164
+ return {'commit': run(top, 'log', '-1', '--format=%h %s').stdout.strip(), **status(root)}
165
+
166
+
167
+ def push(root):
168
+ top = repo_top(root)
169
+ st = status(root)
170
+ if not st['remotes']:
171
+ raise GitError('no remote configured (git remote add origin <url>)')
172
+ if st['upstream']:
173
+ p = run(top, 'push', timeout=180)
174
+ else:
175
+ p = run(top, 'push', '-u', st['remotes'][0], 'HEAD', timeout=180)
176
+ return {'output': (p.stderr + p.stdout).strip(), **status(root)}
177
+
178
+
179
+ PULL = {'ff': ['pull', '--ff-only'],
180
+ 'rebase': ['pull', '--rebase', '--autostash'],
181
+ 'merge': ['pull', '--no-rebase', '--no-edit', '--autostash']}
182
+
183
+
184
+ def pull(root, mode='ff'):
185
+ """Pull. 'ff' refuses when the histories have diverged (diverged: True in the reply); 'rebase' and 'merge' then
186
+ integrate them, and stop with conflict: True when files conflict."""
187
+ if mode not in PULL:
188
+ raise GitError(f'unknown pull mode {mode!r}')
189
+ top = repo_top(root)
190
+ p = run(top, *PULL[mode], timeout=180, check=False)
191
+ out = (p.stdout + p.stderr).strip()
192
+ st = status(root)
193
+ if p.returncode != 0:
194
+ if st['state'] or any(f['conflict'] for f in st['files']):
195
+ return {'output': out, 'conflict': True, **st}
196
+ if mode == 'ff' and ('fast-forward' in out or 'diverg' in out):
197
+ return {'output': out, 'diverged': True, **st}
198
+ raise GitError(out or f'git pull failed ({p.returncode})')
199
+ return {'output': out, **st}
200
+
201
+
202
+ def abort(root):
203
+ top = repo_top(root)
204
+ state = repo_state(top)
205
+ if state is None:
206
+ raise GitError('no merge or rebase in progress')
207
+ run(top, 'rebase' if state == 'rebasing' else 'merge', '--abort')
208
+ return status(root)
209
+
210
+
211
+ def resume(root):
212
+ """Continue a rebase, or commit a merge, once every conflict is resolved and staged."""
213
+ top = repo_top(root)
214
+ st = status(root)
215
+ if any(f['conflict'] for f in st['files']):
216
+ raise GitError('some files still have conflicts: edit them, then mark them resolved')
217
+ if st['state'] == 'rebasing':
218
+ p = run(top, 'rebase', '--continue', check=False)
219
+ st = status(root)
220
+ if p.returncode != 0 and not st['state']:
221
+ raise GitError((p.stderr or p.stdout).strip())
222
+ return {'output': (p.stdout + p.stderr).strip(), 'conflict': bool(st['state']), **st}
223
+ if st['state'] == 'merging':
224
+ run(top, 'commit', '-q', '--no-edit')
225
+ return status(root)
226
+ raise GitError('no merge or rebase in progress')
227
+
228
+
229
+ def fetch(root):
230
+ top = repo_top(root)
231
+ run(top, 'fetch', '--quiet', timeout=120, check=False)
232
+ return status(root)
233
+
234
+
235
+ def log(root, n=20):
236
+ top = repo_top(root)
237
+ if run(top, 'rev-parse', '--verify', 'HEAD', check=False).returncode != 0:
238
+ return {'commits': []}
239
+ fmt = '%h%x1f%an%x1f%ar%x1f%s'
240
+ out = run(top, 'log', f'-{n}', f'--format={fmt}').stdout
241
+ commits = [dict(zip(('hash', 'author', 'when', 'subject'), l.split('\x1f'))) for l in out.splitlines() if l]
242
+ return {'commits': commits}
243
+
244
+
245
+ def show(root, rev):
246
+ top = repo_top(root)
247
+ if not rev or not all(c.isalnum() for c in rev):
248
+ raise GitError('bad revision')
249
+ text = run(top, 'show', '--no-color', '--stat', '--patch', rev).stdout
250
+ return {'rev': rev, 'diff': text[:MAX_DIFF]}
@@ -0,0 +1,132 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>Offline TeX Editor</title>
7
+ <link rel="stylesheet" href="vendor/codemirror/codemirror.min.css">
8
+ <link rel="stylesheet" href="vendor/codemirror/addon/dialog/dialog.min.css">
9
+ <link rel="stylesheet" href="static/app.css">
10
+ </head>
11
+ <body>
12
+ <header>
13
+ <span class="title">Offline TeX Editor</span>
14
+ <span id="mainDoc" class="muted" title="Main document (change it in ⚙)"></span>
15
+ <span class="spacer"></span>
16
+ <span id="status">loading…</span>
17
+ <label class="chk" title="Recompile after every save"><input type="checkbox" id="auto" checked> Auto-compile</label>
18
+ <button id="saveBtn" title="Ctrl/Cmd+S">Save</button>
19
+ <button id="syncBtn" title="Show the cursor position in the PDF (Ctrl/Cmd+J)">Sync → PDF</button>
20
+ <span id="pageNav" title="Current page (type a number and press Enter to jump)"><input id="pageNum" type="text" inputmode="numeric" value="–" aria-label="Page"> / <span id="pageCount">–</span></span>
21
+ <button id="zoomOut" title="Zoom out">−</button><button id="zoomIn" title="Zoom in">+</button>
22
+ <button id="findBtn" title="Find in the PDF (Ctrl/Cmd+F when the editor is not focused)">Find</button>
23
+ <button id="changesBtn" title="Source changes in the latest compile that changed something">Changes</button>
24
+ <button id="toolsBtn" title="Settings and tools" aria-haspopup="dialog" aria-expanded="false">⚙</button>
25
+ <button id="themeBtn" title="Toggle light/dark">◐</button>
26
+ <button class="danger" id="stopBtn" title="Stop the running compile" hidden>Stop</button>
27
+ <button class="primary" id="compileBtn" title="Ctrl/Cmd+Enter">Recompile</button>
28
+ </header>
29
+
30
+ <div id="tools" class="popover" role="dialog" aria-label="Settings and tools" hidden>
31
+ <label class="field">Main document <select id="mainSelect"></select></label>
32
+ <div class="note" id="engineNote"></div>
33
+ <label class="field">Compile only <select id="onlySelect"></select></label>
34
+ <label class="chk" title="Images are drawn as empty boxes, which makes compiling much faster"><input type="checkbox" id="draftChk"> Fast draft compile (images as boxes)</label>
35
+ <label class="chk"><input type="checkbox" id="spellChk"> Spell check (your browser's dictionary)</label>
36
+ <div class="note" id="shellNote"></div>
37
+ <hr>
38
+ <div class="row"><button id="wcBtn">Count words</button><span class="muted" id="wcNote"></span></div>
39
+ <div id="wcOut"></div>
40
+ <hr>
41
+ <div class="keys">
42
+ <kbd>Ctrl/Cmd+S</kbd><span>Save</span>
43
+ <kbd>Ctrl/Cmd+Enter</kbd><span>Recompile</span>
44
+ <kbd>Ctrl/Cmd+J</kbd><span>Show the cursor position in the PDF</span>
45
+ <kbd>Double-click PDF</kbd><span>Jump to the source</span>
46
+ <kbd>Ctrl+Space</kbd><span>Complete commands, \ref and \cite keys</span>
47
+ <kbd>Ctrl/Cmd+/</kbd><span>Comment or uncomment lines</span>
48
+ <kbd>Ctrl/Cmd+F</kbd><span>Find in the file (or in the PDF)</span>
49
+ <kbd>Ctrl/Cmd+Shift+F</kbd><span>Search all files</span>
50
+ </div>
51
+ </div>
52
+
53
+ <main>
54
+ <aside id="side">
55
+ <div id="sideTabs" role="tablist">
56
+ <button data-tab="files" class="on">Files</button><button data-tab="outline">Outline</button><button data-tab="search">Search</button><button data-tab="git">Git <span id="gitBadge"></span></button>
57
+ </div>
58
+ <div id="filesPane" class="pane">
59
+ <input id="filter" placeholder="Filter files…" autocomplete="off" aria-label="Filter files">
60
+ <div class="row">
61
+ <button id="newFileBtn" title="New file">+ File</button><button id="newFolderBtn" title="New folder">+ Folder</button><button id="uploadBtn" title="Add files (or drop them on the list)">Upload</button>
62
+ <input type="file" id="uploadInput" multiple hidden>
63
+ </div>
64
+ <div id="fsForm" hidden><input id="fsInput" autocomplete="off" spellcheck="false"><div id="fsHint"></div></div>
65
+ <div class="scroll" id="files"></div>
66
+ </div>
67
+ <div id="outlinePane" class="pane" hidden><div class="scroll" id="outline"></div></div>
68
+ <div id="searchPane" class="pane" hidden>
69
+ <input id="searchInput" placeholder="Search all files" autocomplete="off" spellcheck="false" aria-label="Search all files">
70
+ <div class="row toggles">
71
+ <label title="Match case"><input type="checkbox" id="sCase"> Aa</label>
72
+ <label title="Whole words"><input type="checkbox" id="sWord"> Word</label>
73
+ <label title="Regular expression (Python syntax; \1 in the replacement)"><input type="checkbox" id="sRegex"> .*</label>
74
+ </div>
75
+ <input id="replaceInput" placeholder="Replace with" autocomplete="off" spellcheck="false" aria-label="Replace with">
76
+ <div class="row"><button id="replaceAllBtn" disabled>Replace all</button><span id="searchCount"></span></div>
77
+ <div class="scroll" id="searchResults"></div>
78
+ </div>
79
+ <div id="gitPane" class="pane" hidden>
80
+ <div id="gitHead"><span id="gitBranch">…</span><span id="gitSync"></span></div>
81
+ <div id="gitState" hidden></div>
82
+ <div class="gitBtns"><button id="gitRefresh" title="Fetch and refresh">⟳</button><button id="gitPull" title="Pull">Pull</button><button id="gitPush" title="Push to the remote">Push</button></div>
83
+ <textarea id="gitMsg" rows="3" placeholder="Commit message (Ctrl/Cmd+Enter to commit)"></textarea>
84
+ <div class="gitBtns"><button class="primary" id="gitCommit">Commit staged</button><button id="gitStageAll" title="Stage every change">Stage all</button></div>
85
+ <div id="gitMsgOut"></div>
86
+ <div class="scroll"><div id="gitLists"></div><div class="gsec">History</div><div id="gitLog"></div></div>
87
+ </div>
88
+ </aside>
89
+ <section id="editorWrap">
90
+ <div id="tabs" role="tablist" aria-label="Open files"></div>
91
+ <div id="banner"><span id="bannerText"></span><span class="spacer"></span>
92
+ <button id="bannerA"></button><button id="bannerB"></button><button id="bannerC" hidden></button></div>
93
+ <textarea id="ed"></textarea>
94
+ <div id="diffView" hidden>
95
+ <div id="diffHead"><b id="diffTitle"></b><span class="spacer"></span>
96
+ <button id="diffOpen">Open file</button><button id="diffClose" title="Esc">Close</button></div>
97
+ <pre id="diffBody"></pre>
98
+ </div>
99
+ <div id="preview" hidden>
100
+ <div id="previewHead"><b id="previewTitle"></b><span class="spacer"></span><button id="previewClose" title="Esc">Close</button></div>
101
+ <div id="previewBody"></div>
102
+ </div>
103
+ </section>
104
+ <div id="split"></div>
105
+ <section id="pdfPane">
106
+ <div id="pdfFind" hidden role="search">
107
+ <input id="findInput" type="text" placeholder="Find in PDF" autocomplete="off" spellcheck="false" aria-label="Find in PDF">
108
+ <span id="findCount" aria-live="polite"></span>
109
+ <button id="findPrev" title="Previous match (Shift+Enter)">↑</button><button id="findNext" title="Next match (Enter)">↓</button><button id="findClose" title="Close (Esc)">×</button>
110
+ </div>
111
+ <div id="pdfWrap" title="Double-click to jump to the source"><div id="pdf"><div id="pdfEmpty">No PDF yet.</div></div></div>
112
+ </section>
113
+ </main>
114
+ <footer>
115
+ <div class="bar" id="logBar"><span>Logs</span><b class="e" id="nErr">0 errors</b><b class="w" id="nWarn">0 warnings</b>
116
+ <span id="logHint" class="muted">click to expand</span></div>
117
+ <div id="log"></div>
118
+ </footer>
119
+
120
+ <script src="vendor/codemirror/codemirror.min.js"></script>
121
+ <script src="vendor/codemirror/mode/stex/stex.min.js"></script>
122
+ <script src="vendor/codemirror/mode/python/python.min.js"></script>
123
+ <script src="vendor/codemirror/mode/markdown/markdown.min.js"></script>
124
+ <script src="vendor/codemirror/addon/dialog/dialog.min.js"></script>
125
+ <script src="vendor/codemirror/addon/search/searchcursor.min.js"></script>
126
+ <script src="vendor/codemirror/addon/search/search.min.js"></script>
127
+ <script src="vendor/codemirror/addon/search/jump-to-line.min.js"></script>
128
+ <script src="vendor/codemirror/addon/edit/matchbrackets.min.js"></script>
129
+ <script src="vendor/codemirror/addon/selection/active-line.min.js"></script>
130
+ <script type="module" src="static/app.js"></script>
131
+ </body>
132
+ </html>
@@ -0,0 +1,205 @@
1
+ """Reading LaTeX, BibTeX and biber logs: errors and warnings with file and line, plus a plain-language hint for the
2
+ common ones. Pure functions over log text, so they are easy to test.
3
+ """
4
+ import re
5
+ from pathlib import Path, PureWindowsPath
6
+
7
+ # "file:line: message", as written with -file-line-error. The path may start with a Windows drive letter.
8
+ ERR = re.compile(r'^((?:[A-Za-z]:)?[^:\n]*?\.[A-Za-z0-9]{1,8}):(\d+): (.*)$')
9
+ WARN = re.compile(r'(?:LaTeX|Package [\w-]+|Class [\w-]+) Warning: (.*?)(?: on input line (\d+))?\.?$')
10
+ WARN_KEYWORDS = ('undefined', 'Citation', 'Reference', 'multiply', 'could not be found', 'Rerun to get')
11
+
12
+ # (pattern on the message, hint). {0}, {1} are the pattern's groups.
13
+ HINTS = [
14
+ (r'Undefined control sequence',
15
+ 'A command is misspelled, or the package that defines it is not loaded. The unknown command is the last one '
16
+ 'shown on the l.N line of the context.'),
17
+ (r'Missing \$ inserted',
18
+ 'Something that only works in math mode (such as _, ^ or \\alpha) is outside it, or a math environment is not '
19
+ 'closed. Write a literal underscore as \\_.'),
20
+ (r"File `([^']+\.(?:sty|cls))' not found",
21
+ '{0} is not installed. With TeX Live, install it with "tlmgr install <package>"; on Debian or Ubuntu, '
22
+ 'install the texlive-* package that contains it.'),
23
+ (r"File `([^']+)' not found",
24
+ 'LaTeX cannot find {0}. Paths are relative to the main file\'s folder, use forward slashes and are '
25
+ 'case-sensitive.'),
26
+ (r'Runaway argument|Paragraph ended before .* was complete|File ended while scanning',
27
+ 'A closing brace } is missing, often on this line or the one before.'),
28
+ (r"Missing \} inserted|Extra \}, or forgotten|Too many \}'s|Missing \{ inserted",
29
+ 'Braces { } (or $ signs) do not pair up near this line.'),
30
+ (r'\\begin\{([^}]*)\} on input line \d+ ended by \\end\{([^}]*)\}',
31
+ '\\begin{{{0}}} is closed by \\end{{{1}}}: an environment is left open or closed in the wrong order.'),
32
+ (r'Missing \\begin\{document\}',
33
+ 'Something is typeset before \\begin{document}: look for stray text in the preamble.'),
34
+ (r'Misplaced alignment tab character &',
35
+ 'An & outside a table or an align environment. Write a literal ampersand as \\&.'),
36
+ (r"There's no line here to end",
37
+ '\\\\ is used where there is no line to end, for example right after a heading or at the start of a '
38
+ 'paragraph. Use a blank line or \\vspace instead.'),
39
+ (r'requires either XeTeX or LuaTeX',
40
+ 'This document needs XeLaTeX or LuaLaTeX. Put "% !TEX program = xelatex" (or lualatex) on the first line of '
41
+ 'the main file, or start the editor with --engine xelatex.'),
42
+ (r'shell.escape|write18',
43
+ 'A package (such as minted or svg) runs external programs. Restart the editor with --shell-escape if you '
44
+ 'trust this project.'),
45
+ (r'Option clash for package (\S+)',
46
+ 'The package {0} is loaded twice with different options. Load it once, or give the options with '
47
+ '\\PassOptionsToPackage before \\documentclass.'),
48
+ (r'Can be used only in preamble', 'This command must come before \\begin{document}.'),
49
+ (r'Not in outer par mode', 'A figure, table or \\marginpar is inside a box, a minipage or another float.'),
50
+ (r'Dimension too large', 'A length is far too big: often an image scaled by a huge factor, or a TikZ '
51
+ 'coordinate far off.'),
52
+ (r'Illegal unit of measure', 'A length is missing its unit (such as pt, cm or \\linewidth).'),
53
+ (r'Unknown graphics extension|Cannot determine size of graphic',
54
+ 'This engine cannot include that image format. pdflatex takes .pdf, .png and .jpg; convert .eps or .svg '
55
+ 'first (or use the svg package with --shell-escape).'),
56
+ (r'Unicode character|Invalid UTF-8|inputenc Error',
57
+ 'A character pdflatex cannot typeset, often from pasted text. Replace it, or compile with XeLaTeX or '
58
+ 'LuaLaTeX ("% !TEX program = xelatex" on the first line).'),
59
+ (r'Environment (\S+) undefined',
60
+ 'The environment {0} is not defined: load the package that provides it (for example amsmath for align) or '
61
+ 'check the spelling.'),
62
+ (r'Lonely \\item', '\\item is outside a list: put it inside itemize, enumerate or description.'),
63
+ (r"Something's wrong--perhaps a missing \\item",
64
+ 'A list has text before its first \\item, or \\\\ starts a paragraph.'),
65
+ (r'Emergency stop|job aborted',
66
+ 'LaTeX stopped early. Fix the first error in the list; the later ones often follow from it.'),
67
+ # warnings
68
+ (r'Citation .* undefined|could not be found',
69
+ 'The key is not in any .bib file this document uses, or the bibliography has errors (see the BibTeX/biber '
70
+ 'messages).'),
71
+ (r'Reference .* undefined',
72
+ 'No \\label with this name, or it is in a part that is not compiled (\\includeonly).'),
73
+ (r'multiply defined', 'Two \\label commands use the same name.'),
74
+ (r'Rerun to get', 'Cross-references did not settle. Recompile; if this keeps happening, delete the build '
75
+ 'folder.'),
76
+ # bibliography tools
77
+ (r"didn't find a database entry", 'This key is cited but is not in any .bib file the document uses.'),
78
+ (r"couldn't open (?:database|style) file (\S+)", 'BibTeX cannot find {0}: check the name in \\bibliography or '
79
+ '\\bibliographystyle.'),
80
+ (r'I was expecting|syntax error|Repeated entry',
81
+ 'This .bib entry is malformed: check for a missing comma, an unbalanced brace or a duplicate key.'),
82
+ ]
83
+ _HINTS = [(re.compile(p), h) for p, h in HINTS]
84
+
85
+
86
+ def hint(message):
87
+ for rx, text in _HINTS:
88
+ m = rx.search(message)
89
+ if m:
90
+ return text.format(*m.groups())
91
+ return None
92
+
93
+
94
+ def relpath(name, root):
95
+ """A log's file name as a project-relative POSIX path, or (name, True) when it is outside the project."""
96
+ name = name[2:] if name.startswith('./') else name
97
+ if root is None:
98
+ return name, False
99
+ p = Path(name)
100
+ if PureWindowsPath(name).anchor and not p.is_absolute(): # C:/... read on a non-Windows system
101
+ return name, True
102
+ p = (p if p.is_absolute() else root / p).resolve()
103
+ if p == root or root in p.parents:
104
+ return p.relative_to(root).as_posix(), False
105
+ return name, True
106
+
107
+
108
+ BIBITEM = re.compile(r'\\bibitem\s*(?:\[.*\])?\s*\{([^}]+)\}|\\entry\{([^}]+)\}')
109
+
110
+
111
+ def bbl_key(bbl, line):
112
+ """The citation key of the entry at `line` of a .bbl file (BibTeX \\bibitem or biblatex \\entry)."""
113
+ try:
114
+ lines = bbl.read_text(encoding='utf-8', errors='replace').splitlines()[:line]
115
+ except OSError:
116
+ return None
117
+ for text in reversed(lines):
118
+ m = BIBITEM.search(text)
119
+ if m:
120
+ return m.group(1) or m.group(2)
121
+ return None
122
+
123
+
124
+ def parse_log(text, root=None, find_key=None):
125
+ """Errors and warnings from a LaTeX .log. root: the project directory, to make paths relative. find_key(key):
126
+ where a bibliography key is defined, as (file, line), to point errors inside the .bbl at the .bib entry."""
127
+ errors, warnings = [], []
128
+ lines = text.splitlines()
129
+ for i, line in enumerate(lines):
130
+ m = ERR.match(line)
131
+ if m:
132
+ name, ln, msg = m.group(1), int(m.group(2)), m.group(3)
133
+ if msg.lstrip().startswith('==> Fatal error occurred'):
134
+ continue # TeX's summary of the error above
135
+ # Package errors continue on lines that start with "(package)".
136
+ j = i + 1
137
+ while j < len(lines) and re.match(r'\([\w@-]+\)\s', lines[j]):
138
+ msg += ' ' + lines[j].split(')', 1)[1].strip()
139
+ j += 1
140
+ ctx = ' '.join(l.strip() for l in lines[j:j + 2] if l.strip())[:300]
141
+ path, external = relpath(name, root)
142
+ e = {'file': path, 'line': ln, 'message': msg, 'context': ctx, 'hint': hint(msg), 'external': external}
143
+ if path.endswith('.bbl') and root is not None:
144
+ key = bbl_key((root / path) if not external else Path(name), ln)
145
+ where = find_key(key) if key and find_key else None
146
+ e['message'] = f'{msg} (in the bibliography entry {key})' if key else msg
147
+ e['hint'] = e['hint'] or 'The problem is in a bibliography entry: check its fields in the .bib file.'
148
+ if where:
149
+ e.update(file=where[0], line=where[1], external=False)
150
+ errors.append(e)
151
+ continue
152
+ m = WARN.search(line)
153
+ if m and any(k in line for k in WARN_KEYWORDS):
154
+ msg = m.group(1)[:200]
155
+ warnings.append({'message': msg, 'line': int(m.group(2)) if m.group(2) else None, 'hint': hint(msg)})
156
+ seen, uniq = set(), []
157
+ for w in warnings:
158
+ if w['message'] not in seen:
159
+ seen.add(w['message'])
160
+ uniq.append(w)
161
+ return errors, uniq
162
+
163
+
164
+ def parse_blg(text):
165
+ """Errors, warnings and database (.bib) names from a BibTeX or biber .blg log."""
166
+ errors, warnings, databases = [], [], []
167
+ lines = text.splitlines()
168
+
169
+ def add(kind, tool, msg, file=None, line=None, context=''):
170
+ item = {'file': file, 'line': line, 'message': f'{tool}: {msg.strip()}', 'context': context,
171
+ 'hint': hint(msg), 'external': False}
172
+ (errors if kind == 'error' else warnings).append(item)
173
+
174
+ for i, line in enumerate(lines):
175
+ # BibTeX
176
+ m = re.match(r'Database file #\d+: (.+)$', line)
177
+ if m:
178
+ databases.append(m.group(1).strip())
179
+ continue
180
+ m = re.search(r'---line (\d+) of file (.+)$', line)
181
+ if m:
182
+ msg = line[:m.start()].strip() or (lines[i - 1].strip() if i else '')
183
+ ctx = ' '.join(l.strip(' :') for l in lines[i + 1:i + 3] if l.strip(' :'))
184
+ add('error', 'BibTeX', msg, m.group(2).strip(), int(m.group(1)), ctx)
185
+ continue
186
+ m = re.match(r'Warning--(.*)$', line)
187
+ if m:
188
+ n = re.match(r'--line (\d+) of file (.+)$', lines[i + 1]) if i + 1 < len(lines) else None
189
+ add('warning', 'BibTeX', m.group(1), n.group(2).strip() if n else None, int(n.group(1)) if n else None)
190
+ continue
191
+ if re.match(r"I couldn't open (?:database|style|auxiliary) file", line) or line.startswith('I found no '):
192
+ add('error', 'BibTeX', line)
193
+ continue
194
+ # biber
195
+ m = re.search(r"Found BibTeX data source '(.+?)'|Looking for bibtex (?:format )?file '(.+?)'", line)
196
+ if m:
197
+ databases.append(m.group(1) or m.group(2))
198
+ continue
199
+ m = re.search(r'\b(ERROR|WARN) - (.*)$', line)
200
+ if m:
201
+ msg = m.group(2)
202
+ f = re.search(r'([^/\\\s]+\.bib)(?:_\d+\.utf8)?, line (\d+)', msg)
203
+ add('error' if m.group(1) == 'ERROR' else 'warning', 'Biber', msg,
204
+ f.group(1) if f else None, int(f.group(2)) if f else None)
205
+ return errors, warnings, list(dict.fromkeys(databases))