Paragraph-level links, preview tooltips

This commit is contained in:
Adrian Rumpold
2025-04-30 12:04:38 +02:00
parent ea7885eeee
commit 7dd913df7b
25 changed files with 569 additions and 102 deletions

View File

@@ -2,7 +2,11 @@ import lxml.etree as ET
from fastapi import APIRouter, FastAPI, Response
from fastapi.middleware.cors import CORSMiddleware
from formex_viewer.formex4 import FormexArticleConverter
from formex_viewer.formex4 import (
FormexArticleConverter,
extract_article,
extract_paragraph,
)
from formex_viewer.main import (
CellarClient,
CellarIdentifier,
@@ -121,21 +125,46 @@ def toc(celex_id: str, language: Language = Language.ENG):
@api_router.get("/{celex_id}/articles/{article_id}/{language}")
def article(celex_id: str, article_id: int, language: Language = Language.ENG):
def article(
celex_id: str,
article_id: int,
language: Language = Language.ENG,
):
"""
Fetch an article from the server.
"""
xml = _get_fmx4_data(celex_id, language)
article = extract_article(xml, article_id=article_id)
article_xpath = "//ARTICLE"
articles = xml.xpath(article_xpath)
for article in articles:
num = article.get("IDENTIFIER").lstrip("0")
if num == str(article_id):
return Response(
FormexArticleConverter(language=language).convert_article(article),
media_type="text/html",
)
if article is None:
return Response(
"Article not found",
status_code=404,
)
return Response(
FormexArticleConverter(language=language).convert_article(article),
media_type="text/html",
)
@api_router.get("/{celex_id}/articles/{article_id}/{parag_id}/{language}")
def paragraph(
celex_id: str,
article_id: int,
parag_id: int,
language: Language = Language.ENG,
):
"""
Fetch a paragraph within an article from the server.
"""
xml = _get_fmx4_data(celex_id, language)
parag = extract_paragraph(xml, article_id=article_id, paragraph_id=parag_id)
return Response(
FormexArticleConverter(language=language).convert_article(parag),
media_type="text/html",
)
app.include_router(api_router, prefix="/api")