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 379ecd6

Browse files
committedJul 11, 2015
Allowing to use WordPress page compiler to compile posts to HTML on import.
1 parent 7427c2d commit 379ecd6

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed
 

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

+47-17
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ class CommandImportWordpress(Command, ImportMixin):
138138
'type': bool,
139139
'help': "Export comments as .wpcomment files",
140140
},
141+
{
142+
'name': 'transform_to_html',
143+
'long': 'transform-to-html',
144+
'default': False,
145+
'type': bool,
146+
'help': "Uses WordPress page compiler to transform WordPress posts directly to HTML during import",
147+
}
141148
]
142149
all_tags = set([])
143150

@@ -169,16 +176,34 @@ def _read_options(self, options, args):
169176
self.export_categories_as_categories = options.get('export_categories_as_categories', False)
170177
self.export_comments = options.get('export_comments', False)
171178

179+
self.transform_to_html = options.get('transform_to_html', False)
180+
172181
self.auth = None
173182
if options.get('download_auth') is not None:
174183
username_password = options.get('download_auth')
175184
self.auth = tuple(username_password.split(':', 1))
176185
if len(self.auth) < 2:
177-
print("Please specify HTTP authentication credentials in the form username:password.")
186+
LOGGER.error("Please specify HTTP authentication credentials in the form username:password.")
178187
return False
179188

180189
self.separate_qtranslate_content = options.get('separate_qtranslate_content')
181190
self.translations_pattern = options.get('translations_pattern')
191+
192+
if self.transform_to_html:
193+
self.wordpress_page_compiler = None
194+
for plugin_info in self.site.plugin_manager.getPluginsOfCategory('PageCompiler'):
195+
if plugin_info.name == 'wordpress':
196+
if not plugin_info.is_activated:
197+
self.site.plugin_manager.activatePluginByName(plugin_info.name)
198+
plugin_info.plugin_object.set_site(self.site)
199+
self.wordpress_page_compiler = plugin_info.plugin_object
200+
break
201+
if not self.wordpress_page_compiler:
202+
LOGGER.error("To compile WordPress posts to HTML, the WordPress post compiler is needed. You can install it via:")
203+
LOGGER.error(" nikola plugin -i wordpress_compiler")
204+
LOGGER.error("Please note that the WordPress post compiler is licensed under the GPL v2.")
205+
return False
206+
182207
return True
183208

184209
def _prepare(self, channel):
@@ -283,8 +308,7 @@ def get_channel_from_file(cls, filename):
283308
channel = tree.find('channel')
284309
return channel
285310

286-
@staticmethod
287-
def populate_context(channel):
311+
def populate_context(self, channel):
288312
wordpress_namespace = channel.nsmap['wp']
289313

290314
context = SAMPLE_CONF.copy()
@@ -313,16 +337,18 @@ def populate_context(channel):
313337
author,
314338
'{{{0}}}author_display_name'.format(wordpress_namespace),
315339
"Joe Example")
316-
context['POSTS'] = '''(
317-
("posts/*.rst", "posts", "post.tmpl"),
318-
("posts/*.txt", "posts", "post.tmpl"),
319-
("posts/*.md", "posts", "post.tmpl"),
320-
)'''
321-
context['PAGES'] = '''(
322-
("stories/*.rst", "stories", "story.tmpl"),
323-
("stories/*.txt", "stories", "story.tmpl"),
324-
("stories/*.md", "stories", "story.tmpl"),
325-
)'''
340+
extensions = ['rst', 'txt', 'md']
341+
if self.transform_to_html:
342+
extensions.append('html')
343+
POSTS = '(\n'
344+
PAGES = '(\n'
345+
for extension in extensions:
346+
POSTS += ' ("posts/*.{0}", "posts", "post.tmpl"),\n'.format(extension)
347+
PAGES += ' ("stories/*.{0}", "stories", "story.tmpl"),\n'.format(extension)
348+
POSTS += ')\n'
349+
PAGES += ')\n'
350+
context['POSTS'] = POSTS
351+
context['PAGES'] = PAGES
326352
context['COMPILERS'] = '''{
327353
"rest": ('.txt', '.rst'),
328354
"markdown": ('.md', '.mdown', '.markdown'),
@@ -462,10 +488,14 @@ def transform_multiple_newlines(self, content):
462488

463489
def transform_content(self, content, post_format):
464490
if post_format == 'wp':
465-
content = self.transform_code(content)
466-
content = self.transform_caption(content)
467-
content = self.transform_multiple_newlines(content)
468-
return content, 'md'
491+
if self.transform_to_html:
492+
content = self.wordpress_page_compiler.compile_to_string(content)
493+
return content, 'html'
494+
else:
495+
content = self.transform_code(content)
496+
content = self.transform_caption(content)
497+
content = self.transform_multiple_newlines(content)
498+
return content, 'md'
469499
elif post_format == 'markdown':
470500
return content, 'md'
471501
elif post_format == 'none':

0 commit comments

Comments
 (0)
Please sign in to comment.