-
Notifications
You must be signed in to change notification settings - Fork 8
/
clean.py
55 lines (47 loc) · 1.84 KB
/
clean.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sublime
import sublime_plugin
import os
import re
from . utils import get_tex_root, search_in_tex
def cleantex(texfile):
settings = sublime.load_settings('LaTeXBox.sublime-settings')
ext = settings.get("clean_ext")
prefix = os.path.splitext(texfile)[0]
for e in ext:
if os.path.isfile(prefix+e):
os.remove(prefix+e)
def cleantexdir(texdir):
settings = sublime.load_settings('LaTeXBox.sublime-settings')
ext = settings.get("clean_ext") + settings.get("clean_ext_force")
ls = os.listdir(texdir)
rexp = "(" + '|'.join(['\\'+e for e in ext]) + ")$"
fnames = [os.path.join(texdir, f) for f in ls if re.search(rexp, f)]
for f in fnames:
os.remove(f)
class LatexBoxCleanCommand(sublime_plugin.WindowCommand):
def run(self, **kwargs):
if "force" in kwargs:
force = kwargs["force"]
else:
force = False
view = self.window.active_view()
tex_root = get_tex_root(view)
tex_dir = os.path.dirname(tex_root)
rexp = r'\\(?:input|include)\{([^\}]*)\}'
results = search_in_tex(rexp, tex_root, recursive=True if force is True else False)
texfiles = [r['result'] for r in results]
texfiles = [f+".tex" if f[-4:].lower() != ".tex" else f for f in texfiles]
texfiles = [tex_root] + [os.path.join(tex_dir, f) for f in texfiles]
if force:
texdirs = list(set([os.path.dirname(f) for f in texfiles]))
for d in texdirs:
if os.path.isdir(d):
cleantexdir(d)
print("Force Clean Build!")
sublime.status_message("Force Clean Build!")
else:
for f in texfiles:
if os.path.isfile(f):
cleantex(f)
print("Clean Build!")
sublime.status_message("Clean Build!")