saturnin_urimagic.sh 3.8KB

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