[Surfraw-devel] searchable_bookmarks

Thomas Zervogiannis tzervo at gmail.com
Sun Apr 22 21:58:36 UTC 2012


Dear all,

I would like your feedback on a patch I have created to implement
bookmarks that support search. The main change is the addition of
the function "browse_or_google" which supports the following idea:

1) If I give a bookmark with a URL that contains %s, then I can
    also give search terms and %s will be substituted with these 
    terms.

    Example bookmark:

    c++ http://stackoverflow.com/search?q=%s

    Advantage: there are a lot of elvi that are made just to support
    this functionality, which in my view is redundant coding. One
    could just insert a one-line bookmark with this search functionality
    instead of them.
    Disadvantage: no autocompletion if it is not an elvi.

    Obviously, one could still use an elvi to have autocompletion or
    do other extra stuff, or write a quick autocompletion function for 
    the bookmarks (TODO?).

2) If I give a bookmark with a URL that does NOT contain %s but I also
    give search terms, the function uses the google elvi and google's 
    site:URL feature to search the terms in the domain specified by the
    bookmark.

    Example bookmark: regular surfraw bookmarks that were used so far

    Advantage: all regular bookmarks also gain a kind of search functionality.
    Comment: what if someone wants to use another search engine? (TODO?)

3) If I give a bookmark with multiple URL's and no search term, the
    function opens the browser to the first URL. If search terms are
    given, the function uses the google elvi and google's
    "site:URL1 OR site:URL2..." syntax to search the terms in all
    URL's of the bookmark. 
    
    Example bookmark:

    zshwiki http://zsh.sourceforge.net/ http://zshwiki.org/

    Advantage: easy and very flexible "bookmark-able" searches that
    can span multiple domains.

    Disadvantage: I used an array for this, which is the only
    bashism to my knowledge that is left in the script.

If you like the above idea and consider incorporating it in surfraw, 
I can try to get rid of the arrays and
work on the TODO's I noted above and/or whatever you send me as feedback.

Another minor change in the patch (which of course can be discarded)
is to fall back to the browser for handling all arguments if they
do not contain a valid elvi or bookmark, which in my opinion is more
robust (one could implement this behaviour as an option, for example
to also keep the older behaviour, which I guess is useful in browsers
such as conkeror or elinks, where a random string in the address bar
is not automatically handled i.e. by a google search).

Regards,
Thomas
-------------- next part --------------
*** surfraw.IN	2012-04-22 09:28:39.391088145 +0300
--- ../surfraw-build/surfraw.IN	2012-04-22 09:29:59.854638255 +0300
***************
*** 169,175 ****
  	file="$2"
  	if [ -r "$file" ]
  	then
! 		@AWK@ -v search="$search" '$1 == search { print $2; }'  $file | head -n 1
  	fi
  }
  
--- 169,175 ----
  	file="$2"
  	if [ -r "$file" ]
  	then
! 		@AWK@ -v search="$search" '$1 == search { for (nLoop = 2;nLoop <= NF; nLoop++) printf("%s ", $nLoop); print ""; }' $file | head -n 1
  	fi
  }
  
***************
*** 495,505 ****
--- 495,563 ----
  	w3_global_usage
  }
  
+ # Browse URL or perform a search on URL(s) if search
+ # terms are provided (use google elvi if URL has no %s
+ # field or multiple URLS)
+ browse_or_google ()
+ {
+     if [ -z "$w3_args" ]; then
+         case "$bookmark" in
+             *"%s"*) 
+                 # Keep only the domain address, the rest could be garbage.
+                 # This bookmark was probably meant to be used like that anyway.
+                 get_domain_from_bookmark
+                 ;;
+         esac
+         w3_browse_url "$bookmark"
+     else
+         escaped_args=$(w3_url_of_arg $w3_args)
+ 
+         case "$bookmark" in
+             *"%s"*) 
+                 bookmark="$(printf '%s\n' "$bookmark" | sed "s|\%s|$escaped_args|")"
+                 w3_browse_url "$bookmark"
+                 ;;
+             *)
+                 n=${#bookmark[*]}
+ 
+                 i=0
+                 while test $i -lt $n; do
+                     bookmark[$i]="site:${bookmark[$i]}"
+                     i=$(($i+1))
+                 done
+ 
+                 googleargs="$w3_args $bookmark"
+ 
+                 i=1
+                 while test $i -lt $n; do
+                     googleargs="$googleargs OR ${bookmark[$i]}"
+                     i=$(($i+1))
+                 done
+ 
+                 google "$googleargs"
+                 ;;
+         esac
+     fi
+ }
+ 
+ get_domain_from_bookmark ()
+ {
+     case "$bookmark" in
+         https://*)
+             bookmark=https://$(echo $bookmark | @AWK@ -F '/' '{print $3}')/
+             ;;
+         *)
+             bookmark=http://$(echo $bookmark | @AWK@ -F '/' '{print $3}')/
+             ;;
+     esac
+ }
+ 
  if [ "$w3_argv0" = "surfraw" -o "$w3_argv0" = "sr" ]
  then
  	w3_config
  	opts=""
  	unquoted_opts=""
+ 	unquoted_searchterms=""
  	elvi=""
  	searchterms=""
  	do_help=0
***************
*** 539,546 ****
--- 597,606 ----
  		  if [ -z "$searchterms" ]
  		  then
  			  searchterms="`quote "$arg"`"
+ 			  unquoted_searchterms="$arg"
  		  else
  			  searchterms="$searchterms `quote "$arg"`"
+ 			  unquoted_searchterms="$unquoted_searchterms $arg"
  		  fi
  	  fi
  	done
***************
*** 557,571 ****
  		then
   			sh -c "$elvidir/$elvi $opts $searchterms"
  		else
! 			bookmark=`bookmark_lookup $elvi`
  			if [ -n "$bookmark" ]
  			then
! 				w3_parse_args $unquoted_opts
! 				w3_browse_url "$bookmark"
  			else
  				echo "`basename $0`: $elvi: No elvis or bookmark with that name"
! 				surfraw_usage >&2
! 				exit 1
  			fi
  	   fi
  	fi
--- 617,633 ----
  		then
   			sh -c "$elvidir/$elvi $opts $searchterms"
  		else
! 			bookmark=(`bookmark_lookup $elvi`)
  			if [ -n "$bookmark" ]
  			then
!                 w3_parse_args $unquoted_opts $unquoted_searchterms
!                 browse_or_google
!                 exit 0
  			else
  				echo "`basename $0`: $elvi: No elvis or bookmark with that name"
! 				echo "Passing everything to browser and hoping for the best..."
!                 $BROWSER "$elvi $opts $unquoted_searchterms"
!                 exit 0
  			fi
  	   fi
  	fi


More information about the Surfraw-devel mailing list