You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

html.go 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) 2014 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. package html
  15. import (
  16. "github.com/blevesearch/bleve/registry"
  17. "github.com/blevesearch/bleve/search/highlight"
  18. )
  19. const Name = "html"
  20. const defaultHTMLHighlightBefore = "<mark>"
  21. const defaultHTMLHighlightAfter = "</mark>"
  22. type FragmentFormatter struct {
  23. before string
  24. after string
  25. }
  26. func NewFragmentFormatter(before, after string) *FragmentFormatter {
  27. return &FragmentFormatter{
  28. before: before,
  29. after: after,
  30. }
  31. }
  32. func (a *FragmentFormatter) Format(f *highlight.Fragment, orderedTermLocations highlight.TermLocations) string {
  33. rv := ""
  34. curr := f.Start
  35. for _, termLocation := range orderedTermLocations {
  36. if termLocation == nil {
  37. continue
  38. }
  39. // make sure the array positions match
  40. if !termLocation.ArrayPositions.Equals(f.ArrayPositions) {
  41. continue
  42. }
  43. if termLocation.Start < curr {
  44. continue
  45. }
  46. if termLocation.End > f.End {
  47. break
  48. }
  49. // add the stuff before this location
  50. rv += string(f.Orig[curr:termLocation.Start])
  51. // add the color
  52. rv += a.before
  53. // add the term itself
  54. rv += string(f.Orig[termLocation.Start:termLocation.End])
  55. // reset the color
  56. rv += a.after
  57. // update current
  58. curr = termLocation.End
  59. }
  60. // add any remaining text after the last token
  61. rv += string(f.Orig[curr:f.End])
  62. return rv
  63. }
  64. func Constructor(config map[string]interface{}, cache *registry.Cache) (highlight.FragmentFormatter, error) {
  65. before := defaultHTMLHighlightBefore
  66. beforeVal, ok := config["before"].(string)
  67. if ok {
  68. before = beforeVal
  69. }
  70. after := defaultHTMLHighlightAfter
  71. afterVal, ok := config["after"].(string)
  72. if ok {
  73. after = afterVal
  74. }
  75. return NewFragmentFormatter(before, after), nil
  76. }
  77. func init() {
  78. registry.RegisterFragmentFormatter(Name, Constructor)
  79. }