File Coverage

blib/lib/Pod/Simple/Role/WithHighlightConfig.pm
Criterion Covered Total %
statement 7 7 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod 0 2 0.0
total 10 12 83.3


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