From a40c9ddaa070e7be0a13cf7e4ef3158aab249e28 Mon Sep 17 00:00:00 2001 From: ItMightBeKaraoke Date: Sun, 10 May 2026 19:03:52 -0500 Subject: [PATCH 1/2] Allow manipulation of comments in the Script Info section The idea here is that a user of this library should have the ability to add Script Info comments to an ass document. When parsing a file, they are also preserved. It may make more sense to allow them in any section, but as I understand it the spec only mentions ";" comments to be available in Script Info so that is all that is implemented here. The existing functionality of ignoring lines starting with ";" in other sections is maintained. Note that while this retains the order of the comments, it will always put them in their customary position at the beginning of the Script Info, before any of the fields, rather than tracking any position they could have interleaved between the field lines. This means they can't be used as comments for particular fields, but I don't think that was ever an intended use case of these comments anyway. --- src/ass/document.py | 8 +++++++- src/ass/section.py | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/ass/document.py b/src/ass/document.py index d919baf..e129fde 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 = [] From 2e4d5b7b197dc2c3d1e7f64b3cffdce8acbcba17 Mon Sep 17 00:00:00 2001 From: ItMightBeKaraoke Date: Mon, 11 May 2026 13:12:06 -0500 Subject: [PATCH 2/2] Remove trailing whitespace --- src/ass/document.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ass/document.py b/src/ass/document.py index e129fde..b33abd1 100644 --- a/src/ass/document.py +++ b/src/ass/document.py @@ -93,7 +93,7 @@ def parse_file(cls, f): if not line: continue - if line.startswith(';'): + if line.startswith(';'): # ";" comments only permitted in Script Info section, ignore otherwise if section == doc.sections.get("Script Info"): section.add_comment(line[1:])