Skip to content
Permalink
Browse files

Fix #2513 — date-based paths in new_post

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 781826ed8fb45c791ee811c7acc96c119faf31a6
Showing with 28 additions and 1 deletion.
  1. +3 −0 CHANGES.txt
  2. +8 −0 nikola/conf.py.in
  3. +2 −0 nikola/nikola.py
  4. +15 −1 nikola/plugins/command/new_post.py
@@ -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
@@ -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
@@ -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"),),
@@ -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):
@@ -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()
@@ -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"

0 comments on commit 781826e

Please sign in to comment.
You can’t perform that action at this time.