www.sh 3.0KB

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