bm-title (1280 bytes)
1 #!/bin/sh 2 3 # bm-title: print the <title> of a web page, or nothing. 4 # When it is installed, "bm --add" offers the title as the description. 5 # SBM_FETCH=0 keeps it off the network. 6 7 if [ $# -ne 1 ]; then 8 printf 'usage: bm-title <url>\n' >&2 9 exit 2 10 fi 11 [ "${SBM_FETCH:-1}" != 0 ] || exit 0 12 13 # Lines are gathered until the title has been closed; awk then leaves, and 14 # curl stops downloading. dd caps it at 64k for pages that are one long line 15 # (it may pass a little less when its reads come up short; never more). 16 curl -sL --max-time 3 -A 'Mozilla/5.0' -- "$1" 2>/dev/null \ 17 | dd bs=1024 count=64 2>/dev/null | LC_ALL=C awk ' 18 function title(s, start, end) { 19 start = index(tolower(s), "<title") 20 if (!start) return 21 s = substr(s, start) 22 s = substr(s, index(s, ">") + 1) 23 end = index(tolower(s), "</title") 24 if (end) s = substr(s, 1, end - 1) 25 gsub(/</, "<", s); gsub(/>/, ">", s); gsub(/"/, "\"", s) 26 gsub(/'|'|'/, "\047", s); gsub(/ /, " ", s) 27 gsub(/&/, "\\&", s) 28 gsub(/[ \t\r]+/, " ", s); sub(/^ /, "", s); sub(/ $/, "", s) 29 print s 30 } 31 { page = page " " $0 } 32 index(tolower($0), "</title") || NR >= 500 { exit } 33 END { title(page) }'