MCPcopy Create free account
hub / github.com/Paper2Poster/Paper2Poster / __init__

Method __init__

docling/backend/html_backend.py:24–49  ·  view source on GitHub ↗
(self, in_doc: "InputDocument", path_or_stream: Union[BytesIO, Path])

Source from the content-addressed store, hash-verified

22
23class HTMLDocumentBackend(DeclarativeDocumentBackend):
24 def __init__(self, in_doc: "InputDocument", path_or_stream: Union[BytesIO, Path]):
25 super().__init__(in_doc, path_or_stream)
26 _log.debug("About to init HTML backend...")
27 self.soup: Optional[Tag] = None
28 # HTML file:
29 self.path_or_stream = path_or_stream
30 # Initialise the parents for the hierarchy
31 self.max_levels = 10
32 self.level = 0
33 self.parents = {} # type: ignore
34 for i in range(0, self.max_levels):
35 self.parents[i] = None
36 self.labels = {} # type: ignore
37
38 try:
39 if isinstance(self.path_or_stream, BytesIO):
40 text_stream = self.path_or_stream.getvalue()
41 self.soup = BeautifulSoup(text_stream, "html.parser")
42 if isinstance(self.path_or_stream, Path):
43 with open(self.path_or_stream, "rb") as f:
44 html_content = f.read()
45 self.soup = BeautifulSoup(html_content, "html.parser")
46 except Exception as e:
47 raise RuntimeError(
48 f"Could not initialize HTML backend for file with hash {self.document_hash}."
49 ) from e
50
51 def is_valid(self) -> bool:
52 return self.soup is not None

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected