Skip to content

Commit

Permalink
Merge pull request #91 from ldelossa/ldelossa/convert-latex
Browse files Browse the repository at this point in the history
feat: convert latex to plain text
  • Loading branch information
npiv authored Jul 30, 2024
2 parents b1985ca + 3227590 commit 225b622
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions chatblade/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from rich.rule import Rule

from chatblade import utils
from pylatexenc.latex2text import LatexNodes2Text


console = Console()
Expand Down Expand Up @@ -85,8 +86,20 @@ def extract_messages(messages, args):
else:
print(message.content.strip())

def format_latex(msg):
converter = LatexNodes2Text()
msg = converter.latex_to_text(msg)

# do no change code blocks to smart quotes, this will break the markdown
# parser.
msg = msg.replace("“", "``")

return msg

def detect_and_format_message(msg, cutoff=None, theme=None):
# convert any latex markup to ASCII.
msg = format_latex(msg)

if cutoff and len(msg) > cutoff:
msg = "... **text shortened** ... " + msg[-cutoff:]
return msg
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ platformdirs==4.2.2
pydantic==2.8.2
pydantic_core==2.20.1
Pygments==2.18.0
pylatexenc==2.10
PyYAML==6.0.1
regex==2024.5.15
requests==2.32.3
Expand Down

0 comments on commit 225b622

Please sign in to comment.