Skip to content

Commit

Permalink
feat: escape underscores that are within latex equations (#137)
Browse files Browse the repository at this point in the history
* Add regex to escape underscores within latex equations

Signed-off-by: Rafael Teixeira de Lima <[email protected]>

* Remove debug

Signed-off-by: Rafael Teixeira de Lima <[email protected]>

* Handle block equations

Signed-off-by: Rafael Teixeira de Lima <[email protected]>

* Add double $ to standalone equations outputs

Signed-off-by: Rafael Teixeira de Lima <[email protected]>

---------

Signed-off-by: Rafael Teixeira de Lima <[email protected]>
  • Loading branch information
rateixei authored Jan 29, 2025
1 parent c9739b2 commit 0d5cd11
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion docling_core/types/doc/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,10 @@ def export_to_markdown( # noqa: C901
text = f"{list_indent}{marker} {item.text}"
mdtexts.append(text)

elif isinstance(item, TextItem) and item.label in [DocItemLabel.FORMULA]:
in_list = False
mdtexts.append(f"$${item.text}$$")

elif isinstance(item, TextItem) and item.label in labels:
in_list = False
if len(item.text) and text_width > 0:
Expand Down Expand Up @@ -2215,10 +2219,14 @@ def escape_underscores(text):
"""Escape underscores but leave them intact in the URL.."""
# Firstly, identify all the URL patterns.
url_pattern = r"!\[.*?\]\((.*?)\)"
# Matches both inline ($...$) and block ($$...$$) LaTeX equations:
latex_pattern = r"\$\$?(?:\\.|[^$\\])*\$\$?"
combined_pattern = f"({url_pattern})|({latex_pattern})"

parts = []
last_end = 0

for match in re.finditer(url_pattern, text):
for match in re.finditer(combined_pattern, text):
# Text to add before the URL (needs to be escaped)
before_url = text[last_end : match.start()]
parts.append(re.sub(r"(?<!\\)_", r"\_", before_url))
Expand Down

0 comments on commit 0d5cd11

Please sign in to comment.