From 46b7da2a77676e7e31ad8b4fb61963b98f1e459a Mon Sep 17 00:00:00 2001 From: zhukang <274546966@qq.com> Date: Wed, 15 Jan 2025 21:19:42 +0800 Subject: [PATCH] feat: improve text format handling and update documentation - Optimize HTML to Markdown conversion - Add smart line break handling - Preserve formatting while cleaning extra spaces - Update README with detailed format handling features - Add text processing highlights section --- project/llmclipboard/README.md | 29 ++++++++++++++++++++++++ project/llmclipboard/llmclipboard/app.py | 18 +++++++++++---- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/project/llmclipboard/README.md b/project/llmclipboard/README.md index e59f33a..6a7dd6f 100644 --- a/project/llmclipboard/README.md +++ b/project/llmclipboard/README.md @@ -12,6 +12,35 @@ - 🌐 完整的跨平台支持 - 🔄 系统托盘支持,后台运行 - 🎨 自适应系统主题 +- 📝 优化的文本格式处理 + +## 🎯 功能亮点 + +### 智能格式转换 + +- **HTML 富文本** + - 保持原始格式(粗体、斜体、链接等) + - 保留图片和表格结构 + - 智能处理段落和换行 + - 清理多余空白行 + +- **纯文本处理** + - 自动识别文本编码 + - 智能处理中文内容 + - 保持段落结构 + - 优化换行格式 + +- **Unicode 支持** + - 完整的 Unicode 字符支持 + - 多语言文本兼容 + - 保持特殊字符格式 + +### 格式优化 + +- 自动清理冗余空行 +- 保持段落间距一致 +- 优化列表和缩进结构 +- 保持代码块格式 ## 🚀 快速开始 diff --git a/project/llmclipboard/llmclipboard/app.py b/project/llmclipboard/llmclipboard/app.py index ca79149..46e0fd9 100644 --- a/project/llmclipboard/llmclipboard/app.py +++ b/project/llmclipboard/llmclipboard/app.py @@ -4,7 +4,7 @@ import configparser from pynput import mouse import win32clipboard import win32con -from html2text import html2text +from html2text import HTML2Text import keyboard import threading import sys @@ -56,21 +56,31 @@ class TextCaptureService: try: content = win32clipboard.GetClipboardData(win32con.CF_HTML) self.logger.info("Clipboard HTML content captured") - return html2text(content) + h = HTML2Text() + h.body_width = 0 # 禁用自动换行 + h.single_line_break = True # 使用单行换行 + h.ignore_emphasis = False + h.ignore_images = False + h.ignore_links = False + h.ignore_tables = False + return h.handle(content).strip() except: pass try: content = win32clipboard.GetClipboardData(win32con.CF_TEXT) self.logger.info("Clipboard TEXT content captured") - return content.decode('gbk') + text = content.decode('gbk') + # 处理纯文本的换行 + return '\n'.join(line.strip() for line in text.splitlines() if line.strip()) except: pass try: content = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT) self.logger.info("Clipboard UNICODE TEXT content captured") - return content + # 处理Unicode文本的换行 + return '\n'.join(line.strip() for line in content.splitlines() if line.strip()) except: return None