forked from adams141/UpNote_Reorganizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpNote_Reorganizer.py
58 lines (41 loc) · 1.64 KB
/
UpNote_Reorganizer.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
import os
import shutil
import frontmatter #pip install python-frontmatter
import re
baseDir = "Notes"
## Pattern is (Files/XXX)
regex = r"(?:[!]\[(?P<caption>.*?)\])\((?P<image>.*?)(?P<description>\".*?\")?\)"
if not os.path.isdir(baseDir):
os.mkdir(baseDir)
for file in os.listdir(os.getcwd()):
if file.endswith(".md"):
note = frontmatter.load(file)
## Copy any images
post_content=note.content
if 'categories' in note:
for c in note['categories']:
directory = c.replace(" / ","/")
dest = os.path.join(baseDir, directory)
if not os.path.isdir(dest):
os.makedirs(dest)
## shutil.copy(file, dest)
else:
dest=baseDir
## shutil.copy(file, baseDir)
print("Processing note "+file)
# Find all file matches
matches = re.finditer(regex, post_content, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
image_path = match.group(2)
# Only local refs
if "Files" in image_path:
from urllib.parse import unquote
converted_path=unquote(image_path)
print("Found image "+converted_path+" in file "+ file+" moving to "+dest)
# Copy to destination
shutil.copy(converted_path, dest)
# Remove file path ref
post_content=post_content.replace("Files/","")
fullpath= os.path.join(dest, file)
outfile = open(fullpath, "w", encoding="utf-8")
outfile.write(post_content)