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