/
toMd.py
executable file
·105 lines (76 loc) · 2.37 KB
/
toMd.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
import os
import json
import cson
import shutil
import git
# Paths to necessary directories
# My paths:
# BOOSTNOTE
# GIT
from paths import *
SOURCE = BOOSTNOTE
DEST = GIT
def cson_to_md():
folder_names = get_folders()
original_files = get_all_files()
copy_files(original_files, folder_names)
def get_folders():
# Retrieve folder names from boostnote.json and perform operations
with open(SOURCE + "/boostnote.json") as boostnote:
folders = json.load(boostnote)["folders"]
return get_folder_names(folders)
def get_folder_names(folders):
folder_names = {}
for i in range(0, len(folders)):
folder_names[folders[i]["key"]] = folders[i]["name"]
return folder_names
def get_all_files():
files_path = SOURCE + "/notes"
files = [files_path + "/" + f for f in os.listdir(files_path) if os.path.isfile(files_path + "/" + f)]
return files
def copy_files(files, folders):
for file in files:
details = get_file_dir_and_name(file)
if details:
folder_name = folders[details["folder"]]
name = details["name"]
content = details["content"]
# replace forward slash in path name
name = name.replace('/', '-')
copy_and_rename_file(file, DEST + "/" + folder_name, name, content)
def get_file_dir_and_name(file):
file = open(file, "r")
notes = cson.load(file)
if notes["type"] == "MARKDOWN_NOTE":
directory = {"folder": notes["folder"], "name": notes["title"], "content": notes["content"]}
else:
directory = None
file.close()
return directory
def copy_and_rename_file(src, dest, name, content):
if not os.path.isdir(dest):
os.makedirs(dest)
new_path = shutil.copy(src, dest)
rewrite_file(new_path, content)
new_name = os.path.split(new_path)[0] + '/' + name + ".md"
os.rename(new_path, new_name)
def rewrite_file(file, content):
file = open(file, "w")
for line in content:
file.write(line)
file.close()
def git_push():
try:
repo = git.Repo(DEST)
repo.git.add(u=True)
repo.index.commit("update notes")
origin = repo.remote(name='origin')
origin.push()
except:
print("unable to push")
finally:
print("notes pushed")
if __name__ == "__main__":
cson_to_md()
git_push()