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