processor.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package processor
  5. import (
  6. "github.com/miniflux/miniflux2/model"
  7. "github.com/miniflux/miniflux2/reader/rewrite"
  8. "github.com/miniflux/miniflux2/reader/sanitizer"
  9. )
  10. // FeedProcessor handles the processing of feed contents.
  11. type FeedProcessor struct {
  12. feed *model.Feed
  13. scraperRules string
  14. rewriteRules string
  15. }
  16. // WithScraperRules adds scraper rules to the processing.
  17. func (f *FeedProcessor) WithScraperRules(rules string) {
  18. f.scraperRules = rules
  19. }
  20. // WithRewriteRules adds rewrite rules to the processing.
  21. func (f *FeedProcessor) WithRewriteRules(rules string) {
  22. f.rewriteRules = rules
  23. }
  24. // Process applies rewrite and scraper rules.
  25. func (f *FeedProcessor) Process() {
  26. for _, entry := range f.feed.Entries {
  27. entry.Content = sanitizer.Sanitize(entry.URL, entry.Content)
  28. entry.Content = rewrite.Rewriter(entry.URL, entry.Content, f.rewriteRules)
  29. }
  30. }
  31. // NewFeedProcessor returns a new FeedProcessor.
  32. func NewFeedProcessor(feed *model.Feed) *FeedProcessor {
  33. return &FeedProcessor{feed: feed}
  34. }