def check_ttml_start(self, subs, language_code, title=None, description=None): top_part = re.match("(.*</head>)", subs.to_xml(), re.DOTALL) if title: title_tag = '<ttm:title>%s</ttm:title>' % (title,) else: title_tag = '<ttm:title></ttm:title>' if description: description_tag = ('<ttm:description>%s</ttm:description>' % (description,)) else: description_tag = '<ttm:description></ttm:description>' correct_start = """\ <tt xmlns:tts="http://www.w3.org/ns/ttml#styling" xmlns:ttp="http://www.w3.org/ns/ttml#parameter" xmlns:ttm="http://www.w3.org/ns/ttml#metadata" xmlns="http://www.w3.org/ns/ttml" xml:lang="{language_code}"> <head> <metadata> {title_tag} {description_tag} <ttm:copyright/> </metadata> <styling> <style xml:id="test-style" tts:color="white" tts:fontSize="18px"/> </styling> <layout> <region xml:id="bottom" style="test-style" tts:origin="0 80%" tts:extent="100% 20%"/> <region xml:id="top" style="test-style" tts:origin="0 0" tts:extent="100% 20%"/> </layout> </head>""".format(language_code=language_code, title_tag=title_tag, description_tag=description_tag) utils.assert_long_text_equal(top_part.group(0), correct_start)
def test_empty_subs(self): utils.assert_long_text_equal(self.subtitles.to_xml(), """\ <tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling" xml:lang="en"> <head/> <body> <div/> </body> </tt> """)
def test_empty_subs(self): utils.assert_long_text_equal( self.subtitles.to_xml(), """\ <tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling" xml:lang="en"> <head/> <body> <div/> </body> </tt> """)
def test_merge_with_header(self): initial_ttml = etree.fromstring("""\ <tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling"> <head> <styling> <style xml:id="style" tts:color="foo" tts:fontSize="bar" /> </styling> <layout> <region xml:id="region" style="style" tts:extent="foo" tts:origin="bar" /> </layout> </head> <body /> </tt>""") result = DFXPGenerator.merge_subtitles( [self.en_subs, self.es_subs, self.fr_subs], initial_ttml=initial_ttml) utils.assert_long_text_equal(result, """\ <tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling" xml:lang=""> <head> <styling> <style xml:id="style" tts:color="foo" tts:fontSize="bar"/> </styling> <layout> <region xml:id="region" style="style" tts:extent="foo" tts:origin="bar"/> </layout> </head> <body> <div xml:lang="en"> <div> <p begin="00:00:01.000" end="00:00:01.500">content</p> </div> </div> <div xml:lang="es"> <div> <p begin="00:00:01.000" end="00:00:01.500">spanish content</p> </div> <div> <p begin="00:00:02.000" end="00:00:02.500">spanish content 2</p> </div> </div> <div xml:lang="fr"> <div> <p begin="00:00:01.000" end="00:00:01.500">french content</p> </div> </div> </body> </tt> """)
def test_with_new_paragraph(self): self.subtitles.append_subtitle(1000, 1500, "content") self.subtitles.append_subtitle(2000, 2500, "content 2", new_paragraph=True) utils.assert_long_text_equal(self.subtitles.to_xml(), """\ <tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling" xml:lang="en"> <head/> <body> <div> <p begin="00:00:01.000" end="00:00:01.500">content</p> </div> <div> <p begin="00:00:02.000" end="00:00:02.500">content 2</p> </div> </body> </tt> """)
def test_with_subs(self): self.subtitles.append_subtitle(1000, 1500, "content") self.subtitles.append_subtitle(2000, 2500, "<i>content</i>", escape=False) self.subtitles.append_subtitle(3000, 3500, " content with space ") utils.assert_long_text_equal(self.subtitles.to_xml(), """\ <tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling" xml:lang="en"> <head/> <body> <div> <p begin="00:00:01.000" end="00:00:01.500">content</p> <p begin="00:00:02.000" end="00:00:02.500"><i>content</i></p> <p begin="00:00:03.000" end="00:00:03.500"> content with space </p> </div> </body> </tt> """)
def test_with_new_paragraph(self): self.subtitles.append_subtitle(1000, 1500, "content") self.subtitles.append_subtitle(2000, 2500, "content 2", new_paragraph=True) utils.assert_long_text_equal( self.subtitles.to_xml(), """\ <tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling" xml:lang="en"> <head/> <body> <div> <p begin="00:00:01.000" end="00:00:01.500">content</p> </div> <div> <p begin="00:00:02.000" end="00:00:02.500">content 2</p> </div> </body> </tt> """)
def test_with_subs(self): self.subtitles.append_subtitle(1000, 1500, "content") self.subtitles.append_subtitle(2000, 2500, "<i>content</i>", escape=False) self.subtitles.append_subtitle(3000, 3500, " content with space ") utils.assert_long_text_equal( self.subtitles.to_xml(), """\ <tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling" xml:lang="en"> <head/> <body> <div> <p begin="00:00:01.000" end="00:00:01.500">content</p> <p begin="00:00:02.000" end="00:00:02.500"><i>content</i></p> <p begin="00:00:03.000" end="00:00:03.500"> content with space </p> </div> </body> </tt> """)
def test_dfxp_merge(self): result = DFXPGenerator.merge_subtitles( [self.en_subs, self.es_subs, self.fr_subs]) utils.assert_long_text_equal(result, """\ <tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling" xml:lang=""> <head> <metadata xmlns:ttm="http://www.w3.org/ns/ttml#metadata"> <ttm:title/> <ttm:description/> <ttm:copyright/> </metadata> <styling xmlns:tts="http://www.w3.org/ns/ttml#styling"> <style xml:id="amara-style" tts:color="white" tts:fontFamily="proportionalSansSerif" tts:fontSize="18px" tts:textAlign="center"/> </styling> <layout xmlns:tts="http://www.w3.org/ns/ttml#styling"> <region xml:id="amara-subtitle-area" style="amara-style" tts:extent="560px 62px" tts:padding="5px 3px" tts:backgroundColor="black" tts:displayAlign="after"/> </layout> </head> <body region="amara-subtitle-area"> <div xml:lang="en"> <div> <p begin="00:00:01.000" end="00:00:01.500">content</p> </div> </div> <div xml:lang="es"> <div> <p begin="00:00:01.000" end="00:00:01.500">spanish content</p> </div> <div> <p begin="00:00:02.000" end="00:00:02.500">spanish content 2</p> </div> </div> <div xml:lang="fr"> <div> <p begin="00:00:01.000" end="00:00:01.500">french content</p> </div> </div> </body> </tt> """)
def test_dfxp_merge(self): en_subs = SubtitleSet('en') es_subs = SubtitleSet('es') en_subs.append_subtitle(1000, 1500, 'content') es_subs.append_subtitle(1000, 1500, 'spanish content') result = self.loader.dfxp_merge([en_subs, es_subs]) utils.assert_long_text_equal(result, """\ <tt xmlns:tts="http://www.w3.org/ns/ttml#styling" xmlns:ttp="http://www.w3.org/ns/ttml#parameter" xmlns:ttm="http://www.w3.org/ns/ttml#metadata" xmlns="http://www.w3.org/ns/ttml" xml:lang=""> <head> <metadata> <ttm:title></ttm:title> <ttm:description></ttm:description> <ttm:copyright/> </metadata> <styling> <style xml:id="test-style" tts:color="white" tts:fontSize="18px"/> </styling> <layout> <region xml:id="bottom" style="test-style" tts:origin="0 80%" tts:extent="100% 20%"/> <region xml:id="top" style="test-style" tts:origin="0 0" tts:extent="100% 20%"/> </layout> </head> <body region="bottom"> <div xml:lang="en"> <div> <p begin="00:00:01.000" end="00:00:01.500">content</p> </div> </div> <div xml:lang="es"> <div> <p begin="00:00:01.000" end="00:00:01.500">spanish content</p> </div> </div> </body> </tt> """)