Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate: Handle single newlines in WordPress comments as line breaks #903

Merged
merged 1 commit into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ Breaking Changes
Bugfixes & Improvements
^^^^^^^^^^^^^^^^^^^^^^^

- TBD
- When importing from WordPress single newlines are now converted to line breaks
(`#903`_, projectgus)

.. _#903: https://github.com/posativ/isso/pull/903

0.13.0.beta1 (2022-06-05)
-------------------------
Expand Down
11 changes: 10 additions & 1 deletion isso/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,18 @@ def migrate(self):
progress.finish("{0} threads, {1} comments".format(
len(items) - skip, self.count))

def _process_comment_content(self, text):
# WordPress comment text renders a single newline between two blocks of
# text as a <br> tag, so add an explicit Markdown line break on import
# (Otherwise multiple blocks of text separated by single newlines are
# all shown as one long line.)
text = re.sub(r'(?!^\n)\n(?!^\n)', ' \n', text, 0)

return strip(text)

def Comment(self, el):
return {
"text": strip(el.find(self.ns + "comment_content").text),
"text": self._process_comment_content(el.find(self.ns + "comment_content").text),
"author": strip(el.find(self.ns + "comment_author").text),
"email": strip(el.find(self.ns + "comment_author_email").text),
"website": strip(el.find(self.ns + "comment_author_url").text),
Expand Down
8 changes: 6 additions & 2 deletions isso/tests/test_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_wordpress(self):
self.assertEqual(
len(db.execute("SELECT id FROM threads").fetchall()), 2)
self.assertEqual(
len(db.execute("SELECT id FROM comments").fetchall()), 7)
len(db.execute("SELECT id FROM comments").fetchall()), 8)

first = db.comments.get(1)
self.assertEqual(first["author"], "Ohai")
Expand All @@ -101,7 +101,11 @@ def test_wordpress(self):
for i in (3, 4, 5):
self.assertEqual(db.comments.get(i)["parent"], second["id"])

last = db.comments.get(6)
# Ensure newlines in wordpress translate to two newlines in isso, to render the same
multiline = db.comments.get(6)
self.assertIn("multiple lines: \nWordPress", multiline["text"])

last = db.comments.get(7)
self.assertEqual(last["author"], "Letzter :/")
self.assertEqual(last["parent"], None)

Expand Down
20 changes: 18 additions & 2 deletions isso/tests/wordpress.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,29 @@
</wp:comment>
<wp:comment>
<wp:comment_id>10</wp:comment_id>
<wp:comment_author><![CDATA[Multiline Author]]></wp:comment_author>
<wp:comment_author_email>[email protected]
</wp:comment_author_email>
<wp:comment_author_url></wp:comment_author_url>
<wp:comment_author_IP>::ffff:86.52.1.0</wp:comment_author_IP>
<wp:comment_date>2022-06-06 12:13:14</wp:comment_date>
<wp:comment_date_gmt>2022-06-06 02:13:14</wp:comment_date_gmt>
<wp:comment_content><![CDATA[When you have a comment with multiple lines:
WordPress exports it like this.]]></wp:comment_content>
<wp:comment_approved>1</wp:comment_approved>
<wp:comment_type></wp:comment_type>
<wp:comment_parent>0</wp:comment_parent>
<wp:comment_user_id>1</wp:comment_user_id>
</wp:comment>
<wp:comment>
<wp:comment_id>11</wp:comment_id>
<wp:comment_author><![CDATA[Letzter :/]]></wp:comment_author>
<wp:comment_author_email>[email protected]
</wp:comment_author_email>
<wp:comment_author_url></wp:comment_author_url>
<wp:comment_author_IP>::ffff:86.56.63.0</wp:comment_author_IP>
<wp:comment_date>2014-04-29 15:21:56</wp:comment_date>
<wp:comment_date_gmt>2014-04-29 15:21:56</wp:comment_date_gmt>
<wp:comment_date>2022-06-07 15:21:56</wp:comment_date>
<wp:comment_date_gmt>2022-06-07 15:21:56</wp:comment_date_gmt>
<wp:comment_content><![CDATA[...]]></wp:comment_content>
<wp:comment_approved>1</wp:comment_approved>
<wp:comment_type></wp:comment_type>
Expand Down