Skip to content

Commit

Permalink
parsing: Accept input with no newline after timestamps on final sub
Browse files Browse the repository at this point in the history
  • Loading branch information
cdown committed Aug 29, 2020
1 parent 6c376ee commit 11089f1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion srt.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
TS_REGEX = re.compile(RGX_TIMESTAMP_PARSEABLE)
MULTI_WS_REGEX = re.compile(r"\n\n+")
SRT_REGEX = re.compile(
r"\s*({idx})\s*{eof}({ts}) *-[ -] *> *({ts}) ?({proprietary}){eof}({content})"
r"\s*({idx})\s*{eof}({ts}) *-[ -] *> *({ts}) ?({proprietary})(?:{eof}|\Z)({content})"
# Many sub editors don't add a blank line to the end, and many editors and
# players accept that. We allow it to be missing in input.
#
Expand Down
16 changes: 15 additions & 1 deletion tests/test_srt.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,22 @@ def test_repr_doesnt_crash(sub):
assert str(sub.index) in repr(sub)


@given(subtitles(), subtitles())
def test_parser_accepts_final_no_newline_no_content(sub1, sub2):
# Limit size so we know how much to remove
sub2.content = ""
subs = [sub1, sub2]

# Remove the last newlines so that there are none. Cannot use rstrip since
# there might be other stuff that gets matched in proprietary
stripped_srt_blocks = srt.compose(subs, reindex=False)[:-2]

reparsed_subs = srt.parse(stripped_srt_blocks)
subs_eq(reparsed_subs, subs)


@given(st.lists(subtitles()))
def test_parser_accepts_no_newline_no_content(subs):
def test_parser_accepts_newline_no_content(subs):
for sub in subs:
# Limit size so we know how many lines to remove
sub.content = ""
Expand Down

0 comments on commit 11089f1

Please sign in to comment.