saturnin_urimagic.sh 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 line;
  65. do
  66. line="$line "
  67. kw=${line%% *}
  68. query=${line#$kw }
  69. debug -v kw query
  70. if test -n "$query";
  71. then
  72. local fmt=$(inigrep -j -1 -p $pfxN.${kw,,})
  73. debug -v fmt
  74. printf "$fmt\n" "$query"
  75. else
  76. inigrep -j -1 -p $pfx0.$kw
  77. fi
  78. done
  79. }
  80. __saturnin_urimagic__flt_exps() {
  81. #
  82. # Hack expressions like bug = 123 out of the text
  83. #
  84. sed -e 's/(\d)\</\n/;'\
  85. | perl -ne '
  86. next unless m/\b([a-zA-Z]\w*\s*=\s*\w+)\b/;
  87. print "$1\n";
  88. ' \
  89. | sed -re 's/\s*=\s*/ /' \
  90. | tr '[:upper:]' '[:lower:]'
  91. }
  92. __saturnin_urimagic__flt_ids() {
  93. #
  94. # Hack doer-like id's (ID#123) out of the text
  95. #
  96. tr ' ' '\n' \
  97. | perl -ne '
  98. next unless m/\b([a-zA-Z]\w*#\w+)\b/;
  99. print "$1\n";
  100. ' \
  101. | tr '#' ' '
  102. }
  103. __saturnin_urimagic__flt_kws() {
  104. #
  105. # Hack out lines that look like kw expressions
  106. #
  107. grep -Ee '^[a-zA-Z]\w*\s+[^=]' -e '^[a-zA-Z]\w*$'
  108. }
  109. __saturnin_urimagic__flt_tags() {
  110. #
  111. # Hack "tags" like bug123 out of the text
  112. #
  113. tr ' ' '\n' \
  114. | grep -E '^[a-zA-Z]+[0-9]+$' \
  115. | sed -r 's/([a-zA-Z]+)([0-9])/\1 \2/'
  116. }
  117. __saturnin_urimagic__flt_uris() {
  118. #
  119. # Hack URIs out of the text.
  120. #
  121. tr ' ' '\n' \
  122. | perl -ne '
  123. next unless m|(\bhttps?://[[:alnum:]_:/.?#&+,%@=;~-]+)\b|;
  124. print "$1\n";
  125. '
  126. }