forked from linogaliana/python-datascientist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanmd.py
More file actions
45 lines (40 loc) · 1.47 KB
/
cleanmd.py
File metadata and controls
45 lines (40 loc) · 1.47 KB
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
import os
import glob
def cleanblog():
# LIST (R)MARKDOWN FILES ----------------
root_dir = os.getcwd()
os.chdir("./content")
types = ('**/*.Rmd', '**/*.md') # the tuple of file types
files_grabbed = []
for files in types:
files_grabbed.extend(glob.glob(files, recursive=True))
list_files = []
for i in files_grabbed:
if "index.md" not in i:
list_files = list_files + [i]
# APPLY cleanyaml
for i in list_files:
cleanyaml(i, root_dir)
def cleanyaml(filename, root_dir):
# READ MARKDOWN --------------
with open(filename, 'r', encoding='utf-8') as f:
text = f.readlines()
new_text = "".join([line for line in text])
s = new_text
# EXTRACT AND CLEAN HEADER ----------
yaml, text = new_text.split('---\n', 2)[1:]
yaml_jupytext, yaml_rmd = yaml.split('title:')
yaml_rmd_title, yaml_rmd_other = yaml_rmd.split('date:')
new_md = "---\n" + yaml_jupytext.rstrip() + "\n---\n" + \
"# " + yaml_rmd_title.replace('"', '').replace("'", "") + \
"\n" + text
# WRITE IN TEMPORARY LOCATION --------------
write_dest = os.path.join(root_dir, "temp" + os.sep + filename)
tempdir = write_dest.rsplit(os.sep, 1)[0]
if not os.path.exists(tempdir):
os.makedirs(tempdir)
mode = 'a' if os.path.exists(write_dest) else 'w'
with open(write_dest, mode, encoding='utf-8') as f:
f.write(new_md)
print("Done: " + filename)
cleanblog()