Class: Rsubtitle::SMI

Inherits:
Subtitle show all
Defined in:
lib/rsubtitle/smi.rb

Constant Summary collapse

SMI_RE =
%r(
  ^<sync\sstart=(\d+)> <p[^>]+> (?:\r?\n)? ( \&nbsp;(?=\r?\n) | (?<=\n).+?(?=\r?\n)  )
)xim

Instance Attribute Summary

Attributes inherited from Subtitle

#entries

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Subtitle

#format

Constructor Details

#initialize(file) ⇒ SMI

Returns a new instance of SMI.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rsubtitle/smi.rb', line 31

def initialize(file)
  @body = open(file).read
  lines = @body.scan(SMI_RE)
  
  @entries = []
  lines.each_with_index do |d, idx|
    next if d[1] == "&nbsp;"

    start = d[0].to_i
    duration = (lines[idx+1].nil?) ? 0 : (lines[idx+1][0].to_i - start)

    sub = d[1]
    sub.gsub!(/<br\s*\/?>/i, "\n") # br -> \n
    sub.gsub!(/<[^>]+?>/i, "") # strip tags
    sub.strip!
    

    e = Entry.new
    e.start = start
    e.body = sub
    e.duration = duration

    @entries << e
  end

end

Class Method Details

.parse(file) ⇒ Object



25
26
27
# File 'lib/rsubtitle/smi.rb', line 25

def parse(file)
  SMI.new(file)
end

Instance Method Details

#[](idx) ⇒ Object



62
63
64
# File 'lib/rsubtitle/smi.rb', line 62

def [](idx)
  @entries[idx]
end

#sizeObject



58
59
60
# File 'lib/rsubtitle/smi.rb', line 58

def size
  @entries.size
end