Skip to content

Commit 03b52e4

Browse files
author
ileler
committed
新增“同步链接”的工具脚本
1 parent 00b13b6 commit 03b52e4

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

sync_link

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#! /usr/bin/env python
2+
import re
3+
from shutil import move
4+
from urllib import parse
5+
from pathlib import Path
6+
7+
class REMatcher(object):
8+
def __init__(self, matchstring):
9+
self.matchstring = matchstring
10+
11+
def match(self,regexp):
12+
self.rematch = re.match(regexp, self.matchstring)
13+
return bool(self.rematch)
14+
15+
def group(self,i):
16+
return self.rematch.group(i)
17+
18+
def get_path(name):
19+
try:
20+
files = Path('./').glob('**/%s.md' % name)
21+
for file in files:
22+
return str(file)
23+
except:
24+
pass
25+
return None
26+
27+
prefix = '##### '
28+
filepath = './README.md'
29+
temppath = filepath + '.tmp'
30+
with open(filepath, 'r') as lines:
31+
with open(temppath, 'w') as file:
32+
for line in lines:
33+
m1 = REMatcher(line)
34+
if not m1.match(r'%s(.*)' % prefix):
35+
file.write(line)
36+
continue
37+
name = m1.group(1)
38+
m2 = REMatcher(name)
39+
if m2.match(r'\[(.*)\]\((.*)\)'):
40+
name = m2.group(1)
41+
path = get_path(name)
42+
file.write(('%s[%s](%s)\n' % (prefix, name, parse.quote(path))) if path else line)
43+
move(temppath, filepath)

0 commit comments

Comments
 (0)