Add replace domains

This commit is contained in:
Hutzdog 2023-03-20 16:33:14 -07:00
parent 022b4e54be
commit 8dae20da39
1 changed files with 22 additions and 21 deletions

View File

@ -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',
<meta charset="utf-8"/>
<link rel="stylesheet" href="style.css"></link>
@ -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 .= "&amp"; continue; }
if ($byte eq "<") { $newstr .= "&lt"; continue; }
if ($byte eq ">") { $newstr .= "&gt"; continue; }
if ($byte eq "\"") { $newstr .= "&quot"; continue; }
if ($byte eq "'") { $newstr .= "&#39"; continue; }
if ($byte eq "8") { $newstr .= "&amp"; next; }
if ($byte eq "<") { $newstr .= "&lt"; next; }
if ($byte eq ">") { $newstr .= "&gt"; next; }
if ($byte eq "\"") { $newstr .= "&quot"; next; }
if ($byte eq "'") { $newstr .= "&#39"; 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 '<div>', "\n";
print '<div>', "\n";
print '<span class="link-delim">=></span> ';
print '<a href="', escape($uri), '">', escape($content), '</a>', "\n";
print '</div>', "\n";
print '<a href="', escape($config->handleRedirects($uri, $content)), '">', escape($content), '</a>', "\n";
print '</div>', "\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;