-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_page.py
More file actions
44 lines (37 loc) · 1.57 KB
/
build_page.py
File metadata and controls
44 lines (37 loc) · 1.57 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
import os
import re
import sys
import subprocess
# name of post
try:
name = sys.argv[1]
except IndexError as e:
raise Exception("Make sure you specify the name of the post " \
"you wish to build") from e
# check file exists
assert os.path.exists('content/' + name + '/' + name + '.ipynb'), 'Bad name'
# convert to markdown
p = subprocess.Popen('powershell.exe jupyter nbconvert --to markdown .\\content\\' +
name + '\\' + name + '.ipynb',
stdout=sys.stdout)
p.communicate()
# format markdown
p = subprocess.Popen(['python.exe',
'.\\content\\markdown_formatter.py',
'.\\content\\' + name + '\\' + name + '.md'])
p.communicate()
# move file to source
if os.path.exists('.\\source\\_posts\\' + name + '.md'):
os.remove('.\\source\\_posts\\' + name + '.md')
os.rename('.\\content\\' + name + '\\' + name + '.md',
'.\\source\\_posts\\' + name + '.md')
# move images and delete folder
if os.path.exists('.\\source\\images\\' + name):
for img in os.listdir('.\\source\\images\\' + name):
if re.match(rf'{name}_\d+_\d+\.\w+', img):
os.remove('.\\source\\images\\' + name + '\\' + img)
if os.path.exists('.\\content\\' + name + '\\' + name + '_files'):
for img in os.listdir('.\\content\\' + name + '\\' + name + '_files'):
os.rename('.\\content\\' + name + '\\' + name + '_files\\' + img,
'.\\source\\images\\' + name + '\\' + img)
os.rmdir('.\\content\\' + name + '\\' + name + '_files')