diff --git a/src/snippet/mod.rs b/src/snippet/mod.rs index 09664968ec..09fd9c8d6f 100644 --- a/src/snippet/mod.rs +++ b/src/snippet/mod.rs @@ -11,6 +11,9 @@ use crate::{Document, Score, Searcher, Term}; const DEFAULT_MAX_NUM_CHARS: usize = 150; +const DEFAULT_SNIPPET_PREFIX: &str = ""; +const DEFAULT_SNIPPET_POSTFIX: &str = ""; + #[derive(Debug)] pub struct FragmentCandidate { score: Score, @@ -55,17 +58,28 @@ impl FragmentCandidate { pub struct Snippet { fragment: String, highlighted: Vec>, + snippet_prefix: String, + snippet_postfix: String, } -const HIGHLIGHTEN_PREFIX: &str = ""; -const HIGHLIGHTEN_POSTFIX: &str = ""; - impl Snippet { - /// Create a new, empty, `Snippet` + /// Create a new `Snippet`. + fn new(fragment: &str, highlighted: Vec>) -> Self { + Self { + fragment: fragment.to_string(), + highlighted, + snippet_prefix: DEFAULT_SNIPPET_PREFIX.to_string(), + snippet_postfix: DEFAULT_SNIPPET_POSTFIX.to_string(), + } + } + + /// Create a new, empty, `Snippet`. pub fn empty() -> Snippet { Snippet { fragment: String::new(), highlighted: Vec::new(), + snippet_prefix: String::new(), + snippet_postfix: String::new(), } } @@ -81,9 +95,9 @@ impl Snippet { for item in collapse_overlapped_ranges(&self.highlighted) { html.push_str(&encode_minimal(&self.fragment[start_from..item.start])); - html.push_str(HIGHLIGHTEN_PREFIX); + html.push_str(&self.snippet_prefix); html.push_str(&encode_minimal(&self.fragment[item.clone()])); - html.push_str(HIGHLIGHTEN_POSTFIX); + html.push_str(&self.snippet_postfix); start_from = item.end; } html.push_str(&encode_minimal( @@ -101,6 +115,12 @@ impl Snippet { pub fn highlighted(&self) -> &[Range] { &self.highlighted } + + /// Sets highlighted prefix and postfix. + pub fn set_snippet_prefix_postfix(&mut self, prefix: &str, postfix: &str) { + self.snippet_prefix = prefix.to_string(); + self.snippet_postfix = postfix.to_string() + } } /// Returns a non-empty list of "good" fragments. @@ -172,17 +192,11 @@ fn select_best_fragment_combination(fragments: &[FragmentCandidate], text: &str) .iter() .map(|item| item.start - fragment.start_offset..item.end - fragment.start_offset) .collect(); - Snippet { - fragment: fragment_text.to_string(), - highlighted, - } + Snippet::new(fragment_text, highlighted) } else { - // when there no fragments to chose from, - // for now create a empty snippet - Snippet { - fragment: String::new(), - highlighted: vec![], - } + // When there are no fragments to chose from, + // for now create an empty snippet. + Snippet::empty() } } @@ -673,4 +687,22 @@ Survey in 2016, 2017, and 2018."#; assert_eq!(snippet.fragment, "abc"); assert_eq!(snippet.to_html(), "abc"); } + + #[test] + fn test_snippet_generator_custom_highlighted_elements() { + let terms = btreemap! { String::from("rust") => 1.0, String::from("language") => 0.9 }; + let fragments = search_fragments(&From::from(SimpleTokenizer), TEST_TEXT, &terms, 100); + let mut snippet = select_best_fragment_combination(&fragments[..], TEST_TEXT); + assert_eq!( + snippet.to_html(), + "Rust is a systems programming language sponsored by\nMozilla which \ + describes it as a "safe" + ); + snippet.set_snippet_prefix_postfix("", ""); + assert_eq!( + snippet.to_html(), + "Rust is a systems programming language \ + sponsored by\nMozilla which describes it as a "safe" + ); + } }