| 1 | package Silki::Schema::TextSearchResult; | ||
| 2 | { | ||
| 3 | $Silki::Schema::TextSearchResult::VERSION = '0.29'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | use HTML::Entities qw( encode_entities ); | ||
| 11 | use Silki::Schema::Page; | ||
| 12 | use Silki::Schema::Wiki; | ||
| 13 | use Silki::Types qw( Str ); | ||
| 14 | use Silki::Util qw( string_is_empty ); | ||
| 15 | |||
| 16 | use Moose; | ||
| 17 | |||
| 18 | has _result => ( | ||
| 19 | is => 'ro', | ||
| 20 | isa => Str, | ||
| 21 | required => 1, | ||
| 22 | init_arg => 'result', | ||
| 23 | ); | ||
| 24 | |||
| 25 | # The marker needs to be something that is unlikely to ever appear in normal | ||
| 26 | # text, does not look like wikitext, and will not changed when HTML-escaped. | ||
| 27 | sub HighlightMarker { | ||
| 28 | return '$@$@$@$'; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub title_for_display { | ||
| 32 | my $self = shift; | ||
| 33 | my $full_title = shift; | ||
| 34 | |||
| 35 | my ( $title, undef ) = split /\n/, $self->_result(), 2; | ||
| 36 | |||
| 37 | my $marker = $self->HighlightMarker(); | ||
| 38 | return encode_entities($full_title) unless $title =~ /\Q$marker/; | ||
| 39 | |||
| 40 | my ($matched) = $title =~ m{\Q$marker\E(.+?)\Q$marker}; | ||
| 41 | |||
| 42 | $full_title = encode_entities($full_title); | ||
| 43 | $full_title =~ s{\Q$matched}{<strong>$matched</strong>}; | ||
| 44 | |||
| 45 | return $full_title; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub body_for_display { | ||
| 49 | my $self = shift; | ||
| 50 | |||
| 51 | my ( undef, $body ) = split /\n/, $self->_result(), 2; | ||
| 52 | |||
| 53 | return q{} if string_is_empty($body); | ||
| 54 | |||
| 55 | my $marker = $self->HighlightMarker(); | ||
| 56 | |||
| 57 | $body = encode_entities($body); | ||
| 58 | |||
| 59 | $body =~ s{\Q$marker\E(.+?)\Q$marker}{<strong>$1</strong>}g; | ||
| 60 | |||
| 61 | $body =~ s/\n\n/ /g; | ||
| 62 | |||
| 63 | return $body; | ||
| 64 | } | ||
| 65 | |||
| 66 | __PACKAGE__->meta()->make_immutable(); | ||
| 67 | |||
| 68 | 1; | ||
| 69 | |||
| 70 | # ABSTRACT: Represents a text search result | ||
| 71 | |||
| 72 | __END__ |