123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_kws() {
  27. #
  28. # Hack out lines that look like kw expressions
  29. #
  30. grep -Ee '^[a-zA-Z]\w*\s+[^=]' -e '^[a-zA-Z]\w*$' \
  31. | debug_pipe kw_lookalikes
  32. }
  33. filter_uris() {
  34. #
  35. # Hack URIs out of the text.
  36. #
  37. tr ' ' '\n' \
  38. | perl -ne '
  39. next unless m|(\bhttps?://[[:alnum:]_:/.?&+%@=;~-]+)\b|;
  40. print "$1\n";
  41. ' \
  42. | debug_pipe uri_lookalikes
  43. }
  44. find_uri() {
  45. #
  46. # Scan stdin for what looks like URI, ID or keyword
  47. #
  48. # Output URIS: first what were apparent uris, then IDs converted to
  49. # URIs, then equal sign expressions, then things that looked like
  50. # kw expressions, converted to URIs.
  51. #
  52. # Apply this filter to args or clipboard, and either use head -1 or
  53. # if you are brave, open all URIs.
  54. #
  55. local d=$(mktemp -d)
  56. pushd $d >&/dev/null
  57. ##
  58. # heat up and fill pipes
  59. #
  60. mkfifo maybe_uris maybe_ids maybe_exprs maybe_kws \
  61. uris uris_from_ids uris_from_exprs uris_from_kws
  62. cat | tee maybe_uris maybe_ids maybe_exprs maybe_kws >/dev/null &
  63. ##
  64. # process each pipe *async* by different filter
  65. #
  66. cat maybe_uris | filter_uris > uris &
  67. cat maybe_ids | filter_ids | id2kw | kw2uri > uris_from_ids &
  68. cat maybe_exprs | filter_exprs | expr2kw | kw2uri > uris_from_exprs &
  69. cat maybe_kws | filter_kws | kw2uri > uris_from_kws &
  70. ##
  71. # print result *sync* in correct order
  72. #
  73. {
  74. cat uris
  75. cat uris_from_ids
  76. cat uris_from_exprs
  77. cat uris_from_kws
  78. } | grep . # throw away empties; add missing LF
  79. popd >&/dev/null
  80. rm -rf $d
  81. }
  82. id2kw() {
  83. #
  84. # Convert doer-like ID to kw expression
  85. #
  86. tr '#' ' '
  87. }
  88. expr2kw() {
  89. #
  90. # Convert equal sign expression to kw expression
  91. #
  92. sed -re 's/\s*=\s*/ /' | tr '[:upper:]' '[:lower:]'
  93. }
  94. kw2uri() {
  95. #
  96. # Turn keyword or query (like "g hello" for google) to URI
  97. #
  98. local line
  99. declare -l line
  100. while read line;
  101. do
  102. line="$line "
  103. local kw=${line%% *}
  104. local query=${line#$kw }
  105. debug -v kw query
  106. if test -n "$query";
  107. then
  108. local fmt=$(cfgrep -j -1 -p www.query.$kw)
  109. debug -v fmt
  110. printf "$fmt\n" "$query"
  111. else
  112. cfgrep -1 -p www.bookmark.$kw
  113. fi
  114. done
  115. }