|
| 1 | +// Copyright (c) 2022 Couchbase, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package plain |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/blevesearch/bleve/v2/registry" |
| 19 | + "github.com/blevesearch/bleve/v2/search/highlight" |
| 20 | +) |
| 21 | + |
| 22 | +const Name = "plain" |
| 23 | + |
| 24 | +const defaultPlainHighlightBefore = "<start>" |
| 25 | +const defaultPlainHighlightAfter = "<end>" |
| 26 | + |
| 27 | +type FragmentFormatter struct { |
| 28 | + before string |
| 29 | + after string |
| 30 | +} |
| 31 | + |
| 32 | +func NewFragmentFormatter(before, after string) *FragmentFormatter { |
| 33 | + return &FragmentFormatter{ |
| 34 | + before: before, |
| 35 | + after: after, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func (a *FragmentFormatter) Format(f *highlight.Fragment, orderedTermLocations highlight.TermLocations) string { |
| 40 | + rv := "" |
| 41 | + curr := f.Start |
| 42 | + for _, termLocation := range orderedTermLocations { |
| 43 | + if termLocation == nil { |
| 44 | + continue |
| 45 | + } |
| 46 | + // make sure the array positions match |
| 47 | + if !termLocation.ArrayPositions.Equals(f.ArrayPositions) { |
| 48 | + continue |
| 49 | + } |
| 50 | + if termLocation.Start < curr { |
| 51 | + continue |
| 52 | + } |
| 53 | + if termLocation.End > f.End { |
| 54 | + break |
| 55 | + } |
| 56 | + // add the stuff before this location |
| 57 | + rv += string(f.Orig[curr:termLocation.Start]) |
| 58 | + // start the highlight tag |
| 59 | + rv += a.before |
| 60 | + // add the term itself |
| 61 | + rv += string(f.Orig[termLocation.Start:termLocation.End]) |
| 62 | + // end the highlight tag |
| 63 | + rv += a.after |
| 64 | + // update current |
| 65 | + curr = termLocation.End |
| 66 | + } |
| 67 | + // add any remaining text after the last token |
| 68 | + rv += string(f.Orig[curr:f.End]) |
| 69 | + |
| 70 | + return rv |
| 71 | +} |
| 72 | + |
| 73 | +func Constructor(config map[string]interface{}, cache *registry.Cache) (highlight.FragmentFormatter, error) { |
| 74 | + before := defaultPlainHighlightBefore |
| 75 | + beforeVal, ok := config["before"].(string) |
| 76 | + if ok { |
| 77 | + before = beforeVal |
| 78 | + } |
| 79 | + after := defaultPlainHighlightAfter |
| 80 | + afterVal, ok := config["after"].(string) |
| 81 | + if ok { |
| 82 | + after = afterVal |
| 83 | + } |
| 84 | + return NewFragmentFormatter(before, after), nil |
| 85 | +} |
| 86 | + |
| 87 | +func init() { |
| 88 | + registry.RegisterFragmentFormatter(Name, Constructor) |
| 89 | +} |
0 commit comments