removed accidentally added whitespace
[catagits/HTML-Zoom.git] / maint / synopsis-extractor
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use FindBin qw($Bin);
7 use File::Find;
8 use File::Spec;
9
10 sub slurp_file {
11     undef $/;
12     open(my $fh, '<', shift) || return;
13     if(my $whole_file = <$fh>) {
14         return $whole_file;
15     } else {
16         return;
17     }
18 }
19
20 sub extract_synopsis {
21     my $string = shift || return;
22     my $head_or_cut = qr[head|cut]x;
23     if($string=~m/^=head1 SYNOPSIS\n(.*?)^=$head_or_cut/sm) {
24         my $extracted = $1;
25         $extracted=~s/^\S.+?$//m; # wipe out non code lines in pod
26         my $begin_end = qr[begin|end]x;
27         $extracted=~s/\n^=$begin_end testinfo\n\n//smg; # remove test block
28         return $extracted;
29     } else {
30         return;
31     }
32 }
33
34 sub normalize_indent {
35     my $extracted = shift || return;
36         if($extracted=~m/([ \t]+)(\S+)/) { 
37         $extracted=~s/^$1//gsm;
38         return $extracted;
39     } else {
40         return;
41     }
42 }
43
44 sub create_test_string {
45     my $extracted = shift || return;
46     return <<TEST
47 use strict;
48 use warnings FATAL => 'all';
49 use Test::More qw(no_plan);
50 $extracted
51 TEST
52 }
53
54 sub create_test_path_from_lib {
55     my $module_name = shift;
56     $module_name =~s/\.pm$//;
57     return File::Spec->catfile($Bin, '..', 't', 'synopsis', lc($module_name).'.t');
58 }
59
60 sub create_or_update_test_file {
61     my ($target, $synopsis_string) = @_;
62     return unless $synopsis_string && $target;
63     print "Writing $target\n";
64     open my $syn_test, '>', $target
65       or die "Couldn't open $target - you screwed something up. Go fix it.\n";
66     print $syn_test $synopsis_string;
67 }
68
69 sub wanted {
70     my $target_path =
71         create_test_path_from_lib $_;
72     my $synopsis_string =
73         create_test_string
74         normalize_indent
75         extract_synopsis
76         slurp_file $File::Find::name;
77     create_or_update_test_file
78         $target_path,
79         $synopsis_string,
80 }
81
82 find(\&wanted, File::Spec->catfile($Bin, '..', 'lib'));
83