9
9
10
10
class MoveEventHandler (FileSystemEventHandler ):
11
11
def on_moved (self , event ):
12
+ if '.git' in event .src_path or '.git' in event .dest_path :
13
+ return
14
+
12
15
if event .is_directory :
13
16
print (f"Directory moved from { event .src_path } to { event .dest_path } " )
14
17
else :
15
18
print (f"File moved from { event .src_path } to { event .dest_path } " )
16
19
17
20
repo_path = os .getcwd ()
18
-
21
+
19
22
source = get_redirect (event .src_path , repo_path )
20
23
destination = get_redirect (event .dest_path , repo_path )
21
24
22
- print (source )
23
- print (destination )
25
+ print ("from_r:" , source )
26
+ print ("to_r:" , destination )
24
27
25
28
# path_r = destination.split("/")[1]
29
+ json_file = f"./redirects/redirects.json"
26
30
file_extensions = ['.mdx' , '.md' ]
27
31
28
32
replace_in_repo (repo_path , source , destination , file_extensions )
29
- redirect (source , destination )
33
+ redirect (json_file , source , destination )
30
34
31
35
32
36
def start_monitoring (path ):
@@ -42,13 +46,13 @@ def start_monitoring(path):
42
46
time .sleep (1 )
43
47
except KeyboardInterrupt :
44
48
observer .stop ()
45
-
49
+
46
50
observer .join ()
47
51
48
52
def find_markdown_links (text ):
49
53
md_link_pattern = r'\[([^\]]+)\]\(([^#)]+)([^)]*)\)'
50
54
matches = re .findall (md_link_pattern , text )
51
-
55
+
52
56
return matches
53
57
54
58
def replace_word_in_file (file_path , target , replacement ):
@@ -70,53 +74,57 @@ def replace_word_in_file(file_path, target, replacement):
70
74
71
75
def replace_in_repo (repo_path , target , replacement , file_extensions = None ):
72
76
for root , dirs , files in os .walk (repo_path ):
73
- print (root , repo_path )
74
- # if root == repo_path:
75
- # for file_name in files:
76
- # if file_name == "navbar.js" or file_name == "redirects.js":
77
- # file_path = os.path.join(root, file_name)
78
- # replace_word_in_file(file_path, target, replacement)
79
- # continue
80
-
81
- # path_list = root.split(repo_path)
82
- # if len(path_list) > 1 and path_list[1] == "/sidebars":
83
- # for file_name in files:
84
- # file_path = os.path.join(root, file_name)
85
- # replace_word_in_file(file_path, target, replacement)
86
- # continue
87
-
88
- # for file_name in files:
89
- # if file_extensions:
90
- # if not any(file_name.endswith(ext) for ext in file_extensions):
91
- # continue
92
-
93
- # if root.find("node_modules") != -1:
94
- # continue
95
-
96
- # file_path = os.path.join(root, file_name)
97
- # replace_word_in_file(file_path, target, replacement)
77
+ if '.git' in root or 'i18n' in root :
78
+ continue
79
+
80
+ if root == repo_path :
81
+ for file_name in files :
82
+ if file_name == "navbar.js" or file_name == "redirects.js" :
83
+ file_path = os .path .join (root , file_name )
84
+ replace_word_in_file (file_path , target , replacement )
85
+ continue
86
+
87
+ path_list = root .split (repo_path )
88
+ if len (path_list ) > 1 and path_list [1 ] == "/sidebars" :
89
+ for file_name in files :
90
+ file_path = os .path .join (root , file_name )
91
+ replace_word_in_file (file_path , target , replacement )
92
+ continue
93
+
94
+ for file_name in files :
95
+ if file_extensions :
96
+ if not any (file_name .endswith (ext ) for ext in file_extensions ):
97
+ continue
98
+
99
+ if root .find ("node_modules" ) != - 1 :
100
+ continue
101
+
102
+ file_path = os .path .join (root , file_name )
103
+ replace_word_in_file (file_path , target , replacement )
98
104
99
105
def get_redirect (path , repo ):
100
- res = os .path .splitext (path )[0 ]
106
+ res = os .path .splitext (path )
107
+ res = res [0 ]
101
108
spl = res .split (repo )
102
- res = spl [1 ].split ('/docs' )[1 ]
109
+ res = spl [1 ].split ('/docs' , 1 )
110
+ res = res [1 ]
103
111
return res
104
112
105
- def redirect (from_r , to_r ):
113
+ def redirect (json_file , from_r , to_r ):
106
114
obj = {
107
115
"from" : from_r ,
108
116
"to" : to_r
109
117
}
110
118
111
- # with open(json_file, 'r') as file:
112
- # data = json.load(file)
113
-
114
- redirect_data .append (obj )
119
+ with open (json_file , 'r' ) as file :
120
+ data = json .load (file )
121
+
122
+ data .append (obj )
123
+
124
+ with open (json_file , 'w' ) as file :
125
+ json .dump (data , file , indent = 4 )
115
126
116
- # with open(json_file, 'w') as file:
117
- # json.dump(obj, file, indent=4)
118
127
119
-
120
128
if __name__ == "__main__" :
121
129
repo_path = os .getcwd ()
122
130
@@ -131,10 +139,6 @@ def redirect(from_r, to_r):
131
139
while True :
132
140
time .sleep (1 )
133
141
except KeyboardInterrupt :
134
- print ("STOPPED" )
135
- json_file = f"./redirects/redirects.json"
136
- with open (json_file , 'w' ) as file :
137
- json .dump (redirect_data , file , indent = 4 )
138
142
observer .stop ()
139
143
140
144
observer .join ()
0 commit comments