-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-ipynb.sh
72 lines (61 loc) · 1.99 KB
/
generate-ipynb.sh
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
#!/bin/bash
#!/bin/bash
# Check if an input file argument is provided
if [ -z "$1" ]; then
echo "Usage: $0 <input_file>"
exit 1
fi
# Define the input text file from the first argument
INPUT_FILE="$1"
# Variables to hold current notebook content
current_file=""
lesson_started=false
# Function to finish and save the current notebook
finish_notebook() {
if [[ $lesson_started == true ]]; then
# Close the JSON structure of the notebook
echo ' ],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}' >> "$current_file"
echo "Generated notebook: $current_file"
fi
}
# Read through the text file line by line
while IFS= read -r line; do
# Check if the line starts with "Lesson", indicating a new file and new markdown cell
if [[ $line == Lesson* ]]; then
# Finish the previous notebook if any
finish_notebook
# Create a new filename based on the lesson name.
lesson_name=$(echo "$line" ) # Get text before the colon
current_file=$(echo "$lesson_name" ).ipynb
# Start a new notebook JSON structure
echo '{
"cells": [' > "$current_file"
# Add the lesson name as a heading 1
echo ' {
"cell_type": "markdown",
"metadata": {},
"source": [
"# '"$lesson_name"'"
]
}' >> "$current_file"
lesson_started=true
elif [[ -z "$line" || "$line" =~ ^[[:space:]]*$ ]]; then
# Skip lines that do not match any condition
continue
else
# Add the subsequent lines as markdown content
echo ' ,{
"cell_type": "markdown",
"metadata": {},
"source": [' >> "$current_file"
echo ' "## '$(echo "$line" | sed 's/"/\\"/g')'" ' >> "$current_file"
echo ' ]
}' >> "$current_file"
fi
done < "$INPUT_FILE"
# Finish the last notebook
finish_notebook