forked from kitian616/jekyll-TeXt-theme
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathopen-post.sh
More file actions
executable file
·108 lines (93 loc) · 2.52 KB
/
Copy pathopen-post.sh
File metadata and controls
executable file
·108 lines (93 loc) · 2.52 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
scriptdir=$(cd "$(dirname "$(readlink -f $0)")"; pwd -P)
#echo "$scriptdir"
postdir="$scriptdir/"
#echo "$postdir"
cd "$postdir/"
# Gather and filter markdown files
files=$(find . -name "*.md" | grep -vE "/_drafts/|/files/|/templ.md|/TODO.md|/refs.md|/*welcome.md|/*header-image.md" | cut -c 3- | sort -r)
#echo "$files"
# Arrays
index_keys=()
index_files=()
perma_keys=()
perma_files=()
titles=()
all_files=()
all_keys=()
display_titles=()
index=1
# Collect metadata
for file in $files; do
permalink=$(grep -m 1 '^permalink:' "$file" | sed -E 's/^permalink:[[:space:]]*//')
title=$(grep -m 1 '^title:' "$file" | sed -E 's/^title:[[:space:]]*//;s/^"//;s/"$//')
[ -z "$title" ] && title="(untitled)"
if [ -z "$permalink" ]; then
key="$index"
index_keys=("${index_keys[@]}" "$index")
index_files=("${index_files[@]}" "$file")
index=$((index + 1))
else
key="$permalink"
perma_keys=("${perma_keys[@]}" "$permalink")
perma_files=("${perma_files[@]}" "$file")
fi
all_keys=("${all_keys[@]}" "$key")
all_files=("${all_files[@]}" "$file")
titles=("${titles[@]}" "$title")
display_titles=("${display_titles[@]}" "$title")
done
# If no input, display list
if [ $# -eq 0 ]; then
for title in "${display_titles[@]}"; do
echo "$title"
done | more
exit 0
fi
# Otherwise, fuzzy open
input="$1"
file_to_open=""
# Exact match: number
i=0
for key in "${index_keys[@]}"; do
if [ "$key" = "$input" ]; then
file_to_open="${index_files[$i]}"
break
fi
i=$((i + 1))
done
# Exact match: permalink
if [ -z "$file_to_open" ]; then
i=0
for key in "${perma_keys[@]}"; do
if [ "$key" = "$input" ]; then
file_to_open="${perma_files[$i]}"
break
fi
i=$((i + 1))
done
fi
# Fuzzy match (permalink or title)
if [ -z "$file_to_open" ]; then
i=0
for key in "${all_keys[@]}"; do
title="${titles[$i]}"
match_key=$(echo "$key" | tr '[:upper:]' '[:lower:]')
match_title=$(echo "$title" | tr '[:upper:]' '[:lower:]')
match_input=$(echo "$input" | tr '[:upper:]' '[:lower:]')
if echo "$match_key" | grep -q "$match_input" || echo "$match_title" | grep -q "$match_input"; then
file_to_open="${all_files[$i]}"
break
fi
i=$((i + 1))
done
fi
# Open or error
if [ -n "$file_to_open" ]; then
vim "$file_to_open"
echo
echo "$file_to_open"
else
echo "No match found for '$input'"
exit 1
fi