Class: EhbrsRubyUtils::Videos::Subtitles::Sanitize::ContentSanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/ehbrs_ruby_utils/videos/subtitles/sanitize/content_sanitizer.rb

Constant Summary collapse

REMOVE_PATTERNS =
[%w[< >], %w[( )], ['[', ']']].map do |args|
  build_pattern(*args)
end.freeze
REMOVE_TERMS =
%w[subtitle osdb legenda @ united4ever unitedteam pt-subs capejuna maniacs
 |]
.map(&:downcase)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_pattern(slim, elim) ⇒ Object



15
16
17
# File 'lib/ehbrs_ruby_utils/videos/subtitles/sanitize/content_sanitizer.rb', line 15

def build_pattern(slim, elim)
  /#{::Regexp.quote(slim)}[^#{::Regexp.quote(elim)}]*#{::Regexp.quote(elim)}/
end

Instance Method Details

#line_processorsObject



29
30
31
32
33
# File 'lib/ehbrs_ruby_utils/videos/subtitles/sanitize/content_sanitizer.rb', line 29

def line_processors
  REMOVE_PATTERNS.map do |pattern|
    ::EhbrsRubyUtils::Videos::Subtitles::Sanitize::WithPatternMatcher.new(pattern)
  end
end

#outputObject



35
36
37
# File 'lib/ehbrs_ruby_utils/videos/subtitles/sanitize/content_sanitizer.rb', line 35

def output
  output_lines.join("\n")
end

#output_line(input_line, last_output_line) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/ehbrs_ruby_utils/videos/subtitles/sanitize/content_sanitizer.rb', line 51

def output_line(input_line, last_output_line)
  text = output_line_text(input_line.text)
  return nil if text.blank?

  r = input_line.dup
  r.sequence = last_output_line.if_present(1) { |v| v.sequence + 1 }
  r.text = text
  r
end

#output_line_text(text) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/ehbrs_ruby_utils/videos/subtitles/sanitize/content_sanitizer.rb', line 61

def output_line_text(text)
  text_processors.each do |term|
    text = term.process(text)
    return nil if text.blank?
  end

  text.map { |line| process_line(line) }.compact_blank
end

#output_linesObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ehbrs_ruby_utils/videos/subtitles/sanitize/content_sanitizer.rb', line 39

def output_lines
  r = []
  last_output_line = nil
  ::SRT::File.parse_string(input).lines.each do |input_line|
    output_line(input_line, last_output_line).if_present do |v|
      r << v
      last_output_line = v
    end
  end
  r
end

#process_line(line) ⇒ Object



70
71
72
# File 'lib/ehbrs_ruby_utils/videos/subtitles/sanitize/content_sanitizer.rb', line 70

def process_line(line)
  remove_tags(line)
end

#remove_tags(line) ⇒ Object



74
75
76
# File 'lib/ehbrs_ruby_utils/videos/subtitles/sanitize/content_sanitizer.rb', line 74

def remove_tags(line)
  line_processors.inject(line) { |a, e| e.process(a) }.strip
end

#text_processorsObject



78
79
80
81
82
# File 'lib/ehbrs_ruby_utils/videos/subtitles/sanitize/content_sanitizer.rb', line 78

def text_processors
  REMOVE_TERMS.map do |term|
    ::EhbrsRubyUtils::Videos::Subtitles::Sanitize::WithTermMatcher.new(term)
  end
end