aboutsummaryrefslogtreecommitdiff
#!/bin/sh

set -e

if [ "$1" = --remove ]; then
    exec find . -name "*-NOTELESS.ipynb" -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\)$/-NOTELESS.ipynb/')"
    NOTEBOOK="$(printf %s "$FILE" |
                sed 's/\(-NOTES[.]md\|\(-NOTELESS\)\?[.]ipynb\)$/.ipynb/')"

    jq --indent 1 --sort-keys '
        . + {
            cells: [
                .cells[] | select(
                    .cell_type != "markdown" or
                    ((.source[0]? + "") | test("^_notes_") | not)
                )
            ]
        }
    ' < "$NOTEBOOK" > "$DST"
done