summaryrefslogtreecommitdiff
path: root/sphinx/quickstart.py
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2016-09-08 11:31:17 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2016-10-13 21:21:57 +0900
commit2981664178c17cfcbd46ee5af6f3329529490b60 (patch)
tree13570ae6122bee851ac686ce82170e4de1449bce /sphinx/quickstart.py
parentc3b6f8cc95211e76ac29d3109b86a3226a8434b9 (diff)
downloadsphinx-git-2981664178c17cfcbd46ee5af6f3329529490b60.tar.gz
sphinx-quickstart supports user templates (ref: #2912)
Diffstat (limited to 'sphinx/quickstart.py')
-rw-r--r--sphinx/quickstart.py32
1 files changed, 29 insertions, 3 deletions
diff --git a/sphinx/quickstart.py b/sphinx/quickstart.py
index f7e6ad38a..61abbb092 100644
--- a/sphinx/quickstart.py
+++ b/sphinx/quickstart.py
@@ -182,6 +182,19 @@ def convert_python_source(source, rex=re.compile(r"[uU]('.*?')")):
return source
+class QuickstartRenderer(SphinxRenderer):
+ def __init__(self, templatedir):
+ self.templatedir = templatedir or ''
+ super(QuickstartRenderer, self).__init__()
+
+ def render(self, template_name, context):
+ user_template = path.join(self.templatedir, path.basename(template_name))
+ if self.templatedir and path.exists(user_template):
+ return self.render_from_file(user_template, context)
+ else:
+ return super(QuickstartRenderer, self).render(template_name, context)
+
+
def ask_user(d):
"""Ask the user for quickstart values missing from *d*.
@@ -357,9 +370,9 @@ directly.''')
print()
-def generate(d, overwrite=True, silent=False):
+def generate(d, overwrite=True, silent=False, templatedir=None):
"""Generate project based on values in *d*."""
- template = SphinxRenderer()
+ template = QuickstartRenderer(templatedir=templatedir)
texescape.init()
indent = ' ' * 4
@@ -588,6 +601,12 @@ def main(argv=sys.argv):
default=True,
help='use make-mode for Makefile/make.bat')
+ group = parser.add_option_group('Project templating')
+ group.add_option('-t', '--templatedir', metavar='TEMPLATEDIR', dest='templatedir',
+ help='template directory for template files')
+ group.add_option('-d', metavar='NAME=VALUE', action='append', dest='variables',
+ help='define a template variable')
+
# parse options
try:
opts, args = parser.parse_args(argv[1:])
@@ -640,7 +659,14 @@ def main(argv=sys.argv):
if isinstance(value, binary_type):
d[key] = term_decode(value)
- generate(d)
+ for variable in d.get('variables', []):
+ try:
+ name, value = variable.split('=')
+ d[name] = value
+ except ValueError:
+ print('Invalid template variable: %s' % variable)
+
+ generate(d, templatedir=opts.templatedir)
if __name__ == '__main__':
sys.exit(main(sys.argv))