Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: cf0ad6b30f
Fetching contributors…

Cannot retrieve contributors at this time

executable file 360 lines (296 sloc) 6.292 kB
#!/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
Jump to Line
Something went wrong with that request. Please try again.