diff options
Diffstat (limited to 'pandoc-iri')
-rwxr-xr-x | pandoc-iri | 23 |
1 files changed, 21 insertions, 2 deletions
@@ -1,5 +1,8 @@ #!/usr/bin/perl +# canonicalize URIs links, beautify their text representations +# set NETWORK_TESTS=1 to also validate and report failures to STDERR + use warnings; use strict; @@ -9,6 +12,9 @@ use Pandoc::Elements; use URI; use URI::Escape; use URI::Find; +use HTTP::Tiny 0.014; + +my ($ua, %links); pandoc_filter( \&link_normalize, @@ -26,12 +32,25 @@ sub link_normalize { return; } +sub normalize_uri { + my $uri = URI->new(uri_unescape(shift))->canonical; + return $uri unless $ENV{'NETWORK_TESTS'}; + return $uri if ( defined $links{$uri} ); + $ua = HTTP::Tiny->new() + unless ( defined $ua ); + $links{$uri} = $ua->head($uri); + printf STDERR "Failed fetching <%s>: %u %s\n", + $uri, $links{$uri}->{status}, $links{$uri}->{reason} + unless ($links{$uri}->{success}); + return $uri; +} + sub pp_uri { - return URI->new(uri_unescape(shift))->canonical->as_string; + return normalize_uri(shift)->as_string; } sub pp_iri { - return URI->new(uri_unescape(shift))->canonical->as_iri; + return normalize_uri(shift)->as_iri; } sub pp_string { |