Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6173414

Browse files
committedJul 11, 2015
Allowing to use WordPress page compiler to render .wp posts.
1 parent 379ecd6 commit 6173414

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed
 

Diff for: ‎nikola/plugins/command/import_wordpress.py

+34-13
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,14 @@ class CommandImportWordpress(Command, ImportMixin):
144144
'default': False,
145145
'type': bool,
146146
'help': "Uses WordPress page compiler to transform WordPress posts directly to HTML during import",
147-
}
147+
},
148+
{
149+
'name': 'use_wordpress_compiler',
150+
'long': 'use-wordpress-compiler',
151+
'default': False,
152+
'type': bool,
153+
'help': "Instead of converting posts to markdown, leave them as is and use the WordPress page compiler",
154+
},
148155
]
149156
all_tags = set([])
150157

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

179186
self.transform_to_html = options.get('transform_to_html', False)
187+
self.use_wordpress_compiler = options.get('use_wordpress_compiler', False)
180188

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

200+
if self.transform_to_html and self.use_wordpress_compiler:
201+
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.")
202+
203+
if self.use_wordpress_compiler:
204+
LOGGER.warn("Make sure to install the WordPress page compiler via")
205+
LOGGER.warn(" nikola plugin -i wordpress_compiler")
206+
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']))
207+
192208
if self.transform_to_html:
193209
self.wordpress_page_compiler = None
194210
for plugin_info in self.site.plugin_manager.getPluginsOfCategory('PageCompiler'):
@@ -349,12 +365,14 @@ def populate_context(self, channel):
349365
PAGES += ')\n'
350366
context['POSTS'] = POSTS
351367
context['PAGES'] = PAGES
352-
context['COMPILERS'] = '''{
353-
"rest": ('.txt', '.rst'),
354-
"markdown": ('.md', '.mdown', '.markdown'),
355-
"html": ('.html', '.htm')
356-
}
357-
'''
368+
COMPILERS = '{\n'
369+
COMPILERS += ''' "rest": ('.txt', '.rst'),''' + '\n'
370+
COMPILERS += ''' "markdown": ('.md', '.mdown', '.markdown')''' + '\n'
371+
COMPILERS += ''' "html": ('.html', '.htm'),''' + '\n'
372+
if self.use_wordpress_compiler:
373+
COMPILERS += ''' "wordpress": ('.wp'),''' + '\n'
374+
COMPILERS += '}'
375+
context['COMPILERS'] = COMPILERS
358376

359377
return context
360378

@@ -490,16 +508,18 @@ def transform_content(self, content, post_format):
490508
if post_format == 'wp':
491509
if self.transform_to_html:
492510
content = self.wordpress_page_compiler.compile_to_string(content)
493-
return content, 'html'
511+
return content, 'html', True
512+
elif self.use_wordpress_compiler:
513+
return content, 'wp', False
494514
else:
495515
content = self.transform_code(content)
496516
content = self.transform_caption(content)
497517
content = self.transform_multiple_newlines(content)
498-
return content, 'md'
518+
return content, 'md', True
499519
elif post_format == 'markdown':
500-
return content, 'md'
520+
return content, 'md', True
501521
elif post_format == 'none':
502-
return content, 'html'
522+
return content, 'html', True
503523
else:
504524
return None
505525

@@ -698,7 +718,7 @@ def import_item(self, item, wordpress_namespace, out_folder=None):
698718
default_language = self.context["DEFAULT_LANG"]
699719
for lang, content in content_translations.items():
700720
try:
701-
content, extension = self.transform_content(content, post_format)
721+
content, extension, rewrite_html = self.transform_content(content, post_format)
702722
except:
703723
LOGGER.error(('Cannot interpret post "{0}" (language {1}) with post ' +
704724
'format {2}!').format(os.path.join(out_folder, slug), lang, post_format))
@@ -725,7 +745,8 @@ def import_item(self, item, wordpress_namespace, out_folder=None):
725745
self.write_content(
726746
os.path.join(self.output_folder,
727747
out_folder, out_content_filename),
728-
content)
748+
content,
749+
rewrite_html)
729750

730751
if self.export_comments:
731752
comments = []

0 commit comments

Comments
 (0)
Please sign in to comment.