-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_hyper-link.py
47 lines (37 loc) · 1.78 KB
/
auto_hyper-link.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
import re
import glob
from contextlib import suppress
url_base_path = r'https://github.com/requisitos-2019-1/Ribon/blob/master/'
backward_path = 'pós-rastreabilidade/Backward/'
forward_path = 'pós-rastreabilidade/Forward/'
lexicos_path = 'Modelagem de Requisitos/Lexicos/'
title_re = re.compile('(?<=Título: ).*')
sinonimos_re = re.compile('(?<=Sinônimos:).*', re.DOTALL)
def hyper(st, filename):
return '['+st+']('+url_base_path+lexicos_path+filename+')'
def sub(path, current_filename, all_files):
path = path+'*.md'
for filename in glob.iglob(path, recursive=True):
if (filename != current_filename):
f2 = open(filename, 'r+')
content = f2.read()
for sin in all_files:
rg_str = r'(?<![_/[])'+sin+r'((?!(.md))(?!\)))'
# >>> la = re.compile('(?<![_/])word((?!(.md))(?!\)))', re.IGNORECASE)
rgx = re.compile(rg_str, re.IGNORECASE)
content = re.sub(rgx, hyper(sin, current_filename.split('/')[1]), content)
f2.seek(0)
f2.truncate()
f2.write(content)
f2.close()
for current_filename in glob.iglob(lexicos_path+'*.md', recursive=True):
if not re.search('Descontinuado', current_filename):
print(current_filename)
f = open(current_filename).read()
title = title_re.findall(f)[0]
with suppress(Exception):
sinonimos = sinonimos_re.findall(f)[0].replace('\n', '').replace('.', '').split('- ')
sinonimos = list(filter(None, sinonimos))
all_files = [title] + sinonimos
sub(forward_path, current_filename, all_files)
sub(backward_path, current_filename, all_files)