允许保存不内嵌图片的markdown(文件夹形式)
This commit is contained in:
@@ -6,6 +6,8 @@ import re
|
||||
import threading
|
||||
import uuid
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
|
||||
|
||||
class MaskDict:
|
||||
@@ -161,6 +163,35 @@ def embed_inline_image_from_zip(zip_bytes: bytes, filename_in_zip: str, encoding
|
||||
return modified_md_content
|
||||
|
||||
|
||||
def unembed_base64_images_to_zip(markdown:str,folder_name:str,markdown_name:str,image_folder_name="images")->io.BytesIO:
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
subfolder = os.path.join(temp_dir, folder_name)#所有的操作都在这个subfolder里进行
|
||||
os.makedirs(subfolder, exist_ok=True)
|
||||
image_folder=os.path.join(subfolder,image_folder_name)
|
||||
os.makedirs(image_folder,exist_ok=True)
|
||||
pattern=r"!\[(.*?)\]\(data:(.*?);.*base64,(.*)\)"
|
||||
def unembed_base64_images(match:re.Match)->str:
|
||||
b64data = match.group(3)
|
||||
extension=mimetypes.guess_extension(match.group(2))
|
||||
image_id=uuid.uuid1().hex[:8]
|
||||
image_name=f"{image_id}{extension}"
|
||||
url=f"./{image_folder_name}/{image_name}"
|
||||
#创建对应的image文件
|
||||
with open(os.path.join(image_folder,image_name),"wb") as f:
|
||||
f.write(base64.b64decode(b64data))
|
||||
return f""
|
||||
modified_md_content = re.sub(pattern, unembed_base64_images,markdown)
|
||||
with open(os.path.join(subfolder,f"{markdown_name}"),"w") as f:
|
||||
f.write(modified_md_content)
|
||||
zip_buffer=io.BytesIO()
|
||||
folder_path=Path(subfolder)
|
||||
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
||||
for file in folder_path.rglob('*'):
|
||||
if file.is_file():
|
||||
zipf.write(file, file.relative_to(folder_path.parent))
|
||||
|
||||
return zip_buffer
|
||||
|
||||
def clean_markdown_math_block(markdown):
|
||||
"""清除公式块的多余空格字符"""
|
||||
|
||||
@@ -173,9 +204,7 @@ def clean_markdown_math_block(markdown):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
markdown = r"""
|
||||
$$
|
||||
R T _ { k } ^ { i } ( t ) = \frac { \sum _ { t ^ { \prime } \in [ t - W , t ] } R R _ { k } ^ { i } ( t ^ { \prime } ) \times D R _ { k } ^ { i } ( t ^ { \prime } ) } { \sum _ { t ^ { \prime } \in [ t - W , t ] } D R _ { k } ^ { i } ( t ^ { \prime } ) }
|
||||
$$
|
||||
"""
|
||||
print(clean_markdown_math_block(markdown))
|
||||
with open(r"C:\Users\jxgm\Desktop\translate\docutranslate\tests\files\test7.md",'r') as f:
|
||||
markdown=f.read()
|
||||
print(unembed_base64_images_to_zip(markdown))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user