File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments