repo: open-search-descriptor
action: blob
revision: 
path_from: uri-search-handler
revision_from: refs/heads/master:
path_to: 
revision_to: 
git.thebackupbox.net
open-search-descriptor
git clone git://git.thebackupbox.net/open-search-descriptor

blob of:

open-search-descriptor

/ uri-search-handler

blob_plain of this file

refs/heads/master:/uri-search-handler

 #!/usr/bin/env bash
 export QUERY_STRING="$(printf "%s\n" "$1" | uricut -q)"
 q="$(query_param q)"

 if [ ! "$q" ];then
   printf "usage: first argument needs a search URI with query param q=[search-target]" >&2
   exit 1
 fi
 ## use this to get
 ## the search engines out of firefox:
 ## lz4jsoncat /home/epoch/.mozilla/firefox/h3fhtjwb.default/search.json.mozlz4
 ## they'll be in json format though. -___-"


 ### if you want to cache the template URLs
 CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/open-search-descriptor"
 mkdir -p "${CACHE_DIR}"
 TEMPLATE_CACHE="${CACHE_DIR}/search-templates"
 TEMPLATE_CACHE_XML="${CACHE_DIR}/search-templates-from-xml"
 TEMPLATE_CACHE_FIREFOX="${CACHE_DIR}/search-templates-from-json"
 printf 'caching to: %s\n' "$TEMPLATE_CACHE"
 FIREFOX_SEARCH=~/.mozilla/firefox/*/search.json.mozlz4
 ### stuff may break if you have spaces or :s or =s in here... good luck.
 #make -d -f- < ${TEMPLATE_CACHE}
 ${TEMPLATE_CACHE_XML}: ${XDG_CONFIG_HOME:-$HOME/.config}/open-search-descriptor/*.xml
 	xmlstarlet sel -t -v '//_:Url[@rel="results" or not(@rel)]/@template' -t -o " " -t -v '//_:Description' -t -n \$^ | html_entities_decode > "${TEMPLATE_CACHE_XML}"
 ${TEMPLATE_CACHE_FIREFOX}: ${FIREFOX_SEARCH}
 	lz4jsoncat \$^ | jq -r '.engines[] | ._urls[0].template,._name' | paste - - | grep -v ^null | tr '\t' ' ' >> "${TEMPLATE_CACHE_JSON}"
 EOF
 template="$(choose 'search engine for "'"$q"'"' < "${TEMPLATE_CACHE}" | cut '-d ' -f1)"
 ### elif you do no want to cache
 #template="$(xmlstarlet sel -t -v '//_:Url[not(@rel)]/@template' -t -o " " -t -v '//_:Description' -t -n ${XDG_CONFIG_HOME:-$HOME/.config}/open-search-descriptor/*.xml | html_entities_decode | choose 'search engine for "'"$q"'"' | cut '-d ' -f1)"
 ### endif

 if [ ! "$template" ];then
   exit 2
 fi
 ### you can replace uristart with xdg-open if you want
 exec uristart "$(printf "%s\n" "$template" | sed 's/{searchTerms}/'"$q"'/')"