fix: apply code review feedback on CONT handling, retry guard, title escaping, lazy file open, and 4-space indent
Agent-Logs-Url: https://github.com/jaredhowland/gedcom-to-markdown/sessions/6c8ce2c8-7699-47a3-99f1-909cfa5ac7cd Co-authored-by: jaredhowland <1877384+jaredhowland@users.noreply.github.com>
This commit is contained in:
1 parent
676b4014ca
commit
c68264978d
2 files changed
+34
-30
No files matched your search
+9
-8
@@ -81,28 +81,29 @@ def resolve_gedcom_text(parser, value: str, element=None) -> str:
|
|||||||
if not target:
|
if not target:
|
||||||
return ""
|
return ""
|
||||||
text = target.get_value() or ""
|
text = target.get_value() or ""
|
||||||
# Respect CONT vs CONC semantics: CONT => newline, CONC => concatenate
|
# Respect CONT vs CONC semantics: CONT => newline, CONC => concatenate.
|
||||||
|
# An empty CONT line represents a blank line in GEDCOM and must still
|
||||||
|
# contribute a newline so paragraph breaks are preserved.
|
||||||
for sub in target.get_child_elements():
|
for sub in target.get_child_elements():
|
||||||
tag = sub.get_tag()
|
tag = sub.get_tag()
|
||||||
val = (sub.get_value() or "")
|
val = (sub.get_value() or "")
|
||||||
if not val:
|
|
||||||
continue
|
|
||||||
if tag == "CONC":
|
if tag == "CONC":
|
||||||
text += val
|
if val:
|
||||||
|
text += val
|
||||||
elif tag == "CONT":
|
elif tag == "CONT":
|
||||||
text += "\n" + val
|
text += "\n" + val
|
||||||
return text
|
return text
|
||||||
|
|
||||||
# Inline text with possible CONT/CONC children
|
# Inline text with possible CONT/CONC children.
|
||||||
|
# Same rule: empty CONC is a no-op, but empty CONT preserves a blank line.
|
||||||
text = value or ""
|
text = value or ""
|
||||||
if element is not None:
|
if element is not None:
|
||||||
for sub in element.get_child_elements():
|
for sub in element.get_child_elements():
|
||||||
tag = sub.get_tag()
|
tag = sub.get_tag()
|
||||||
val = (sub.get_value() or "")
|
val = (sub.get_value() or "")
|
||||||
if not val:
|
|
||||||
continue
|
|
||||||
if tag == "CONC":
|
if tag == "CONC":
|
||||||
text += val
|
if val:
|
||||||
|
text += val
|
||||||
elif tag == "CONT":
|
elif tag == "CONT":
|
||||||
text += "\n" + val
|
text += "\n" + val
|
||||||
return text
|
return text
|
||||||
|
|||||||
+25
-22
@@ -136,6 +136,8 @@ class MarkdownGenerator:
|
|||||||
self._generate_sources_index(parser)
|
self._generate_sources_index(parser)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('Failed to generate sources index')
|
logger.exception('Failed to generate sources index')
|
||||||
|
# Prevent repeated expensive retries on persistent failures (e.g., IO/permissions)
|
||||||
|
self._sources_index_generated = True
|
||||||
|
|
||||||
return file_path
|
return file_path
|
||||||
|
|
||||||
@@ -526,14 +528,16 @@ class MarkdownGenerator:
|
|||||||
|
|
||||||
If title and publ are present, title is rendered as a link to publ.
|
If title and publ are present, title is rendered as a link to publ.
|
||||||
If only title present, render plain title. If only publ present, use the URL as link text.
|
If only title present, render plain title. If only publ present, use the URL as link text.
|
||||||
|
Title text is escaped to prevent markdown emphasis characters from triggering formatting.
|
||||||
"""
|
"""
|
||||||
title = self._collapse_single_line(title)
|
title = self._collapse_single_line(title)
|
||||||
publ = self._collapse_single_line(publ)
|
publ = self._collapse_single_line(publ)
|
||||||
|
escaped_title = self._escape_markdown(title)
|
||||||
|
|
||||||
if title and publ:
|
if title and publ:
|
||||||
return f"[{title}]({publ})"
|
return f"[{escaped_title}]({publ})"
|
||||||
if title:
|
if title:
|
||||||
return title
|
return escaped_title
|
||||||
if publ:
|
if publ:
|
||||||
return f"[{publ}]({publ})"
|
return f"[{publ}]({publ})"
|
||||||
return "(Unknown source)"
|
return "(Unknown source)"
|
||||||
@@ -602,18 +606,13 @@ class MarkdownGenerator:
|
|||||||
else:
|
else:
|
||||||
sources_dir = self.output_dir / "sources"
|
sources_dir = self.output_dir / "sources"
|
||||||
|
|
||||||
written_any = False
|
|
||||||
i = 0
|
i = 0
|
||||||
|
|
||||||
# Prepare the directory but delay creating the file until we have at least
|
|
||||||
# one source entry to write. We'll open the file and stream entries as we
|
|
||||||
# find them.
|
|
||||||
sources_dir.mkdir(parents=True, exist_ok=True)
|
|
||||||
index_file = sources_dir / "Index.md"
|
index_file = sources_dir / "Index.md"
|
||||||
|
|
||||||
with open(index_file, "w", encoding="utf-8") as f:
|
# Delay creating the file and directory until the first source is found so
|
||||||
f.write("# Sources Index\n\n")
|
# that no empty Index.md is left behind when there are no SOUR records.
|
||||||
|
f = None
|
||||||
|
try:
|
||||||
for elem in elem_dict.values():
|
for elem in elem_dict.values():
|
||||||
try:
|
try:
|
||||||
if elem.get_tag() != "SOUR":
|
if elem.get_tag() != "SOUR":
|
||||||
@@ -642,6 +641,11 @@ class MarkdownGenerator:
|
|||||||
note_text = self._collapse_preserve_lines(note_text)
|
note_text = self._collapse_preserve_lines(note_text)
|
||||||
|
|
||||||
if title or publ or note_text:
|
if title or publ or note_text:
|
||||||
|
if f is None:
|
||||||
|
# First source found: create directory and open file
|
||||||
|
sources_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
f = open(index_file, "w", encoding="utf-8")
|
||||||
|
f.write("# Sources Index\n\n")
|
||||||
i += 1
|
i += 1
|
||||||
entry = self._format_source_entry(title, publ)
|
entry = self._format_source_entry(title, publ)
|
||||||
f.write(f"{i}. {entry}\n")
|
f.write(f"{i}. {entry}\n")
|
||||||
@@ -650,16 +654,12 @@ class MarkdownGenerator:
|
|||||||
note_text = self._repair_broken_html_tags(note_text)
|
note_text = self._repair_broken_html_tags(note_text)
|
||||||
lines = [self._escape_markdown(ln) for ln in note_text.splitlines()]
|
lines = [self._escape_markdown(ln) for ln in note_text.splitlines()]
|
||||||
self._write_multiline_note_block(f, lines, nested=True)
|
self._write_multiline_note_block(f, lines, nested=True)
|
||||||
written_any = True
|
|
||||||
|
|
||||||
f.write("\n")
|
if f is not None:
|
||||||
|
f.write("\n")
|
||||||
# If nothing was actually written, remove the empty index file
|
finally:
|
||||||
if not written_any and index_file.exists():
|
if f is not None:
|
||||||
try:
|
f.close()
|
||||||
index_file.unlink()
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Mark that the global sources index has been generated so we don't do this again
|
# Mark that the global sources index has been generated so we don't do this again
|
||||||
self._sources_index_generated = True
|
self._sources_index_generated = True
|
||||||
@@ -753,8 +753,11 @@ class MarkdownGenerator:
|
|||||||
|
|
||||||
total = len(lines)
|
total = len(lines)
|
||||||
if nested:
|
if nested:
|
||||||
indent = " "
|
# Use 4-space indent so nested content is correctly parsed as continuation
|
||||||
cont_indent = " "
|
# of the parent ordered list item even for multi-digit list numbers (e.g. "10.")
|
||||||
|
# in CommonMark-compliant renderers.
|
||||||
|
indent = " "
|
||||||
|
cont_indent = " "
|
||||||
# First line as a nested bullet; add two spaces if there are continuation lines
|
# First line as a nested bullet; add two spaces if there are continuation lines
|
||||||
if total > 1:
|
if total > 1:
|
||||||
f.write(f"{indent}- {lines[0]} \n")
|
f.write(f"{indent}- {lines[0]} \n")
|
||||||
|
|||||||
Reference in new issue
Block a user