saturnin_urimagic.sh 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/bin/bash
  2. ffoo import inigrep
  3. ffoo import pretty
  4. __saturnin_urimagic__deref() {
  5. #
  6. # Turn keyword or query (like "g hello" for google) to URI
  7. #
  8. local line # input line, eg. "g hello dolly" or "bug 1234"
  9. local kw # keyword part, eg. "g" or "bug"
  10. local query # query part, eg. "hello dolly" or "1234"
  11. while read line;
  12. do
  13. line="$line "
  14. kw=${line%% *}
  15. query=${line#$kw }
  16. debug -v kw query
  17. if test -n "$query";
  18. then
  19. local fmt=$(inigrep -j -1 -p www.query.${kw,,})
  20. debug -v fmt
  21. printf "$fmt\n" "$query"
  22. else
  23. inigrep -j -1 -p www.bookmark.$kw
  24. fi
  25. done
  26. }
  27. __saturnin_urimagic__flt_exps() {
  28. #
  29. # Hack expressions like bug = 123 out of the text
  30. #
  31. sed -e 's/(\d)\</\n/;'\
  32. | perl -ne '
  33. next unless m/\b([a-zA-Z]\w*\s*=\s*\w+)\b/;
  34. print "$1\n";
  35. ' \
  36. | sed -re 's/\s*=\s*/ /' \
  37. | tr '[:upper:]' '[:lower:]'
  38. }
  39. __saturnin_urimagic__flt_ids() {
  40. #
  41. # Hack doer-like id's (ID#123) out of the text
  42. #
  43. tr ' ' '\n' \
  44. | perl -ne '
  45. next unless m/\b([a-zA-Z]\w*#\w+)\b/;
  46. print "$1\n";
  47. ' \
  48. | tr '#' ' '
  49. }
  50. __saturnin_urimagic__flt_kws() {
  51. #
  52. # Hack out lines that look like kw expressions
  53. #
  54. grep -Ee '^[a-zA-Z]\w*\s+[^=]' -e '^[a-zA-Z]\w*$'
  55. }
  56. __saturnin_urimagic__flt_tags() {
  57. #
  58. # Hack "tags" like bug123 out of the text
  59. #
  60. tr ' ' '\n' \
  61. | grep -E '^[a-zA-Z]+[0-9]+$' \
  62. | sed -r 's/([a-zA-Z]+)([0-9])/\1 \2/'
  63. }
  64. __saturnin_urimagic__flt_uris() {
  65. #
  66. # Hack URIs out of the text.
  67. #
  68. tr ' ' '\n' \
  69. | perl -ne '
  70. next unless m|(\bhttps?://[[:alnum:]_:/.?#&+,%@=;~-]+)\b|;
  71. print "$1\n";
  72. '
  73. }
  74. saturnin_urimagic__scan() {
  75. #
  76. # Scan stdin for what looks like URI, ID or keyword
  77. #
  78. # Output URIs: first what were apparent uris, then IDs converted to
  79. # URIs, then equal sign expressions, then "tags" like bug1234 (i.e.
  80. # letters+nubmbers, no spaces), then things that looked like
  81. # kw expressions, converted to URIs.
  82. #
  83. # Note that kw expressions (e.g. "bug 123") work only if they start
  84. # the line.
  85. #
  86. # Apply this filter to args or clipboard, and either use head -1 or
  87. # if you are brave, open all URIs.
  88. #
  89. local d=$(mktemp -d -t saturnin_urimagic.XXXXXXXX)
  90. local p=__saturnin_urimagic__
  91. pushd $d >&/dev/null
  92. ##
  93. # heat up and fill pipes
  94. #
  95. mkfifo maybe_uris maybe_ids maybe_exps maybe_tags maybe_kws \
  96. uris uris_from_ids uris_from_exps uris_from_tags uris_from_kws
  97. cat | tee maybe_uris maybe_ids maybe_exps maybe_tags maybe_kws \
  98. >/dev/null &
  99. ##
  100. # process each pipe *async* by different filter
  101. #
  102. < maybe_uris ${p}flt_uris > uris &
  103. < maybe_ids ${p}flt_ids | ${p}deref > uris_from_ids &
  104. < maybe_exps ${p}flt_exps | ${p}deref > uris_from_exps &
  105. < maybe_tags ${p}flt_tags | ${p}deref > uris_from_tags &
  106. < maybe_kws ${p}flt_kws | ${p}deref > uris_from_kws &
  107. ##
  108. # print result *sync* in correct order
  109. #
  110. {
  111. cat uris
  112. cat uris_from_ids
  113. cat uris_from_exps
  114. cat uris_from_tags
  115. cat uris_from_kws
  116. } | grep . # throw away empties; add missing LF
  117. popd >&/dev/null
  118. rm -rf $d
  119. }