From 29f24e1504de8ce8c724be6af9ef25883b7d3390 Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 20 Jan 2022 15:20:34 -0500 Subject: Pretty up the document --- gmi.pl | 98 +++++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 25 deletions(-) (limited to 'gmi.pl') diff --git a/gmi.pl b/gmi.pl index 03563dd..4f81a4f 100755 --- a/gmi.pl +++ b/gmi.pl @@ -28,15 +28,19 @@ use Term::ReadLine; # CPAN use Smart::Comments; # CPAN my $wrapper = Text::Wrapper->new(columns=>70, body_start=>''); -$ENV{pager} = 'less'; +#$ENV{PAGER} = 'most'; +$ENV{PAGER} = 'less -R'; my $use_pager = 1; my $pager_text_wrap_auto = 1; my $doc_out = 1; # display doc for human consumption? -my $doc; +#my $doc; +my @doc; my @links; my $current_url = ""; my @history; my $prompt = ""; +my $pretty_links = 1; +my $pretty_headers = 1; my %commands = ( 'url' => [sub { url($_[0]) }, 'Go to the specified URL.'], @@ -83,9 +87,11 @@ my %commands = ( 'd' => [sub { display() }, 'Alias of `display\'.' ], 'usepager' => [sub { toggle($use_pager) }, 'This toggles pager use. 1 = use pager, 0 = stdout.' ], + 'pretty_links' => [sub { toggle($pretty_links) }, "0 = do nothing, 1 = pretty. Currently $pretty_links."], + 'pretty_headers' => [sub { toggle($pretty_headers) }, "0 = do nothing, 1 = pretty. Currently $pretty_headers."], 'textwrap' => [sub { textwrap($_[0]) }, 'Set textwrap length.\nGiving \'auto\' will ' . "automatically determine the appropriate length. (Currently $wrapper->{columns})"], - 'pager' => [sub { pager($_[0]) }, "Set which pager to use. (currently $ENV{pager})"], + 'pager' => [sub { pager($_[0]) }, "Set which pager to use. (currently $ENV{PAGER})"], 'save' => [sub { ; }, 'Save the config settings. (Not yet implimented)'], 'help' => [sub { help($_[0]); }, 'Use help before a command to get extra info. (cmds to see commands)'], 'cmds' => [sub {cmds()}, 'Returns the available commands.'], @@ -150,7 +156,7 @@ sub url { my ($url) = @_; ### $url if (!$url) { print("Example: [gemini://]capsule.com/\n"); return 1;} - if ( $url !~ m|^.*://.*|) { + if (! has_scheme($url) ) { $url = "gemini://$url"; } @@ -170,36 +176,30 @@ sub url { ); if ($cl) { - my $path; - if (! $url->{path}) { - $path = '' - } - else { - $path = normalize_path("$url->{path}"); - } - #$url->{path} .= "/" if ($need_slash); - $current_url = return_url($url); ### $current_url ### $url - # ["absolute_url", "description"] - update_history( ["$current_url", ""] ); # gemini spec: # is an absolute path print $cl "$current_url\r\n"; - undef($doc); + #undef($doc); + undef(@doc); - while (<$cl>) { - $doc .= $_; - } + @doc = <$cl>; - get_links($doc); + # ["absolute_url", "description"] + update_history( ["$current_url", ""] ); + + get_links(); + + pretty_links() if ($pretty_links); + pretty_headers() if ($pretty_headers); if ($doc_out) { - display($doc); + display(); } } else { @@ -323,9 +323,10 @@ sub return_url { sub get_links { undef(@links); - my ($doc) = @_; - for my $line (split('\n', $doc)) { - if ($line =~ m/^=>[\s]*([\w\d\-\\\/\.\:\~\?\=\#]+)[\s]*(.+)?$/gm) { + #for my $line (split('\n', $doc)) { + for (@doc) { + #if ($line =~ m/^=>[\s]*([\w\d\-\\\/\.\:\~\?\=\#]+)[\s]*(.+)?$/gm) { + if ($_ =~ m/^=>[\s]*([\w\d\-\\\/\.\:\~\?\=\#]+)[\s]*(.+)?$/gm) { if ($2) { push(@links, ["$1", "$2"]); } @@ -488,11 +489,17 @@ sub display { my ($wc) = GetTerminalSize(); $wrapper->columns($wc); } + my $doc; + for (@doc) { + $doc .= $_; + } if ($use_pager) { - IO::Pager::open(my $FH) or warn($!); + my $token = IO::Pager::open(my $FH) or warn($!); + #print $FH $wrapper->wrap($doc); print $FH $wrapper->wrap($doc); } else { + #print($wrapper->wrap($doc)); print($wrapper->wrap($doc)); } } @@ -546,3 +553,44 @@ sub cmds { } print $wrapper->wrap($c); } + +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"; + $counter++; + } + } +} + +sub pretty_headers { + for (@doc) { + if ((substr($_,0,3)) eq '###') { + $_ =~ s/^###[\s]*(.*)$/colored("$1",'underline')/e; + } + if ((substr($_,0,2)) eq '##') { + $_ =~ s/^##[\s]*(.*)$/colored("$1",'bold')/e; + } + if ((substr($_,0,1)) eq '#') { + $_ =~ s/^#[\s]*(.*)$/colored("$1",'bold', 'underline')/e; + } + } +} + +sub _is_not_scheme_gemini { + ### @_ + my ($thing) = $_[0]; + if (has_scheme($thing)) { + $_[0] =~ m|(.*)://.*|; + if ($1 ne 'gemini') { + return " $1"; + } + } + return ''; +} -- cgit v1.2.3