aboutsummaryrefslogtreecommitdiff
path: root/make-notes.sh
blob: 9c36b00ddd3add1ea6ba51c5651e753eda2a8416 (plain)
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
#!/bin/sh

set -e

if [ "$1" = --remove ]; then
    exec find . -name "*-NOTES.md" -delete
fi

if ! command -v jq >/dev/null; then
    exec guix shell jq -- "$0" "$@"
fi

if [ $# -lt 1 ]; then
    exec find . -name "*.ipynb" -and \
         '!' -wholename "*/.ipynb_checkpoints/*" -and \
         '!' -name "*-NOTELESS.ipynb" \
         -exec "$0" '{}' \;
fi

for FILE in "$@"; do
    DST="$(printf %s "$FILE" |
           sed 's/\(-NOTES[.]md\|\(-NOTELESS\)\?[.]ipynb\)$/-NOTES.md/')"
    NOTEBOOK="$(printf %s "$FILE" |
                sed 's/\(-NOTES[.]md\|\(-NOTELESS\)\?[.]ipynb\)$/.ipynb/')"

    jq --join-output '
        .cells[] |
        select(.cell_type == "markdown") |
        if (.source[0]? + "") | test("^_notes_") then
            "\n", .source[1:][], "\n\n"
        else
            .source[] |
            select(test("^#")) |
            if test("\n$") then . else . + "\n" end
        end
    ' < "$NOTEBOOK" > "$DST"
done