Skip to content

Commit

Permalink
Allow parsing SRT blocks with dot as msec delimiter
Browse files Browse the repository at this point in the history
We previously added support for this to srt_timestamp_to_timedelta() in
0ad7b55, but we didn't update the regex to allow it as input to parse(). As
such, you could only use it when manually calling srt_timestamp_to_timedelta().
  • Loading branch information
cdown committed Jan 3, 2016
1 parent 095120a commit 2f61dad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion srt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
log = logging.getLogger(__name__)

SRT_REGEX = re.compile(
r'(\d+)\n(\d+:\d+:\d+,\d+) --> (\d+:\d+:\d+,\d+) ?([^\n]*)\n(.*?)'
r'(\d+)\n(\d+:\d+:\d+[,.]\d+) --> (\d+:\d+:\d+[,.]\d+) ?([^\n]*)\n(.*?)'
# Many sub editors don't add a blank line to the end, and many editors
# accept it. We allow it in input.
r'(?:\n|\Z)(?:\n|\Z)'
Expand Down
18 changes: 18 additions & 0 deletions tests/test_srt.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,21 @@ def test_parser_didnt_match_to_end_raises(subs, fake_idx, fake_hours, garbage):

with assert_raises(srt.SRTParseError):
list(srt.parse(composed))


@given(st.lists(subtitles()))
def test_parser_can_parse_with_dot_msec_delimiter(subs):
original_srt_blocks = [sub.to_srt() for sub in subs]
dot_srt_blocks = []

for srt_block in original_srt_blocks:
srt_lines = srt_block.split('\n')
# We should only do the first two, as it might also be in the
# proprietary metadata, causing this test to fail.
dot_timestamp = srt_lines[1].replace(',', '.', 2)
srt_lines[1] = dot_timestamp
dot_srt_blocks.append('\n'.join(srt_lines))

composed_with_dots = ''.join(dot_srt_blocks)
reparsed_subs = srt.parse(composed_with_dots)
subs_eq(reparsed_subs, subs)

0 comments on commit 2f61dad

Please sign in to comment.