| 
									
										
										
										
											2017-11-28 14:19:07 +01:00
										 |  |  | " Test for URLs in help documents. | 
					
						
							|  |  |  | " | 
					
						
							|  |  |  | " Opens a new window with all found URLS followed by return code from curl | 
					
						
							|  |  |  | " (anything other than 0 means unreachable) | 
					
						
							|  |  |  | " | 
					
						
							|  |  |  | " Written by Christian Brabandt. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func Test_check_URLs() | 
					
						
							| 
									
										
										
										
											2023-11-04 08:57:09 +00:00
										 |  |  | "20.10.23, added by Restorer | 
					
						
							| 
									
										
										
										
											2017-11-28 14:19:07 +01:00
										 |  |  |   if has("win32") | 
					
						
							| 
									
										
										
										
											2023-11-04 08:57:09 +00:00
										 |  |  |     let s:outdev = 'nul' | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     let s:outdev = '/dev/null' | 
					
						
							| 
									
										
										
										
											2017-11-28 14:19:07 +01:00
										 |  |  |   endif | 
					
						
							| 
									
										
										
										
											2023-11-04 08:57:09 +00:00
										 |  |  | " Restorer: For Windows users. If "curl" or "weget" is installed on the system | 
					
						
							|  |  |  | " but not in %PATH%, add the full routes for them to this environment variable. | 
					
						
							| 
									
										
										
										
											2017-11-28 14:19:07 +01:00
										 |  |  |   if executable('curl') | 
					
						
							|  |  |  |     " Note: does not follow redirects! | 
					
						
							| 
									
										
										
										
											2023-11-04 08:57:09 +00:00
										 |  |  |     let s:command1 = 'curl --silent --fail --output ' ..s:outdev.. ' --head ' | 
					
						
							|  |  |  |     let s:command2 = "" | 
					
						
							| 
									
										
										
										
											2017-11-28 14:19:07 +01:00
										 |  |  |   elseif executable('wget') | 
					
						
							|  |  |  |     " Note: only allow a couple of redirects | 
					
						
							| 
									
										
										
										
											2023-11-04 08:57:09 +00:00
										 |  |  |     let s:command1 = 'wget --quiet -S --spider --max-redirect=2 --timeout=5 --tries=2 -O ' ..s:outdev.. ' ' | 
					
						
							|  |  |  |     let s:command2 = "" | 
					
						
							|  |  |  |   elseif has("win32") "20.10.23, added by Restorer | 
					
						
							|  |  |  |     if executable('powershell') | 
					
						
							|  |  |  |       if 2 == system('powershell -nologo -noprofile "$psversiontable.psversion.major"') | 
					
						
							|  |  |  |         echoerr 'To work in OS Windows requires the program "PowerShell" version 3.0 or higher' | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |       endif | 
					
						
							|  |  |  |       let s:command1 =  | 
					
						
							|  |  |  |             \ "powershell -nologo -noprofile \"{[Net.ServicePointManager]::SecurityProtocol = 'Tls12, Tls11, Tls, Ssl3'};try{(Invoke-WebRequest -MaximumRedirection 2 -TimeoutSec 5 -Uri " | 
					
						
							|  |  |  |       let s:command2 = ').StatusCode}catch{exit [int]$Error[0].Exception.Status}"' | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2017-11-28 14:19:07 +01:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2023-11-04 08:57:09 +00:00
										 |  |  |     echoerr 'Only works when "curl" or "wget", or "powershell" is available' | 
					
						
							| 
									
										
										
										
											2017-11-28 14:19:07 +01:00
										 |  |  |     return | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-04 08:57:09 +00:00
										 |  |  |   " Do the testing. | 
					
						
							|  |  |  |   set report =999 | 
					
						
							|  |  |  |   set nomore shm +=s | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 14:19:07 +01:00
										 |  |  |   let pat='\(https\?\|ftp\)://[^\t* ]\+' | 
					
						
							|  |  |  |   exe 'helpgrep' pat | 
					
						
							|  |  |  |   helpclose | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let urls = map(getqflist(), 'v:val.text') | 
					
						
							|  |  |  |   " do not use submatch(1)! | 
					
						
							|  |  |  |   let urls = map(urls, {key, val -> matchstr(val, pat)}) | 
					
						
							|  |  |  |   " remove examples like user@host (invalid urls) | 
					
						
							|  |  |  |   let urls = filter(urls, 'v:val !~ "@"') | 
					
						
							|  |  |  |   " Remove example URLs which are invalid | 
					
						
							|  |  |  |   let urls = filter(urls, {key, val -> val !~ '\<\(\(my\|some\)\?host\|machine\|hostname\|file\)\>'}) | 
					
						
							|  |  |  |   new | 
					
						
							|  |  |  |   put =urls | 
					
						
							|  |  |  |   " remove some more invalid items | 
					
						
							|  |  |  |   " empty lines | 
					
						
							| 
									
										
										
										
											2023-11-04 08:57:09 +00:00
										 |  |  |   "20.10.23, Restorer: '_' is a little faster, see `:h global` | 
					
						
							|  |  |  |   v/./d _ | 
					
						
							| 
									
										
										
										
											2017-11-28 14:19:07 +01:00
										 |  |  |   " remove # anchors | 
					
						
							|  |  |  |   %s/#.*$//e | 
					
						
							|  |  |  |   " remove trailing stuff (parenthesis, dot, comma, quotes), but only for HTTP | 
					
						
							|  |  |  |   " links | 
					
						
							| 
									
										
										
										
											2023-11-04 08:57:09 +00:00
										 |  |  |   g/^h/s#[.),'"`/>][:.,]\?$## | 
					
						
							|  |  |  |   g#^[hf]t\?tp:/\(/\?\.*\)$#d _ | 
					
						
							|  |  |  |   silent! g/ftp://,$/d _ | 
					
						
							|  |  |  |   silent! g/=$/d _ | 
					
						
							| 
									
										
										
										
											2017-11-28 14:19:07 +01:00
										 |  |  |   let a = getline(1,'$') | 
					
						
							|  |  |  |   let a = uniq(sort(a)) | 
					
						
							| 
									
										
										
										
											2023-11-04 08:57:09 +00:00
										 |  |  |   %d _ | 
					
						
							| 
									
										
										
										
											2017-11-28 14:19:07 +01:00
										 |  |  |   call setline(1, a) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   %s/.*/\=TestURL(submatch(0))/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   " highlight the failures | 
					
						
							|  |  |  |   /.* \([0-9]*[1-9]\|[0-9]\{2,}\)$ | 
					
						
							|  |  |  | endfunc | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestURL(url) | 
					
						
							|  |  |  |   " Relies on the return code to determine whether a page is valid | 
					
						
							|  |  |  |   echom printf("Testing URL: %d/%d %s", line('.'), line('$'), a:url) | 
					
						
							| 
									
										
										
										
											2023-11-04 08:57:09 +00:00
										 |  |  |   call system(s:command1 .. shellescape(a:url) .. s:command2) | 
					
						
							| 
									
										
										
										
											2017-11-28 14:19:07 +01:00
										 |  |  |   return printf("%s %d", a:url, v:shell_error) | 
					
						
							|  |  |  | endfunc | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | call Test_check_URLs() | 
					
						
							| 
									
										
										
										
											2023-11-04 08:57:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | " vim: sw=2 sts=2 et |