Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement #2262
  • Loading branch information
ralsina committed Mar 8, 2016
1 parent 2f7c91b commit db08856
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -4,6 +4,7 @@ New in master
Features
--------

* New ``--one-file`` option to wordpress importer (Issue #2262)
* New Pygal-based chart shortcode (Issue #2170)
* Add ``post_type`` to post-list directive (Issue #2272)
* Use ``sys.executable`` for launching pip in ``plugin`` (Issue #2275)
Expand Down
4 changes: 2 additions & 2 deletions nikola/nikola.py
Expand Up @@ -827,7 +827,7 @@ def __init__(self, **config):
# Set persistent state facility
self.state = Persistor(os.path.join('state_data.json'))

# Set cache facility
# Set cache 0facility
self.cache = Persistor(os.path.join(self.config['CACHE_FOLDER'], 'cache_data.json'))

def init_plugins(self, commands_only=False):
Expand Down Expand Up @@ -870,7 +870,7 @@ def init_plugins(self, commands_only=False):
# FIXME TemplateSystem should not be needed
if p[-1].details.get('Nikola', 'PluginCategory') not in {'Command', 'Template'}:
bad_candidates.add(p)
else: # Not commands-only
elif self.configured: # Not commands-only, and configured
# Remove compilers we don't use
if p[-1].name in self.bad_compilers:
bad_candidates.add(p)
Expand Down
17 changes: 17 additions & 0 deletions nikola/plugins/basic_import.py
Expand Up @@ -140,6 +140,23 @@ def write_content(cls, filename, content, rewrite_html=True):
with open(filename, "wb+") as fd:
fd.write(content)

@classmethod
def write_post(cls, filename, content, headers, compiler, rewrite_html=True):
if rewrite_html:
try:
doc = html.document_fromstring(content)
doc.rewrite_links(replacer)
content = html.tostring(doc, encoding='utf8')
except etree.ParserError:
pass
if isinstance(content, utils.bytes_str):
content = content.decode('utf-8')
compiler.create_post(
filename,
content=content,
onefile=True,
**headers)

@staticmethod
def write_metadata(filename, title, slug, post_date, description, tags, **kwargs):
"""Write metadata to meta file."""
Expand Down
57 changes: 49 additions & 8 deletions nikola/plugins/command/import_wordpress.py
Expand Up @@ -196,9 +196,30 @@ class CommandImportWordpress(Command, ImportMixin):
'default': 'first',
'help': 'lower: Convert all tag and category names to lower case\nfirst: Keep first spelling of tag or category name',
},
{
'name': 'one_file',
'long': 'one-file',
'default': False,
'type': bool,
'help': "Save imported posts in the more modern one-file format.",
},
]
all_tags = set([])

def _get_compiler(self):
"""Return whatever compiler we will use."""
self._find_wordpress_compiler()
if self.wordpress_page_compiler is not None:
return self.wordpress_page_compiler
plugin_info = self.site.plugin_manager.getPluginByName('markdown', 'PageCompiler')
if plugin_info is not None:
if not plugin_info.is_activated:
self.site.plugin_manager.activatePluginByName(plugin_info.name)
plugin_info.plugin_object.set_site(self.site)
return plugin_info.plugin_object
else:
LOGGER.error("Can't find markdown post compiler.")

def _find_wordpress_compiler(self):
"""Find WordPress compiler plugin."""
if self.wordpress_page_compiler is not None:
Expand All @@ -223,6 +244,8 @@ def _read_options(self, options, args):
'putting these arguments before the filename if you '
'are running into problems.'.format(args))

self.onefile = options.get('one_file', False)

self.import_into_existing_site = False
self.url_map = {}
self.timezone = None
Expand Down Expand Up @@ -931,14 +954,32 @@ def import_postpage_item(self, item, wordpress_namespace, out_folder=None, attac
meta_slug = slug
tags, other_meta = self._create_metadata(status, excerpt, tags, categories,
post_name=os.path.join(out_folder, slug))
self.write_metadata(os.path.join(self.output_folder, out_folder,
out_meta_filename),
title, meta_slug, post_date, description, tags, **other_meta)
self.write_content(
os.path.join(self.output_folder,
out_folder, out_content_filename),
content,
rewrite_html)

meta = {
"title": title,
"slug": meta_slug,
"date": post_date,
"description": description,
"tags": ','.join(tags),
}
meta.update(other_meta)
if self.onefile:
self.write_post(
os.path.join(self.output_folder,
out_folder, out_content_filename),
content,
meta,
self._get_compiler(),
rewrite_html)
else:
self.write_metadata(os.path.join(self.output_folder, out_folder,
out_meta_filename),
title, meta_slug, post_date, description, tags, **other_meta)
self.write_content(
os.path.join(self.output_folder,
out_folder, out_content_filename),
content,
rewrite_html)

if self.export_comments:
comments = []
Expand Down
2 changes: 2 additions & 0 deletions tests/test_command_import_wordpress.py
Expand Up @@ -15,6 +15,7 @@ class BasicCommandImportWordpress(BaseTestCase):
def setUp(self):
self.module = nikola.plugins.command.import_wordpress
self.import_command = self.module.CommandImportWordpress()
self.import_command.onefile = False
self.import_filename = os.path.abspath(os.path.join(
os.path.dirname(__file__), 'wordpress_export_example.xml'))

Expand Down Expand Up @@ -206,6 +207,7 @@ def test_importing_posts_and_attachments(self):

write_metadata = mock.MagicMock()
write_content = mock.MagicMock()
write_post = mock.MagicMock()
write_attachments_info = mock.MagicMock()
download_mock = mock.MagicMock()

Expand Down

0 comments on commit db08856

Please sign in to comment.