feat: add source parsing and linking

- Respect GEDCOM CONC vs CONT semantics (concatenate vs newline)
 - Generate global sources index once (avoid O(N*S) stall)
 - Repair broken HTML tags spanning GEDCOM lines for whitelist tags
 - Escape markdown emphasis characters in notes to prevent formatting leakage
 - Consistent handling of CONT/CONC; skip empty CONT lines; handle trailing spaces for hard breaks
 - Added tests covering CONT/CONC and source index
This commit is contained in:
Jared Howland committed 2026-04-17 13:42:04 -06:00
1 parent 48e339c119
commit 6259bfffd6
4 files changed
+586 -36

No files matched your search

+110
View File
@@ -0,0 +1,110 @@
from pathlib import Path
from src.gedcom_parser import GedcomParser
from src.individual import Individual
from src.markdown_generator import MarkdownGenerator
def _make_output_dir(tmp_path):
out = tmp_path / "output"
out.mkdir()
return out
class TestNotesContConc:
def test_note_cont_and_conc_inline(self, tmp_path):
gedcom = """0 HEAD
1 SOUR TestApp
1 GEDC
2 VERS 5.5.1
1 CHAR UTF-8
0 @N1@ NOTE
1 CONT First line
1 CONC -continued
1 CONT Next paragraph
0 @I1@ INDI
1 NAME John /Doe/
1 NOTE @N1@
0 TRLR
"""
f = tmp_path / "note.ged"
f.write_text(gedcom, encoding='utf-8')
parser = GedcomParser(f)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser.parser)
out = _make_output_dir(tmp_path)
gen = MarkdownGenerator(out)
note = gen.generate_note(person)
content = note.read_text()
# Ensure CONC concatenated without newline
assert "First line-continued" in content
# Ensure CONT produced a newline (rendered as separate paragraph / hard break)
assert "Next paragraph" in content
def test_note_reference_to_note_with_cont_conc(self, tmp_path):
gedcom = """0 HEAD
1 SOUR TestApp
1 GEDC
2 VERS 5.5.1
1 CHAR UTF-8
0 @N1@ NOTE
1 CONT Line one
1 CONC continued
1 CONT Line two
0 @I1@ INDI
1 NAME Jane /Smith/
1 NOTE @N1@
0 TRLR
"""
f = tmp_path / "note_ref.ged"
f.write_text(gedcom, encoding='utf-8')
parser = GedcomParser(f)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser.parser)
out = _make_output_dir(tmp_path)
gen = MarkdownGenerator(out)
note = gen.generate_note(person)
content = note.read_text()
assert "Line onecontinued" in content
assert "Line two" in content
def test_source_note_cont_conc_and_index(self, tmp_path):
gedcom = """0 HEAD
1 SOUR TestApp
1 GEDC
2 VERS 5.5.1
1 CHAR UTF-8
0 @N1@ NOTE
1 CONT Source line
1 CONC -more
0 @S1@ SOUR
1 TITL Example Source
1 NOTE @N1@
0 @I1@ INDI
1 NAME Alpha /Beta/
1 SOUR @S1@
0 TRLR
"""
f = tmp_path / "sources.ged"
f.write_text(gedcom, encoding='utf-8')
parser = GedcomParser(f)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser.parser)
out = _make_output_dir(tmp_path)
gen = MarkdownGenerator(out)
# generate a person note which should also trigger sources index generation
gen.generate_note(person)
index = out / 'sources' / 'Index.md'
assert index.exists()
content = index.read_text()
# Ensure concatenation in source note
assert 'Source line-more' in content
+104
View File
@@ -0,0 +1,104 @@
import pytest
from gedcom_parser import GedcomParser
from individual import Individual
from markdown_generator import MarkdownGenerator
def _make_output_dir(tmp_path):
out = tmp_path / "output"
out.mkdir()
return out
class TestSourcesSection:
def test_sources_happy_path_with_publ_and_title(self, tmp_path):
gedcom = """0 HEAD
1 SOUR TestApp
1 GEDC
2 VERS 5.5.1
1 CHAR UTF-8
0 @S1@ SOUR
1 TITL Overland Travels Pioneer Detail
1 PUBL http://history.example/pioneer?id=6262
0 @I1@ INDI
1 NAME John /Doe/
1 SOUR @S1@
0 TRLR
"""
f = tmp_path / "sources.ged"
f.write_text(gedcom, encoding='utf-8')
parser = GedcomParser(f)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser.parser)
out = _make_output_dir(tmp_path)
gen = MarkdownGenerator(out)
note = gen.generate_note(person)
content = note.read_text()
assert '## Sources' in content
# Whitespace in title should be collapsed
assert '1. [Overland Travels Pioneer Detail](http://history.example/pioneer?id=6262)' in content
# Also an Index file should be created in sources/Index.md
index = out / 'sources' / 'Index.md'
assert index.exists()
idx_content = index.read_text()
assert '[Overland Travels Pioneer Detail](http://history.example/pioneer?id=6262)' in idx_content
def test_sources_title_only_no_publ(self, tmp_path):
gedcom = """0 HEAD
1 SOUR TestApp
1 GEDC
2 VERS 5.5.1
1 CHAR UTF-8
0 @S2@ SOUR
1 TITL Local Archive Record
0 @I1@ INDI
1 NAME Jane /Smith/
1 SOUR @S2@
0 TRLR
"""
f = tmp_path / "sources2.ged"
f.write_text(gedcom, encoding='utf-8')
parser = GedcomParser(f)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser.parser)
out = _make_output_dir(tmp_path)
gen = MarkdownGenerator(out)
note = gen.generate_note(person)
content = note.read_text()
assert '## Sources' in content
assert '1. Local Archive Record' in content
def test_sources_publ_but_no_title_uses_url_as_text(self, tmp_path):
gedcom = """0 HEAD
1 SOUR TestApp
1 GEDC
2 VERS 5.5.1
1 CHAR UTF-8
0 @S3@ SOUR
1 PUBL https://example.org/doc/123
0 @I1@ INDI
1 NAME Alice /Doe/
1 SOUR @S3@
0 TRLR
"""
f = tmp_path / "sources3.ged"
f.write_text(gedcom, encoding='utf-8')
parser = GedcomParser(f)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser.parser)
out = _make_output_dir(tmp_path)
gen = MarkdownGenerator(out)
note = gen.generate_note(person)
content = note.read_text()
assert '## Sources' in content
assert '1. [https://example.org/doc/123](https://example.org/doc/123)' in content