File Coverage

blib/lib/Doc/Simply/Extractor.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Doc::Simply::Extractor;
2              
3 4     4   2222 use strict;
  4         10  
  4         138  
4 4     4   23 use warnings;
  4         7  
  4         110  
5              
6             # This is a dummy package containing Extractor::SlashStar & Extractor::SimplePound
7             #
8             # The ->extract method returns an ARRAY (reference) yielding either:
9             #
10             # line =>
11             # block =>
12              
13 4     4   3728 use Text::FixEOL;
  4         15080  
  4         286  
14             our $fixer = Text::FixEOL->new;
15              
16             package # Hide
17             Doc::Simply::Extractor::SlashStar;
18              
19             =head1 NAME
20              
21             Doc::Simply::Extractor::SlashStar - Extract content from /* ... */ and // ... style commentary
22              
23             =head1 DESCRIPTION
24              
25             Doc::Simply::Extractor::SlashStar uses L to parse JavaScript, Java, C, C++ content and extract
26             only the comments
27              
28             =cut
29              
30 4     4   3712 use Any::Moose;
  4         166371  
  4         28  
31 4     4   5058 use Doc::Simply::Carp;
  4         12  
  4         27  
32              
33 4     4   7695 use String::Comments::Extract;
  0            
  0            
34              
35             sub extract {
36             my $self = shift;
37             my $source = shift;
38              
39             return unless $source;
40              
41             $source = $fixer->fix_eol($source);
42             my $comments = String::Comments::Extract::SlashStar->extract($source);
43              
44             my @comments;
45             while ($comments =~ m{/\*(.*?)\*/|//(.*?)$}msg) {
46             next unless defined $1 || defined $2;
47             push @comments, defined $1 ? [ block => $1 ] : [ line => $2 ];
48             }
49              
50             return \@comments;
51             }
52              
53             package # Hide
54             Doc::Simply::Extractor::SimplePound;
55              
56             =head1 NAME
57              
58             Doc::Simply::Extractor::SimplePound - Extract content from # ... style commentary
59              
60             =cut
61              
62             use Any::Moose;
63             use Doc::Simply::Carp;
64              
65             # TODO Does not deal with multi-line strings, etc.
66              
67             has _extractor => qw/is ro lazy_build 1/;
68             sub _build__extractor {
69             my $self = shift;
70             return Doc::Simply::Extract::Match->new(filter => sub { return unless s/^\s*#//; $_ });
71             }
72              
73             sub extract {
74             my $self = shift;
75             return $self->_extractor->extract(@_);
76             }
77              
78             package Doc::Simply::Extractor::Filter;
79              
80             use Any::Moose;
81             use Doc::Simply::Carp;
82              
83             has filter => qw/is ro required 1 isa CodeRef/;
84              
85             sub extract {
86             my $self = shift;
87             my $source = shift;
88              
89             return unless $source;
90              
91             my (@source, @comments)
92             ;
93             if (ref $source eq "ARRAY") {
94             @source = @$source;
95             }
96             elsif (ref $source eq "") {
97             $source = $fixer->fix_eol($source);
98             @source = split m/\n/, $source;
99             }
100             else {
101             croak "Don't understand source $source";
102             }
103              
104             my $filter = $self->filter;
105              
106             {
107             local $_;
108             for my $line (@source) {
109             next unless $line;
110             next unless defined ($line = $filter->($_));
111             push @comments, [ line => $line ];
112             }
113             }
114              
115             return \@comments;
116             }
117              
118             1;