import os import re from pathlib import Path import os def merge_style_with_md_files(md_file_path, style_file_path): # 检查样式文件是否存在 if not os.path.isfile(style_file_path): raise FileNotFoundError(f"样式文件 {style_file_path} 不存在。") # 读取样式文件内容 with open(style_file_path, 'r', encoding='utf-8') as f: style_content = f.read() # 遍历指定目录下的所有文件 for filename in os.listdir(md_file_path): if filename.startswith('section') and filename.endswith('.md'): file_path = os.path.join(md_file_path, filename) # 合并样式内容与 .md 文件内容 if os.path.exists(file_path): with open(file_path, 'r+', encoding='utf-8') as f: original_content = f.read() # 将指针移动到文件开头以覆盖原有内容 f.seek(0) f.write(style_content + '\n\n' + original_content) # 清除输出缓冲区以确保所有数据都已写入文件 f.truncate() def remove_trailing_dashes(directory): """ 从 Markdown 文件中移除位于文件末尾且后面没有其他内容(除了可能的换行符)的连续破折号(---)。 """ for filename in os.listdir(directory): # 检查文件是否以 section 开头且为 .md 文件 if filename.startswith('section') and filename.endswith('.md'): filepath = os.path.join(directory, filename) # 读取文件内容 with open(filepath, 'r', encoding='utf-8') as file: content = file.read() # 检查文件末尾是否有连续的破折号(---),并且之后没有其他内容(除了可能的换行符) if content.rstrip().endswith('---') and content.rstrip('---').endswith('\n'): # 移除末尾的连续破折号(---)及其后面的换行符 content = content.rstrip('---\n') # 替换文件中的所有 "------" 为空字符串 content = content.replace("------", "") content = re.sub(r'\n{3,}', '\n\n', content) # 写入更新后的内容 with open(filepath, 'w', encoding='utf-8') as file: file.write(content) def remove_empty_lines(filename): # 读取文件内容 with open(filename, 'r', encoding='utf-8') as file: content = file.read() # 替换文件中的所有 "------" 为空字符串 content = content.replace("------", "") with open(filename, 'w', encoding='utf-8') as file: file.write(content) def append_string_to_file(file_path): # 读取文件内容 with open(file_path, 'r', encoding='utf-8') as file: content = file.read() # 在文件末尾追加字符串 '---' new_content = content + '---' # 写入新内容 with open(file_path, 'w', encoding='utf-8') as file: file.write(new_content) def insert_logo(file_path, logo_path): # 读取文件内容 with open(file_path, 'r', encoding='utf-8') as file: content = file.read() # 获取 Logo 文件名 logo_filename = Path(logo_path).name # 定义要插入的字符串 insert_str = f""" """ # 使用正则表达式替换 # 只替换独立出现的 "---",不包括被其他破折号包围的情况 new_content = re.sub(r'(?