Repository: getnikola/nikola

Issue:
import_wordpress: UnicodeEncodeError when writing non-ASCII headers for comments
Hi,

playing around with the new comments importer I stumbled over the following bug: my dump contains the following line:

```
<wp:comment_author><![CDATA[Stéphane]]></wp:comment_author>
```

Running `nikola import_wordpress --export-comments` results in the following traceback:

```
Traceback (most recent call last):
  File "/path/lib/python2.7/site-packages/doit/doit_cmd.py", line 168, in run
    return command.parse_execute(args)
  File "/path/lib/python2.7/site-packages/doit/cmd_base.py", line 122, in parse_execute
    return self.execute(params, args)
  File "/path/lib/python2.7/site-packages/nikola/plugin_categories.py", line 127, in execute
    return self._execute(options, args)
  File "/path/lib/python2.7/site-packages/nikola/plugins/command/import_wordpress.py", line 325, in _execute
    self.import_posts(channel)
  File "/path/lib/python2.7/site-packages/nikola/plugins/command/import_wordpress.py", line 982, in import_posts
    self.process_item_if_post_or_page(item)
  File "/path/lib/python2.7/site-packages/nikola/plugins/command/import_wordpress.py", line 963, in process_item_if_post_or_page
    out_folder_slug = self.import_postpage_item(item, wordpress_namespace, 'posts', attachments)
  File "/path/lib/python2.7/site-packages/nikola/plugins/command/import_wordpress.py", line 916, in import_postpage_item
    self._write_comment(os.path.join(self.output_folder, out_folder, comment_filename), comment)
  File "/path/lib/python2.7/site-packages/nikola/plugins/command/import_wordpress.py", line 722, in _write_comment
    write_header_line(fd, "author", comment["author"])
  File "/path/lib/python2.7/site-packages/nikola/plugins/command/import_wordpress.py", line 714, in write_header_line
    header_content = str(header_content).replace('\n', ' ')
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 2: ordinal not in range(128)
```



Discussion:

Resolving patch:
diff --git a/nikola/plugins/command/import_wordpress.py b/nikola/plugins/command/import_wordpress.py
--- a/nikola/plugins/command/import_wordpress.py
+++ b/nikola/plugins/command/import_wordpress.py
@@ -50,7 +50,7 @@
 
 from nikola.plugin_categories import Command
 from nikola import utils
-from nikola.utils import req_missing
+from nikola.utils import req_missing, unicode_str
 from nikola.plugins.basic_import import ImportMixin, links
 from nikola.nikola import DEFAULT_TRANSLATIONS_PATTERN
 from nikola.plugins.command.init import SAMPLE_CONF, prepare_config, format_default_translations_config
@@ -689,7 +689,7 @@ def _extract_comment(self, comment, wordpress_namespace):
         elif approved == 'spam' or approved == 'trash':
             pass
         else:
-            LOGGER.warn("Unknown comment approved status: " + str(approved))
+            LOGGER.warn("Unknown comment approved status: {0}".format(approved))
         parent = int(get_text_tag(comment, "{{{0}}}comment_parent".format(wordpress_namespace), 0))
         if parent == 0:
             parent = None
@@ -710,7 +710,7 @@ def write_header_line(fd, header_field, header_content):
             """Write comment header line."""
             if header_content is None:
                 return
-            header_content = str(header_content).replace('\n', ' ')
+            header_content = unicode_str(header_content).replace('\n', ' ')
             line = '.. ' + header_field + ': ' + header_content + '\n'
             fd.write(line.encode('utf8'))
 
@@ -911,7 +911,7 @@ def import_postpage_item(self, item, wordpress_namespace, out_folder=None, attac
                         comments.append(comment)
 
                 for comment in comments:
-                    comment_filename = slug + "." + str(comment['id']) + ".wpcomment"
+                    comment_filename = "{0}.{1}.wpcomment".format(slug, comment['id'])
                     self._write_comment(os.path.join(self.output_folder, out_folder, comment_filename), comment)
 
             return (out_folder, slug)


Test patch: