diff --git a/src/ass/document.py b/src/ass/document.py index d919baf..b33abd1 100644 --- a/src/ass/document.py +++ b/src/ass/document.py @@ -90,7 +90,13 @@ def parse_file(cls, f): " usually '%s'" % cls.PREFERRED_ENCODING.name) line = line.strip() - if not line or line.startswith(';'): + if not line: + continue + + if line.startswith(';'): + # ";" comments only permitted in Script Info section, ignore otherwise + if section == doc.sections.get("Script Info"): + section.add_comment(line[1:]) continue if line.startswith('[') and line.endswith(']'): diff --git a/src/ass/section.py b/src/ass/section.py index 6469588..24d2d75 100644 --- a/src/ass/section.py +++ b/src/ass/section.py @@ -84,6 +84,11 @@ def add_line(self, field_name, field): def dump(self): yield "[{}]".format(self.name) + # Comments are customarily at the beginning + if hasattr(self, "comments"): + for c in self.comments: + yield ";{}".format(c) + for k, v in self._fields.items(): yield "{}: {}".format(k, _Field.dump(v)) @@ -149,3 +154,10 @@ class ScriptInfoSection(FieldSection): "WrapStyle": _Field("WrapStyle", int, default=0), "ScaledBorderAndShadow": _Field("ScaledBorderAndShadow", str, default="yes") } + + def add_comment(self, comment): + self.comments.append(comment) + + def __init__(self, name, fields=None): + super().__init__(name, fields=fields) + self.comments = []