aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/bookmarks
blob: 58ad8f5455a35ed57177efc17feaf67fda12588e (plain)
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
#!/bin/sh -eu

date=$(date +%s)
program=$(basename "$0")
key="QdUEdQBncNzJgIJqTA30"
hxnormalize="hxnormalize -l 99999999 -x"
items=/tmp/bookmarks_items_$key
titles=/tmp/bookmarks_titles_$key
anchors=/tmp/bookmarks_anchors_$key
dependencies="$(command -V xmlstarlet hxnormalize paste \
                hxselect hxincl xdg-open rofi basename \
                | awk '{ print $1, $3 }' | column -t)"
base=\
"<!DOCTYPE NETSCAPE-Bookmark-file-1>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<title>Bookmarks</title>
<h1>Bookmarks</h1>
<dl>
<!-- include $items --></dl>
"

bookmarks_help() {
{ [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ] || [ "$#" = 0 ]; } && printf \
"
Usage: %s [FLAGS]... [ARGUMENTS]...

The program $program shall execute basic operations
on Netscape Bookmark HTML files.

  Command List:

    $program compile [FILES]...  Combine two or more bookmark.html files.
    $program show [FILE]         Show list of bookmarks.
    $program check               Check dependencies.
    $program -h --help           Show this help menu.
" "$program" && exit;
printf "Error: Unknown argument '%s'.\n" "$@" && exit 1;
}

bookmarks_combine () {
  for file in "$@"
  do $hxnormalize "$file" | sed 's/&/&amp;/g' \
    | xmlstarlet edit -S \
      --delete '//a/@add_date' \
      --delete '//a/@icon' \
      --delete '//a/@icon_uri' \
      --delete '//a/@last_charset' \
      --delete '//a/@last_modified' \
      --delete '//a/@last_modified' \
      --delete '//a/@tags' \
    | $hxnormalize | hxselect -i -s '\n' dt;
  done | sort | awk '!removeDuplicates[$0]++' >> "$items";
  printf "%s" "$base" | hxincl -f -x > "bookmarks-$date.html";
  realpath "bookmarks-$date.html";
}

bookmarks_show () {
  $hxnormalize "${1:-}" | hxselect -c -i -s '\n' 'a::attr(href)' > "$anchors"
  $hxnormalize "${1:-}" | hxselect -c -i -s '\n' 'a' > "$titles"
  xdg-open "$(paste -d' ' "$anchors" "$titles" \
  | rofi -font "sans 12" -matching regex -padding 20 -dmenu -i -p 'Bookmarks' \
  | awk '{ print $1 }')";
}

[ "${1:-}" = "show"    ] && shift 1 && bookmarks_show    "$@" && exit;
[ "${1:-}" = "combine" ] && shift 1 && bookmarks_combine "$@" && exit;
[ "${1:-}" = "check"   ] && shift 1 && printf '%s\n' "$dependencies" && exit;

bookmarks_help "$@";