Add replace domains
This commit is contained in:
parent
022b4e54be
commit
8dae20da39
39
cap2site.pl
39
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',
|
||||
<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 .= "&"; 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,15 +180,9 @@ 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 '<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";
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue
Block a user