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 => '@', web_schemes => '@',
standalone => '$', standalone => '$',
head => '$', head => '$',
local_domains => '$', local_domains => '%',
}); });
our $DEFAULT = Config->new( our $DEFAULT = Config->new(
@ -30,7 +30,7 @@ our $DEFAULT = Config->new(
}, },
web_schemes => ["http", "https", "mailto", "gemini"], web_schemes => ["http", "https", "mailto", "gemini"],
standalone => 0, standalone => 0,
local_domains => [], replace_domains => {},
head => <<~'EOF', head => <<~'EOF',
<meta charset="utf-8"/> <meta charset="utf-8"/>
<link rel="stylesheet" href="style.css"></link> <link rel="stylesheet" href="style.css"></link>
@ -49,12 +49,19 @@ sub isA($$$) {
return grep $1, $self->extensions->{type}; return grep $1, $self->extensions->{type};
} }
sub isLocal($$$) { sub handleRedirects($$$) {
my ($self,$url,$content) = @_; 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); # TODO: find more elegant mechanism for non-redirecting links links
return grep $1, $self->{local_domains}; 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; package State;
@ -82,11 +89,11 @@ my sub escape($) {
my $newstr = ''; my $newstr = '';
foreach my $byte (split '', $str) { foreach my $byte (split '', $str) {
if ($byte eq "8") { $newstr .= "&amp"; continue; } if ($byte eq "8") { $newstr .= "&amp"; next; }
if ($byte eq "<") { $newstr .= "&lt"; continue; } if ($byte eq "<") { $newstr .= "&lt"; next; }
if ($byte eq ">") { $newstr .= "&gt"; continue; } if ($byte eq ">") { $newstr .= "&gt"; next; }
if ($byte eq "\"") { $newstr .= "&quot"; continue; } if ($byte eq "\"") { $newstr .= "&quot"; next; }
if ($byte eq "'") { $newstr .= "&#39"; continue; } if ($byte eq "'") { $newstr .= "&#39"; next; }
$newstr .= $byte; $newstr .= $byte;
} }
@ -173,16 +180,10 @@ sub parse($$$) {
} }
elsif ($config->isUrl($uri)) { elsif ($config->isUrl($uri)) {
if ($config->isLocal($uri, $content)) { print '<div>', "\n";
# TODO: local http
$uri =~ s/gemini:/https:/;
$uri =~ s/\.gmi$/\.html/;
}
print '<div>', "\n";
print '<span class="link-delim">=></span> '; print '<span class="link-delim">=></span> ';
print '<a href="', escape($uri), '">', escape($content), '</a>', "\n"; print '<a href="', escape($config->handleRedirects($uri, $content)), '">', escape($content), '</a>', "\n";
print '</div>', "\n"; print '</div>', "\n";
} }
else { else {
@ -223,7 +224,7 @@ GetOptions (
'inline-video!' => \$config->inline->{video}, 'inline-video!' => \$config->inline->{video},
'inline-image!' => \$config->inline->{image}, 'inline-image!' => \$config->inline->{image},
'standalone!' => \$config->{standalone}, 'standalone!' => \$config->{standalone},
'local-domain=s' => \$config->{local_domains}, 'replace-domain=s%' => \$config->{replace_domains},
) or pod2usage(-exitval => 1, -verbose => 0); ) or pod2usage(-exitval => 1, -verbose => 0);
pod2usage(-verbose => $help) if $help; pod2usage(-verbose => $help) if $help;