Skip to content

Commit

Permalink
Allowing to use WordPress page compiler to render .wp posts.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Jul 11, 2015
1 parent 379ecd6 commit 6173414
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions nikola/plugins/command/import_wordpress.py
Expand Up @@ -144,7 +144,14 @@ class CommandImportWordpress(Command, ImportMixin):
'default': False,
'type': bool,
'help': "Uses WordPress page compiler to transform WordPress posts directly to HTML during import",
}
},
{
'name': 'use_wordpress_compiler',
'long': 'use-wordpress-compiler',
'default': False,
'type': bool,
'help': "Instead of converting posts to markdown, leave them as is and use the WordPress page compiler",
},
]
all_tags = set([])

Expand Down Expand Up @@ -177,6 +184,7 @@ def _read_options(self, options, args):
self.export_comments = options.get('export_comments', False)

self.transform_to_html = options.get('transform_to_html', False)
self.use_wordpress_compiler = options.get('use_wordpress_compiler', False)

self.auth = None
if options.get('download_auth') is not None:
Expand All @@ -189,6 +197,14 @@ def _read_options(self, options, args):
self.separate_qtranslate_content = options.get('separate_qtranslate_content')
self.translations_pattern = options.get('translations_pattern')

if self.transform_to_html and self.use_wordpress_compiler:
LOGGER.warn("It does not make sense to combine --transform-to-html with --use-wordpress-compiler, as the first converts all posts to HTML and the latter option affects zero posts.")

if self.use_wordpress_compiler:
LOGGER.warn("Make sure to install the WordPress page compiler via")
LOGGER.warn(" nikola plugin -i wordpress_compiler")
LOGGER.warn("in your imported blog's folder ({0}), if you haven't installed it system-wide or user-wide. Otherwise, your newly imported blog won't compile.".format(options['output_folder']))

if self.transform_to_html:
self.wordpress_page_compiler = None
for plugin_info in self.site.plugin_manager.getPluginsOfCategory('PageCompiler'):
Expand Down Expand Up @@ -349,12 +365,14 @@ def populate_context(self, channel):
PAGES += ')\n'
context['POSTS'] = POSTS
context['PAGES'] = PAGES
context['COMPILERS'] = '''{
"rest": ('.txt', '.rst'),
"markdown": ('.md', '.mdown', '.markdown'),
"html": ('.html', '.htm')
}
'''
COMPILERS = '{\n'
COMPILERS += ''' "rest": ('.txt', '.rst'),''' + '\n'
COMPILERS += ''' "markdown": ('.md', '.mdown', '.markdown')''' + '\n'
COMPILERS += ''' "html": ('.html', '.htm'),''' + '\n'
if self.use_wordpress_compiler:
COMPILERS += ''' "wordpress": ('.wp'),''' + '\n'
COMPILERS += '}'
context['COMPILERS'] = COMPILERS

return context

Expand Down Expand Up @@ -490,16 +508,18 @@ def transform_content(self, content, post_format):
if post_format == 'wp':
if self.transform_to_html:
content = self.wordpress_page_compiler.compile_to_string(content)
return content, 'html'
return content, 'html', True
elif self.use_wordpress_compiler:
return content, 'wp', False
else:
content = self.transform_code(content)
content = self.transform_caption(content)
content = self.transform_multiple_newlines(content)
return content, 'md'
return content, 'md', True
elif post_format == 'markdown':
return content, 'md'
return content, 'md', True
elif post_format == 'none':
return content, 'html'
return content, 'html', True
else:
return None

Expand Down Expand Up @@ -698,7 +718,7 @@ def import_item(self, item, wordpress_namespace, out_folder=None):
default_language = self.context["DEFAULT_LANG"]
for lang, content in content_translations.items():
try:
content, extension = self.transform_content(content, post_format)
content, extension, rewrite_html = self.transform_content(content, post_format)
except:
LOGGER.error(('Cannot interpret post "{0}" (language {1}) with post ' +
'format {2}!').format(os.path.join(out_folder, slug), lang, post_format))
Expand All @@ -725,7 +745,8 @@ def import_item(self, item, wordpress_namespace, out_folder=None):
self.write_content(
os.path.join(self.output_folder,
out_folder, out_content_filename),
content)
content,
rewrite_html)

if self.export_comments:
comments = []
Expand Down

0 comments on commit 6173414

Please sign in to comment.