Skip to content

Commit

Permalink
Merge pull request #253 from Kevin-Lee/task/252/fix-no-title
Browse files Browse the repository at this point in the history
Fix #252 - Parsing SAMI file fails when there's no `<TITLE>` tag
  • Loading branch information
kevin-lee committed May 16, 2023
2 parents d3ae720 + a04fbb1 commit 7ddef9e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ object SmiParser {
case Some(ParseStatus.SamiEnd) =>
effectOf((title, acc).asRight)
case Some(ParseStatus.HeadStart) =>
parseAll(skipUntil(rest, ParseStatus.TitleStart), title, acc)
parseAll(skipUntil(rest, ParseStatus.TitleStart, ParseStatus.StyleStart), title, acc)
case Some(ParseStatus.HeadEnd) =>
parseAll(skipUntil(rest, ParseStatus.BodyStart), title, acc)
case Some(ParseStatus.TitleStart) =>
Expand Down Expand Up @@ -186,14 +186,18 @@ object SmiParser {
}

@tailrec
private def skipUntil(lineAndLineNums: Seq[(String, Int)], target: ParseStatus): Seq[(String, Int)] =
private def skipUntil(
lineAndLineNums: Seq[(String, Int)],
target: ParseStatus,
orAnyOf: ParseStatus*
): Seq[(String, Int)] =
lineAndLineNums match {
case (line, index) +: rest =>
val preprocessed = line.removeEmptyChars
parseNonLine(preprocessed)
.find(_ === target) match {
.find(parseStatus => parseStatus === target || orAnyOf.contains(parseStatus)) match {
case None =>
skipUntil(rest, target)
skipUntil(rest, target, orAnyOf: _*)
case Some(_) =>
lineAndLineNums
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<SAMI>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<SYNC Start=4561><P><font color="#ec14bd">This is a test subtitle.</Font> by <font color="Skyblue" size=10 face=HY수평선B>Kevin Lee</font>
<SYNC Start=7000><P>&nbsp;
<SYNC Start=10231><P><font color="Gold"> 자 막 </Font>: <font color="Skyblue"> 테 스 트 자 막</font><br><font color=Lime>(kevinlee.io)</font>
<SYNC Start=15831><P>&nbsp;
<SYNC Start=15831><P><font color="#80ffff">Modified </Font>by <font color="Lime">Kevin Lee<br><font color="#ec14bd">(https://kevinlee.io)</font>
<SYNC Start=19000><P>&nbsp;
<SYNC Start=87274><P><font color="Gold">헬로우 월드!</font>
<SYNC Start=89741><P>&nbsp;
<SYNC Start=90674><P>안녕
<SYNC Start=91741><P>&nbsp;
<SYNC Start=196380><P>이건 그냥 테스트를 위한 자막이에요.
<SYNC Start=198115><P>&nbsp;
<SYNC Start=199950><P>테스트를 위한 또다른 자막 입니다.
<SYNC Start=202119><P>&nbsp;
<SYNC Start=202186><P>Ok
<SYNC Start=204155><P>&nbsp;
<SYNC Start=204221><P>이제 영어도 한번 넣어 볼까요?
<SYNC Start=206957><P>&nbsp;
<SYNC Start=212029><P>짜잔!
<SYNC Start=213763><P>&nbsp;
<SYNC Start=213830><P>Lorem Ipsum is simply dummy text of the printing and typesetting industry
<SYNC Start=215533><P>&nbsp;
<SYNC Start=215598><P>영어도 잘 되는거 같군요.
<SYNC Start=217868><P>&nbsp;
<SYNC Start=222672><P>블라블라.
<SYNC Start=224041><P>&nbsp;
<SYNC Start=224108><P>이제 자막에 색깔을 입혀 볼까요?
<SYNC Start=225242><P>&nbsp;
<SYNC Start=249699><P>위에 이미 했지만...
<SYNC Start=250767><P>&nbsp;
<SYNC Start=262274><P><font color="Gold">Gold color로 입혀 봤습니다.</font>
<SYNC Start=8873754><P>&nbsp;
</BODY>
</SAMI>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<SAMI>
<HEAD>
<STYLE TYPE="text/css">
<!--
P { font-family: Arial; font-weight: normal; color: white; background-color: black; text-align: center; }
.KRCC { name: Korean; lang: ko-KR ; SAMIType: CC ; }
-->
</STYLE>
</HEAD>
<BODY>
<-- Open play menu, choose Captions and Subtiles, On if available -->
<-- Open tools menu, Security, Show local captions when present -->
<!-- Another test comment -->
<!--
자막 번역: 누군가 2021. 01. 01
-->
<SYNC Start=4561 End=7000><P Class=KRCC>
<font color="#ec14bd">This is a test subtitle.</Font> by <font color="Skyblue" size=10 face=HY수평선B>Kevin Lee</font>
<SYNC Start=10231><P Class=KRCC><font color="Gold"> 자 막 </Font>: <font color="Skyblue"> 테 스 트 자 막</font><br><font color=Lime>(kevinlee.io)</font>
<SYNC Start=15831 End=19000><P Class=KRCC><font color="#80ffff">Modified </Font>by <font color="Lime">Kevin Lee<br><font color="#ec14bd">(https://kevinlee.io)</font>
<SYNC Start=20754><P Class=KRCC>&nbsp;
<SYNC Start=87274><P Class=KRCC><font color="Gold">헬로우 월드!</font>
<SYNC Start=89741 End=90674><P Class=KRCC>&nbsp;
<SYNC Start=90674><P Class=KRCC>안녕
<SYNC Start=91741 End=100000><P Class=KRCC>&nbsp;
<SYNC Start=196380><P Class=KRCC>
이건 그냥 테스트를 위한 자막이에요.
<SYNC Start=198115 End=199950><P Class=KRCC>&nbsp;
<SYNC Start=199950><P Class=KRCC>테스트를 위한 또다른 자막 입니다.
<SYNC Start=202119><P Class=KRCC>&nbsp;
<SYNC Start=202186><P Class=KRCC>Ok
<SYNC Start=204155><P Class=KRCC>&nbsp;
<SYNC Start=204221><P Class=KRCC>이제 영어도 한번 넣어 볼까요?
<SYNC Start=206957><P Class=KRCC>&nbsp;
<!-- Test comment - 중간에 코멘트도 넣고 -->
<SYNC Start=212029><P Class=KRCC>짜잔!
<SYNC Start=213763><P Class=KRCC>&nbsp;
<SYNC Start=213830><P Class=KRCC>Lorem Ipsum is simply dummy text of the printing and typesetting industry
<SYNC Start=215533><P Class=KRCC>&nbsp;
<SYNC Start=215598><P Class=KRCC>영어도 잘 되는거 같군요.
<-- Test comment 2 -->
<SYNC Start=217868><P Class=KRCC>&nbsp;
<SYNC Start=222672><P Class=KRCC>블라블라.
<SYNC Start=224041><P Class=KRCC>&nbsp;
<SYNC Start=224108><P Class=KRCC>이제 자막에 색깔을 입혀 볼까요?
<SYNC Start=225242><P Class=KRCC>&nbsp;
<SYNC Start=249699><P Class=KRCC>위에 이미 했지만...
<SYNC Start=250767><P Class=KRCC>&nbsp;
<SYNC Start=262274><P Class=KRCC><font color="Gold">Gold color로 입혀 봤습니다.</font>
<SYNC Start=8873754><P Class=KRCC>&nbsp;
</BODY>
</SAMI>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import whatsub.*
object SmiParserSpec extends Properties {
override def tests: List[Test] = List(
ParserGoldenTester.goldenTestParse[Smi]("golden/test-src.smi", "golden/test-smi-parsed.smi", SmiParser.parse[IO]),
ParserGoldenTester
.goldenTestParse[Smi]("golden/test-src-no-title.smi", "golden/test-smi-parsed-no-title.smi", SmiParser.parse[IO]),
)

}

0 comments on commit 7ddef9e

Please sign in to comment.