-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfmt-links
More file actions
executable file
·54 lines (44 loc) · 1.29 KB
/
fmt-links
File metadata and controls
executable file
·54 lines (44 loc) · 1.29 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env zsh
string=$1
attach=$2
string_copy=$string
matches=()
# Determine the correct file extension
function dirntype() {
local EXT=$1
case "$EXT" in
photo) echo -n $2 ;;
audio) echo -n "m4a" ;;
pdfattachment) echo -n $2 ;;
*)
echo -n 'NONE'
;;
esac
}
# Check if the string contains any Day One links
if [[ $string_copy =~ (dayone-moment:/) ]]; then
# Only look for links to photos, audio, or PDFs
pattern="(dayone-moment:/(/|audio/|pdfAttachment/)[a-zA-Z0-9]{32})"
while [[ $string_copy =~ $pattern ]]; do
# Add the matched text to the "matches" array
matches+=("${match[1]}")
# Remove the instance from the string copy
string_copy=${string_copy#*$match[1]}
done
for match in $matches; do
# Remove the dayone dir
# Note: An \r was causing trouble—Windows BS
local ID=`echo -n "$match" | sed -E 's#dayone-moment:/(/|audio/|pdfAttachment/)*##g'`
local LINE=`bat $attach | grep $ID | tr -d "\n" | tr -d "\r"`
local elements=("${(s/,/)LINE}")
local MD5=$elements[1]
local TYPE=`echo $elements[3] | tr '[:upper:]' '[:lower:]'`
local FLTYPE=`echo $elements[4]`
local EXT=`dirntype $TYPE $FLTYPE`
local FL=../$TYPE\/$MD5.$EXT
# Replace it in the main string
string=`echo $string | sed -E "s#$match#$FL#g"`
echo $FL
done
fi
echo $string