diff options
author | jake <jake@jakes-mail.top> | 2022-01-20 17:15:54 -0500 |
---|---|---|
committer | jake <jake@jakes-mail.top> | 2022-01-20 17:15:54 -0500 |
commit | 45a04da1d30e77a0290d58cf1b02a8b3980cb09d (patch) | |
tree | 3853e547f5a9560649c9f40d15b6a246fee08767 /gmi.pl | |
parent | 29f24e1504de8ce8c724be6af9ef25883b7d3390 (diff) |
no longer agnostic towards status codes, pretty modes behave better
Diffstat (limited to 'gmi.pl')
-rwxr-xr-x | gmi.pl | 78 |
1 files changed, 53 insertions, 25 deletions
@@ -10,7 +10,7 @@ use bytes; use feature qw(refaliasing); no warnings qw(experimental::refaliasing); -our $VERSION = 'v0.0.22'; +our $VERSION = 'v0.0.23'; # TODO: # back() only works once; should fix this @@ -25,7 +25,7 @@ use Term::ANSIColor; # Core use Path::Naive qw(normalize_path); # CPAN use Text::ParseWords; # Core use Term::ReadLine; # CPAN -use Smart::Comments; # CPAN +#use Smart::Comments; # CPAN my $wrapper = Text::Wrapper->new(columns=>70, body_start=>''); #$ENV{PAGER} = 'most'; @@ -41,6 +41,8 @@ my @history; my $prompt = ""; my $pretty_links = 1; my $pretty_headers = 1; +my $pre_block = 0; +my $status_code; my %commands = ( 'url' => [sub { url($_[0]) }, 'Go to the specified URL.'], @@ -189,17 +191,19 @@ sub url { undef(@doc); @doc = <$cl>; - + # ["absolute_url", "description"] update_history( ["$current_url", ""] ); - - get_links(); + + if (determine_status_code()) { + get_links(); - pretty_links() if ($pretty_links); - pretty_headers() if ($pretty_headers); + pretty_links() if ($pretty_links); + pretty_headers() if ($pretty_headers); - if ($doc_out) { - display(); + if ($doc_out) { + display(); + } } } else { @@ -207,6 +211,18 @@ sub url { } } +sub determine_status_code { + $status_code = substr($doc[0],0,1); + if ($status_code == 2) { + return 1; + } + else { + print "$doc[0]"; + return 0; + } + ### $status_code; +} + sub urlrelative { my ($urlr) = @_; if (! $urlr) { $urlr = ""} @@ -326,7 +342,8 @@ sub get_links { #for my $line (split('\n', $doc)) { for (@doc) { #if ($line =~ m/^=>[\s]*([\w\d\-\\\/\.\:\~\?\=\#]+)[\s]*(.+)?$/gm) { - if ($_ =~ m/^=>[\s]*([\w\d\-\\\/\.\:\~\?\=\#]+)[\s]*(.+)?$/gm) { + _pre_block($_); + if ($_ =~ m/^=>[\s]*([\w\d\-\\\/\.\:\~\?\=\#]+)[\s]*(.+)?$/gm and not $pre_block) { if ($2) { push(@links, ["$1", "$2"]); } @@ -494,7 +511,7 @@ sub display { $doc .= $_; } if ($use_pager) { - my $token = IO::Pager::open(my $FH) or warn($!); + IO::Pager::open(my $FH) or warn($!); #print $FH $wrapper->wrap($doc); print $FH $wrapper->wrap($doc); } @@ -513,7 +530,9 @@ sub toggle { else { $t = 1; } - print "$t\n"; + if (! $_[1]) { + print "$t\n"; + } } sub textwrap { @@ -557,13 +576,15 @@ sub cmds { sub pretty_links { my $counter = 1; for (@doc) { - if ((substr($_,0,2)) eq '=>') { - # $1$2 $3 - #$_ =~ s|^=>[\s]*((.*?)://.*?)?[\s]*(.+)$|&_say_scheme_gemini($1) . "$3"|e; - - $_ =~ s/^=>[\s]*([\w\d\-\\\/\.\:\~\?\=\#]+)[\s]*(.+)?$/ - colored("[$counter" . _is_not_scheme_gemini($1) . "]",'underline') . " $2"/xe; - #$_ = colored("[$counter" . _is_not_scheme_gemini($1) ."]",'red') . " $2\n"; + _pre_block($_); + if ((substr($_,0,2)) eq '=>' and not $pre_block) { + $_ =~ m/^=>[\s]*([\w\d\-\\\/\.\:\~\?\=\#]+)[\s]*(.+)?$/; + if ($2) { + $_ = colored("[$counter" . _is_not_scheme_gemini($1) . "]",'underline') . " $2"; + } + else { + $_ = colored("[$counter" . _is_not_scheme_gemini($1) . "]",'underline') . " $1"; + } $counter++; } } @@ -571,14 +592,15 @@ sub pretty_links { sub pretty_headers { for (@doc) { - if ((substr($_,0,3)) eq '###') { - $_ =~ s/^###[\s]*(.*)$/colored("$1",'underline')/e; + _pre_block($_); + if ((substr($_,0,3)) eq '###' and not $pre_block) { + $_ =~ s/^###[\s]*(.*)$/colored("$1",'underline')/e; } - if ((substr($_,0,2)) eq '##') { - $_ =~ s/^##[\s]*(.*)$/colored("$1",'bold')/e; + if ((substr($_,0,2)) eq '##' and not $pre_block) { + $_ =~ s/^##[\s]*(.*)$/colored("$1",'bold')/e; } - if ((substr($_,0,1)) eq '#') { - $_ =~ s/^#[\s]*(.*)$/colored("$1",'bold', 'underline')/e; + if ((substr($_,0,1)) eq '#' and not $pre_block) { + $_ =~ s/^#[\s]*(.*)$/colored("$1",'bold', 'underline')/e; } } } @@ -594,3 +616,9 @@ sub _is_not_scheme_gemini { } return ''; } + +sub _pre_block { + if (substr($_[0],0,3) eq '```') { + toggle($pre_block,1); + } +} |