Class: Ttml::Document
- Inherits:
-
Object
- Object
- Ttml::Document
- Defined in:
- lib/ttml.rb
Overview
Minimal Timed Text Markup Language parsing and extraction.
Example:
>> doc = Ttml::Document.new('test/sample.xml')
=> [Ttml::Document]
>> doc.copyright
=> '(c) 2012 loop23'
>> doc.subtitles
=> [All subtitles]
>> doc.subtitles(0.0, 100.0)
=> [Subtitles from beginning to 100 seconds]
Instance Attribute Summary collapse
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
-
#namespaces ⇒ Object
readonly
Returns the value of attribute namespaces.
Instance Method Summary collapse
-
#copyright ⇒ Object
Returns document copyright.
-
#description ⇒ Object
Returns document description.
-
#initialize(file_or_stream) ⇒ Document
constructor
A new instance of Document.
-
#subtitle_stream(from = 0.0, to = 99999999.0) ⇒ Object
Returns subtitles from “from” to “to” (inclusive) as an array (or all subtitles if both are missing).
-
#title ⇒ Object
Returns document title.
Constructor Details
#initialize(file_or_stream) ⇒ Document
Returns a new instance of Document.
20 21 22 23 24 25 26 27 |
# File 'lib/ttml.rb', line 20 def initialize file_or_stream stream = file_or_stream.is_a?(IO) ? file_or_stream : File.open(file_or_stream) @doc = Nokogiri::XML(stream) @namespaces = @doc.collect_namespaces # puts "Ho namespaces? #{ @namespaces.inspect }" @subs_ns = @namespaces.invert["http://www.w3.org/2006/10/ttaf1"] @meta_ns = @namespaces.invert["http://www.w3.org/2006/10/ttaf1#metadata"].sub(/^xmlns:/,'') end |
Instance Attribute Details
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
18 19 20 |
# File 'lib/ttml.rb', line 18 def doc @doc end |
#namespaces ⇒ Object (readonly)
Returns the value of attribute namespaces.
18 19 20 |
# File 'lib/ttml.rb', line 18 def namespaces @namespaces end |
Instance Method Details
#copyright ⇒ Object
Returns document copyright
51 52 53 |
# File 'lib/ttml.rb', line 51 def copyright doc.xpath("//#{ @meta_ns }:copyright")[0].children[0].content end |
#description ⇒ Object
Returns document description
46 47 48 |
# File 'lib/ttml.rb', line 46 def description doc.xpath("//#{ @meta_ns }:description")[0].children[0].content end |
#subtitle_stream(from = 0.0, to = 99999999.0) ⇒ Object
Returns subtitles from “from” to “to” (inclusive) as an array (or all subtitles if both are missing). I tried using xpath functions, without success, as in xmlns:div/xmlns:p - any ideas?
33 34 35 36 37 38 |
# File 'lib/ttml.rb', line 33 def subtitle_stream from = 0.0, to = 99999999.0 doc.xpath("/#{ @subs_ns }:tt/#{ @subs_ns }:body/#{ @subs_ns }:div/#{ @subs_ns }:p").select {|n| # puts "Vedo se #{ n['begin'].to_f } >= #{ from } e se #{ n['end'].to_f } <= #{ to }" (n['begin'].to_f >= from) && (n['end'].to_f <= to) } end |
#title ⇒ Object
Returns document title
41 42 43 |
# File 'lib/ttml.rb', line 41 def title doc.xpath("//#{ @meta_ns }:title")[0].children[0].content end |