| 1 | package App::CSE::Lucy::Highlight::Highlighter; | ||
| 2 | $App::CSE::Lucy::Highlight::Highlighter::VERSION = '0.016'; | ||
| 3 | use base qw/Lucy::Highlight::Highlighter/; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | ## See issue https://github.com/dagolden/class-insideout/issues/6 | ||
| 10 | ## to know why we cannot use that for now. | ||
| 11 | # use Class::InsideOut qw( private register ); | ||
| 12 | |||
| 13 | # Inside out attributes. | ||
| 14 | my %cse_command; | ||
| 15 | my %cse; | ||
| 16 | |||
| 17 | =head2 new | ||
| 18 | |||
| 19 | Adds the cse_command new argument. | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | sub new{ | ||
| 24 | my ($class, %options) = @_; | ||
| 25 | my $cse_command = delete $options{'cse_command'} || confess("Missing cse_command"); | ||
| 26 | my $self = $class->SUPER::new(%options); | ||
| 27 | # register($self); | ||
| 28 | $cse_command{ $self } = $cse_command; | ||
| 29 | $cse{ $self } = $cse_command->cse(); | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | =head2 encode | ||
| 34 | |||
| 35 | Overrides the Lucy encode method to avoid any HTMLI-zation. | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | sub encode{ | ||
| 40 | my ($self, $text) = @_; | ||
| 41 | return $text; | ||
| 42 | } | ||
| 43 | |||
| 44 | =head2 highlight | ||
| 45 | |||
| 46 | Highlights the bit of text using either colors or pure text. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub highlight{ | ||
| 51 | my ($self, $text) = @_; | ||
| 52 | |||
| 53 | my $cse = $cse{ $self }; | ||
| 54 | |||
| 55 | if( $cse->interactive() ){ | ||
| 56 | return $cse->colorizer->colored($text , 'yellow on_black'); | ||
| 57 | }else{ | ||
| 58 | return '[>'.$text.'<]'; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | sub DESTROY{ | ||
| 63 | my ($self) = @_; | ||
| 64 | delete $cse_command{ $self }; | ||
| 65 | delete $cse{ $self }; | ||
| 66 | $self->SUPER::DESTROY(); | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; |