@@ -138,6 +138,13 @@ class CommandImportWordpress(Command, ImportMixin):
138
138
'type' : bool ,
139
139
'help' : "Export comments as .wpcomment files" ,
140
140
},
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
+ }
141
148
]
142
149
all_tags = set ([])
143
150
@@ -169,16 +176,34 @@ def _read_options(self, options, args):
169
176
self .export_categories_as_categories = options .get ('export_categories_as_categories' , False )
170
177
self .export_comments = options .get ('export_comments' , False )
171
178
179
+ self .transform_to_html = options .get ('transform_to_html' , False )
180
+
172
181
self .auth = None
173
182
if options .get ('download_auth' ) is not None :
174
183
username_password = options .get ('download_auth' )
175
184
self .auth = tuple (username_password .split (':' , 1 ))
176
185
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." )
178
187
return False
179
188
180
189
self .separate_qtranslate_content = options .get ('separate_qtranslate_content' )
181
190
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
+
182
207
return True
183
208
184
209
def _prepare (self , channel ):
@@ -283,8 +308,7 @@ def get_channel_from_file(cls, filename):
283
308
channel = tree .find ('channel' )
284
309
return channel
285
310
286
- @staticmethod
287
- def populate_context (channel ):
311
+ def populate_context (self , channel ):
288
312
wordpress_namespace = channel .nsmap ['wp' ]
289
313
290
314
context = SAMPLE_CONF .copy ()
@@ -313,16 +337,18 @@ def populate_context(channel):
313
337
author ,
314
338
'{{{0}}}author_display_name' .format (wordpress_namespace ),
315
339
"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
326
352
context ['COMPILERS' ] = '''{
327
353
"rest": ('.txt', '.rst'),
328
354
"markdown": ('.md', '.mdown', '.markdown'),
@@ -462,10 +488,14 @@ def transform_multiple_newlines(self, content):
462
488
463
489
def transform_content (self , content , post_format ):
464
490
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'
469
499
elif post_format == 'markdown' :
470
500
return content , 'md'
471
501
elif post_format == 'none' :
0 commit comments