saturnin_urimagic.sh 3.5KB

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