Files
fotoplaner/venv/lib/python3.12/site-packages/docling/backend/xml/jats_backend.py
T
anders 16c54c6653 Tilføjet venv med docling og afhængigheder
Testfiler opdelt i PNG og previews
2026-10-27 12:20:23 +01:00

1453 lines
52 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# SPDX-FileCopyrightText: The Docling Contributors
# SPDX-License-Identifier: MIT
"""Backend to parse articles in JATS (Journal Article Tag Suite) XML format.
JATS is a standard XML format used by publishers and journal archives including
PubMed Central (PMC), bioRxiv, and medRxiv for representing journal articles.
Security Note:
This module uses lxml.etree.XMLParser with secure configuration to protect
against XML External Entity (XXE) attacks and XML bombs. The parser is
configured with:
- resolve_entities: False (prevents entity resolution attacks)
- no_network: True (blocks all network access)
- dtd_validation: False (disables DTD validation)
- load_dtd: False (prevents loading external DTDs)
This configuration ensures safe parsing of JATS XML files while blocking
external entity fetching and preventing XXE attacks.
"""
from __future__ import annotations
import logging
import traceback
import warnings
from dataclasses import dataclass, replace
from io import BytesIO
from pathlib import Path
from typing import Final, cast
from docling_core.types.doc import (
DocItemLabel,
DoclingDocument,
DocumentOrigin,
GroupItem,
GroupLabel,
ImageRef,
NodeItem,
TableCell,
TableData,
TextItem,
)
from docling_core.types.doc.document import Formatting, Script
from lxml import etree
from pydantic import AnyUrl, ValidationError
from typing_extensions import TypedDict, override
from docling.backend.abstract_backend import DeclarativeDocumentBackend
from docling.backend.html_backend import HTMLDocumentBackend
from docling.backend.utils.image_resource_loader import ImageResourceLoader
from docling.backend.utils.table_spans import table_width
from docling.datamodel.backend_options import JatsBackendOptions
from docling.datamodel.base_models import InputFormat
from docling.datamodel.document import InputDocument
from docling.exceptions import DocumentLoadError
_BS4_AVAILABLE: bool = False
_BS4_IMPORT_ERROR: ImportError | None = None
try: # pragma: no cover - import-time guard
from bs4 import BeautifulSoup, NavigableString, Tag
from lxml import etree
_BS4_AVAILABLE = True
except ImportError as e: # pragma: no cover - import-time guard
_BS4_IMPORT_ERROR = e
_INSTALL_HINT = (
"The 'beautifulsoup4' and 'lxml' packages are required to process JATS files. "
"Install them with `pip install 'docling-slim[format-xml-jats]'`."
)
_log = logging.getLogger(__name__)
JATS_DTD_URL: Final[list[str]] = ["JATS-journalpublishing", "JATS-archive"]
DEFAULT_HEADER_ACKNOWLEDGMENTS: Final[str] = "Acknowledgments"
DEFAULT_HEADER_ABSTRACT: Final[str] = "Abstract"
DEFAULT_HEADER_FOOTNOTES: Final[str] = "Footnotes"
DEFAULT_HEADER_REFERENCES: Final[str] = "References"
DEFAULT_TEXT_ETAL: Final[str] = "et al."
_XLINK_HREF: Final[str] = "{http://www.w3.org/1999/xlink}href"
# `list-type` values that number the items, from the JATS 1.3 tag library.
_ORDERED_LIST_TYPES: Final[frozenset[str]] = frozenset(
{"order", "alpha-lower", "alpha-upper", "roman-lower", "roman-upper"}
)
# Elements that hold a whole article inside another one, such as a decision
# letter or an author reply, from the JATS 1.3 tag library.
_SUB_ARTICLE_TAGS: Final[tuple[str, ...]] = ("sub-article", "response")
_NOT_IN_SUB_ARTICLE: Final[str] = "not(ancestor::sub-article or ancestor::response)"
_RASTER_IMAGE_SUFFIXES: Final[tuple[str, ...]] = (
".jpg",
".jpeg",
".png",
".tif",
".tiff",
".gif",
)
# Maps JATS formatting tags to docling-core formatting attributes.
_JATS_FORMAT_TAG_MAP: Final[dict[str, dict[str, bool | Script]]] = {
"bold": {"bold": True},
"italic": {"italic": True},
"underline": {"underline": True},
"strike": {"strikethrough": True},
"sub": {"script": Script.SUB},
"sup": {"script": Script.SUPER},
}
@dataclass(slots=True)
class InlineSegment:
"""An ordered inline run of styled text or an inline formula.
Attributes:
label: Docling item label that classifies this inline run when it is
emitted as a text item.
text: Literal text carried by the run, or the LaTeX body when the run
is a formula.
formatting: Emphasis accumulated from the enclosing tags (bold, italic,
underline, strike, sub, sup), or ``None`` when the run is unstyled.
hyperlink: External link target inherited from an enclosing ``ext-link``,
or ``None`` when the run is unlinked.
"""
label: DocItemLabel
text: str
formatting: Formatting | None = None
hyperlink: AnyUrl | Path | None = None
class AbstractSection(TypedDict):
"""A single titled section inside a structured abstract."""
title: str
# Raw <p> nodes are walked at emit time to preserve inline styling.
paragraphs: list[etree._Element]
class Abstract(TypedDict):
label: str
paragraphs: list[etree._Element]
sections: list[AbstractSection] # structured sub-sections (<sec> children)
class Author(TypedDict):
name: str
affiliation_names: list[str]
class Citation(TypedDict):
author_names: str
title: str
source: str
year: str
volume: str
page: str
pub_id: str
publisher_name: str
publisher_loc: str
class Table(TypedDict):
label: str
caption: str
content: str
class XMLComponents(TypedDict):
title: str
authors: list[Author]
abstract: list[Abstract]
class JatsDocumentBackend(DeclarativeDocumentBackend):
"""Backend to parse articles in XML format tagged according to JATS definition.
The Journal Article Tag Suite (JATS) is an definition standard for the
representation of journal articles in XML format. Several publishers and journal
archives provide content in JATS format, including PubMed Central® (PMC), bioRxiv,
medRxiv, or Springer Nature.
Refer to https://jats.nlm.nih.gov for more details on JATS.
The code from this document backend has been developed by modifying parts of the
PubMed Parser library (version 0.5.0, released on 12.08.2024):
Achakulvisut et al., (2020).
Pubmed Parser: A Python Parser for PubMed Open-Access XML Subset and MEDLINE XML
Dataset XML Dataset.
Journal of Open Source Software, 5(46), 1979,
https://doi.org/10.21105/joss.01979
"""
@override
def __init__(
self,
in_doc: InputDocument,
path_or_stream: BytesIO | Path,
options: JatsBackendOptions | None = None,
) -> None:
if not _BS4_AVAILABLE:
raise ImportError(_INSTALL_HINT) from _BS4_IMPORT_ERROR
if options is None:
options = JatsBackendOptions()
super().__init__(in_doc, path_or_stream, options)
self.options: JatsBackendOptions
self.path_or_stream = path_or_stream
self.base_path: str | None = (
str(options.source_uri)
if options.source_uri is not None
else (
str(path_or_stream)
if options.enable_local_fetch and isinstance(path_or_stream, Path)
else None
)
)
self._image_loader = ImageResourceLoader(
enable_local_fetch=options.enable_local_fetch,
enable_remote_fetch=options.enable_remote_fetch,
)
# Initialize the root of the document hierarchy
self.root: NodeItem | None = None
self.hlevel: int = 0
self.valid: bool = False
try:
if isinstance(self.path_or_stream, BytesIO):
self.path_or_stream.seek(0)
parser = etree.XMLParser(
resolve_entities=False,
load_dtd=False,
no_network=True,
dtd_validation=False,
)
self.tree: etree._ElementTree = etree.parse(
self.path_or_stream, parser=parser
)
doc_info: etree.DocInfo = self.tree.docinfo
if doc_info.system_url and any(
kwd in doc_info.system_url for kwd in JATS_DTD_URL
):
self.valid = True
return
for ent in doc_info.internalDTD.iterentities():
if ent.system_url and any(
kwd in ent.system_url for kwd in JATS_DTD_URL
):
self.valid = True
return
except Exception as exc:
raise DocumentLoadError(
f"Could not initialize JATS backend for file with hash {self.document_hash}."
) from exc
@override
def is_valid(self) -> bool:
return self.valid
@classmethod
@override
def supports_pagination(cls) -> bool:
return False
@override
def unload(self):
if isinstance(self.path_or_stream, BytesIO):
self.path_or_stream.close()
self.path_or_stream = None
@classmethod
@override
def supported_formats(cls) -> set[InputFormat]:
return {InputFormat.XML_JATS}
@override
def convert(self) -> DoclingDocument:
try:
# Create empty document
origin = DocumentOrigin(
filename=self.file.name or "file",
mimetype="application/xml",
binary_hash=self.document_hash,
)
doc = DoclingDocument(name=self.file.stem or "file", origin=origin)
self.hlevel = 0
# Get metadata XML components
xml_components: XMLComponents = self._parse_metadata()
# Add metadata to the document
self._add_metadata(doc, xml_components)
# walk over the XML body
body = self.tree.xpath(f"//body[{_NOT_IN_SUB_ARTICLE}]")
if self.root and len(body) > 0:
self._walk_linear(doc, self.root, body[0])
# walk over the XML back matter
back = self.tree.xpath(f"//back[{_NOT_IN_SUB_ARTICLE}]")
if self.root and len(back) > 0:
self._walk_linear(doc, self.root, back[0])
# walk over the sub-articles
sub_articles = self.tree.xpath(
" | ".join(
f"//{tag}[{_NOT_IN_SUB_ARTICLE}]" for tag in _SUB_ARTICLE_TAGS
)
)
if self.root:
for sub_article in sub_articles:
self._add_sub_article(doc, self.root, sub_article)
except Exception:
_log.error(traceback.format_exc())
return doc
@staticmethod
def _get_text(node: etree._Element, sep: str | None = None) -> str:
skip_tags = ["term", "disp-formula", "inline-formula"]
text: str = (
node.text.replace("\n", " ")
if (node.tag not in skip_tags and node.text)
else ""
)
for child in list(node):
if child.tag not in skip_tags:
# TODO: apply styling according to child.tag when supported by docling-core
text += JatsDocumentBackend._get_text(child, sep)
if sep:
text = text.rstrip(sep) + sep
text += child.tail.replace("\n", " ") if child.tail else ""
return text
@staticmethod
def _get_label(node: etree._Element) -> str:
"""Return the text of the `<label>` child of `node`, or an empty string."""
label = node.find("label")
return JatsDocumentBackend._get_text(label).strip() if label is not None else ""
@staticmethod
def _normalize_whitespace(text: str | None) -> str:
return " ".join(text.split()) if text else ""
@staticmethod
def _get_node_text(node: etree._Element) -> str:
return JatsDocumentBackend._normalize_whitespace(" ".join(node.itertext()))
@staticmethod
def _parse_abstract_section(section_node: etree._Element) -> AbstractSection:
"""Parse an abstract section while retaining its paragraph nodes."""
title_nodes = section_node.xpath("title|label")
title = (
JatsDocumentBackend._get_node_text(title_nodes[0]) if title_nodes else ""
)
paragraphs: list[etree._Element] = [
child_node
for child_node in section_node
if child_node.tag == "p" and "".join(child_node.itertext()).strip()
]
return AbstractSection(title=title, paragraphs=paragraphs)
@staticmethod
def _parse_structured_name(
name_node: etree._Element,
order: tuple[str, ...] = ("prefix", "given-names", "surname", "suffix"),
) -> str:
name_parts: list[str] = []
for tag_name in order:
for part_node in name_node.xpath(tag_name):
part_text = JatsDocumentBackend._get_node_text(part_node)
if part_text:
name_parts.append(part_text)
if name_parts:
return JatsDocumentBackend._normalize_whitespace(" ".join(name_parts))
return JatsDocumentBackend._get_node_text(name_node)
@staticmethod
def _parse_name_alternatives(node: etree._Element) -> str:
for tag_name in ["name", "string-name", "collab-name", "collab"]:
for name_node in node.xpath(tag_name):
if tag_name == "name":
name = JatsDocumentBackend._parse_structured_name(name_node)
else:
name = JatsDocumentBackend._get_node_text(name_node)
if name:
return name
return ""
@staticmethod
def _parse_contrib_name(author_node: etree._Element) -> str:
for name_node in author_node.xpath("name"):
name = JatsDocumentBackend._parse_structured_name(name_node)
if name:
return name
for name_node in author_node.xpath("string-name"):
name = JatsDocumentBackend._get_node_text(name_node)
if name:
return name
for alternatives_node in author_node.xpath("name-alternatives"):
name = JatsDocumentBackend._parse_name_alternatives(alternatives_node)
if name:
return name
for tag_name in ["collab-name", "collab"]:
for name_node in author_node.xpath(tag_name):
name = JatsDocumentBackend._get_node_text(name_node)
if name:
return name
for tag_name in ["collab-name-alternatives", "collab-alternatives"]:
for alternatives_node in author_node.xpath(tag_name):
name = JatsDocumentBackend._parse_name_alternatives(alternatives_node)
if name:
return name
if author_node.xpath("anonymous"):
return "Anonymous"
return ""
def _find_metadata(self) -> etree._Element | None:
meta_names: list[str] = ["article-meta", "book-part-meta"]
meta: etree._Element | None = None
for name in meta_names:
node = self.tree.xpath(f".//{name}")
if len(node) > 0:
meta = node[0]
break
return meta
def _parse_abstract(self) -> list[Abstract]:
abs_list: list[Abstract] = []
for abs_node in self.tree.xpath(".//abstract"):
paragraphs: list[etree._Element] = []
sections: list[AbstractSection] = []
for child_node in abs_node:
if child_node.tag == "p":
if "".join(child_node.itertext()).strip():
paragraphs.append(child_node)
elif child_node.tag == "sec":
section = JatsDocumentBackend._parse_abstract_section(child_node)
if section["paragraphs"]:
sections.append(section)
label_node = abs_node.xpath("title|label")
label = (
JatsDocumentBackend._get_node_text(label_node[0]) if label_node else ""
)
abstract: Abstract = Abstract(
label=label,
paragraphs=paragraphs,
sections=sections,
)
abs_list.append(abstract)
return abs_list
def _parse_authors(self) -> list[Author]:
# Get mapping between affiliation ids and names
authors: list[Author] = []
meta: etree._Element | None = self._find_metadata()
if meta is None:
return authors
affiliation_names = []
for affiliation_node in meta.xpath(".//aff[@id]"):
aff = ", ".join([t for t in affiliation_node.itertext() if t.strip()])
aff = aff.replace("\n", " ")
label = affiliation_node.xpath("label")
if label:
# TODO: once superscript is supported, add label with formatting
aff = aff.removeprefix(f"{label[0].text}, ")
affiliation_names.append(aff)
affiliation_ids_names = dict(
zip(meta.xpath(".//aff[@id]/@id"), affiliation_names)
)
# Get author names and affiliation names
for author_node in meta.xpath(
'.//contrib-group/contrib[@contrib-type="author"]'
):
author: Author = {
"name": "",
"affiliation_names": [],
}
# Affiliation names
affiliation_ids = [
a.attrib["rid"] for a in author_node.xpath('xref[@ref-type="aff"]')
]
for id in affiliation_ids:
if id in affiliation_ids_names:
author["affiliation_names"].append(affiliation_ids_names[id])
# Name
author["name"] = JatsDocumentBackend._parse_contrib_name(author_node)
if not author["name"]:
continue
authors.append(author)
return authors
def _parse_title(self) -> str:
meta_names: list[str] = [
"article-meta",
"collection-meta",
"book-meta",
"book-part-meta",
]
title_names: list[str] = ["article-title", "subtitle", "title", "label"]
titles: list[str] = [
" ".join(
JatsDocumentBackend._normalize_whitespace(elem.text)
for elem in list(title_node)
if elem.tag in title_names
).strip()
for title_node in self.tree.xpath(
"|".join([f".//{item}/title-group" for item in meta_names])
)
]
text = " - ".join(titles)
return text
def _parse_metadata(self) -> XMLComponents:
"""Parsing JATS document metadata."""
xml_components: XMLComponents = {
"title": self._parse_title(),
"authors": self._parse_authors(),
"abstract": self._parse_abstract(),
}
return xml_components
def _add_abstract(
self, doc: DoclingDocument, xml_components: XMLComponents
) -> None:
for abstract in xml_components["abstract"]:
sections = abstract["sections"]
paragraphs = abstract["paragraphs"]
title = abstract["label"] or DEFAULT_HEADER_ABSTRACT
# Skip empty abstracts.
if not paragraphs and not sections:
continue
abstract_heading = doc.add_heading(
parent=self.root, text=title, level=self.hlevel + 1
)
# Inline-group serializer spacing is tracked in docling-core#693.
for paragraph in paragraphs:
self._walk_linear(doc, abstract_heading, paragraph)
# Structured abstract: emit each <sec> as a sub-heading with
# its own paragraph(s) beneath the abstract heading.
for section in sections:
section_title = section["title"]
if section_title:
section_parent: NodeItem = doc.add_heading(
parent=abstract_heading,
text=section_title,
level=self.hlevel + 2,
)
else:
section_parent = abstract_heading
for paragraph in section["paragraphs"]:
self._walk_linear(doc, section_parent, paragraph)
return
def _add_authors(self, doc: DoclingDocument, xml_components: XMLComponents) -> None:
# TODO: once docling supports text formatting, add affiliation reference to
# author names through superscripts
authors: list = [item["name"] for item in xml_components["authors"]]
authors_str = ", ".join(authors)
affiliations: list = [
item
for author in xml_components["authors"]
for item in author["affiliation_names"]
]
affiliations_str = "; ".join(list(dict.fromkeys(affiliations)))
if authors_str:
doc.add_text(
parent=self.root,
text=authors_str,
label=DocItemLabel.PARAGRAPH,
)
if affiliations_str:
doc.add_text(
parent=self.root,
text=affiliations_str,
label=DocItemLabel.PARAGRAPH,
)
return
def _add_citation(self, doc: DoclingDocument, parent: NodeItem, text: str) -> None:
if isinstance(parent, GroupItem) and parent.label == GroupLabel.LIST:
doc.add_list_item(text=text, enumerated=False, parent=parent)
else:
doc.add_text(text=text, label=DocItemLabel.TEXT, parent=parent)
return
def _parse_element_citation(self, node: etree._Element) -> str:
citation: Citation = {
"author_names": "",
"title": "",
"source": "",
"year": "",
"volume": "",
"page": "",
"pub_id": "",
"publisher_name": "",
"publisher_loc": "",
}
_log.debug("Citation parsing started")
# Author names: surname before given-names (citation format).
names = []
for name_node in node.xpath(".//name"):
name_str = JatsDocumentBackend._parse_structured_name(
name_node, order=("surname", "given-names")
)
if name_str:
names.append(name_str)
etal_node = node.xpath(".//etal")
if len(etal_node) > 0:
etal_text = etal_node[0].text or DEFAULT_TEXT_ETAL
names.append(etal_text)
citation["author_names"] = ", ".join(names)
titles: list[str] = [
"article-title",
"chapter-title",
"data-title",
"issue-title",
"part-title",
"trans-title",
]
title_node: etree._Element | None = None
for name in titles:
name_node = node.xpath(name)
if len(name_node) > 0:
title_node = name_node[0]
break
citation["title"] = (
JatsDocumentBackend._get_text(title_node)
if title_node is not None
else JatsDocumentBackend._normalize_whitespace(node.text)
)
# Journal, year, publisher name, publisher location, volume, elocation
fields: list[str] = [
"source",
"year",
"publisher-name",
"publisher-loc",
"volume",
]
for item in fields:
item_node = node.xpath(item)
if len(item_node) > 0:
citation[item.replace("-", "_")] = ( # type: ignore[literal-required]
JatsDocumentBackend._normalize_whitespace(item_node[0].text)
)
# Publication identifier
if len(node.xpath("pub-id")) > 0:
pub_id: list[str] = []
for id_node in node.xpath("pub-id"):
id_type = id_node.get("assigning-authority") or id_node.get(
"pub-id-type"
)
id_text = id_node.text
if id_type and id_text:
pub_id.append(
id_type.replace("\n", " ").strip().upper()
+ ": "
+ id_text.replace("\n", " ").strip()
)
if pub_id:
citation["pub_id"] = ", ".join(pub_id)
# Pages
if len(node.xpath("elocation-id")) > 0:
citation["page"] = JatsDocumentBackend._normalize_whitespace(
node.xpath("elocation-id")[0].text
)
elif len(node.xpath("fpage")) > 0:
citation["page"] = JatsDocumentBackend._normalize_whitespace(
node.xpath("fpage")[0].text
)
if len(node.xpath("lpage")) > 0:
lpage = JatsDocumentBackend._normalize_whitespace(
node.xpath("lpage")[0].text
)
if lpage:
citation["page"] += "–" + lpage # noqa: RUF001
# Flatten the citation to string
text = ""
if citation["author_names"]:
text += citation["author_names"].rstrip(".") + ". "
if citation["title"]:
text += citation["title"] + ". "
if citation["source"]:
text += citation["source"] + ". "
if citation["publisher_name"]:
if citation["publisher_loc"]:
text += f"{citation['publisher_loc']}: "
text += citation["publisher_name"] + ". "
if citation["volume"]:
text = text.rstrip(". ")
text += f" {citation['volume']}. "
if citation["page"]:
text = text.rstrip(". ")
if citation["volume"]:
text += ":"
text += citation["page"] + ". "
if citation["year"]:
text = text.rstrip(". ")
text += f" ({citation['year']})."
if citation["pub_id"]:
text = text.rstrip(".") + ". "
text += citation["pub_id"]
_log.debug("Citation flattened")
return text
def _add_equation(
self, doc: DoclingDocument, parent: NodeItem, node: etree._Element
) -> None:
formula = JatsDocumentBackend._extract_tex_math(node)
if formula:
doc.add_text(label=DocItemLabel.FORMULA, text=formula, parent=parent)
return
@staticmethod
def _extract_tex_math(node: etree._Element) -> str | None:
if not node.text:
return None
text = node.text.strip()
for delimiter in ("$$", "$"):
if (
len(text) > 2 * len(delimiter)
and text.startswith(delimiter)
and text.endswith(delimiter)
):
text = text[len(delimiter) : -len(delimiter)].strip()
break
return text or None
@staticmethod
def _merge_formatting(formatting: Formatting | None, tag: str) -> Formatting | None:
if tag not in _JATS_FORMAT_TAG_MAP:
return formatting
base = formatting if formatting else Formatting()
return base.model_copy(update=_JATS_FORMAT_TAG_MAP[tag])
@staticmethod
def _parse_ext_link_href(href: str | None) -> AnyUrl | Path | None:
if href is None or not (href := href.strip()):
return None
try:
return AnyUrl(href)
except ValidationError:
return Path(href)
@staticmethod
def _strip_segments(segments: list[InlineSegment]) -> list[InlineSegment]:
stripped: list[InlineSegment] = []
for segment in segments:
text = segment.text.strip()
if text:
stripped.append(replace(segment, text=text))
return stripped
@staticmethod
def _walk_inline_formula(
node: etree._Element,
formatting: Formatting | None = None,
hyperlink: AnyUrl | Path | None = None,
) -> list[InlineSegment]:
current = JatsDocumentBackend._merge_formatting(formatting, node.tag)
segments: list[InlineSegment] = []
if node.text:
text = JatsDocumentBackend._normalize_xml_line_breaks(node.text)
if text:
segments.append(
InlineSegment(
label=DocItemLabel.TEXT,
text=text,
formatting=current,
hyperlink=hyperlink,
)
)
for child in node:
tag = child.tag
if not isinstance(tag, str) or tag.endswith("}math"):
# Skip comments, processing instructions, and MathML.
pass
elif tag == "tex-math":
formula = JatsDocumentBackend._extract_tex_math(child)
if formula is not None:
segments.append(
InlineSegment(
label=DocItemLabel.FORMULA,
text=formula,
hyperlink=hyperlink,
)
)
else:
segments.extend(
JatsDocumentBackend._walk_inline_formula(child, current, hyperlink)
)
if child.tail:
tail = JatsDocumentBackend._normalize_xml_line_breaks(child.tail)
if tail:
segments.append(
InlineSegment(
label=DocItemLabel.TEXT,
text=tail,
formatting=current,
hyperlink=hyperlink,
)
)
return segments
@staticmethod
def _normalize_xml_line_breaks(text: str) -> str:
"""Collapse XML formatting whitespace around line breaks."""
if "\n" not in text:
return text
chunks = text.split("\n")
middle = [chunk.strip(" \t") for chunk in chunks[1:-1]]
normalized = [
chunks[0].rstrip(" \t"),
*(chunk for chunk in middle if chunk),
chunks[-1].lstrip(" \t"),
]
return " ".join(normalized)
@staticmethod
def _append_run(
segments: list[InlineSegment],
text: str,
formatting: Formatting | None,
hyperlink: AnyUrl | Path | None = None,
) -> None:
"""Append text, coalescing when formatting and hyperlink both match."""
text = JatsDocumentBackend._normalize_xml_line_breaks(text)
if not text:
return
if (
segments
and segments[-1].label == DocItemLabel.TEXT
and segments[-1].formatting == formatting
and segments[-1].hyperlink == hyperlink
):
segments[-1] = replace(segments[-1], text=segments[-1].text + text)
else:
segments.append(
InlineSegment(
label=DocItemLabel.TEXT,
text=text,
formatting=formatting,
hyperlink=hyperlink,
)
)
@staticmethod
def _extend_segments(
segments: list[InlineSegment], more: list[InlineSegment]
) -> None:
"""Extend ``segments``, coalescing text with equal formatting and hyperlink."""
for segment in more:
if segment.label == DocItemLabel.TEXT:
JatsDocumentBackend._append_run(
segments,
segment.text,
segment.formatting,
segment.hyperlink,
)
else:
segments.append(segment)
@staticmethod
def _emit_inline(
doc: DoclingDocument, parent: NodeItem, segments: list[InlineSegment]
) -> None:
"""Emit inline segments under ``parent``, wrapping many in an inline group."""
segments = JatsDocumentBackend._strip_segments(segments)
if not segments:
return
container = doc.add_inline_group(parent=parent) if len(segments) > 1 else parent
for segment in segments:
doc.add_text(
label=segment.label,
text=segment.text,
formatting=segment.formatting,
hyperlink=segment.hyperlink,
parent=container,
)
def _add_figure_captions(
self, doc: DoclingDocument, parent: NodeItem, node: etree._Element
) -> None:
label_node = node.xpath("label")
label: str | None = (
JatsDocumentBackend._get_text(label_node[0]).strip() if label_node else ""
)
caption_node = node.xpath("caption")
caption: str | None
if len(caption_node) > 0:
caption = ""
for caption_par in list(caption_node[0]):
if caption_par.xpath(".//supplementary-material"):
continue
caption += JatsDocumentBackend._get_text(caption_par).strip() + " "
caption = caption.strip()
else:
caption = None
# TODO: format label vs caption once styling is supported
fig_text: str = f"{label}{' ' if label and caption else ''}{caption}"
fig_caption: TextItem | None = (
doc.add_text(label=DocItemLabel.CAPTION, text=fig_text)
if fig_text
else None
)
doc.add_picture(
parent=parent,
caption=fig_caption,
image=self._load_figure_image(node),
)
return
def _load_figure_image(self, node: etree._Element) -> ImageRef | None:
"""Load an explicitly enabled local JATS figure image."""
if (
not self.options.fetch_images
or self.base_path is None
or not ImageResourceLoader.is_local_path(self.base_path)
):
return None
graphic_nodes = node.xpath("graphic | alternatives/graphic")
if not graphic_nodes:
return None
missing_hrefs: list[str] = []
for graphic_node in graphic_nodes:
href = graphic_node.get(_XLINK_HREF)
if href is None or not href.strip():
continue
href = href.strip()
if not ImageResourceLoader.is_local_path(href):
continue
# An absolute rendition is invalid for a confined local base, but it
# must not prevent a later relative rendition from being used.
if ImageResourceLoader.is_absolute_path(href):
warnings.warn(
"Could not process an image from "
f"{href}: Absolute paths are not allowed with local base_path."
)
continue
if Path(href).suffix.lower() == ".svg":
_log.warning("Skipping unsupported JATS SVG figure image: %s", href)
continue
candidate_hrefs = [href]
if not Path(href).suffix:
candidate_hrefs.extend(
f"{href}{suffix}" for suffix in _RASTER_IMAGE_SUFFIXES
)
found_candidate = False
for candidate_href in candidate_hrefs:
try:
resolved_href = self._image_loader.resolve_relative_path(
candidate_href, self.base_path
)
except ValueError as e:
warnings.warn(f"Could not process an image from {href}: {e}")
return None
# Extensionless JATS references require probing several conventional
# suffixes. Check readability first so expected probe misses do not
# emit one warning per suffix; decoding remains centralized in the
# loader.
if Path(resolved_href).is_file():
found_candidate = True
image_ref = self._image_loader.create_image_ref(
resolved_href, self.base_path
)
if image_ref is not None:
return image_ref
if not found_candidate:
missing_hrefs.append(href)
if missing_hrefs:
warnings.warn(
"Could not process JATS figure image(s) "
f"{', '.join(missing_hrefs)}: no matching local file exists."
)
return None
def _add_metadata(
self, doc: DoclingDocument, xml_components: XMLComponents
) -> None:
self._add_title(doc, xml_components)
self._add_authors(doc, xml_components)
self._add_abstract(doc, xml_components)
return
@staticmethod
def parse_table_data(element: Tag) -> TableData | None:
# TODO, see how to implement proper support for rich tables from HTML backend
nested_tables = element.find("table")
if nested_tables is not None:
_log.debug("Skipping nested table.")
return None
# Find the number of rows and columns (taking into account spans)
num_rows = 0
row_cell_spans: list[list[tuple[int, int]]] = []
for row in element("tr"):
cell_spans: list[tuple[int, int]] = []
is_row_header = True
if not isinstance(row, Tag):
continue
for cell in row(["td", "th"]):
if not isinstance(row, Tag):
continue
cell_tag = cast(Tag, cell)
col_span, row_span = HTMLDocumentBackend._get_cell_spans(cell_tag)
cell_spans.append((col_span, row_span))
if cell_tag.name == "td" or row_span == 1:
is_row_header = False
row_cell_spans.append(cell_spans)
if not is_row_header:
num_rows += 1
num_cols = table_width(row_cell_spans)
_log.debug(f"The table has {num_rows} rows and {num_cols} cols.")
grid: list = [[None for _ in range(num_cols)] for _ in range(num_rows)]
data = TableData(num_rows=num_rows, num_cols=num_cols, table_cells=[])
# Iterate over the rows in the table
start_row_span = 0
row_idx = -1
for row in element("tr"):
if not isinstance(row, Tag):
continue
# For each row, find all the column cells (both <td> and <th>)
cells = row(["td", "th"])
# Check if cell is in a column header or row header
col_header = True
row_header = True
for html_cell in cells:
if isinstance(html_cell, Tag):
_, row_span = HTMLDocumentBackend._get_cell_spans(html_cell)
if html_cell.name == "td":
col_header = False
row_header = False
elif row_span == 1:
row_header = False
if not row_header:
row_idx += 1
start_row_span = 0
else:
start_row_span += 1
# Extract the text content of each cell
col_idx = 0
for html_cell in cells:
if not isinstance(html_cell, Tag):
continue
# extract inline formulas
for formula in html_cell("inline-formula"):
math_parts = formula.text.split("$$")
if len(math_parts) == 3:
math_formula = f"$${math_parts[1]}$$"
formula.replace_with(NavigableString(math_formula))
# TODO: extract content correctly from table-cells with lists
text = HTMLDocumentBackend.get_text(html_cell).strip()
col_span, row_span = HTMLDocumentBackend._get_cell_spans(html_cell)
if row_header:
row_span -= 1
while (
col_idx < num_cols
and grid[row_idx + start_row_span][col_idx] is not None
):
col_idx += 1
# Keep the cell within the table so the fill below stays
# proportional to the table size, not to the declared spans.
row_span = min(row_span, max(num_rows - (row_idx + start_row_span), 1))
col_span = min(col_span, max(num_cols - col_idx, 1))
for r in range(start_row_span, start_row_span + row_span):
for c in range(col_span):
if row_idx + r < num_rows and col_idx + c < num_cols:
grid[row_idx + r][col_idx + c] = text
table_cell = TableCell(
text=text,
row_span=row_span,
col_span=col_span,
start_row_offset_idx=start_row_span + row_idx,
end_row_offset_idx=start_row_span + row_idx + row_span,
start_col_offset_idx=col_idx,
end_col_offset_idx=col_idx + col_span,
column_header=col_header,
row_header=((not col_header) and html_cell.name == "th"),
)
data.table_cells.append(table_cell)
return data
def _add_table(
self, doc: DoclingDocument, parent: NodeItem, table_xml_component: Table
) -> None:
soup = BeautifulSoup(table_xml_component["content"], "html.parser")
table_tag = soup.find("table")
if not isinstance(table_tag, Tag):
return
data = JatsDocumentBackend.parse_table_data(table_tag)
# TODO: format label vs caption once styling is supported
label = table_xml_component["label"]
caption = table_xml_component["caption"]
table_text: str = f"{label}{' ' if label and caption else ''}{caption}"
table_caption: TextItem | None = (
doc.add_text(label=DocItemLabel.CAPTION, text=table_text)
if table_text
else None
)
if data is not None:
doc.add_table(data=data, parent=parent, caption=table_caption)
return
def _add_tables(
self, doc: DoclingDocument, parent: NodeItem, node: etree._Element
) -> None:
table: Table = {"label": "", "caption": "", "content": ""}
# Content
if len(node.xpath("table")) > 0:
table_content_node = node.xpath("table")[0]
elif len(node.xpath("alternatives/table")) > 0:
table_content_node = node.xpath("alternatives/table")[0]
else:
table_content_node = None
if table_content_node is not None:
table["content"] = etree.tostring(table_content_node).decode("utf-8")
# Caption
caption_node = node.xpath("caption")
caption: str | None
if caption_node:
caption = ""
for caption_par in list(caption_node[0]):
if caption_par.xpath(".//supplementary-material"):
continue
caption += JatsDocumentBackend._get_text(caption_par).strip() + " "
caption = caption.strip()
else:
caption = None
if caption is not None:
table["caption"] = caption
# Label
if len(node.xpath("label")) > 0:
table["label"] = node.xpath("label")[0].text
try:
self._add_table(doc, parent, table)
except Exception:
_log.warning(f"Skipping unsupported table in {self.file!s}")
return
def _add_title(self, doc: DoclingDocument, xml_components: XMLComponents) -> None:
self.root = doc.add_text(
parent=None,
text=xml_components["title"],
label=DocItemLabel.TITLE,
)
return
def _add_footnote_group(
self,
doc: DoclingDocument,
parent: NodeItem,
node: etree._Element,
) -> None:
footnote_segments: list[list[InlineSegment]] = []
for fn in node.iterchildren(tag="fn"):
segments = JatsDocumentBackend._strip_segments(
JatsDocumentBackend._walk_inline_formula(fn)
)
if segments:
# Keep the FOOTNOTE label on plain text runs; formulas stay
# labeled as formulas.
footnote_segments.append(
[
(
replace(segment, label=DocItemLabel.FOOTNOTE)
if segment.label == DocItemLabel.TEXT
else segment
)
for segment in segments
]
)
if not footnote_segments:
return
title = node.xpath("title")
title_text = (
JatsDocumentBackend._get_node_text(title[0]) or DEFAULT_HEADER_FOOTNOTES
if title
else DEFAULT_HEADER_FOOTNOTES
)
hlevel: int = self.hlevel + 1
heading = doc.add_heading(text=title_text, parent=parent, level=hlevel)
footnote_group = doc.add_group(
label=GroupLabel.LIST,
name="footnotes",
parent=heading,
)
for segments in footnote_segments:
list_item = doc.add_list_item(parent=footnote_group, text="")
JatsDocumentBackend._emit_inline(doc, list_item, segments)
def _add_sub_article(
self,
doc: DoclingDocument,
parent: NodeItem,
node: etree._Element,
) -> None:
"""Add the body and back matter of a `<sub-article>` or `<response>`.
Like a `<sec>`, the content goes under a heading with the title of the
sub-article, if it has one. A nested sub-article goes under the
heading of the sub-article that contains it.
"""
title = node.xpath(
"front-stub/title-group/article-title"
" | front/article-meta/title-group/article-title"
)
text = (
JatsDocumentBackend._normalize_whitespace(
JatsDocumentBackend._get_text(title[0])
)
if title
else ""
)
new_parent = parent
if text:
self.hlevel += 1
new_parent = doc.add_heading(text=text, parent=parent, level=self.hlevel)
for child in node.iterchildren("body", "back"):
self._walk_linear(doc, new_parent, child)
for child in node.iterchildren(*_SUB_ARTICLE_TAGS):
self._add_sub_article(doc, new_parent, child)
if text:
self.hlevel -= 1
def _walk_linear(
self,
doc: DoclingDocument,
parent: NodeItem,
node: etree._Element,
formatting: Formatting | None = None,
hyperlink: AnyUrl | Path | None = None,
) -> list[InlineSegment]:
skip_tags = ["term"]
flush_tags = ["ack", "sec", "list", "boxed-text", "disp-formula", "fig"]
new_parent: NodeItem = parent
current = JatsDocumentBackend._merge_formatting(formatting, node.tag)
current_hyperlink = hyperlink
if node.tag == "ext-link":
ext_link = JatsDocumentBackend._parse_ext_link_href(node.get(_XLINK_HREF))
if ext_link is not None:
current_hyperlink = ext_link
inline_segments: list[InlineSegment] = []
if node.tag not in skip_tags and node.text:
JatsDocumentBackend._append_run(
inline_segments, node.text, current, current_hyperlink
)
for child in list(node):
stop_walk: bool = False
# flush pending inline content before a block child in a paragraph
if node.tag == "p" and child.tag in flush_tags:
JatsDocumentBackend._emit_inline(doc, parent, inline_segments)
inline_segments = []
# add elements and decide whether to stop walking
if child.tag in ("sec", "ack"):
header = child.xpath("title|label")
text: str | None = None
if len(header) > 0:
text = JatsDocumentBackend._get_text(header[0])
elif child.tag == "ack":
text = DEFAULT_HEADER_ACKNOWLEDGMENTS
if text:
self.hlevel += 1
new_parent = doc.add_heading(
text=text, parent=parent, level=self.hlevel
)
elif child.tag == "list":
new_parent = doc.add_group(
label=GroupLabel.LIST, name="list", parent=parent
)
elif child.tag == "list-item":
# TODO: address non-paragraph, non-list content inside list-item
# (e.g. disp-formula, fig, table-wrap)
text_parts: list[str] = []
nested_lists: list[etree._Element] = []
for elem in child:
if elem.tag == "p":
text_parts.append(JatsDocumentBackend._get_text(elem).strip())
elif elem.tag == "list":
nested_lists.append(elem)
text = " ".join(part for part in text_parts if part)
new_parent = doc.add_list_item(
text=text,
enumerated=node.get("list-type", "").strip().lower()
in _ORDERED_LIST_TYPES,
marker=JatsDocumentBackend._get_label(child),
parent=parent,
)
for nested in nested_lists:
nested_group = doc.add_group(
label=GroupLabel.LIST, name="list", parent=new_parent
)
self._walk_linear(doc, nested_group, nested)
stop_walk = True
elif child.tag == "fig":
self._add_figure_captions(doc, parent, child)
stop_walk = True
elif child.tag == "table-wrap":
self._add_tables(doc, parent, child)
stop_walk = True
elif child.tag == "suplementary-material":
stop_walk = True
elif child.tag == "fn-group":
self._add_footnote_group(
doc,
parent,
child,
)
stop_walk = True
elif child.tag == "ref-list" and node.tag != "ref-list":
header = child.xpath("title|label")
text = (
JatsDocumentBackend._get_text(header[0])
if len(header) > 0
else DEFAULT_HEADER_REFERENCES
)
new_parent = doc.add_heading(text=text, parent=parent)
new_parent = doc.add_group(
parent=new_parent, label=GroupLabel.LIST, name="list"
)
elif child.tag == "element-citation":
text = self._parse_element_citation(child)
self._add_citation(doc, parent, text)
stop_walk = True
elif child.tag == "mixed-citation":
text = JatsDocumentBackend._get_text(child).strip()
self._add_citation(doc, parent, text)
stop_walk = True
elif child.tag == "tex-math":
self._add_equation(doc, parent, child)
stop_walk = True
elif child.tag == "inline-formula":
# Inline formula: tex-math stays inline, unlike block <disp-formula>.
JatsDocumentBackend._extend_segments(
inline_segments,
JatsDocumentBackend._walk_inline_formula(
child, current, current_hyperlink
),
)
stop_walk = True
# step into child
if not stop_walk:
child_segments = self._walk_linear(
doc, new_parent, child, current, current_hyperlink
)
if not (node.getparent().tag == "p" and node.tag in flush_tags):
JatsDocumentBackend._extend_segments(
inline_segments, child_segments
)
if child.tag in ("sec", "ack") and text:
self.hlevel -= 1
# pick up the tail text
if child.tail:
JatsDocumentBackend._append_run(
inline_segments, child.tail, current, current_hyperlink
)
# emit the paragraph, or backpropagate inline content to the parent
if node.tag == "p":
JatsDocumentBackend._emit_inline(doc, parent, inline_segments)
return []
return inline_segments