check_urls.vim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. "20.10.23, added by Restorer
  9. if has("win32")
  10. let s:outdev = 'nul'
  11. else
  12. let s:outdev = '/dev/null'
  13. endif
  14. " Restorer: For Windows users. If "curl" or "wget" is installed on the system
  15. " but not in %PATH%, add the full path to them to %PATH% environment variable.
  16. if executable('curl')
  17. " Note: does not follow redirects!
  18. let s:command1 = 'curl --silent --max-time 5 --fail --output ' ..s:outdev.. ' --head '
  19. let s:command2 = ""
  20. elseif executable('wget')
  21. " Note: only allow a couple of redirects
  22. let s:command1 = 'wget --quiet -S --spider --max-redirect=2 --timeout=5 --tries=2 -O ' ..s:outdev.. ' '
  23. let s:command2 = ""
  24. elseif has("win32") "20.10.23, added by Restorer
  25. if executable('powershell')
  26. if 2 == system('powershell -nologo -noprofile "$psversiontable.psversion.major"')
  27. echoerr 'To work in OS Windows requires the program "PowerShell" version 3.0 or higher'
  28. return
  29. endif
  30. let s:command1 =
  31. \ "powershell -nologo -noprofile \"{[Net.ServicePointManager]::SecurityProtocol = 'Tls12, Tls11, Tls, Ssl3'};try{(Invoke-WebRequest -MaximumRedirection 2 -TimeoutSec 5 -Uri "
  32. let s:command2 = ').StatusCode}catch{exit [int]$Error[0].Exception.Status}"'
  33. endif
  34. else
  35. echoerr 'Only works when "curl" or "wget", or "powershell" is available'
  36. return
  37. endif
  38. " Do the testing.
  39. set report =999
  40. set nomore shm +=s
  41. let pat='\(https\?\|ftp\)://[^\t* ]\+'
  42. exe 'helpgrep' pat
  43. helpclose
  44. let urls = map(getqflist(), 'v:val.text')
  45. " do not use submatch(1)!
  46. let urls = map(urls, {key, val -> matchstr(val, pat)})
  47. " remove examples like user@host (invalid urls)
  48. let urls = filter(urls, 'v:val !~ "@"')
  49. " Remove example URLs which are invalid
  50. let urls = filter(urls, {key, val -> val !~ '\<\(\(my\|some\)\?host\|machine\|hostname\|file\)\>'})
  51. new
  52. put =urls
  53. " remove some more invalid items
  54. " empty lines
  55. "20.10.23, Restorer: '_' is a little faster, see `:h global`
  56. v/./d _
  57. " remove # anchors
  58. %s/#.*$//e
  59. " remove trailing stuff (parenthesis, dot, comma, quotes), but only for HTTP
  60. " links
  61. g/^h/s#[.),'"`/>][:.,]\?$##
  62. g#^[hf]t\?tp:/\(/\?\.*\)$#d _
  63. silent! g/ftp://,$/d _
  64. silent! g/=$/d _
  65. let a = getline(1,'$')
  66. let a = uniq(sort(a))
  67. %d _
  68. call setline(1, a)
  69. %s/.*/\=TestURL(submatch(0))/
  70. " highlight the failures
  71. /.* \([0-9]*[1-9]\|[0-9]\{2,}\)$
  72. endfunc
  73. func TestURL(url)
  74. " Relies on the return code to determine whether a page is valid
  75. echom printf("Testing URL: %d/%d %s", line('.'), line('$'), a:url)
  76. call system(s:command1 .. shellescape(a:url) .. s:command2)
  77. return printf("%s %d", a:url, v:shell_error)
  78. endfunc
  79. call Test_check_URLs()
  80. " vim: sw=2 sts=2 et