From c8ed9a0ade0df86937475499ed65c0830007a3a6 Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Fri, 15 Aug 2025 18:44:01 +0700 Subject: [PATCH] Fix sage --docbuild -D --- src/sage_docbuild/__main__.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/sage_docbuild/__main__.py b/src/sage_docbuild/__main__.py index 6459596a28b..9b2dc01de15 100644 --- a/src/sage_docbuild/__main__.py +++ b/src/sage_docbuild/__main__.py @@ -77,6 +77,7 @@ import os import sys from pathlib import Path +from functools import partial import sphinx.ext.intersphinx @@ -156,20 +157,20 @@ def help_examples(s=""): return s -def help_documents(): +def help_documents(parser, args): """ Append and return a tabular list of documents, including a shortcut 'all' for all documents, available to the Sage documentation builder. """ - docs = get_documents() + args.source_dir = preprocess_source_dir(parser, args.source_dir) + docs = [str(p) for p in get_all_documents(args.source_dir)] s = "DOCUMENTs:\n" s += format_columns(docs) s += "\n" - if 'reference' in docs: - s += "Other valid document names take the form 'reference/DIR', where\n" - s += "DIR is a subdirectory of src/doc/en/reference/.\n" - s += "This builds just the specified part of the reference manual.\n" + s += "Other valid document names take the form 'reference/DIR', where\n" + s += "DIR is a subdirectory of src/doc/en/reference/.\n" + s += "This builds just the specified part of the reference manual.\n" s += "DOCUMENT may also have the form 'file=/path/to/FILE', which builds\n" s += "the documentation for the specified file.\n" return s @@ -220,7 +221,7 @@ class help_message_long(argparse.Action): and exits. """ def __call__(self, parser, namespace, values, option_string=None): - help_funcs = [help_usage, help_description, help_documents, + help_funcs = [help_usage, help_description, functools.partial(help_documents, parser, namespace), help_formats, help_commands] for f in help_funcs: print(f()) @@ -253,7 +254,7 @@ class help_wrapper(argparse.Action): """ def __call__(self, parser, namespace, values, option_string=None): if option_string in ['-D', '--documents']: - print(help_documents(), end="") + print(help_documents(parser, namespace), end="") if option_string in ['-F', '--formats']: print(help_formats(), end="") if self.dest == 'commands': @@ -262,6 +263,15 @@ def __call__(self, parser, namespace, values, option_string=None): sys.exit(0) +def preprocess_source_dir(parser, val): + if val is None: + val = Path(os.environ.get('SAGE_DOC_SRC', 'src/doc')) + p = val.absolute() + if not p.is_dir(): + parser.error(f"Source directory {p} does not exist.") + return p + + def setup_parser(): """ Set up and return a command-line ArgumentParser instance for the @@ -452,14 +462,8 @@ def main(): # Parse the command-line. parser = setup_parser() args: BuildOptions = parser.parse_args() # type: ignore + args.source_dir = preprocess_source_dir(parser, args.source_dir) - # Check that the docs source directory exists - if args.source_dir is None: - args.source_dir = Path(os.environ.get('SAGE_DOC_SRC', 'src/doc')) - args.source_dir = args.source_dir.absolute() - if not args.source_dir.is_dir(): - parser.error(f"Source directory {args.source_dir} does not exist.") - if args.all_documents: if args.all_documents == 'reference': docs = get_all_reference_documents(args.source_dir / 'en')