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

Method convert

docling/backend/md_backend.py:363–428  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

361 return {InputFormat.MD}
362
363 def convert(self) -> DoclingDocument:
364 _log.debug("converting Markdown...")
365
366 origin = DocumentOrigin(
367 filename=self.file.name or "file",
368 mimetype="text/markdown",
369 binary_hash=self.document_hash,
370 )
371
372 doc = DoclingDocument(name=self.file.stem or "file", origin=origin)
373
374 if self.is_valid():
375 # Parse the markdown into an abstract syntax tree (AST)
376 marko_parser = Markdown()
377 parsed_ast = marko_parser.parse(self.markdown)
378 # Start iterating from the root of the AST
379 self._iterate_elements(
380 element=parsed_ast,
381 depth=0,
382 doc=doc,
383 parent_item=None,
384 visited=set(),
385 )
386 self._process_inline_text(None, doc) # handle last hanging inline text
387 self._close_table(doc=doc) # handle any last hanging table
388
389 # if HTML blocks were detected, export to HTML and delegate to HTML backend
390 if self._html_blocks > 0:
391
392 # export to HTML
393 html_backend_cls = HTMLDocumentBackend
394 html_str = doc.export_to_html()
395
396 def _restore_original_html(txt, regex):
397 _txt, count = re.subn(regex, "", txt)
398 if count != self._html_blocks:
399 raise RuntimeError(
400 "An internal error has occurred during Markdown conversion."
401 )
402 return _txt
403
404 # restore original HTML by removing previouly added markers
405 for regex in [
406 rf"<pre>\s*<code>\s*{_START_MARKER}",
407 rf"{_STOP_MARKER}\s*</code>\s*</pre>",
408 ]:
409 html_str = _restore_original_html(txt=html_str, regex=regex)
410 self._html_blocks = 0
411
412 # delegate to HTML backend
413 stream = BytesIO(bytes(html_str, encoding="utf-8"))
414 in_doc = InputDocument(
415 path_or_stream=stream,
416 format=InputFormat.HTML,
417 backend=html_backend_cls,
418 filename=self.file.name,
419 )
420 html_backend_obj = html_backend_cls(

Callers

nothing calls this directly

Calls 6

is_validMethod · 0.95
_iterate_elementsMethod · 0.95
_process_inline_textMethod · 0.95
_close_tableMethod · 0.95
InputDocumentClass · 0.90
parseMethod · 0.45

Tested by

no test coverage detected