-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive_clippings.sh
executable file
·52 lines (41 loc) · 1.21 KB
/
archive_clippings.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
#!/usr/bin/env bash
set -euf -o pipefail
TEMP="$HOME"/Documents/Kindle-clippings/_temp_clips.txt
# Get file from Kindle
DEV_PATH=/Volumes/Kindle/documents
CLIPS="$DEV_PATH"/My\ Clippings.txt
cp "$CLIPS" "./_ori_clips-$(date -u +"%y%m%d-%H%M").txt"
# learned on https://stackoverflow.com/a/7216394/4341322
extract_title() {
grep --invert-match --max-count=1 "^\s*$" "$1"
}
format_title() {
# Prepare title string as kebap-case-filename
echo "$1" |
perl -pe 's/\W+/-/g' |
perl -pe 's/^-+//g' |
perl -pe 's/\b(-+)?$/.txt/'
}
extract_clips() {
grep --invert-match "$1" "$2"
}
format_clips() {
# Remove original separators & timestamps
# Convert descriptions into short ASCIDOC headings
perl -pe 's/^=+//g' "$1" |
perl -pe 's/ \|[^[:punct:]]+,[ .\w]+(:\d+)+( \w+)?\s+//gi' |
perl -pe 's/- [A-Z][a-z]+ /\r\n== /g' |
perl -pe 's/^== N/==== N/g'
}
# Remove title & save clips into temp file
TITLE=$(extract_title "$CLIPS")
extract_clips "$TITLE" "$CLIPS" >"$TEMP"
TITLE_F=$(format_title "$TITLE")
touch "$TITLE_F"
format_clips "$TEMP" >>"$TITLE_F"
$EDITOR "$TITLE_F"
# Clean up locally & on device
trash "$TEMP"
echo "" >"$CLIPS"
# Copy clippings onto device for review
cp "$TITLE_F" "$DEV_PATH"