[Surfraw-commits] [SCM] surfraw - a fast unix command line interface to WWW branch, master, updated. surfraw_2-2-5_1-242-g846275f

Kyle Isom coder at kyleisom.net
Tue Oct 16 21:19:07 UTC 2012


The following commit has been merged in the master branch:
commit 852b59995515456771d37011c3c7b14b213fab84
Author: Kyle Isom <coder at kyleisom.net>
Date:   Tue Oct 16 14:56:45 2012 -0600

    Add support for searchable bookmarks.

diff --git a/AUTHORS b/AUTHORS
index 4de85f3..39871dd 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -123,6 +123,9 @@ Jason Ryan <jasonwryan at surfraw.org>
 Sara Fauzia <sara at archlinux.us>
         archpkg fix.
 
+Thomas Zervogiannis <tzervo at gmail.com>
+        Searchable bookmarks.
+
 Paulo Almeida <paulo at diasalmeida.com>
         elvi: priberam
 
diff --git a/surfraw.1.IN b/surfraw.1.IN
index fcca0dd..e47e2de 100644
--- a/surfraw.1.IN
+++ b/surfraw.1.IN
@@ -27,7 +27,7 @@ surfraw \- a fast unix command line interface to WWW services
 .B sr \-elvi
 .br
 .B sr
-.I bookmark
+.I [options] bookmark [search words]
 .br
 
 .SH DESCRIPTION
@@ -79,6 +79,12 @@ eg:
 
  ntk   http://www.ntk.net/
 
+If the url contains the string \fB%s\fP, you can specify an
+optional argument to the bookmark. Without an argument, only
+the domain will be returned. With arguments, \fb%s\fP will
+be replaced by the arguments. This obviates the need to
+create elvi for sites with very simple search options.
+
 To invoke a bookmark, use "surfraw bookmark" or "sr bookmark",
 and if an elvis of that name doesn't exist, it searches for a
 bookmark of that name instead.
diff --git a/surfraw.IN b/surfraw.IN
index 945ca7e..e360605 100755
--- a/surfraw.IN
+++ b/surfraw.IN
@@ -169,7 +169,7 @@ bookmark_file_search () {
 	file="$2"
 	if [ -r "$file" ]
 	then
-		@AWK@ -v search="$search" '$1 == search { print $2; }'  $file | head -n 1
+		@AWK@ -v search="$search" '$1 == search { for (nLoop = 2;nLoop <= NF; nLoop++) printf("%s ", $nLoop); print ""; }' $file | head -n 1
 	fi
 }
 
@@ -268,6 +268,10 @@ w3_global_usage () {
 #-------------------------------------79 cols----------------------------------
     cat <<EOF
 Global options:
+  -bookmark-search-elvis=ELVIS	If args are passed to a non-searchable
+				bookmark, search with this elvis instead.
+				Default: $SURFRAW_bookmark_search_elvis
+				Environment: SURFRAW_bookmark_search_elvis
   -browser=EXECUTABLE		Set browser
 				Default: $SURFRAW_browser
   -elvi				List Surfraw mechanisms for conquering evil
@@ -330,6 +334,8 @@ w3_parse_option () {
     esac
     w3_parse_option_hook "$opt" "$optarg" && return 0
     case "$opt" in
+    -bookmark-search-elvis=*)
+        setopt SURFRAW_bookmark_search_elvis   $optarg ;;
 	-browser=*)	    setopt   SURFRAW_browser		$optarg	;;
 	-elvi)		    echo " GLOBAL ELVI:"
 			    list_elvi "$elvidir"
@@ -483,7 +489,7 @@ quote () {
 surfraw_usage() {
 	cat <<EOF
 Usage: $w3_argv0 [-help] [-elvi] elvis [elvioptions] [search terms]
-       $w3_argv0 [options] bookmark
+       $w3_argv0 [options] bookmark [search terms]
     If you wish to run the elvi directly, run "surfraw-update-path" to append
 	$elvidir to your \$PATH in your shell's config files.
     See surfraw-update-path(1) for details
@@ -495,11 +501,41 @@ EOF
 	w3_global_usage
 }
 
+# Browse URL or perform a search on URL(s) if search terms
+# are provided (use the user's elvis of choice if URL has no
+# %s field or multiple if multiple URLs are provided)
+parse_bookmark()
+{
+    if [ -z "$w3_args" ]; then
+        # Keep only the domain address, the rest could be garbage.
+        # This bookmark was probably meant to be used like that anyway.
+        echo "$bookmark" | grep -q '%s' && \
+            bookmark=$(echo $bookmark | @AWK@ -F '/' '{print $1 "//" $3}')
+    else
+        escaped_args=$(w3_url_of_arg $w3_args)
+
+        case "$bookmark" in
+            *"%s"*)
+                bookmark="$(echo $bookmark | sed "s|\%s|$escaped_args|")"
+                ;;
+            *)
+                [ -z $SURFRAW_bookmark_search_elvis ] && SURFRAW_bookmark_search_elvis=google
+                bookmark=$(echo $bookmark | sed -e 's|^|site:|' \
+                                                -e 's|\ |\ OR\ site:|g' \
+                                                -e "s|^|$SURFRAW_bookmark_search_elvis |" \
+                                                -e "s|$| $w3_args|")
+                ;;
+        esac
+    fi
+    echo $bookmark
+}
+
 if [ "$w3_argv0" = "surfraw" -o "$w3_argv0" = "sr" ]
 then
 	w3_config
 	opts=""
 	unquoted_opts=""
+	unquoted_searchterms=""
 	elvi=""
 	searchterms=""
 	do_help=0
@@ -539,8 +575,10 @@ then
 		  if [ -z "$searchterms" ]
 		  then
 			  searchterms="`quote "$arg"`"
+			  unquoted_searchterms="$arg"
 		  else
 			  searchterms="$searchterms `quote "$arg"`"
+			  unquoted_searchterms="$unquoted_searchterms $arg"
 		  fi
 	  fi
 	done
@@ -560,12 +598,15 @@ then
 			bookmark=`bookmark_lookup $elvi`
 			if [ -n "$bookmark" ]
 			then
-				w3_parse_args $unquoted_opts
-				w3_browse_url "$bookmark"
+                w3_parse_args $unquoted_opts $unquoted_searchterms
+                bookmark=$(parse_bookmark)
+                w3_browse_url "$bookmark"
+                exit 0
 			else
 				echo "`basename $0`: $elvi: No elvis or bookmark with that name"
-				surfraw_usage >&2
-				exit 1
+				echo "Passing everything to browser and hoping for the best..."
+                $BROWSER "$elvi $opts $unquoted_searchterms"
+                exit 0
 			fi
 	   fi
 	fi

-- 
surfraw -  a fast unix command line interface to WWW



More information about the Surfraw-commits mailing list