formatting links, copying files

This commit is contained in:
sejo 2021-06-16 13:18:59 -05:00
parent bbdd816a6e
commit cbb50dcca0
1 changed files with 31 additions and 18 deletions

View File

@ -1,30 +1,43 @@
import re
import os
import shutil
def filename2Wikilink( s ):
s = s.replace('_',' ')
s = s.replace('.gmo','')
return '{'+s+'}'
return '{' + s.replace('_',' ').replace('.gmo','') + '}'
def wikilink2Filename( s ):
return s.strip("{}").replace(' ','_') + '.gmi'
os.chdir('src/')
matches = {}
incoming = {}
for filename in os.listdir():
if filename.endswith('.gmo') and filename != 'pages.gmo' and filename != 'index.gmo':
if filename.endswith('.gmo'):
# copy to tmp
shutil.copy(filename, '../tmp/'+filename)
# convert filename to wikilink
wikilink=filename2Wikilink(filename)
with open(filename) as f:
for line in f:
m=re.search("\{.+\}",line)
if m:
# print(m)
match = m.group(0)
if match not in matches:
matches[match] = set()
matches[match].add(wikilink)
# open file and search for all outgoing links
file = open(filename)
for line in file:
m=re.search("\{.+\}",line)
if m:
match = m.group(0) # get matched string
if match not in incoming: # create a new set for that page
incoming[match] = set()
# add this file
incoming[match].add(wikilink)
file.close()
for k,v in matches.items():
print(k)
print(v)
print('')
os.chdir('../tmp/')
for key,links in incoming.items():
filename = wikilink2Filename(key)
if filename != 'pages.gmo' and filename != 'index.gmo':
print( '## incoming links' )
for link in links:
print('=> {} {}'.format(wikilink2Filename(link), link) )
print('')