forked from rwbaumg/admin-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract-bookmarks.sh
executable file
·92 lines (81 loc) · 2.26 KB
/
extract-bookmarks.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
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
#!/bin/bash
# Extract URLs from an HTML document, sort and write unique entries in CSV format.
# This script can be used to collate multiple exported browser bookmarks backup files.
# FORMAT="csv"
# FORMAT="trac-links"
# FORMAT="trac-table"
# FORMAT="md-links"
function usage() {
echo >&2 "Usage: $0 <bookmarks_html_file>..."
}
function create_csv() {
if [ -z "$1" ]; then
usage
exit 1
fi
for f in "$@"; do
if [ ! -e "$f" ]; then
echo >&2 "ERROR: Failed to locate HTML file(s): $*"
exit 1
fi
done
# shellcheck disable=2002
cat "$@" | while read -r line; do
link=$(echo "${line}" | grep -Pio '(?<=HREF\=\")[^\"]+(?=\")')
desc=$(echo "${line}" | grep -Pio '(?<=\"\>)[^\"]+(?=\<\/A\>)')
if [ ! -z "${link}" ]; then
echo "\"${link}\",\"${desc}\""
fi
done
}
function create_trac_links() {
if [ -z "$1" ]; then
usage
exit 1
fi
for f in "$@"; do
if [ ! -e "$f" ]; then
echo >&2 "ERROR: Failed to locate HTML file(s): $*"
exit 1
fi
done
# shellcheck disable=2002
cat "$@" | while read -r line; do
link=$(echo "${line}" | grep -Pio '(?<=HREF\=\")[^\"]+(?=\")')
desc=$(echo "${line}" | grep -Pio '(?<=\"\>)[^\"]+(?=\<\/A\>)')
if [ ! -z "${link}" ]; then
echo "- [${link// /\%20/} ${desc}]"
fi
done
}
if [ -z "$1" ]; then
usage
exit 1
fi
if [ -z "${FORMAT+set}" ]; then
export FORMAT="csv"
fi
if [ -z "${FORMAT}" ]; then
echo >&2 "ERROR: Output format not specified."
exit 1
fi
case "$FORMAT" in
default|csv)
if ! OUTPUT=$(create_csv "$@"); then
echo >&2 "ERROR: Failed to create CSV file."
exit 1
fi
;;
trac|trac-links)
if ! OUTPUT=$(create_trac_links "$@"); then
echo >&2 "ERROR: Failed to create Trac links markup."
exit 1
fi
;;
*)
echo >&2 "ERROR: Unsupported output format '${FORMAT}'."
exit 1
;;
esac
echo "${OUTPUT}" | sort | uniq
exit 0