Skip to content

Commit

Permalink
Merge pull request #6233 from tk0miya/refactor_quickstart
Browse files Browse the repository at this point in the history
refactor quickstart
  • Loading branch information
tk0miya committed Apr 4, 2019
2 parents 44c49f4 + 53fb247 commit 86d4823
Showing 1 changed file with 76 additions and 80 deletions.
156 changes: 76 additions & 80 deletions sphinx/cmd/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@
('imgmath', __('include math, rendered as PNG or SVG images')),
('mathjax', __('include math, rendered in the browser by MathJax')),
('ifconfig', __('conditional inclusion of content based on config values')),
('viewcode',
__('include links to the source code of documented Python objects')),
('githubpages',
__('create .nojekyll file to publish the document on GitHub pages')),
('viewcode', __('include links to the source code of documented Python objects')),
('githubpages', __('create .nojekyll file to publish the document on GitHub pages')),
])

DEFAULTS = {
Expand Down Expand Up @@ -140,8 +138,7 @@ def boolean(x):
def suffix(x):
# type: (str) -> str
if not (x[0:1] == '.' and len(x) > 1):
raise ValidationError(__("Please enter a file suffix, "
"e.g. '.rst' or '.txt'."))
raise ValidationError(__("Please enter a file suffix, e.g. '.rst' or '.txt'."))
return x


Expand Down Expand Up @@ -246,16 +243,16 @@ def ask_user(d):
"""

print(bold(__('Welcome to the Sphinx %s quickstart utility.')) % __display_version__)
print(__('''
Please enter values for the following settings (just press Enter to
accept a default value, if one is given in brackets).'''))
print()
print(__('Please enter values for the following settings (just press Enter to\n'
'accept a default value, if one is given in brackets).'))

if 'path' in d:
print(bold(__('''
Selected root path: %s''') % d['path']))
print()
print(bold(__('Selected root path: %s')) % d['path'])
else:
print(__('''
Enter the root path for documentation.'''))
print()
print(__('Enter the root path for documentation.'))
d['path'] = do_prompt(__('Root path for the documentation'), '.', is_path)

while path.isfile(path.join(d['path'], 'conf.py')) or \
Expand All @@ -265,70 +262,68 @@ def ask_user(d):
'selected root path.')))
print(__('sphinx-quickstart will not overwrite existing Sphinx projects.'))
print()
d['path'] = do_prompt(__('Please enter a new root path (or just Enter '
'to exit)'), '', is_path)
d['path'] = do_prompt(__('Please enter a new root path (or just Enter to exit)'),
'', is_path)
if not d['path']:
sys.exit(1)

if 'sep' not in d:
print(__('''
You have two options for placing the build directory for Sphinx output.
Either, you use a directory "_build" within the root path, or you separate
"source" and "build" directories within the root path.'''))
d['sep'] = do_prompt(__('Separate source and build directories (y/n)'),
'n', boolean)
print()
print(__('You have two options for placing the build directory for Sphinx output.\n'
'Either, you use a directory "_build" within the root path, or you separate\n'
'"source" and "build" directories within the root path.'))
d['sep'] = do_prompt(__('Separate source and build directories (y/n)'), 'n', boolean)

if 'dot' not in d:
print(__('''
Inside the root directory, two more directories will be created; "_templates"
for custom HTML templates and "_static" for custom stylesheets and other static
files. You can enter another prefix (such as ".") to replace the underscore.'''))
print()
print(__('Inside the root directory, two more directories will be created; "_templates"\n' # NOQA
'for custom HTML templates and "_static" for custom stylesheets and other static\n' # NOQA
'files. You can enter another prefix (such as ".") to replace the underscore.')) # NOQA
d['dot'] = do_prompt(__('Name prefix for templates and static dir'), '_', ok)

if 'project' not in d:
print(__('''
The project name will occur in several places in the built documentation.'''))
print()
print(__('The project name will occur in several places in the built documentation.'))
d['project'] = do_prompt(__('Project name'))
if 'author' not in d:
d['author'] = do_prompt(__('Author name(s)'))

if 'version' not in d:
print(__('''
Sphinx has the notion of a "version" and a "release" for the
software. Each version can have multiple releases. For example, for
Python the version is something like 2.5 or 3.0, while the release is
something like 2.5.1 or 3.0a1. If you don't need this dual structure,
just set both to the same value.'''))
print()
print(__('Sphinx has the notion of a "version" and a "release" for the\n'
'software. Each version can have multiple releases. For example, for\n'
'Python the version is something like 2.5 or 3.0, while the release is\n'
'something like 2.5.1 or 3.0a1. If you don\'t need this dual structure,\n'
'just set both to the same value.'))
d['version'] = do_prompt(__('Project version'), '', allow_empty)
if 'release' not in d:
d['release'] = do_prompt(__('Project release'), d['version'], allow_empty)

if 'language' not in d:
print(__('''
If the documents are to be written in a language other than English,
you can select a language here by its language code. Sphinx will then
translate text that it generates into that language.
For a list of supported codes, see
http://sphinx-doc.org/config.html#confval-language.'''))
print()
print(__('If the documents are to be written in a language other than English,\n'
'you can select a language here by its language code. Sphinx will then\n'
'translate text that it generates into that language.\n'
'\n'
'For a list of supported codes, see\n'
'http://sphinx-doc.org/config.html#confval-language.'))
d['language'] = do_prompt(__('Project language'), 'en')
if d['language'] == 'en':
d['language'] = None

if 'suffix' not in d:
print(__('''
The file name suffix for source files. Commonly, this is either ".txt"
or ".rst". Only files with this suffix are considered documents.'''))
print()
print(__('The file name suffix for source files. Commonly, this is either ".txt"\n'
'or ".rst". Only files with this suffix are considered documents.'))
d['suffix'] = do_prompt(__('Source file suffix'), '.rst', suffix)

if 'master' not in d:
print(__('''
One document is special in that it is considered the top node of the
"contents tree", that is, it is the root of the hierarchical structure
of the documents. Normally, this is "index", but if your "index"
document is a custom template, you can also set this to another filename.'''))
d['master'] = do_prompt(__('Name of your master document (without suffix)'),
'index')
print()
print(__('One document is special in that it is considered the top node of the\n'
'"contents tree", that is, it is the root of the hierarchical structure\n'
'of the documents. Normally, this is "index", but if your "index"\n'
'document is a custom template, you can also set this to another filename.'))
d['master'] = do_prompt(__('Name of your master document (without suffix)'), 'index')

while path.isfile(path.join(d['path'], d['master'] + d['suffix'])) or \
path.isfile(path.join(d['path'], 'source', d['master'] + d['suffix'])):
Expand All @@ -341,29 +336,27 @@ def ask_user(d):
'existing file and press Enter'), d['master'])

if 'extensions' not in d:
print(__('Indicate which of the following Sphinx extensions should be '
'enabled:'))
print(__('Indicate which of the following Sphinx extensions should be enabled:'))
d['extensions'] = []
for name, description in EXTENSIONS.items():
if do_prompt('%s: %s (y/n)' % (name, description), 'n', boolean):
d['extensions'].append('sphinx.ext.%s' % name)

# Handle conflicting options
if {'sphinx.ext.imgmath', 'sphinx.ext.mathjax'}.issubset(d['extensions']):
print(__('Note: imgmath and mathjax cannot be enabled at the same '
'time. imgmath has been deselected.'))
print(__('Note: imgmath and mathjax cannot be enabled at the same time. '
'imgmath has been deselected.'))
d['extensions'].remove('sphinx.ext.imgmath')

if 'makefile' not in d:
print(__('''
A Makefile and a Windows command file can be generated for you so that you
only have to run e.g. `make html' instead of invoking sphinx-build
directly.'''))
print()
print(__('A Makefile and a Windows command file can be generated for you so that you\n'
'only have to run e.g. `make html\' instead of invoking sphinx-build\n'
'directly.'))
d['makefile'] = do_prompt(__('Create Makefile? (y/n)'), 'y', boolean)

if 'batchfile' not in d:
d['batchfile'] = do_prompt(__('Create Windows command file? (y/n)'),
'y', boolean)
d['batchfile'] = do_prompt(__('Create Windows command file? (y/n)'), 'y', boolean)
print()


Expand Down Expand Up @@ -447,17 +440,18 @@ def write_file(fpath, content, newline=None):
return
print()
print(bold(__('Finished: An initial directory structure has been created.')))
print(__('''
You should now populate your master file %s and create other documentation
source files. ''') % masterfile + ((d['makefile'] or d['batchfile']) and __('''\
Use the Makefile to build the docs, like so:
make builder
''') or __('''\
Use the sphinx-build command to build the docs, like so:
sphinx-build -b builder %s %s
''') % (srcdir, builddir)) + __('''\
where "builder" is one of the supported builders, e.g. html, latex or linkcheck.
'''))
print()
print(__('You should now populate your master file %s and create other documentation\n'
'source files. ') % masterfile, end='')
if d['makefile'] or d['batchfile']:
print(__('Use the Makefile to build the docs, like so:\n'
' make builder'))
else:
print(__('Use the sphinx-build command to build the docs, like so:\n'
' sphinx-build -b builder %s %s') % (srcdir, builddir))
print(__('where "builder" is one of the supported builders, '
'e.g. html, latex or linkcheck.'))
print()


def valid_dir(d):
Expand Down Expand Up @@ -492,16 +486,18 @@ def valid_dir(d):

def get_parser():
# type: () -> argparse.ArgumentParser
description = __(
"\n"
"Generate required files for a Sphinx project.\n"
"\n"
"sphinx-quickstart is an interactive tool that asks some questions about your\n"
"project and then generates a complete documentation directory and sample\n"
"Makefile to be used with sphinx-build.\n"
)
parser = argparse.ArgumentParser(
usage='%(prog)s [OPTIONS] <PROJECT_DIR>',
epilog=__("For more information, visit <http://sphinx-doc.org/>."),
description=__("""
Generate required files for a Sphinx project.
sphinx-quickstart is an interactive tool that asks some questions about your
project and then generates a complete documentation directory and sample
Makefile to be used with sphinx-build.
"""))
description=description)

parser.add_argument('-q', '--quiet', action='store_true', dest='quiet',
default=None,
Expand Down Expand Up @@ -601,8 +597,8 @@ def main(argv=sys.argv[1:]):
try:
if 'quiet' in d:
if not {'project', 'author'}.issubset(d):
print(__('''"quiet" is specified, but any of "project" or \
"author" is not specified.'''))
print(__('"quiet" is specified, but any of "project" or '
'"author" is not specified.'))
return 1

if {'quiet', 'project', 'author'}.issubset(d):
Expand Down

0 comments on commit 86d4823

Please sign in to comment.