Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: cf0ad6b30f
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 360 lines (296 sloc) 6.292 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
#!/usr/bin/env bash

# version

VERSION="0.4.1"

# bookmarks dir

DIR=~/Dropbox

# thumbs cache

THUMBS=$DIR/bookmark-thumbs

# bookmarks file

BOOKMARKS=$DIR/bookmarks

# HTML file

HTML=/tmp/bm.html

#
# Output usage info
#

usage() {
  cat <<EOF

Usage: bm [options] [cmd]

Commands:

# add a bookmark with the given url, description, and optional tags
$ bm add <url> [desc] [tag...]

# open the first bookmark matching <query>
$ bm open <query>
$ bm <query>

# search the bookmarks via full-text <query>
$ bm search <query>

# list tags
$ bm tags

# list bookmarks available
$ bm list
$ bm ls
$ bm

# display statistics about the bookmarks
$ bm statistics
$ bm stats

# view bookmark screenshots in your default browser
$ bm view design
$ bm view

# clear all bookmarks
$ bm clear

Options:

-V, --version output bm version
-h, --help output this help information

EOF
}

#
# Add a bookmark
#
# <url> [description] [tag ...]
#

save_bookmark() {
  local url=$1
  local desc=$2
  local tags=${@:3}
  echo
echo " Added bookmark"
  echo
echo " url: $url"
  echo " description: $desc"
  echo " tags: $tags"
  echo
echo "$url|$desc|$tags" >> $BOOKMARKS
}

#
# List all bookmarks
#

list_bookmarks() {
  echo
cat $BOOKMARKS \
    | awk '
BEGIN { FS = "|" }
{
printf " \033[36m%s\033[0m\n", $1
printf " \033[33m%s\033[0m\n", $2
printf " \033[90m%s\033[0m\n\n", $3
}' \
    | sed 's/http:\/\///'
  echo
}

#
# Search all bookmarks with <query>
#

search_bookmarks() {
  echo
cat $BOOKMARKS \
    | grep $1 \
    | awk '
BEGIN { FS = "|" }
{
printf " \033[36m%s\033[0m\n", $1
printf " \033[33m%s\033[0m\n", $2
printf " \033[90m%s\033[0m\n\n", $3
}' \
    | sed 's/http:\/\///'
  echo
}

#
# Open first bookmark matching <query>
#

open_bookmark() {
  cat $BOOKMARKS \
    | grep $1 \
    | cut -d '|' -f 1 \
    | xargs open
}

#
# Diplay some statistics about the bookmarks
#

show_statistics() {
  local total_bookmarks=$(cat $BOOKMARKS | wc -l | sed 's/^ *//')
  local total_tags=$(list_tags | wc -l | sed 's/^ *//')

  echo
echo " total bookmarks: $total_bookmarks"
  echo " total tags: $total_tags"
  echo " top 10 tags: `display_top_ten_tags`"
  echo
}

#
# Output tags.
#

list_tags() {
  cat $BOOKMARKS \
    | cut -f3 -d '|' \
    | tr " " "\n" \
    | sort \
    | uniq -c \
    | sort -nr
}

#
# Display the 10 most frequently used tags
#

display_top_ten_tags() {
  echo
printf "%20s %s\n" count tag
  list_tags \
    | head -10 \
    | awk '{ printf("%20d %s\n", $1, $2) }'
}


#
# Stylesheet
#

style() {
  cat <<EOF
<style>
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
padding: 50px 0 5px 50px;
background: #1f1f1f url(http://subtlepatterns.com/patterns/black-Linen.png);
font: 12px "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.bm {
position: relative;
float: left;
margin: 15px;
padding: 1px;
opacity: 1;
border: 15px solid black;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-webkit-transition: -webkit-box-shadow 600ms;
-moz-transition: -webkit-box-shadow 600ms;
-webkit-box-shadow: 0 0 1px 0 #555, 0 0 10px rgba(0,0,0,.5);
-moz-box-shadow: 0 0 1px 0 #555, 0 0 10px rgba(0,0,0,.5);
overflow: hidden;
}
.bm:hover {
-webkit-box-shadow: 0 0 40px #1ab0ff
, 0 0 3px #06bdff
, 0 1px 1px #4ee2ff
, 0 1px 0 #fff;
-moz-box-shadow: 0 0 40px #1ab0ff
, 0 0 3px #06bdff
, 0 1px 1px #4ee2ff
, 0 1px 0 #fff;
}
.bm:hover img {
opacity: 1;
}
.bm img {
width: 200px;
height: 150px;
opacity: .5;
-webkit-transition: opacity 200ms;
-moz-transition: opacity 200ms;
}
.bm p {
margin: 0;
padding: 10px;
width: 100%;
background: rgba(0,0,0,.75);
position: absolute;
left: 0;
color: white;
letter-spacing: 1px;
bottom: -50px;
-webkit-font-smoothing: antialiased;
-webkit-transition: bottom 200ms ease-in;
}
.bm:hover p {
bottom: 0;
}
</style>
EOF
}

#
# List urls
#

urls() {
  cat $BOOKMARKS \
    | grep "$1" \
    | cut -d '|' -f 1
}

#
# Generate bookmark screenshots
#

create_bookmark_screenshots() {
  which webkit2png > /dev/null

  if test $? -ne 0; then
echo
echo " You need webkit2png(1) installed for this feature:"
    echo " $ brew install webkit2png"
    echo
exit
fi

local urls=`urls`

  # thumbs dir
  mkdir -p $THUMBS

  # fetch thumbs
  for url in $urls; do
local path=$THUMBS/`echo $url | md5`.png
    if ! test -f $path; then
webkit2png -C -o out.png $url
      mv out.png-clipped.png $path
      echo " ... added $path"
    fi
done

rm -f out.png-{clipped,full,thumb}.png
}

#
# View bookmark screenshots grep <pattern>
#

view_bookmark_screenshots() {
  create_bookmark_screenshots

  echo "<title>Bookmarks</title>" > $HTML
  style >> $HTML

  (
    while read line; do
      # TODO: slow... refactor
      local url=`echo "$line" | cut -d '|' -f 1`
      local desc=`echo "$line" | cut -d '|' -f 2`
      local tags=`echo "$line" | cut -d '|' -f 3`
      local path=$THUMBS/`echo $url | md5`.png
      echo "
<div class=bm>
<a href='$url'>
<img src='$path' />
</a>
<p>$desc</p>
</div>
" >> $HTML
    done
  ) < <(cat $BOOKMARKS | grep "$1")

  open $HTML
}

#
# Remove all bookmarks
#

clear_bookmarks() {
  rm $BOOKMARKS
  rm -fr $THUMBS
}

# no args

if test $# -eq 0; then
list_bookmarks
  exit
fi

# parse args

while test $# -ne 0; do
arg=$1; shift
case $arg in
    -V|--version) echo $VERSION; exit ;;
    -h|--help) usage; exit ;;
    tags) list_tags; exit ;;
    ls|list) list_bookmarks; exit ;;
    search) search_bookmarks "$@"; exit ;;
    open) open_bookmark "$@"; exit ;;
    add) save_bookmark "$@"; exit ;;
    view) view_bookmark_screenshots "$@"; exit ;;
    stats|statistics) show_statistics; exit ;;
    clear) clear_bookmarks; exit ;;
    *) open_bookmark $arg; exit ;;
  esac
done
Something went wrong with that request. Please try again.