summaryrefslogtreecommitdiff
path: root/gmi.pl
diff options
context:
space:
mode:
Diffstat (limited to 'gmi.pl')
-rwxr-xr-xgmi.pl104
1 files changed, 69 insertions, 35 deletions
diff --git a/gmi.pl b/gmi.pl
index 9ed3f3b..15c8b20 100755
--- a/gmi.pl
+++ b/gmi.pl
@@ -10,7 +10,7 @@ use bytes;
use feature qw(refaliasing);
no warnings qw(experimental::refaliasing);
-our $VERSION = 'v0.0.18';
+our $VERSION = 'v0.0.19';
# TODO:
# back() only works once; should fix this
@@ -77,7 +77,6 @@ my %commands = (
'display' => [sub { display() }, 'Display the current page.' ],
'd' => [sub { display() }, 'Alias of `display\'.' ],
- 'aaa' => [sub { aaa() }, "This command returns `aaa'." ],
'usepager' => [sub { toggle('pager') }, 'This toggles pager use. 1 = use pager, 0 = stdout.' ],
'textwrap' => [sub { textwrap($_[0]) }, 'Set textwrap length.\nGiving \'auto\' will ' .
"automatically determine the appropriate length. (Currently $wrapper->{columns})"],
@@ -110,8 +109,8 @@ while ( defined ($_ = $term->readline($prompt)) ) {
}
sub do_command {
- # referenced command, referenced detail
- my ($rCmd, $rDetail) = @_;
+ # referenced command, referenced arg
+ my ($rCmd, $rArg) = @_;
# ## @_
# checking if %commands even has the command
@@ -121,7 +120,7 @@ sub do_command {
my $rsub = $commands{$$rCmd}[0];
#running the subroutine
- &$rsub($$rDetail);
+ &$rsub($$rArg);
}
# options doesn't have the command
else {
@@ -151,17 +150,27 @@ sub url {
my ($url, $mode) = @_;
### $url
if (!$url) { print("Example: [gemini://]capsule.com/\n"); return 1;}
+ if ($url eq '/') {
+ $url = './';
+ }
if ( $url !~ m|^.*://.*|) {
$url = "gemini://$url";
}
$url = parse_url($url);
# ## $url
+ my $need_slash;
# relative movement
if ($mode and $mode eq 1) {
if (!$current_url) {print "Relative movement impossible: no current URL.\n"; return 1;}
my $c_url = parse_url($current_url);
+ #if ($url->{host} and $url->{host} eq '.') { # './' or '/'
+ # $need_slash = 1;
+ #}
+
+ ### $url
+ ### $need_slash
if ($url->{path}) {
$url->{path} = "$url->{host}/$url->{path}";
}
@@ -211,8 +220,9 @@ sub url {
$path = ''
}
else {
- $path = "$url->{path}";
+ $path = normalize_path("$url->{path}");
}
+ $url->{path} .= "/" if ($need_slash);
$current_url = return_url($url);
### $current_url
@@ -223,7 +233,7 @@ sub url {
# gemini spec: <URL><CR><LF>
# <URL> is an absolute path
- print $cl "$url->{scheme}://$url->{host}/$path\r\n";
+ print $cl "$current_url\r\n";
undef($doc);
@@ -351,13 +361,14 @@ sub nav {
my $link;
- if ($setting == 0 and @links) {
+ if ($setting == 0 and @links and exists $links[$n][0]) {
$link = $links[$n][0];
- } elsif ($setting == 1 and @history) {
+ } elsif ($setting == 1 and @history and exists $history[$n][0]) {
$link = $history[$n][0];
}
else {
- print("'$n' isn't available.\n");
+ my $m = $n+1;
+ print("'$m' isn't available.\n");
return 1;
}
@@ -367,31 +378,58 @@ sub nav {
}
else {
my $c_url = parse_url($current_url);
- # relative navigation madness
- # ../../blog/my-post.gmi
- if ($link =~ m|\.{1,2}/.*|) {
- my $parsed_url_path = split_url_path($c_url->{path}, 256);
- my $url_path;
-
- # request doc will be different
- pop(@$parsed_url_path);
- for (@$parsed_url_path) {
- $url_path .= "/$_";
+ my $end_with_slash = 1 if ($link =~ m|.*/$|);
+ my $begin_with_slash = 1 if ($link =~ m|^/.*|);
+
+ ### current url doesnt have a path ?
+# if (! $c_url->{path}) {
+# delete($c_url->{path});
+# $c_url->{path} = $link;
+# if ($begin_with_slash) {
+# substr($c_url->{path},0,1,'');
+# }
+# url(return_url($c_url));
+# return 0;
+# }
+
+ ### absolute location ?
+ if ($begin_with_slash) {
+ delete($c_url->{path});
+ $c_url->{path} = $link;
+ if ($end_with_slash){
+ $c_url->{path} .= "/";
}
- substr($url_path, 0, 1, ''); # removing the first /
- ### $url_path
- ### $link
- $c_url->{path} = normalize_path("$url_path/$link");
-
- #url("$new_url->{scheme}://$new_url->{host}$path");
+ substr($c_url->{path},0,1,'');
url(return_url($c_url));
+ return 0;
}
- # normal navigation of current domain, probably something like:
- # blog/my-post.gmi
- else {
- #url("$new_url->{scheme}://$new_url->{host}/$link");
- $c_url->{path} = "$link";
+
+ ### current url is a directory ?
+ if ($current_url =~ m|.*/$|) {
+ $current_url .= $link;
+ #if ($end_with_slash) {
+ # $current_url .= "/";
+ #}
+ url(return_url($current_url));
+ return 0;
+ }
+ else {
+ ### current url must NOT be a directory
+ my $d;
+ if ($c_url->{path}) {
+ my $cp_url = split_url_path($c_url->{path}, 256);
+ pop(@$cp_url);
+ for (@$cp_url) {
+ $d .= "$_/";
+ }
+ }
+ $d .= $link;
+ $c_url->{path} = normalize_path($d);
+ if ($end_with_slash) {
+ $c_url->{path} .= "/";
+ }
url(return_url($c_url));
+ return 0;
}
}
}
@@ -420,10 +458,6 @@ sub back {
$doc_out = $dok_out if ($display == 1);
}
-sub aaa {
- print("aaa\n");
-}
-
sub display {
if ($pager_text_wrap_auto) {
my ($wc) = GetTerminalSize();