#!/usr/bin/perl

# work around modest URL parsing <https://github.com/jgm/pandoc/issues/1838>

use warnings;
use strict;

use Path::Tiny;
use Regexp::Common qw(net);
use URI::Escape;

my $infile = shift;
my $outfile = shift || $infile;

$_ = path($infile)->slurp_utf8;

my $uri_path = qr!https?://$RE{net}{domain}{-nospace}(?::\d+)?\K(/\S*?)!;
my $uri_end = qr!(?=[.,;]?[\s<\"\|}\]])!;

s|\b$uri_path$uri_end|uri_escape(uri_unescape($1), "^A-Za-z0-9/\.")|eg;

path($outfile)->spew_utf8($_);

1;