7eb4c5
From aa7a2c99bff2a8d02d75f6b9f7155483cc94318c Mon Sep 17 00:00:00 2001
7eb4c5
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
7eb4c5
Date: Tue, 13 Aug 2019 16:49:21 +0200
7eb4c5
Subject: [PATCH 2/2] Search for X<> in the whole perlop document
7eb4c5
MIME-Version: 1.0
7eb4c5
Content-Type: text/plain; charset=UTF-8
7eb4c5
Content-Transfer-Encoding: 8bit
7eb4c5
7eb4c5
perlop documents many operators before "Regexp Quote-Like Operators"
7eb4c5
(X<operator, regexp>) section. A change introduced with "Refactor
7eb4c5
search_perlop RT#86506" (d8b23dcb1a) commit started to ignore those
7eb4c5
operators. E.g. A search for '==' did not found anything. A search for
7eb4c5
'<>' returned too many text and broke POD syntax.
7eb4c5
7eb4c5
This patch searches for X<> index entries in all sections and
7eb4c5
considers =head keywords in addition to =item as section delimeters.
7eb4c5
7eb4c5
Because some X<> entries exists on more places, this patch implements
7eb4c5
this strategy: First =item section that contains the X<> entry is
7eb4c5
returned. If there is no =item sections, last =head section is
7eb4c5
returned. If the =item entry is empty (like for 'tr'), the the output
7eb4c5
continues up to and including a next non-empty =item. This strategy is
7eb4c5
implemented in one pass.
7eb4c5
7eb4c5
Signed-off-by: Petr Písař <ppisar@redhat.com>
7eb4c5
---
7eb4c5
 lib/Pod/Perldoc.pm        | 116 ++++++++++++++++++++++++++------------
7eb4c5
 t/03_builtin_pod_output.t |   8 +++
7eb4c5
 2 files changed, 89 insertions(+), 35 deletions(-)
