diff options
Diffstat (limited to 'nikola/plugins/command/init.py')
| -rw-r--r-- | nikola/plugins/command/init.py | 110 |
1 files changed, 63 insertions, 47 deletions
diff --git a/nikola/plugins/command/init.py b/nikola/plugins/command/init.py index 96caad8..d7eeed7 100644 --- a/nikola/plugins/command/init.py +++ b/nikola/plugins/command/init.py @@ -28,18 +28,71 @@ from __future__ import print_function import os import shutil import codecs +import json from mako.template import Template import nikola +from nikola.nikola import DEFAULT_TRANSLATIONS_PATTERN from nikola.plugin_categories import Command from nikola.utils import get_logger, makedirs, STDERR_HANDLER from nikola.winutils import fix_git_symlinked LOGGER = get_logger('init', STDERR_HANDLER) +SAMPLE_CONF = { + 'BLOG_AUTHOR': "Your Name", + 'BLOG_TITLE': "Demo Site", + 'SITE_URL': "http://getnikola.com/", + 'BLOG_EMAIL': "joe@demo.site", + 'BLOG_DESCRIPTION': "This is a demo site for Nikola.", + 'DEFAULT_LANG': "en", + 'THEME': 'bootstrap3', + 'COMMENT_SYSTEM': 'disqus', + 'COMMENT_SYSTEM_ID': 'nikolademo', + 'TRANSLATIONS_PATTERN': DEFAULT_TRANSLATIONS_PATTERN, + 'POSTS': """( +("posts/*.rst", "posts", "post.tmpl"), +("posts/*.txt", "posts", "post.tmpl"), +)""", + 'PAGES': """( +("stories/*.rst", "stories", "story.tmpl"), +("stories/*.txt", "stories", "story.tmpl"), +)""", + 'COMPILERS': """{ +"rest": ('.rst', '.txt'), +"markdown": ('.md', '.mdown', '.markdown'), +"textile": ('.textile',), +"txt2tags": ('.t2t',), +"bbcode": ('.bb',), +"wiki": ('.wiki',), +"ipynb": ('.ipynb',), +"html": ('.html', '.htm'), +# PHP files are rendered the usual way (i.e. with the full templates). +# The resulting files have .php extensions, making it possible to run +# them without reconfiguring your server to recognize them. +"php": ('.php',), +# Pandoc detects the input from the source filename +# but is disabled by default as it would conflict +# with many of the others. +# "pandoc": ('.rst', '.md', '.txt'), +}""", + 'REDIRECTIONS': [], +} + + +# In order to ensure proper escaping, all variables but the three +# pre-formatted ones are handled by json.dumps(). +def prepare_config(config): + """Parse sample config with JSON.""" + p = config.copy() + p.update(dict((k, json.dumps(v)) for k, v in p.items() + if k not in ('POSTS', 'PAGES', 'COMPILERS'))) + return p + class CommandInit(Command): + """Create a new site.""" name = "init" @@ -57,40 +110,6 @@ class CommandInit(Command): } ] - SAMPLE_CONF = { - 'BLOG_AUTHOR': "Your Name", - 'BLOG_TITLE': "Demo Site", - 'SITE_URL': "http://getnikola.com/", - 'BLOG_EMAIL': "joe@demo.site", - 'BLOG_DESCRIPTION': "This is a demo site for Nikola.", - 'DEFAULT_LANG': "en", - 'THEME': 'bootstrap3', - - 'POSTS': """( - ("posts/*.rst", "posts", "post.tmpl"), - ("posts/*.txt", "posts", "post.tmpl"), -)""", - 'PAGES': """( - ("stories/*.rst", "stories", "story.tmpl"), - ("stories/*.txt", "stories", "story.tmpl"), -)""", - 'COMPILERS': """{ - "rest": ('.rst', '.txt'), - "markdown": ('.md', '.mdown', '.markdown'), - "textile": ('.textile',), - "txt2tags": ('.t2t',), - "bbcode": ('.bb',), - "wiki": ('.wiki',), - "ipynb": ('.ipynb',), - "html": ('.html', '.htm'), - # Pandoc detects the input from the source filename - # but is disabled by default as it would conflict - # with many of the others. - # "pandoc": ('.rst', '.md', '.txt'), -}""", - 'REDIRECTIONS': '[]', - } - @classmethod def copy_sample_site(cls, target): lib_path = cls.get_path_to_nikola_modules() @@ -105,7 +124,7 @@ class CommandInit(Command): conf_template = Template(filename=template_path) conf_path = os.path.join(target, 'conf.py') with codecs.open(conf_path, 'w+', 'utf8') as fd: - fd.write(conf_template.render(**cls.SAMPLE_CONF)) + fd.write(conf_template.render(**prepare_config(SAMPLE_CONF))) @classmethod def create_empty_site(cls, target): @@ -122,16 +141,13 @@ class CommandInit(Command): print("Usage: nikola init folder [options]") return False target = args[0] - if target is None: - print(self.usage) + if not options or not options.get('demo'): + self.create_empty_site(target) + LOGGER.info('Created empty site at {0}.'.format(target)) else: - if not options or not options.get('demo'): - self.create_empty_site(target) - LOGGER.notice('Created empty site at {0}.'.format(target)) - else: - self.copy_sample_site(target) - LOGGER.notice("A new site with example data has been created at " - "{0}.".format(target)) - LOGGER.notice("See README.txt in that folder for more information.") - - self.create_configuration(target) + self.copy_sample_site(target) + LOGGER.info("A new site with example data has been created at " + "{0}.".format(target)) + LOGGER.info("See README.txt in that folder for more information.") + + self.create_configuration(target) |
