123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- #!/bin/bash
-
- ffoo import inigrep
- ffoo import pretty
-
-
- saturnin_urimagic__scan() {
- #
- # Scan stdin for what looks like URI, ID or keyword
- #
- # Output URIs: first what were apparent uris, then IDs converted to
- # URIs, then equal sign expressions, then "tags" like bug1234 (i.e.
- # letters+nubmbers, no spaces), then things that looked like
- # kw expressions, converted to URIs.
- #
- # Note that kw expressions (e.g. "bug 123") work only if they start
- # the line.
- #
- # Apply this filter to args or clipboard, and either use head -1 or
- # if you are brave, open all URIs.
- #
- local d=$(mktemp -d -t saturnin_urimagic.XXXXXXXX)
- local p=__saturnin_urimagic__
- pushd $d >&/dev/null
- ##
- # heat up and fill pipes
- #
- mkfifo maybe_uris maybe_ids maybe_exps maybe_tags maybe_kws \
- uris uris_from_ids uris_from_exps uris_from_tags uris_from_kws
- cat | tee maybe_uris maybe_ids maybe_exps maybe_tags maybe_kws \
- >/dev/null &
- ##
- # process each pipe *async* by different filter
- #
- < maybe_uris ${p}flt_uris > uris &
- < maybe_ids ${p}flt_ids | ${p}deref > uris_from_ids &
- < maybe_exps ${p}flt_exps | ${p}deref > uris_from_exps &
- < maybe_tags ${p}flt_tags | ${p}deref > uris_from_tags &
- < maybe_kws ${p}flt_kws | ${p}deref > uris_from_kws &
- ##
- # print result *sync* in correct order
- #
- {
- cat uris
- cat uris_from_ids
- cat uris_from_exps
- cat uris_from_tags
- cat uris_from_kws
- } | grep . # throw away empties; add missing LF
- popd >&/dev/null
- rm -rf $d
- }
-
- __saturnin_urimagic__deref() {
- #
- # Turn keyword or query (like "g hello" for google) to URI
- #
- local line # input line, eg. "g hello dolly" or "bug 1234"
- local kw # keyword part, eg. "g" or "bug"
- local query # query part, eg. "hello dolly" or "1234"
- while read line;
- do
- line="$line "
- kw=${line%% *}
- query=${line#$kw }
- debug -v kw query
- if test -n "$query";
- then
- local fmt=$(inigrep -j -1 -p www.query.${kw,,})
- debug -v fmt
- printf "$fmt\n" "$query"
- else
- inigrep -j -1 -p www.bookmark.$kw
- fi
- done
- }
-
- __saturnin_urimagic__flt_exps() {
- #
- # Hack expressions like bug = 123 out of the text
- #
- sed -e 's/(\d)\</\n/;'\
- | perl -ne '
- next unless m/\b([a-zA-Z]\w*\s*=\s*\w+)\b/;
- print "$1\n";
- ' \
- | sed -re 's/\s*=\s*/ /' \
- | tr '[:upper:]' '[:lower:]'
- }
-
- __saturnin_urimagic__flt_ids() {
- #
- # Hack doer-like id's (ID#123) out of the text
- #
- tr ' ' '\n' \
- | perl -ne '
- next unless m/\b([a-zA-Z]\w*#\w+)\b/;
- print "$1\n";
- ' \
- | tr '#' ' '
- }
-
- __saturnin_urimagic__flt_kws() {
- #
- # Hack out lines that look like kw expressions
- #
- grep -Ee '^[a-zA-Z]\w*\s+[^=]' -e '^[a-zA-Z]\w*$'
- }
-
- __saturnin_urimagic__flt_tags() {
- #
- # Hack "tags" like bug123 out of the text
- #
- tr ' ' '\n' \
- | grep -E '^[a-zA-Z]+[0-9]+$' \
- | sed -r 's/([a-zA-Z]+)([0-9])/\1 \2/'
- }
-
- __saturnin_urimagic__flt_uris() {
- #
- # Hack URIs out of the text.
- #
- tr ' ' '\n' \
- | perl -ne '
- next unless m|(\bhttps?://[[:alnum:]_:/.?#&+,%@=;~-]+)\b|;
- print "$1\n";
- '
- }
|