forked from barryclark/jekyll-now
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.sh
More file actions
39 lines (30 loc) · 1.15 KB
/
convert.sh
File metadata and controls
39 lines (30 loc) · 1.15 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
#!/bin/bash
#
# Convert Jupyter notebook files (in _jupyter folder) to markdown files (in _drafts folder).
#
# Arguments:
# $1 filename (excluding extension)
#
# Generate a filename with today's date.
filename=$(date +%Y-%m-%d)-$1
# Jupyter will put all the assets associated with the notebook in a folder with this naming convention.
foldername=$filename"_files"
# Do the conversion.
jupyter nbconvert ./_jupyter/$1.ipynb --to markdown --output-dir=./_posts --output=$filename
# Move the images.
echo "Moving images..."
mv ./_posts/$foldername/* ./images
# Remove the now empty folder.
rmdir ./_posts/$foldername
# Go through the markdown file and rewrite image paths.
echo "Rewriting image paths..."
sed -i.tmp -e "s/$foldername/\/images/g" ./_posts/$filename.md
# Remove backup file created by sed command.
rm ./_posts/$filename.md.tmp
# Check if the conversion has left a blank line at the top of the file.
firstline=$(head -n 1 ./_posts/$filename.md)
if [ "$firstline" = "" ]; then
# If it has, get rid of it.
tail -n +2 "./_posts/$filename.md" > "./_posts/$filename.tmp" && mv "./_posts/$filename.tmp" "./_posts/$filename.md"
fi
echo "Done converting."