7eb4c5
7eb4c5
diff --git a/lib/Pod/Perldoc.pm b/lib/Pod/Perldoc.pm
7eb4c5
index cd52aa2..b54cc23 100644
7eb4c5
--- a/lib/Pod/Perldoc.pm
7eb4c5
+++ b/lib/Pod/Perldoc.pm
7eb4c5
@@ -1153,6 +1153,20 @@ sub search_perlvar {
7eb4c5
 
7eb4c5
 #..........................................................................
7eb4c5
 
7eb4c5
+# Check whether an item POD section contains any documentation text. The POD
7eb4c5
+# section is passed as refernce to list of lines.
7eb4c5
+# If there is no text, return true; otherwise false.
7eb4c5
+sub item_has_no_text {
7eb4c5
+    for (@{$_[0]}) {
7eb4c5
+        next if /^=over\s/;
7eb4c5
+        next if /^=item\s/;
7eb4c5
+        next if /^X</;
7eb4c5
+        next if /^\s*$/;
7eb4c5
+        return 0;
7eb4c5
+    }
7eb4c5
+    return 1;
7eb4c5
+}
7eb4c5
+
7eb4c5
 sub search_perlop {
7eb4c5
   my ($self,$found_things,$pod) = @_;
7eb4c5
 
7eb4c5
@@ -1166,60 +1180,92 @@ sub search_perlop {
7eb4c5
 
7eb4c5
   my $thing = $self->opt_f;
7eb4c5
 
7eb4c5
-  my $previous_line;
7eb4c5
+  my @previous_lines;
7eb4c5
+  my $stop_line;
7eb4c5
+  my $wrap_into_over;
7eb4c5
   my $push = 0;
7eb4c5
-  my $seen_item = 0;
7eb4c5
-  my $skip = 1;
7eb4c5
+  my $pod_candidate = [];
7eb4c5
 
7eb4c5
   while( my $line = <$fh> ) {
7eb4c5
     $line =~ /^=encoding\s+(\S+)/ && $self->set_encoding($fh, $1);
7eb4c5
-    # only start search after we hit the operator section
7eb4c5
-    if ($line =~ m!^X<operator, regexp>!) {
7eb4c5
-        $skip = 0;
7eb4c5
-    }
7eb4c5
 
7eb4c5
-    next if $skip;
7eb4c5
-
7eb4c5
-    # strategy is to capture the previous line until we get a match on X<$thingy>
7eb4c5
-    # if the current line contains X<$thingy>, then we push "=over", the previous line, 
7eb4c5
-    # the current line and keep pushing current line until we see a ^X<some-other-thing>, 
7eb4c5
-    # then we chop off final line from @$pod and add =back
7eb4c5
+    # A strategy is to capture the previous lines from =head or =item until we
7eb4c5
+    # get a match on X<$thing>.  If the current line contains X<$thing>, then
7eb4c5
+    # we push "=over" (in case of =item), the previous lines, the current line
7eb4c5
+    # and keep pushing current line until we see a terminating POD keyworkd
7eb4c5
+    # (=head, =item, =over, corrsponding to the starting POD keyword). Then we
7eb4c5
+    # append =back (in case of =item).
7eb4c5
     #
7eb4c5
-    # At that point, Bob's your uncle.
7eb4c5
-
7eb4c5
-    if ( $line =~ m!X<+\s*\Q$thing\E\s*>+!) {
7eb4c5
-        if ( $previous_line ) {
7eb4c5
-            push @$pod, "=over 8\n\n", $previous_line;
7eb4c5
-            $previous_line = "";
7eb4c5
+    # If this was =item, we are done. If the =item was empty (like two
7eb4c5
+    # consequtive =item-s documented at once) we continue gathering other
7eb4c5
+    # =item-s until we get some content. Then we are done.
7eb4c5
+    #
7eb4c5
+    # If this was a =head, we stash the POD section and do another search in
7eb4c5
+    # hope we will found =item section. (=item sections tends to be more
7eb4c5
+    # focused on =X<$thing> than =head sections.) If did not found any =item
7eb4c5
+    # section, we will return the last found =head section.
7eb4c5
+
7eb4c5
+    if ( $line =~ m!X<+\s*\Q$thing\E\s*>+! ) {
7eb4c5
+        if ( @previous_lines ) {
7eb4c5
+            push @$pod_candidate, "=over 8\n\n" if $wrap_into_over;
7eb4c5
+            push @$pod_candidate, @previous_lines;
7eb4c5
+            @previous_lines = ();
7eb4c5
         }
7eb4c5
-        push @$pod, $line;
7eb4c5
+        push @$pod_candidate, $line;
7eb4c5
         $push = 1;
7eb4c5
 
7eb4c5
     }
7eb4c5
-    elsif ( $push and $line =~ m!^=item\s*.*$! ) {
7eb4c5
-        $seen_item = 1;
7eb4c5
-    }
7eb4c5
-    elsif ( $push and $seen_item and $line =~ m!^X<+\s*[ a-z,?-]+\s*>+!) {
7eb4c5
+    elsif ( $push and $line =~ m/$stop_line/ ) {
7eb4c5
         $push = 0;
7eb4c5
-        $seen_item = 0;
7eb4c5
-        last;
7eb4c5
+
7eb4c5
+        # X exists twice in perlop. Prefer =item location over =head
7eb4c5
+        # location. We assume =item is more specific.
7eb4c5
+        if ($wrap_into_over) {
7eb4c5
+            # However, the X =item section is empty (except of bunch of
7eb4c5
+            # X<> kewords) and documented in the next =item section. Thus
7eb4c5
+            # continue until the so far gathered text looks empty.
7eb4c5
+            if ($line =~ /^=item\s/ && item_has_no_text($pod_candidate)) {
7eb4c5
+                $push = 1;
7eb4c5
+                push @$pod_candidate, $line;
7eb4c5
+                # and continue appending following =item section
7eb4c5
+            } else {
7eb4c5
+                # We have an =item with a content.
7eb4c5
+                push @$pod_candidate, "\n\n=back\n";
7eb4c5
+                # Replace pod with the candidate
7eb4c5
+                @$pod = @$pod_candidate;
7eb4c5
+                last;
7eb4c5
+            }
7eb4c5
+        } else {
7eb4c5
+            # Copy the candidate to pod
7eb4c5
+            push @$pod, @$pod_candidate;
7eb4c5
+            $pod_candidate = [];
7eb4c5
+            # And search for another occurance of the X<> reference with the
7eb4c5
+            # prospect it will be an =item.
7eb4c5
+        }
7eb4c5
     }
7eb4c5
     elsif ( $push ) {
7eb4c5
-        push @$pod, $line;
7eb4c5
-    }
7eb4c5
-
7eb4c5
-    else {
7eb4c5
-        $previous_line = $line;
7eb4c5
+        push @$pod_candidate, $line;
7eb4c5
+    }
7eb4c5
+
7eb4c5
+    if ( !$push ) {
7eb4c5
+        # Gather a smallest block starting with "=head" or "=item"
7eb4c5
+        if ($line =~ /^=head([1234])\s/) {
7eb4c5
+            $stop_line = join('', 1..$1);
7eb4c5
+            $stop_line = qr/^=head[$stop_line]\s/;
7eb4c5
+            $wrap_into_over = 0;
7eb4c5
+            @previous_lines = ();
7eb4c5
+        } elsif ($line =~ /^=item\s/) {
7eb4c5
+            $stop_line = qr/^=(?:item\s|back\b)/;
7eb4c5
+            $wrap_into_over = 1;
7eb4c5
+            @previous_lines = ();
7eb4c5
+        }
7eb4c5
+        push @previous_lines, $line;
7eb4c5
     }
7eb4c5
 
7eb4c5
   } #end while
7eb4c5
 
7eb4c5
   # we overfilled by 1 line, so pop off final array element if we have any
7eb4c5
   if ( scalar @$pod ) {
7eb4c5
-    pop @$pod;
7eb4c5
-
7eb4c5
-    # and add the =back
7eb4c5
-    push @$pod, "\n\n=back\n";
7eb4c5
     DEBUG > 8 and print "PERLOP POD --->" . (join "", @$pod) . "<---\n";
7eb4c5
   }
7eb4c5
   else {
7eb4c5
diff --git a/t/03_builtin_pod_output.t b/t/03_builtin_pod_output.t
7eb4c5
index 70f8549..d42a242 100644
7eb4c5
--- a/t/03_builtin_pod_output.t
7eb4c5
+++ b/t/03_builtin_pod_output.t
7eb4c5
@@ -24,6 +24,14 @@ my %builtins = (
7eb4c5
         qr/\A\s+"tr\/\*SEARCHLIST\*\/\*REPLACEMENTLIST\*\/cdsr"\n/,
7eb4c5
         qr/\n\s+eval "tr\/\$oldlist\/\$newlist\/, 1" or die \$\@;\n\n\z/
7eb4c5
     ],
7eb4c5
+    '==' => [ # CPAN RT#126015
7eb4c5
+        qr/\A\s+Equality Operators\n/,
7eb4c5
+        qr/\n\s+if \( fc\(\$x\) eq fc\(\$y\) \) \{ \.\.\. \}\n\n\z/
7eb4c5
+    ],
7eb4c5
+    '<>' => [ # CPAN RT#126015
7eb4c5
+        qr/\A\s+I\/O Operators\n/,
7eb4c5
+        qr/\n\s+for its regular truth value\.\n\n\z/
7eb4c5
+    ]
7eb4c5
 );
7eb4c5
 
7eb4c5
 plan tests => 5 * scalar keys %builtins;
7eb4c5
-- 
7eb4c5
2.21.0
7eb4c5