check_urls.vim 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. " Test for URLs in help documents.
  2. "
  3. " Opens a new window with all found URLS followed by return code from curl
  4. " (anything other than 0 means unreachable)
  5. "
  6. " Written by Christian Brabandt.
  7. func Test_check_URLs()
  8. if has("win32")
  9. echoerr "Doesn't work on MS-Windows"
  10. return
  11. endif
  12. if executable('curl')
  13. " Note: does not follow redirects!
  14. let s:command = 'curl --silent --fail --output /dev/null --head '
  15. elseif executable('wget')
  16. " Note: only allow a couple of redirects
  17. let s:command = 'wget --quiet -S --spider --max-redirect=2 --timeout=5 --tries=2 -O /dev/null '
  18. else
  19. echoerr 'Only works when "curl" or "wget" is available'
  20. return
  21. endif
  22. let pat='\(https\?\|ftp\)://[^\t* ]\+'
  23. exe 'helpgrep' pat
  24. helpclose
  25. let urls = map(getqflist(), 'v:val.text')
  26. " do not use submatch(1)!
  27. let urls = map(urls, {key, val -> matchstr(val, pat)})
  28. " remove examples like user@host (invalid urls)
  29. let urls = filter(urls, 'v:val !~ "@"')
  30. " Remove example URLs which are invalid
  31. let urls = filter(urls, {key, val -> val !~ '\<\(\(my\|some\)\?host\|machine\|hostname\|file\)\>'})
  32. new
  33. put =urls
  34. " remove some more invalid items
  35. " empty lines
  36. v/./d
  37. " remove # anchors
  38. %s/#.*$//e
  39. " remove trailing stuff (parenthesis, dot, comma, quotes), but only for HTTP
  40. " links
  41. g/^h/s#[.,)'"/>][:.]\?$##
  42. g#^[hf]t\?tp:/\(/\?\.*\)$#d
  43. silent! g/ftp://,$/d
  44. silent! g/=$/d
  45. let a = getline(1,'$')
  46. let a = uniq(sort(a))
  47. %d
  48. call setline(1, a)
  49. " Do the testing.
  50. set nomore
  51. %s/.*/\=TestURL(submatch(0))/
  52. " highlight the failures
  53. /.* \([0-9]*[1-9]\|[0-9]\{2,}\)$
  54. endfunc
  55. func TestURL(url)
  56. " Relies on the return code to determine whether a page is valid
  57. echom printf("Testing URL: %d/%d %s", line('.'), line('$'), a:url)
  58. call system(s:command . shellescape(a:url))
  59. return printf("%s %d", a:url, v:shell_error)
  60. endfunc
  61. call Test_check_URLs()