Skip to content

Commit

Permalink
fix getnikola#1385 -- add -i, --import option to new_post
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Dec 29, 2014
1 parent 3f82cb6 commit edd25dd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 23 deletions.
3 changes: 2 additions & 1 deletion AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@ yarko <https://github.com/yarko>
Alex Walters <https://github.com/tritium21>
Carsten Grohmann <https://github.com/CarstenGrohmann>
Aurelien Naldi <https://github.com/aurelien-naldi>
Andreas Linz <https://github.com/KLINGTdotNET>
Andreas Linz <https://github.com/KLINGTdotNET>
小明 <https://github.com/dongweiming>
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ New in master
Features
--------

* Added ``-i``, ``--import`` argument to ``new_post`` for importing
existing post contents (Issue #1385)
* Added warning about ``password`` in posts being insecure
(Issue #1547)
* INDEXES_TITLE and INDEXES_PAGES are translatable (Issue #1544)
Expand Down
8 changes: 8 additions & 0 deletions nikola/plugins/command/new_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ class CommandNewPage(Command):
'help': 'Markup format for the page, one of rest, markdown, wiki, '
'bbcode, html, textile, txt2tags',
},
{
'name': 'import',
'short': 'i',
'long': 'import',
'type': str,
'default': '',
'help': 'Import an existing file instead of creating a placeholder'
},
]

def _execute(self, options, args):
Expand Down
29 changes: 17 additions & 12 deletions nikola/plugins/command/new_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ class CommandNewPost(Command):
'help': 'Schedule the post based on recurrence rule'
},
{
'name': 'file',
'name': 'import',
'short': 'i',
'long': 'file',
'long': 'import',
'type': str,
'default': '',
'help': 'Can import existing file'
'help': 'Import an existing file instead of creating a placeholder'
},

]
Expand Down Expand Up @@ -241,7 +241,7 @@ def _execute(self, options, args):
tags = options['tags']
onefile = options['onefile']
twofile = options['twofile']
exist_file = options['file'] or None
import_file = options['import']

if is_page:
LOGGER = PAGELOGGER
Expand Down Expand Up @@ -272,12 +272,12 @@ def _execute(self, options, args):
self.site.config['COMPILERS'],
self.site.config['post_pages'])

if exist_file is not None:
# Current it only affect `ipynb` format
print("Import Existed {0}".format(content_type.title()))
if import_file:
print("Importing Existing {xx}".format(xx=content_type.title()))
print("-----------------------\n")
else:
print("Creating New {0}".format(content_type.title()))
print("-----------------\n")
print("Creating New {xx}".format(xx=content_type.title()))
print("-----------------\n")
if title is not None:
print("Title:", title)
else:
Expand Down Expand Up @@ -340,11 +340,16 @@ def _execute(self, options, args):
onefile = False
LOGGER.warn('This compiler does not support one-file posts.')

content = "Write your {0} here.".format('page' if is_page else 'post')
if import_file:
with io.open(import_file, 'r', encoding='utf-8') as fh:
content = fh.read()
else:
# ipynb's create_post depends on this exact string, take care
# if you're changing it
content = "Write your {0} here.".format('page' if is_page else 'post')
compiler_plugin.create_post(
txt_path, content=content, onefile=onefile, title=title,
slug=slug, date=date, tags=tags, is_page=is_page,
exist_file=exist_file, **metadata)
slug=slug, date=date, tags=tags, is_page=is_page, **metadata)

event = dict(path=txt_path)

Expand Down
15 changes: 6 additions & 9 deletions nikola/plugins/compile/ipynb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,18 @@ def compile_html(self, source, dest, is_two_file=True):
out_file.write(body)

def create_post(self, path, **kw):
# content and onefile are ignored by ipynb.
kw.pop('content', None)
content = kw.pop('content', None)
onefile = kw.pop('onefile', False)
# is_page is not needed to create the file
kw.pop('is_page', False)
exist_file = kw.pop('exist_file')

makedirs(os.path.dirname(path))
if onefile:
raise Exception('The one-file format is not supported by this compiler.')
if exist_file is not None and os.path.exists(exist_file):
with io.open(exist_file) as efd:
with io.open(path, "w+", encoding="utf8") as fd:
fd.writelines(efd.readlines())
else:
with io.open(path, "w+", encoding="utf8") as fd:
with io.open(path, "w+", encoding="utf8") as fd:
if not content.startswith("Write your"):
fd.write(content)
else:
fd.write("""{
"metadata": {
"name": ""
Expand Down
3 changes: 2 additions & 1 deletion nikola/plugins/compile/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def create_post(self, path, **kw):
with io.open(path, "w+", encoding="utf8") as fd:
if onefile:
fd.write(write_metadata(metadata))
fd.write('\n' + content)
fd.write('\n')
fd.write(content)

def set_site(self, site):
for plugin_info in site.plugin_manager.getPluginsOfCategory("RestExtension"):
Expand Down

0 comments on commit edd25dd

Please sign in to comment.