ttml 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ttml.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Luca S.G. de Marinis
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Ttml
2
+
3
+ Very simple Timed Text Markup Language parsing - I needed to parse a ttml file
4
+ and couldn't find a ruby implementation (probably because you don't really
5
+ need one!), so I wrote one. In the future I may support writing a ttml file
6
+ and/or a better, richer API.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'ttml'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install ttml
21
+
22
+ ## Usage
23
+
24
+ tt = Ttml.Document.new('test/sample.xml)
25
+ # Returns events until 100.0 seconds
26
+ tt.subtitle_stream(:from => 0.0, :to => 100.0).each { |event|
27
+ puts event.inspect
28
+ }
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ end
9
+
10
+ desc "Run tests"
11
+ task :default => :test
@@ -0,0 +1,3 @@
1
+ module Ttml
2
+ VERSION = "0.0.2"
3
+ end
data/lib/ttml.rb ADDED
@@ -0,0 +1,52 @@
1
+ require "ttml/version"
2
+ require "nokogiri"
3
+
4
+ module Ttml
5
+ # Minimal Timed Text Markup Language parsing and extraction.
6
+ #
7
+ # Example:
8
+ # >> doc = Ttml::Document.new('test/sample.xml')
9
+ # => [Ttml::Document]
10
+ # >> doc.copyright
11
+ # => '(c) 2012 loop23'
12
+ # >> doc.subtitles
13
+ # => [All subtitles]
14
+ # >> doc.subtitles(0.0, 100.0)
15
+ # => [Subtitles from beginning to 100 seconds]
16
+ class Document
17
+
18
+ attr_reader :doc
19
+
20
+ def initialize file_or_stream
21
+ stream = file_or_stream.is_a?(IO) ? file_or_stream : File.open(file_or_stream)
22
+ @doc = Nokogiri::XML(stream)
23
+ end
24
+
25
+ # Returns subtitles from "from" to "to" (inclusive) as an array
26
+ # (or all subtitles if both are missing).
27
+ # I tried using xpath functions, without success,
28
+ # as in xmlns:div/xmlns:p[number(@begin)=>746.63] - any ideas?
29
+ def subtitle_stream from = 0.0, to = 99999999.0
30
+ doc.xpath("/xmlns:tt/xmlns:body/xmlns:div/xmlns:p").select {|n|
31
+ # puts "Vedo se #{ n['begin'].to_f } >= #{ from } e se #{ n['end'].to_f } <= #{ to }"
32
+ (n['begin'].to_f >= from) && (n['end'].to_f <= to)
33
+ }
34
+ end
35
+
36
+ # Returns document title
37
+ def title
38
+ doc.xpath("//ns2:title")[0].children[0].content
39
+ end
40
+
41
+ # Returns document description
42
+ def description
43
+ doc.xpath("//ns2:description")[0].children[0].content
44
+ end
45
+
46
+ # Returns document copyright
47
+ def copyright
48
+ doc.xpath("//ns2:copyright")[0].children[0].content
49
+ end
50
+
51
+ end
52
+ end
data/test/sample.xml ADDED
@@ -0,0 +1,54 @@
1
+ <?xml version='1.0' encoding='UTF-8'?>
2
+ <tt xmlns:ns3="http://www.w3.org/2006/10/ttaf1#style"
3
+ xmlns:ns2="http://www.w3.org/2006/10/ttaf1#metadata" xmlns="http://www.w3.org/2006/10/ttaf1" xml:lang="">
4
+ <head>
5
+ <ns2:title>Timed Text DFPX</ns2:title>
6
+ <ns2:copyright>2012 (c) loop</ns2:copyright>
7
+ <styling>
8
+ <style ns3:textAlign="center" ns3:fontSize="22px" ns3:fontFamily="proportionalSansSerif" ns3:color="yellow"
9
+ xml:id="s1" />
10
+ <style ns3:color="yellow" style="s1" xml:id="s2" />
11
+ <style ns3:textAlign="end" style="s1" xml:id="s1Right" />
12
+ <style ns3:textAlign="start" style="s2" xml:id="s2Left" />
13
+ </styling>
14
+ <layout>
15
+ <region xml:id="subtitleArea" ns3:padding="5px 3px" ns3:extent="560px 62px" ns3:displayAlign="after"
16
+ ns3:backgroundColor="black" style="s1" />
17
+ </layout>
18
+ </head>
19
+ <body region="subtitleArea">
20
+ <div>
21
+ <p end="362.05s" begin="358.72s"><![CDATA[<p align="left" ><font color="#ffa500">Signori Consiglieri siete pregati di prendere posto</font></p>]]></p>
22
+ <p end="405.94s" begin="405.81s"><![CDATA[<p align="left" ><font color="#ffa500">e</font></p>]]></p>
23
+ <p end="463.13s" begin="462.12s"><![CDATA[<p align="left" ><font color="#ffa500">di attivare</font></p>]]></p>
24
+ <p end="472s" begin="470.44s"><![CDATA[<p align="left" ><font color="#ffa500">la carta.</font></p>]]></p>
25
+ <p end="474.63s" begin="472s"><![CDATA[<p align="left" ><font color="#ffa500">Il Segretario</font></p>]]></p>
26
+ <p end="475.92s" begin="474.63s"><![CDATA[<p align="left" ><font color="#ffa500">Generale dia corso all'appello.</font></p>]]></p>
27
+ <p end="621.21s" begin="618.41s"><![CDATA[<p align="left" ><font color="#ffa500">Avuta la totalita' dei signori Consiglieri,</font></p>]]></p>
28
+ <p end="624.53s" begin="621.21s"><![CDATA[<p align="left" ><font color="#ffa500">dichiaro aperti i lavori del Consiglio Provinciale,</font></p>]]></p>
29
+ <p end="627.3s" begin="624.53s"><![CDATA[<p align="left" ><font color="#ffa500">ricordando a tutti i signori Consiglieri, al signor</font></p>]]></p>
30
+ <p end="630.25s" begin="627.3s"><![CDATA[<p align="left" ><font color="#ffa500">Presidente della Provincia e ai signori Assessori,</font></p>]]></p>
31
+ <p end="631.98s" begin="630.25s"><![CDATA[<p align="left" ><font color="#ffa500">che i</font></p>]]></p>
32
+ <p end="633.73s" begin="631.98s"><![CDATA[<p align="left" ><font color="#ffa500"></font></p>]]></p>
33
+ <p end="635.83s" begin="633.73s"><![CDATA[<p align="left" ><font color="#ffa500">lavori del Consiglio Provinciale sono in</font></p>]]></p>
34
+ <p end="639.53s" begin="635.83s"><![CDATA[<p align="left" ><font color="#ffa500">trasmissione diretta.</font></p>]]></p>
35
+ <p end="643.37s" begin="639.53s"><![CDATA[<p align="left" ><font color="#ffa500">Passiamo alla trattazione dell'ordine del giorno, la prima proposta</font></p>]]></p>
36
+ <p end="647.09s" begin="643.37s"><![CDATA[<p align="left" ><font color="#ffa500">e': 'Approvazione del verbale della seduta del Consiglio Provinciale</font></p>]]></p>
37
+ <p end="650.46s" begin="647.09s"><![CDATA[<p align="left" ><font color="#ffa500">del 3 dicembre 2009, del 16</font></p>]]></p>
38
+ <p end="654.02s" begin="650.46s"><![CDATA[<p align="left" ><font color="#ffa500">dicembre 2009 e del 21 dicembre 2009'.</font></p>]]></p>
39
+ <p end="656.21s" begin="654.02s"><![CDATA[<p align="left" ><font color="#ffa500"></font></p>]]></p>
40
+ <p end="658.41s" begin="656.21s"><![CDATA[<p align="left" ><font color="#ffa500">Chi avesse delle osservazioni da muovere</font></p>]]></p>
41
+ <p end="662.19s" begin="658.41s"><![CDATA[<p align="left" ><font color="#ffa500">in ordine al contenuto dei verbali, e' autorizzato.</font></p>]]></p>
42
+ <p end="664.58s" begin="662.19s"><![CDATA[<p align="left" ><font color="#ffa500"></font></p>]]></p>
43
+ <p end="669.4s" begin="664.58s"><![CDATA[<p align="left" ><font color="#ffa500">Nessuna questione' Pongo in votazione l'approvazione del verbale</font></p>]]></p>
44
+ <p end="672.38s" begin="669.4s"><![CDATA[<p align="left" ><font color="#ffa500">del Consiglio Provinciale del 3 dicembre 2009. Prego.</font></p>]]></p>
45
+ <p end="709.28s" begin="703.82s"><![CDATA[<p align="left" ><font color="#ffa500">37 votanti, 37 favorevoli. Il Consiglio approva.</font></p>]]></p>
46
+ <p end="713.9s" begin="709.28s"><![CDATA[<p align="left" ><font color="#ffa500">Pongo in votazione il verbale del Consiglio Provinciale</font></p>]]></p>
47
+ <p end="717.37s" begin="713.9s"><![CDATA[<p align="left" ><font color="#ffa500">del 16 dicembre 2009.</font></p>]]></p>
48
+ <p end="719.52s" begin="717.37s"><![CDATA[<p align="left" ><font color="#ffa500">I Signori Consiglieri sono pregati di votare.</font></p>]]></p>
49
+ <p end="744.2s" begin="738.91s"><![CDATA[<p align="left" ><font color="#ffa500">36 votanti, 36 favorevoli. Il Consiglio approva.</font></p>]]></p>
50
+ <p end="746.63s" begin="744.2s"><![CDATA[<p align="left" ><font color="#ffa500">Pongo in votazione il</font></p>]]></p>
51
+ <p end="749.38s" begin="746.63s"><![CDATA[<p align="left" ><font color="#ffa500">verbale della seduta del 21 dicembre 2009.</font></p>]]></p>
52
+ </div>
53
+ </body>
54
+ </tt>
data/test/test_ttml.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'test/unit'
2
+ require 'ttml'
3
+
4
+ class TtmlTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @doc = Ttml::Document.new(File.join(File.dirname(__FILE__), 'sample.xml'))
8
+ end
9
+
10
+ def test_class
11
+ assert @doc.is_a?(Ttml::Document)
12
+ end
13
+
14
+ def test_copyright
15
+ assert_equal '2012 (c) loop', @doc.copyright
16
+ end
17
+
18
+ def test_title
19
+ assert_equal 'Timed Text DFPX', @doc.title
20
+ end
21
+
22
+ def test_subs_no_param
23
+ assert @doc.subtitle_stream.is_a?(Array), "Not a NodeSet!"
24
+ end
25
+
26
+ def test_subs_start_param
27
+ assert @doc.subtitle_stream(99999999.0).empty?
28
+ assert_equal 1, @doc.subtitle_stream(746.0).size
29
+ end
30
+
31
+ def test_subs_end_param
32
+ assert @doc.subtitle_stream(0.0, 0.0).empty?
33
+ assert_equal 1, @doc.subtitle_stream(746.63, 749.38).size
34
+ end
35
+
36
+
37
+ end
data/ttml.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/ttml/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Luca S.G. de Marinis"]
6
+ gem.email = ["loop23@gmail.com"]
7
+ gem.description = %q{Parse a ttml file}
8
+ gem.summary = %q{Minimal parsing for timed text markup language (http://www.w3.org/TR/ttaf1-dfxp/)}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "ttml"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Ttml::VERSION
17
+
18
+ gem.add_runtime_dependency "nokogiri"
19
+
20
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ttml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Luca S.G. de Marinis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Parse a ttml file
31
+ email:
32
+ - loop23@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE
40
+ - README.md
41
+ - Rakefile
42
+ - lib/ttml.rb
43
+ - lib/ttml/version.rb
44
+ - test/sample.xml
45
+ - test/test_ttml.rb
46
+ - ttml.gemspec
47
+ homepage: ''
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.24
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Minimal parsing for timed text markup language (http://www.w3.org/TR/ttaf1-dfxp/)
71
+ test_files:
72
+ - test/sample.xml
73
+ - test/test_ttml.rb