Skip to content

Commit

Permalink
Add support for download links from TEI metadata (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
scmmmh committed Feb 22, 2024
1 parent 9f5d324 commit a4e9511
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/de/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Änderungsgeschichte

## In Entwicklung

* **Neu**: Unterstützung für Downloadlinks in TEI Dateien

## 1.2.0 (06.02.2024)

* **Neu**: TEI Funktion zum Parsen von ISO8601 Daten
Expand Down
4 changes: 4 additions & 0 deletions docs/en/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## In Development

* **New**: Support download links in TEI documents

## 1.2.0 (06.02.2024)

* **New**: Added an ISO8601 formatting TEI function
Expand Down
15 changes: 12 additions & 3 deletions uedition/ext/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,36 @@ class TextSection(BaseModel):


class SingleFieldRule(BaseModel):
"""Validation model for a TEI field rule."""
"""Validation model for a TEI field rule with a single value."""

title: str
type: Literal["single"] = "single"
content: str


class ListFieldRule(BaseModel):
"""Validation model for a TEI field rule."""
"""Validation model for a TEI field rule with a list of values."""

title: str
type: Literal["list"]
content: str


class DownloadFieldRule(BaseModel):
"""Validation model for a TEI field rule that creates a download link."""

title: str
type: Literal["download"]
content: str
target: str


class FieldsSection(BaseModel):
"""Validation model for a TEI fields section."""

title: str
type: Literal["fields"]
fields: list[SingleFieldRule | ListFieldRule]
fields: list[SingleFieldRule | ListFieldRule | DownloadFieldRule]


class TEIConfig(BaseModel):
Expand Down
25 changes: 24 additions & 1 deletion uedition/ext/tei/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from docutils import nodes
from lxml import etree
from sphinx import addnodes
from sphinx.application import Sphinx
from sphinx.parsers import Parser as SphinxParser
from sphinx.writers.html import HTMLWriter
Expand Down Expand Up @@ -81,17 +82,18 @@ def parse(self: "TEIParser", inputstring: str, document: nodes.document) -> None
section_title.append(nodes.Text(conf_section["title"]))
section.append(section_title)
if conf_section["type"] == "text":
# Process a text section
source = root.xpath(conf_section["content"], namespaces=namespaces)
if len(source) > 0:
if conf_section["sort"]:
source.sort(key=self._sort_key(conf_section["sort"]))
# print(child.xpath(conf_section["sort"], namespaces=namespaces))
doc_section.append(section)
tmp = nodes.section()
for child in source:
self._walk_tree(child, tmp, conf_section["mappings"])
self._wrap_sections(section, tmp)
elif conf_section["type"] == "fields":
# Process a field or metadata section
doc_section.append(section)
fields = nodes.definition_list()
section.append(fields)
Expand All @@ -100,6 +102,8 @@ def parse(self: "TEIParser", inputstring: str, document: nodes.document) -> None
self._parse_single_field(fields, field, root)
elif field["type"] == "list":
self._parse_list_field(fields, field, root)
elif field["type"] == "download":
self._parse_download_field(fields, field, root)
document.append(doc_section)

def _sort_key(
Expand Down Expand Up @@ -284,6 +288,25 @@ def _parse_list_field(self: "TEIParser", parent: etree.Element, field: dict, roo
li.append(dd)
parent.append(li)

def _parse_download_field(self: "TEIParser", parent: etree.Element, field: dict, root: etree.Element) -> None:
"""Parse a download metadata field."""
content = root.xpath(field["content"], namespaces=namespaces)
target = root.xpath(field["target"], namespaces=namespaces)
if len(content) > 0 and len(target) > 0:
if isinstance(content, list):
content = content[0]
if isinstance(target, list):
target = target[0]
if content.strip() and target.strip():
li = nodes.definition_list_item()
dt = nodes.term()
dt.append(nodes.Text(field["title"]))
li.append(dt)
dd = nodes.definition()
dd.append(addnodes.download_reference(content, content, reftarget=target))
li.append(dd)
parent.append(li)


def setup(app: Sphinx) -> None:
"""Set up the TEI Sphinx extension."""
Expand Down

0 comments on commit a4e9511

Please sign in to comment.