This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Lib.py
124 lines (109 loc) · 3.93 KB
/
Lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-
import sublime,os
setlists = {}
def get_default_set():
global setlists
SETTINGS_FILE = "modeCSS.sublime-settings"
settings = sublime.load_settings(SETTINGS_FILE)
setlists["notSel"] = settings.get("notSel","nonce")
setlists["all_in_one"] = bool(settings.get("all_in_one",False))
setlists["remove_semicolon"] = bool(settings.get("remove_semicolon",False))
setlists["delete_comments"] = bool(settings.get("delete_comments",True))
setlists["add_pic_time_suffix"] = bool(settings.get("add_pic_time_suffix",False))
setlists["pic_time_suffix_extension"] = bool(settings.get("pic_time_suffix_extension",False))
setlists["pic_version_str"] = settings.get("pic_version_str","v")
setlists["default_porject_path"] = settings.get("default_porject_path","")
setlists["base64_fold"] = bool(settings.get("base64_fold",True))
return setlists
def cut_region(region,region_begin,region_end):
'''返回region减少前后region后的新区间'''
region = max_point(region)
_a = region_begin.b
_b = region_end.a
return sublime.Region(_a,_b)
def max_point(region):
'''返回整理后的区间,(a,b)且a<b'''
_a = region.a
_b = region.b
if _a >_b:
return sublime.Region(_b,_a)
else:
return sublime.Region(_a,_b)
def expand_to_css_rule(view, cur_point):
'''取得光标所在的样式定义'''
rule = '^\w*[^{}\n]+ ?\{([^{}])*\}'
css_rules = view.find_all(rule)
for css_rule in css_rules:
if css_rule.contains(cur_point):
return css_rule
return cur_point
def build_time_suffix():
'''生成时间缀'''
import time
t = time.time()
t1 = time.localtime(time.time())
return time.strftime("%Y%m%d_%H%M%S", time.localtime())
def expand_to_style(view, cur_point):
'''取得HTML文件中的样式定义'''
rule = '<[\s]*?style[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?style[\s]*?>'
css_rules = view.find_all(rule)
if css_rules:
return css_rules
return cur_point
def get_cur_point(view, region):
'''取得当前行选区区间'''
region = max_point(region)
# 起点坐标
_x = sublime.Region(region.a, region.a)
# 终点坐标
_y = sublime.Region(region.b, region.b)
x = max_point(expand_to_css_rule(view, _x))
y = max_point(expand_to_css_rule(view, _y))
_region = max_point(sublime.Region(x.a, y.b))
# 如果不是CSS内容,则尝试用HTML读取
if region == _region:
x = max_point(expand_to_img(view, _x))
y = max_point(expand_to_img(view, _y))
_region = max_point(sublime.Region(x.a, y.b))
return _region
def expand_to_img(view, img_point):
'''取得HTML文件中的img'''
rule = '<img[^>]+src\s*=\s*[\'\"]([^\'\"]+)[\'\"][^>]*>'
img_rules = view.find_all(rule)
for img_rule in img_rules:
if img_rule.contains(img_point):
return img_rule
return img_point
def get_dis(view):
'''取得文件所在的目录'''
if view.file_name():
path = os.path.normpath(os.path.normcase(view.file_name()))
return os.path.dirname(path)
def point_to_region(point_):
'''转换成区域'''
if len(point_) > 0:
return sublime.Region(int(point_[0]), int(point_[1]))
def get_abs_path(path_a,path_b):
'''将路径a的相对路径,转换为路径b的绝对路径'''
if path_b:
_path_b = os.path.normpath(os.path.normcase(path_b))
if path_a:
_path_a = os.path.normpath(os.path.normcase(path_a))
else:
_path_a = ""
return os.path.join(_path_b,_path_a)
def region_and_str(region,region_,str_):
'''转换区域为字符串'''
r = []
s = []
l = []
for _region in region_:
_s = int(region.a) + int(_region.start())
_e = int(region.a) + int(_region.end())
r.append((_s,_e))
for str in str_:
s.append(str)
for n in range(len(r)):
l.append([r[n],s[n]])
if l:
return l