-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.py
59 lines (34 loc) · 1.24 KB
/
misc.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
48
49
50
51
52
53
54
55
56
57
58
59
import os
import re
import fileinput
def change_file_extension():
dir_ = 'docs/_posts'
post_filenames = os.listdir(dir_)
for filename in post_filenames:
print(filename.replace('.md', '.html'))
file = '/Users/mhemenway/git/bai/intro.md'
def translate_footnotes(filepath):
'''
- find all ^[...] and put in container with brackets stripped
- replace with [^#]
- move to first line break, add break and add leader [^#]:
'''
with open(filepath, mode='r', encoding='utf-8') as f:
text = f.read()
note_pattern = re.compile(r'(\^\[)((\[.*?\])|(.*?))(\])(.*?)(?=\^\[|$)',
flags=re.DOTALL)
notes_w_ref = []
for i, note in enumerate(note_pattern.findall(text)):
# print(note_pattern.findall(text)[0])
ref = '[^{num}]:'.format(num=str(i))
note_text = note[1]
notes_w_ref.append(ref + ' ' + note_text)
new_text = note_pattern.sub(build_ref, text)
return new_text + ('\n\n'.join(notes_w_ref))
def build_ref(s):
if not hasattr(build_ref, 'counter'):
build_ref.counter = 0
ref = '[^{number}]'.format(number=build_ref.counter) + s.group(6)
build_ref.counter += 1
return ref
print(translate_footnotes(file))