saturnin_www.sh 3.5KB

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