Skip to content

Commit

Permalink
Fix #2513 — date-based paths in new_post
Browse files Browse the repository at this point in the history
Available as:

nikola new_post -d
nikola new_post --date-path
NEW_POST_DATE_PATH in conf.py
  • Loading branch information
Kwpolska committed Jan 7, 2017
1 parent 5838a4d commit 781826e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.txt
Expand Up @@ -22,6 +22,9 @@ Bugfixes
Features
--------

* Add ``nikola new_post -d`` and ``NEW_POST_DATE_PATH`` to allow
automatic creation of year/month/day (date-based) directory
structures (Issue #2513)
* Allow enabling pretty URLs with per-post setting (Issue #2613)
* Add a ``sort_posts`` function (available as Jinja filter in global
context), which allows general-purpose timeline sorting (Issue
Expand Down
8 changes: 8 additions & 0 deletions nikola/conf.py.in
Expand Up @@ -206,6 +206,14 @@ COMPILERS = ${COMPILERS}
# Set to False for two-file posts, with separate metadata.
# ONE_FILE_POSTS = True

# Use date-based path when creating posts?
# Can be enabled on a per-post basis with `nikola new_post -d`.
# NEW_POST_DATE_PATH = False

# What format to use when creating posts with date paths?
# Default is '%Y/%m/%d', other possibilities include '%Y' or '%Y/%m'.
# NEW_POST_DATE_PATH_FORMAT = '%Y/%m/%d'

# If this is set to True, the DEFAULT_LANG version will be displayed for
# untranslated posts.
# If this is set to False, then posts that are not translated to a language
Expand Down
2 changes: 2 additions & 0 deletions nikola/nikola.py
Expand Up @@ -534,6 +534,8 @@ def __init__(self, **config):
'MARKDOWN_EXTENSIONS': ['fenced_code', 'codehilite'], # FIXME: Add 'extras' in v8
'MAX_IMAGE_SIZE': 1280,
'MATHJAX_CONFIG': '',
'NEW_POST_DATE_PATH': False,
'NEW_POST_DATE_PATH_FORMAT': '%Y/%m/%d',
'OLD_THEME_SUPPORT': True,
'OUTPUT_FOLDER': 'output',
'POSTS': (("posts/*.txt", "posts", "post.tmpl"),),
Expand Down
16 changes: 15 additions & 1 deletion nikola/plugins/command/new_post.py
Expand Up @@ -204,7 +204,14 @@ class CommandNewPost(Command):
'default': '',
'help': 'Import an existing file instead of creating a placeholder'
},

{
'name': 'date-path',
'short': 'd',
'long': 'date-path',
'type': bool,
'default': False,
'help': 'Create post with date path (eg. year/month/day, see NEW_POST_DATE_PATH_FORMAT in config)'
},
]

def _execute(self, options, args):
Expand Down Expand Up @@ -234,6 +241,9 @@ def _execute(self, options, args):
twofile = options['twofile']
import_file = options['import']
wants_available = options['available-formats']
date_path_opt = options['date-path']
date_path_auto = self.site.config['NEW_POST_DATE_PATH']
date_path_format = self.site.config['NEW_POST_DATE_PATH_FORMAT'].strip('/')

if wants_available:
self.print_compilers()
Expand Down Expand Up @@ -330,10 +340,14 @@ def _execute(self, options, args):
pattern = os.path.basename(entry[0])
suffix = pattern[1:]
output_path = os.path.dirname(entry[0])
if date_path_auto or date_path_opt:
output_path += os.sep + datetime.datetime.now(self.site.tzinfo).strftime(date_path_format)

txt_path = os.path.join(output_path, slug + suffix)
meta_path = os.path.join(output_path, slug + ".meta")
else:
if date_path_opt:
LOGGER.warn("A path has been specified, ignoring -d")
txt_path = os.path.join(self.site.original_cwd, path)
meta_path = os.path.splitext(txt_path)[0] + ".meta"

Expand Down

0 comments on commit 781826e

Please sign in to comment.