ttml 0.0.2 → 0.0.3
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/lib/ttml.rb +9 -5
- data/lib/ttml/version.rb +1 -1
- data/test/sample_2.xml +17 -0
- data/test/test_ttml.rb +8 -1
- data/ttml.gemspec +1 -1
- metadata +5 -3
data/lib/ttml.rb
CHANGED
@@ -15,11 +15,15 @@ module Ttml
|
|
15
15
|
# => [Subtitles from beginning to 100 seconds]
|
16
16
|
class Document
|
17
17
|
|
18
|
-
attr_reader :doc
|
18
|
+
attr_reader :doc, :namespaces
|
19
19
|
|
20
20
|
def initialize file_or_stream
|
21
21
|
stream = file_or_stream.is_a?(IO) ? file_or_stream : File.open(file_or_stream)
|
22
22
|
@doc = Nokogiri::XML(stream)
|
23
|
+
@namespaces = @doc.collect_namespaces
|
24
|
+
# puts "Ho namespaces? #{ @namespaces.inspect }"
|
25
|
+
@subs_ns = @namespaces.invert["http://www.w3.org/2006/10/ttaf1"]
|
26
|
+
@meta_ns = @namespaces.invert["http://www.w3.org/2006/10/ttaf1#metadata"].sub(/^xmlns:/,'')
|
23
27
|
end
|
24
28
|
|
25
29
|
# Returns subtitles from "from" to "to" (inclusive) as an array
|
@@ -27,7 +31,7 @@ module Ttml
|
|
27
31
|
# I tried using xpath functions, without success,
|
28
32
|
# as in xmlns:div/xmlns:p[number(@begin)=>746.63] - any ideas?
|
29
33
|
def subtitle_stream from = 0.0, to = 99999999.0
|
30
|
-
doc.xpath("
|
34
|
+
doc.xpath("/#{ @subs_ns }:tt/#{ @subs_ns }:body/#{ @subs_ns }:div/#{ @subs_ns }:p").select {|n|
|
31
35
|
# puts "Vedo se #{ n['begin'].to_f } >= #{ from } e se #{ n['end'].to_f } <= #{ to }"
|
32
36
|
(n['begin'].to_f >= from) && (n['end'].to_f <= to)
|
33
37
|
}
|
@@ -35,17 +39,17 @@ module Ttml
|
|
35
39
|
|
36
40
|
# Returns document title
|
37
41
|
def title
|
38
|
-
doc.xpath("
|
42
|
+
doc.xpath("//#{ @meta_ns }:title")[0].children[0].content
|
39
43
|
end
|
40
44
|
|
41
45
|
# Returns document description
|
42
46
|
def description
|
43
|
-
doc.xpath("
|
47
|
+
doc.xpath("//#{ @meta_ns }:description")[0].children[0].content
|
44
48
|
end
|
45
49
|
|
46
50
|
# Returns document copyright
|
47
51
|
def copyright
|
48
|
-
doc.xpath("
|
52
|
+
doc.xpath("//#{ @meta_ns }:copyright")[0].children[0].content
|
49
53
|
end
|
50
54
|
|
51
55
|
end
|
data/lib/ttml/version.rb
CHANGED
data/test/sample_2.xml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
<?xml version='1.0'?><ns2:tt xmlns:ns4="http://www.w3.org/2006/10/ttaf1#parameter" xmlns:ns3="http://www.w3.org/2006/10/ttaf1#style" xmlns:ns2="http://www.w3.org/2006/10/ttaf1" xmlns:ns1="http://www.w3.org/2006/10/ttaf1#metadata" xml:lang="it"><ns2:head><ns1:title>Timed Text DFPX</ns1:title>
|
2
|
+
<ns1:copyright>2010 (c) PerVoice S.p.A.</ns1:copyright>
|
3
|
+
<ns2:styling><ns2:style xml:id="s1" ns3:textAlign="left" ns3:fontSize="22px" ns3:fontFamily="proportionalSansSerif" ns3:color="yellow"/>
|
4
|
+
<ns2:style xml:id="s2" ns3:color="yellow" style="s1"/>
|
5
|
+
<ns2:style xml:id="s1Right" ns3:textAlign="end" style="s1"/>
|
6
|
+
<ns2:style xml:id="s2Left" ns3:textAlign="start" style="s2"/>
|
7
|
+
</ns2:styling>
|
8
|
+
<ns2:layout><ns2:region ns3:padding="5px 3px" ns3:extent="560px 62px" ns3:displayAlign="after" ns3:backgroundColor="black" style="s1" xml:id="subtitleArea"/>
|
9
|
+
</ns2:layout>
|
10
|
+
</ns2:head>
|
11
|
+
<ns2:body region="subtitleArea"><ns2:div><ns2:p end="104.34s" begin="104.34s"><![CDATA[<p align="left" ><font color="#ffa500">Sono</font></p>]]></ns2:p>
|
12
|
+
<ns2:p end="107.76s" begin="104.34s"><![CDATA[<p align="left" ><font color="#ffa500"></font></p>]]></ns2:p>
|
13
|
+
<ns2:p end="117.8s" begin="107.76s"><![CDATA[<p align="left" ><font color="#ffa500">presenti 25</font></p>]]></ns2:p>
|
14
|
+
<ns2:p end="117.8s" begin="117.8s"><![CDATA[<p align="left" ><font color="#ffa500">Consiglieri,</font></p>]]></ns2:p>
|
15
|
+
</ns2:div>
|
16
|
+
</ns2:body>
|
17
|
+
</ns2:tt>
|
data/test/test_ttml.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
1
2
|
require 'test/unit'
|
2
3
|
require 'ttml'
|
3
4
|
|
@@ -20,7 +21,7 @@ class TtmlTest < Test::Unit::TestCase
|
|
20
21
|
end
|
21
22
|
|
22
23
|
def test_subs_no_param
|
23
|
-
assert @doc.subtitle_stream.is_a?(Array)
|
24
|
+
assert @doc.subtitle_stream.is_a?(Array)
|
24
25
|
end
|
25
26
|
|
26
27
|
def test_subs_start_param
|
@@ -33,5 +34,11 @@ class TtmlTest < Test::Unit::TestCase
|
|
33
34
|
assert_equal 1, @doc.subtitle_stream(746.63, 749.38).size
|
34
35
|
end
|
35
36
|
|
37
|
+
def test_other_file
|
38
|
+
# (with different namespaces)
|
39
|
+
doc = Ttml::Document.new(File.join(File.dirname(__FILE__), 'sample_2.xml'))
|
40
|
+
assert_equal 'Timed Text DFPX', doc.title
|
41
|
+
assert @doc.subtitle_stream.is_a?(Array)
|
42
|
+
end
|
36
43
|
|
37
44
|
end
|
data/ttml.gemspec
CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
|
|
6
6
|
gem.email = ["loop23@gmail.com"]
|
7
7
|
gem.description = %q{Parse a ttml file}
|
8
8
|
gem.summary = %q{Minimal parsing for timed text markup language (http://www.w3.org/TR/ttaf1-dfxp/)}
|
9
|
-
gem.homepage = ""
|
9
|
+
gem.homepage = "http://github.com/loop23/ttml"
|
10
10
|
|
11
11
|
gem.files = `git ls-files`.split($\)
|
12
12
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ttml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -42,9 +42,10 @@ files:
|
|
42
42
|
- lib/ttml.rb
|
43
43
|
- lib/ttml/version.rb
|
44
44
|
- test/sample.xml
|
45
|
+
- test/sample_2.xml
|
45
46
|
- test/test_ttml.rb
|
46
47
|
- ttml.gemspec
|
47
|
-
homepage:
|
48
|
+
homepage: http://github.com/loop23/ttml
|
48
49
|
licenses: []
|
49
50
|
post_install_message:
|
50
51
|
rdoc_options: []
|
@@ -70,4 +71,5 @@ specification_version: 3
|
|
70
71
|
summary: Minimal parsing for timed text markup language (http://www.w3.org/TR/ttaf1-dfxp/)
|
71
72
|
test_files:
|
72
73
|
- test/sample.xml
|
74
|
+
- test/sample_2.xml
|
73
75
|
- test/test_ttml.rb
|