File Coverage

blib/lib/Pod/Simple/Role/WithHighlightConfig.pm
Criterion Covered Total %
statement 10 10 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 0 2 0.0
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Pod::Simple::Role::WithHighlightConfig;
2 1     1   723 use Moo::Role;
  1         2  
  1         9  
3              
4             our $VERSION = '0.004000';
5             $VERSION =~ tr/_//d;
6              
7 1     1   694 use namespace::clean;
  1         3  
  1         9  
8              
9       1 0   sub BUILD {}
10              
11             after BUILD => sub {
12             $_[0]->accept_targets('highlighter');
13             };
14              
15             has _highlight_config => (is => 'rw', init_arg => undef);
16             has _highlight_config_text => (is => 'rw', clearer => 1, predicate => 1, init_arg => undef);
17              
18             around _handle_element_start => sub {
19             my $orig = shift;
20             my $self = shift;
21             my ($element, $item) = @_;
22             if ($element eq 'for' && $item->{target_matching} eq 'highlighter') {
23             $self->_highlight_config({});
24             $self->_highlight_config_text('');
25             }
26             elsif ($element eq 'Verbatim' && $self->_highlight_config) {
27             local $self->{_verbatim_sub} = $orig;
28             return $self->start_highlight($item, $self->_highlight_config);
29             }
30             else {
31             $self->$orig(@_);
32             }
33             };
34              
35             around _handle_text => sub {
36             my $orig = shift;
37             my $self = shift;
38             my ($text) = @_;
39             if ($self->_has_highlight_config_text) {
40             $self->_highlight_config_text($self->_highlight_config_text . $text);
41             }
42             else {
43             $self->$orig(@_);
44             }
45             };
46              
47              
48             around _handle_element_end => sub {
49             my $orig = shift;
50             my $self = shift;
51             my ($element, $item) = @_;
52              
53             if ($element eq 'for' and $self->_has_highlight_config_text) {
54             my $text = $self->_highlight_config_text;
55             $self->_clear_highlight_config_text;
56             s/^\s+//, s/\s+$// for $text;
57             my $config = {};
58             for my $config_item (map { [ split /=/, $_, 2 ] } split /\s+/, $text) {
59             my ($key, $value) = @$config_item;
60             if ($key =~ /^(?:start_line|highlight|line_numbers|language)$/) {
61             if(!defined $value || !length $value) {
62             $self->whine($item->{start_line}, "Invalid empty $key setting.")
63             }
64             elsif ($key eq 'start_line' && $value !~ /^\d+$/) {
65             $self->whine($item->{start_line}, "Invalid non-number ($value) for $key setting.")
66             }
67             elsif ($key eq 'highlight' && $value !~ /^\d+(?:-\d+)?(?:,\d+(?:-\d+)?)*$/) {
68             $self->whine($item->{start_line}, "Invalid number sequence ($value) for $key setting.")
69             }
70             elsif ($key eq 'line_numbers' && $value !~ /^[01]$/) {
71             $self->whine($item->{start_line}, "Invalid boolean ($value) for $key setting.")
72             }
73             else {
74             $config->{$key} = $value;
75             }
76             }
77             elsif (!defined $value) {
78             $config->{language} = $key;
79             }
80             else {
81             $self->whine($item->{start_line}, "Invalid setting \"$key\".")
82             }
83             }
84             $self->_highlight_config($config);
85             }
86             else {
87             $self->$orig(@_);
88             }
89             };
90              
91             sub start_highlight {
92 6     6 0 9 my $self = shift;
93 6         14 my $orig = $self->{_verbatim_sub};
94 6         13 my ($item, $config) = @_;
95 6         25 $self->$orig(my $verb = 'Verbatim', $item, $config);
96             }
97              
98             1;
99             __END__