saturnin_urimagic.sh 3.7KB

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