代码格式化

This commit is contained in:
xunbu
2025-05-10 10:32:39 +08:00
parent 2b9715bc74
commit 64bb9a966a
4 changed files with 20 additions and 21 deletions

9
.idea/workspace.xml generated
View File

@@ -5,13 +5,8 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="6b18b44a-df57-4212-a857-9e291ebe5dd2" name="更改" comment=""> <list default="true" id="6b18b44a-df57-4212-a857-9e291ebe5dd2" name="更改" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/docutranslate/Agents/__init__.py" beforeDir="false" afterPath="$PROJECT_DIR$/docutranslate/Agents/__init__.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/docutranslate/Agents/agent.py" beforeDir="false" afterPath="$PROJECT_DIR$/docutranslate/Agents/agent.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/docutranslate/translater.py" beforeDir="false" afterPath="$PROJECT_DIR$/docutranslate/translater.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/docutranslate/translater.py" beforeDir="false" afterPath="$PROJECT_DIR$/docutranslate/translater.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/docutranslate/utils/markdown_splitter.py" beforeDir="false" afterPath="$PROJECT_DIR$/docutranslate/utils/markdown_splitter.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pyproject.toml" beforeDir="false" afterPath="$PROJECT_DIR$/pyproject.toml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/uv.lock" beforeDir="false" afterPath="$PROJECT_DIR$/uv.lock" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -402,7 +397,7 @@
<workItem from="1746788668813" duration="25000" /> <workItem from="1746788668813" duration="25000" />
<workItem from="1746791230782" duration="3129000" /> <workItem from="1746791230782" duration="3129000" />
<workItem from="1746799824552" duration="317000" /> <workItem from="1746799824552" duration="317000" />
<workItem from="1746801217905" duration="14612000" /> <workItem from="1746801217905" duration="14698000" />
</task> </task>
<servers /> <servers />
</component> </component>

View File

@@ -1,2 +1,2 @@
from .agent import Agent,AgentArgs from .agent import Agent, AgentArgs
from .markdown_agent import MDRefineAgent,MDTranslateAgent from .markdown_agent import MDRefineAgent, MDTranslateAgent

View File

@@ -2,15 +2,17 @@ from pathlib import Path
from typing import Literal from typing import Literal
import markdown2 import markdown2
# import mdformat
from docutranslate.Agents import Agent, AgentArgs
from docutranslate.Agents import MDRefineAgent, MDTranslateAgent from docutranslate.Agents import MDRefineAgent, MDTranslateAgent
from docutranslate.Agents.agent import Agent, AgentArgs
from docutranslate.utils.convert import file2markdown_embed_images from docutranslate.utils.convert import file2markdown_embed_images
from docutranslate.utils.markdown_splitter import split_markdown_text from docutranslate.utils.markdown_splitter import split_markdown_text
from docutranslate.utils.markdown_utils import uris2placeholder, placeholder2_uris, MaskDict from docutranslate.utils.markdown_utils import uris2placeholder, placeholder2_uris, MaskDict
# import mdformat
class FileTranslater: class FileTranslater:
def __init__(self, file_path: Path | str | None = None, chunksize: int = 4096, base_url="", key=None, def __init__(self, file_path: Path | str | None = None, chunksize: int = 4096, base_url="", key=None,
model_id="", temperature=0.7, max_concurrent=20, docling_artifact: Path | str | None = None, model_id="", temperature=0.7, max_concurrent=20, docling_artifact: Path | str | None = None,
@@ -69,7 +71,8 @@ class FileTranslater:
return result return result
def read_file(self, file_path: Path | str | None = None, formula=False, code=False, save=False, def read_file(self, file_path: Path | str | None = None, formula=False, code=False, save=False,
save_format: Literal["markdown", "html"] = "markdown", refine=False, refine_agent:Agent|None=None): save_format: Literal["markdown", "html"] = "markdown", refine=False,
refine_agent: Agent | None = None):
if file_path is None: if file_path is None:
if self.file_path is None: if self.file_path is None:
raise Exception("未设置文件路径") raise Exception("未设置文件路径")
@@ -104,7 +107,7 @@ class FileTranslater:
print("markdown已修正") print("markdown已修正")
return self.markdown return self.markdown
def translate_markdown_by_agent(self, translate_agent: Agent|None=None): def translate_markdown_by_agent(self, translate_agent: Agent | None = None):
print("正在翻译markdown") print("正在翻译markdown")
chuncks = self._split_markdown_into_chunks() chuncks = self._split_markdown_into_chunks()

View File

@@ -3,7 +3,6 @@ import threading
import uuid import uuid
class MaskDict: class MaskDict:
def __init__(self): def __init__(self):
self._dict = {} self._dict = {}
@@ -33,8 +32,9 @@ class MaskDict:
with self._lock: with self._lock:
return item in self._dict return item in self._dict
# def uris2placeholder(markdown:str, mask_dict:MaskDict): # def uris2placeholder(markdown:str, mask_dict:MaskDict):
##替换整个uri ##替换整个uri
# def uri2placeholder(match: re.Match): # def uri2placeholder(match: re.Match):
# id = mask_dict.create_id() # id = mask_dict.create_id()
# mask_dict.set(id, match.group()) # mask_dict.set(id, match.group())
@@ -44,7 +44,7 @@ class MaskDict:
# markdown = re.sub(uri_pattern, uri2placeholder, markdown) # markdown = re.sub(uri_pattern, uri2placeholder, markdown)
# return markdown # return markdown
def uris2placeholder(markdown:str, mask_dict:MaskDict): def uris2placeholder(markdown: str, mask_dict: MaskDict):
##只替换uri里的链接部分保留标题 ##只替换uri里的链接部分保留标题
def uri2placeholder(match: re.Match): def uri2placeholder(match: re.Match):
id = mask_dict.create_id() id = mask_dict.create_id()
@@ -54,10 +54,12 @@ def uris2placeholder(markdown:str, mask_dict:MaskDict):
uri_pattern = r'(!?\[.*?\])\((.*?)\)' uri_pattern = r'(!?\[.*?\])\((.*?)\)'
markdown = re.sub(uri_pattern, uri2placeholder, markdown) markdown = re.sub(uri_pattern, uri2placeholder, markdown)
return markdown return markdown
def placeholder2_uris(markdown:str, mask_dict:MaskDict):
def placeholder2uri(match:re.Match):
id=match.group(1) def placeholder2_uris(markdown: str, mask_dict: MaskDict):
uri=mask_dict.get(id) def placeholder2uri(match: re.Match):
id = match.group(1)
uri = mask_dict.get(id)
if uri is None: if uri is None:
return match.group() return match.group()
return uri return uri
@@ -67,6 +69,5 @@ def placeholder2_uris(markdown:str, mask_dict:MaskDict):
return markdown return markdown
if __name__ == '__main__': if __name__ == '__main__':
pass pass