Skip to content

Commit

Permalink
srt: add option keep_original_newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
milahu committed Mar 10, 2024
1 parent 5661336 commit 319ad18
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pysubs2/subrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def guess_format(cls, text):
return "srt"

@classmethod
def from_file(cls, subs, fp, format_, keep_html_tags=False, keep_unknown_html_tags=False, keep_newlines=False, **kwargs):
def from_file(cls, subs, fp, format_, keep_html_tags=False, keep_unknown_html_tags=False, keep_newlines=False, keep_original_newlines=False, **kwargs):
"""
See :meth:`pysubs2.formats.FormatBase.from_file()`
Expand Down Expand Up @@ -93,6 +93,11 @@ def prepare_text(lines):

# Handle the general case.
s = b"".join(lines).strip()
if not keep_original_newlines:
# reading file to bytestring preserves the original line endings
# convert to unix line endings
s = re.sub(rb"\r\n", rb"\n", s)
s = re.sub(rb"\r", rb"\n", s)
s = re.sub(rb"\n+ *\d+ *$", b"", s) # strip number of next subtitle
if not keep_html_tags:
s = re.sub(rb"< *i *>", rb"{\\i1}", s)
Expand Down

0 comments on commit 319ad18

Please sign in to comment.