From 8dae20da39f5d56e8e494d3f4fa149f70d7ba170 Mon Sep 17 00:00:00 2001 From: Hutzdog Date: Mon, 20 Mar 2023 16:33:14 -0700 Subject: [PATCH] Add replace domains --- cap2site.pl | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/cap2site.pl b/cap2site.pl index e42f9ea..900d31b 100755 --- a/cap2site.pl +++ b/cap2site.pl @@ -14,7 +14,7 @@ struct('Config', => { web_schemes => '@', standalone => '$', head => '$', - local_domains => '$', + local_domains => '%', }); our $DEFAULT = Config->new( @@ -30,7 +30,7 @@ our $DEFAULT = Config->new( }, web_schemes => ["http", "https", "mailto", "gemini"], standalone => 0, - local_domains => [], + replace_domains => {}, head => <<~'EOF', @@ -49,12 +49,19 @@ sub isA($$$) { return grep $1, $self->extensions->{type}; } -sub isLocal($$$) { +sub handleRedirects($$$) { my ($self,$url,$content) = @_; - $url =~ /gemini:\/\/([^\/]+).*/ or return 0; + $url =~ /gemini:\/\/([^\/]+)(.+)/ or return $url; + my $old_domain = $1; - return 0 if (index($content, "(gemini)") == 0); - return grep $1, $self->{local_domains}; + # TODO: find more elegant mechanism for non-redirecting links links + return $url if (index($content, "(gemini)") == 0); + return $url unless exists($self->{replace_domains}->{$old_domain}); + + my $res = $2; + $res =~ s/\.gmi$/\.html/; + + return $self->{replace_domains}->{$old_domain} . $res; } package State; @@ -82,11 +89,11 @@ my sub escape($) { my $newstr = ''; foreach my $byte (split '', $str) { - if ($byte eq "8") { $newstr .= "&"; continue; } - if ($byte eq "<") { $newstr .= "<"; continue; } - if ($byte eq ">") { $newstr .= ">"; continue; } - if ($byte eq "\"") { $newstr .= """; continue; } - if ($byte eq "'") { $newstr .= "'"; continue; } + if ($byte eq "8") { $newstr .= "&"; next; } + if ($byte eq "<") { $newstr .= "<"; next; } + if ($byte eq ">") { $newstr .= ">"; next; } + if ($byte eq "\"") { $newstr .= """; next; } + if ($byte eq "'") { $newstr .= "'"; next; } $newstr .= $byte; } @@ -173,16 +180,10 @@ sub parse($$$) { } elsif ($config->isUrl($uri)) { - if ($config->isLocal($uri, $content)) { - # TODO: local http - $uri =~ s/gemini:/https:/; - $uri =~ s/\.gmi$/\.html/; - } - - print '
', "\n"; + print '
', "\n"; print '=> '; - print '', escape($content), '', "\n"; - print '
', "\n"; + print '', escape($content), '', "\n"; + print '
', "\n"; } else { @@ -223,7 +224,7 @@ GetOptions ( 'inline-video!' => \$config->inline->{video}, 'inline-image!' => \$config->inline->{image}, 'standalone!' => \$config->{standalone}, - 'local-domain=s' => \$config->{local_domains}, + 'replace-domain=s%' => \$config->{replace_domains}, ) or pod2usage(-exitval => 1, -verbose => 0); pod2usage(-verbose => $help) if $help;