1111from tempfile import mkdtemp
1212from invoke import task , Context
1313
14+
15+ def _get_sphinx_paths (ctx : Context , source : str | None , target : str | None ) -> tuple [str , str ]:
16+ src = source or getattr (ctx .sphinx , "source" , "doc" )
17+ dst = target or getattr (ctx .sphinx , "target" , "doc/_build/html" )
18+ return src , dst
19+
1420logger = logging .getLogger (__name__ )
1521
1622# 在非 Windows 平台上使用 pty
@@ -35,7 +41,9 @@ def build(ctx: Context,
3541 language : str | None = None ,
3642 source : str | None = None ,
3743 target : str | None = None ,
38- nitpick : bool = False ) -> None :
44+ nitpick : bool = False ,
45+ jobs : str = "auto" ,
46+ keep_going : bool = True ) -> None :
3947 """构建项目的 Sphinx 文档。
4048
4149 Args:
@@ -46,17 +54,18 @@ def build(ctx: Context,
4654 source: 源目录,覆盖配置设置
4755 target: 输出目录,覆盖配置设置
4856 nitpick: 是否启用更严格的警告/错误检查"""
49- source = source or ctx .sphinx .source
50- target = target or ctx .sphinx .target
57+ source , target = _get_sphinx_paths (ctx , source , target )
5158
52- # 处理语言选项
5359 if language :
54- opts = f' -D language={ language } '
55- target = f' { target } /{ language } '
60+ opts = f" { opts } -D language={ language } " . strip ()
61+ target = f" { target } /{ language } "
5662
57- # 处理严格模式选项
5863 if nitpick :
59- opts += " -n -W -T"
64+ opts = f"{ opts } -n -W -T" .strip ()
65+
66+ opts = f"{ opts } -j { jobs } " .strip ()
67+ if keep_going :
68+ opts = f"{ opts } --keep-going" .strip ()
6069
6170 # 执行构建命令
6271 cmd = f"sphinx-build -b { builder } { opts } { source } { target } "
@@ -76,14 +85,12 @@ def intl(ctx: Context, language: str = 'en') -> None:
7685 target = Path (ctx .sphinx .target ).parent / 'gettext'
7786
7887 if language == 'en' :
79- # 英语是源语言,需要重新生成 POT 文件
80- clean ( ctx )
88+ if target . exists ():
89+ rmtree ( target )
8190 build (ctx , target = target , opts = opts )
8291 elif language :
83- # 确保 POT 文件已生成
84- if not Path (target ).exists ():
92+ if not target .exists ():
8593 build (ctx , target = target , opts = opts )
86- # 更新指定语言的翻译
8794 ctx .run (f'sphinx-intl update -p { target } -l { language } ' )
8895 # 以下代码已注释掉,因为当前项目可能不需要
8996 # for DIR in ['pages', 'posts', 'shop']:
@@ -109,13 +116,16 @@ def doctest(ctx: Context) -> None:
109116
110117@task
111118def tree (ctx : Context ) -> None :
112- """使用 'tree' 程序显示文档内容结构。
113-
114- Args:
115- ctx: Invoke 上下文对象,包含 sphinx 配置信息"""
116- # 定义需要忽略的文件和目录模式
117119 ignore = ".git|*.pyc|*.swp|dist|*.egg-info|_static|_build|_templates"
118- ctx .run (f'tree -Ca -I "{ ignore } " { ctx .sphinx .source } ' )
120+ try :
121+ ctx .run (f'tree -Ca -I "{ ignore } " { ctx .sphinx .source } ' , pty = PTY )
122+ except Exception :
123+ root = Path (ctx .sphinx .source )
124+ for p in sorted (root .rglob ("*" )):
125+ rel = p .relative_to (root )
126+ if any (part in ignore .split ("|" ) for part in rel .parts ):
127+ continue
128+ print (rel )
119129
120130
121131# 文档站点配置集合创建函数
@@ -132,8 +142,8 @@ def create_docs(project_configs: Union[List[Dict[str, str]], Dict[str, Dict[str,
132142 ...
133143
134144def create_docs (
135- source : Union [str , List [Dict [str , str ]], Dict [str , Dict [str , str ]], None ] = None ,
136- target : str = '.temp/ html' ,
145+ source : Union [str , List [Dict [str , str ]], Dict [str , Dict [str , str ]], None ] = None ,
146+ target : str = 'doc/_build/ html' ,
137147 children : str = ''
138148) -> Collection :
139149 """创建文档站点配置集合。
@@ -183,7 +193,7 @@ def create_docs(
183193 # 获取项目配置,提供默认值
184194 name = config .get ('name' , f'doc_{ i + 1 } ' )
185195 proj_source = config .get ('source' , 'doc' )
186- proj_target = config .get ('target' , f'.temp /html/{ name } ' )
196+ proj_target = config .get ('target' , f'doc/_build /html/{ name } ' )
187197 proj_children = config .get ('children' , '' )
188198
189199 # 创建项目特定的子命令集合
@@ -209,7 +219,7 @@ def create_docs(
209219 for name , config in project_configs .items ():
210220 # 获取项目配置
211221 proj_source = config .get ('source' , 'doc' )
212- proj_target = config .get ('target' , f'.temp /html/{ name } ' )
222+ proj_target = config .get ('target' , f'doc/_build /html/{ name } ' )
213223 proj_children = config .get ('children' , '' )
214224
215225 # 创建项目特定的子命令集合
@@ -234,7 +244,7 @@ def create_docs(
234244 return main_namespace
235245
236246
237- def sites (source : str = 'doc' , target : str = '.temp /html' , children : str = '' ) -> Collection :
247+ def sites (source : str = 'doc' , target : str = 'doc/_build /html' , children : str = '' ) -> Collection :
238248 """创建文档站点配置集合。
239249
240250 为不同的文档站点创建配置好的 Invoke 集合,用于构建 Sphinx 文档。
0 commit comments