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