I am synopsis extractor!
John Napiorkowski [Fri, 24 Sep 2010 01:12:38 +0000 (21:12 -0400)]
maint/synopsis-extractor
t/synopsis.t [deleted file]

index f23a999..fb67966 100755 (executable)
@@ -9,7 +9,7 @@ use File::Spec;
 
 sub slurp_file {
     undef $/;
-    open(my $fh, '<', shift);
+    open(my $fh, '<', shift) || return;
     if(my $whole_file = <$fh>) {
         return $whole_file;
     } else {
@@ -21,7 +21,12 @@ sub extract_synopsis {
     my $string = shift || return;
     my $head_or_cut = qr[head|cut]x;
     if($string=~m/^=head1 SYNOPSIS\n(.*?)^=$head_or_cut/sm) {
-        return $1;
+        my $extracted = $1;
+        $extracted=~s/^\S.+?$//m; # wipe out non code lines in pod
+        my $begin_end = qr[begin|end]x;
+        $extracted=~s/\n^=$begin_end testinfo\n\n//smg; # remove
+
+        return $extracted;
     } else {
         return;
     }
@@ -29,8 +34,7 @@ sub extract_synopsis {
 
 sub normalize_indent {
     my $extracted = shift || return;
-
-    if($extracted=~m/([ \t]+)(\S+)/) {
+        if($extracted=~m/([ \t]+)(\S+)/) { 
         $extracted=~s/^$1//gsm;
         return $extracted;
     } else {
@@ -54,24 +58,28 @@ sub create_test_path_from_lib {
     return File::Spec->catfile($Bin, '..', 't', 'synopsis', lc($module_name).'.t');
 }
 
-sub create_or_update_test {
-    my ($string, $target) = @_;
-    return unless $string && $target;
+sub create_or_update_test_file {
+    my ($target, $synopsis_string) = @_;
+    return unless $synopsis_string && $target;
     print "Writing $target\n";
     open my $syn_test, '>', $target
       or die "Couldn't open $target - you screwed something up. Go fix it.\n";
-    print $syn_test $string;
+    print $syn_test $synopsis_string;
 }
 
 find(sub {
-    return unless $_=~/pm$/;
-    create_or_update_test(
-        (create_test_string
+    my $target_path =
+        create_test_path_from_lib $_;
+    my $synopsis_string =
+        create_test_string
         normalize_indent
         extract_synopsis
-        slurp_file $File::Find::name),
-        create_test_path_from_lib($_),
-    );
-}, File::Spec->catfile($Bin, '..', 'lib'));
+        slurp_file $File::Find::name;
+
+    create_or_update_test_file
+        $target_path,
+        $synopsis_string,
+
+    }, File::Spec->catfile($Bin, '..', 'lib'));
 
 
diff --git a/t/synopsis.t b/t/synopsis.t
deleted file mode 100644 (file)
index 8a3ac11..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-use strict;
-use warnings FATAL => 'all';
-use Test::More qw(no_plan);
-
-use HTML::Zoom;
-my $template = <<HTML;
-<html>
-  <head>
-    <title>Hello people</title>
-  </head>
-  <body>
-    <h1 id="greeting">Placeholder</h1>
-    <div id="list">
-      <span>
-        <p>Name: <span class="name">Bob</span></p>
-        <p>Age: <span class="age">23</span></p>
-      </span>
-      <hr class="between" />
-    </div>
-  </body>
-</html>
-HTML
-my $output = HTML::Zoom
-  ->from_html($template)
-  ->select('title, #greeting')->replace_content('Hello world & dog!')
-  ->select('#list')->repeat_content(
-      [
-        sub {
-          $_->select('.name')->replace_content('Matt')
-            ->select('.age')->replace_content('26')
-        },
-        sub {
-          $_->select('.name')->replace_content('Mark')
-            ->select('.age')->replace_content('0x29')
-        },
-        sub {
-          $_->select('.name')->replace_content('Epitaph')
-            ->select('.age')->replace_content('<redacted>')
-        },
-      ],
-      { repeat_between => '.between' }
-    )
-  ->to_html;
-my $expect = <<HTML;
-<html>
-  <head>
-    <title>Hello world &amp; dog!</title>
-  </head>
-  <body>
-    <h1 id="greeting">Hello world &amp; dog!</h1>
-    <div id="list">
-      <span>
-        <p>Name: <span class="name">Matt</span></p>
-        <p>Age: <span class="age">26</span></p>
-      </span>
-      <hr class="between" />
-      <span>
-        <p>Name: <span class="name">Mark</span></p>
-        <p>Age: <span class="age">0x29</span></p>
-      </span>
-      <hr class="between" />
-      <span>
-        <p>Name: <span class="name">Epitaph</span></p>
-        <p>Age: <span class="age">&lt;redacted&gt;</span></p>
-      </span>
-      
-    </div>
-  </body>
-</html>
-HTML
-is($output, $expect, 'Synopsis code works ok');;