Hierarchical TOC + routing

This commit is contained in:
Adrian Rumpold
2025-04-23 13:27:54 +02:00
parent 6dcf39dc58
commit 2165ce0d5b
11 changed files with 202 additions and 87 deletions

View File

@@ -28,7 +28,7 @@ type CacheKey = tuple[str, Language]
CACHE: dict[CacheKey, str] = {}
def _get_fmx4_data(celex_id: str, language: Language) -> str:
def _get_fmx4_data(celex_id: str, language: Language) -> ET.Element:
"""
Fetch the FMX4 data from the server.
"""
@@ -43,9 +43,10 @@ def _get_fmx4_data(celex_id: str, language: Language) -> str:
)
fmx4_data = client.publication_text(cellar_id, ContentType.ZIP_FMX4)
CACHE[(celex_id, language)] = fmx4_data
xml = ET.fromstring(fmx4_data.encode("utf-8"))
CACHE[(celex_id, language)] = xml
return fmx4_data
return xml
@app.get("/{celex_id}/articles")
@@ -53,8 +54,7 @@ def article_ids(celex_id: str, language: Language = Language.ENG):
"""
Fetch the article IDs from the server.
"""
fmx4_data = _get_fmx4_data(celex_id, language)
xml = ET.fromstring(fmx4_data.encode("utf-8"))
xml = _get_fmx4_data(celex_id, language)
article_xpath = "//ARTICLE/@IDENTIFIER"
article_ids = xml.xpath(article_xpath)
@@ -65,18 +65,16 @@ def article_ids(celex_id: str, language: Language = Language.ENG):
@app.get("/{celex_id}/toc/{language}")
def toc(celex_id: str, language: Language = Language.ENG):
"""
Fetch the table of contents from the server.
"""
fmx4_data = _get_fmx4_data(celex_id, language)
xml = ET.fromstring(fmx4_data.encode("utf-8"))
toc = []
for division in xml.xpath("//DIVISION"):
def _handle_division(division: ET.Element, level: int):
print(division)
title = ti_el[0] if (ti_el := division.xpath("TITLE/TI//text()")) else ""
subtitle = sti_el[0] if (sti_el := division.xpath("TITLE/STI//text()")) else ""
subdivisions = []
for subdivision in division.xpath("DIVISION") or []:
subdivisions.append(_handle_division(subdivision, level + 1))
articles = []
for article in division.xpath("ARTICLE") or []:
art_id = article.get("IDENTIFIER")
@@ -94,14 +92,23 @@ def toc(celex_id: str, language: Language = Language.ENG):
"subtitle": art_subtitle,
}
)
toc.append(
{
"title": title,
"type": "division",
"subtitle": subtitle,
"articles": articles,
}
)
return {
"type": "division",
"title": title,
"subtitle": subtitle,
"level": level,
"content": subdivisions + articles,
}
"""
Fetch the table of contents from the server.
"""
xml = _get_fmx4_data(celex_id, language)
toc = []
for division in xml.xpath("//ENACTING.TERMS/DIVISION"):
toc.append(_handle_division(division, 0))
return toc
@@ -111,8 +118,7 @@ def article(celex_id: str, article_id: int, language: Language = Language.ENG):
"""
Fetch an article from the server.
"""
fmx4_data = _get_fmx4_data(celex_id, language)
xml = ET.fromstring(fmx4_data.encode("utf-8"))
xml = _get_fmx4_data(celex_id, language)
article_xpath = "//ARTICLE"
articles = xml.xpath(article_xpath)