49 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			49 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Python
		
	
	
	
| import subprocess
 | |
| import os
 | |
| import shutil
 | |
| 
 | |
| def generate_ebook(source_dir="documentation/docs", build_dir="documentation/docs/_build", output_format="epub"):
 | |
|     """
 | |
|     使用 Sphinx 生成电子书 (EPUB 或 HTML)。
 | |
|     """
 | |
|     print(f"开始生成 {output_format} 格式的电子书...")
 | |
| 
 | |
|     # 确保构建目录存在
 | |
|     if not os.path.exists(build_dir):
 | |
|         os.makedirs(build_dir)
 | |
| 
 | |
|     # 清理之前的构建
 | |
|     if os.path.exists(build_dir):
 | |
|         shutil.rmtree(build_dir)
 | |
|         print(f"清理目录: {build_dir}")
 | |
|     else:
 | |
|         print(f"构建目录不存在,无需清理: {build_dir}")
 | |
| 
 | |
|     # 构建文档
 | |
|     build_command = [
 | |
|         f"./documentation/analysis/phallic-worship-analysis/venv/bin/sphinx-build",
 | |
|         "-b", output_format,
 | |
|         source_dir,
 | |
|         os.path.join(build_dir, output_format)
 | |
|     ]
 | |
|     print(f"执行构建命令: {' '.join(build_command)}")
 | |
|     try:
 | |
|         subprocess.run(build_command, check=True, cwd='.')
 | |
|         print(f"{output_format} 电子书生成成功!")
 | |
|         return True
 | |
|     except subprocess.CalledProcessError as e:
 | |
|         print(f"生成 {output_format} 电子书失败: {e}")
 | |
|         return False
 | |
| 
 | |
| if __name__ == "__main__":
 | |
|     # 示例用法:生成 EPUB
 | |
|     if generate_ebook(output_format="epub"):
 | |
|         print("EPUB 电子书已生成。")
 | |
|     else:
 | |
|         print("EPUB 电子书生成失败。")
 | |
| 
 | |
|     # 示例用法:生成 HTML
 | |
|     if generate_ebook(output_format="html"):
 | |
|         print("HTML 网站已生成。")
 | |
|     else:
 | |
|         print("HTML 网站生成失败。") |