in python/moz/l10n/formats/dtd/parse.py [0:0]
def dtd_parse(source: str | bytes) -> Resource[Message]:
"""
Parse a .dtd file into a message resource.
The parsed resource will not include any metadata.
"""
entries: list[Entry[Message] | Comment] = []
resource = Resource(Format.dtd, [Section((), entries)])
pos = 0
at_newline = True
comment: str = ""
if not isinstance(source, str):
source = source.decode()
for match in re_comment.finditer(source):
cstart = match.start(0)
has_prev_entries = False
for entry in dtd_iter(source, pos, endpos=cstart):
if isinstance(entry, str):
if entry and not entry.isspace():
raise ValueError(f"Unexpected content in DTD: {entry}")
lines = entry.split("\n")
if comment and len(lines) > 2:
if entries or resource.comment:
entries.append(Comment(comment))
else:
resource.comment = comment
comment = ""
at_newline = len(lines) > 1
else:
if comment:
entry.comment = comment
comment = ""
entries.append(entry)
has_prev_entries = True
nc = match.group(1).strip().replace("\r\n", "\n")
comment = f"{comment}\n{nc}" if comment else nc
if comment:
if not at_newline and has_prev_entries:
prev = entries[-1]
pc = prev.comment
prev.comment = f"{pc}\n{comment}" if pc else comment
comment = ""
if re_entity.search(comment):
entries.append(Comment(comment))
comment = ""
pos = match.end(0)
if len(source) > pos:
for entry in dtd_iter(source, pos):
if isinstance(entry, str):
if entry and not entry.isspace():
raise ValueError(f"Unexpected content in DTD: {entry}")
else:
if comment:
entry.comment = comment
comment = ""
entries.append(entry)
if comment:
entries.append(Comment(comment))
return resource