Skip to content

Commit 30ef218

Browse files
authored
Merge pull request isso-comments#903 from projectgus/bugfix/migrate_wordpress_paragraphs
migrate: Handle single newlines in WordPress comments as line breaks
2 parents b1021d1 + 0cf4c17 commit 30ef218

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

CHANGES.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ Breaking Changes
1717
Bugfixes & Improvements
1818
^^^^^^^^^^^^^^^^^^^^^^^
1919

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

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

2325
0.13.0.beta1 (2022-06-05)
2426
-------------------------

isso/migrate.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,18 @@ def migrate(self):
225225
progress.finish("{0} threads, {1} comments".format(
226226
len(items) - skip, self.count))
227227

228+
def _process_comment_content(self, text):
229+
# WordPress comment text renders a single newline between two blocks of
230+
# text as a <br> tag, so add an explicit Markdown line break on import
231+
# (Otherwise multiple blocks of text separated by single newlines are
232+
# all shown as one long line.)
233+
text = re.sub(r'(?!^\n)\n(?!^\n)', ' \n', text, 0)
234+
235+
return strip(text)
236+
228237
def Comment(self, el):
229238
return {
230-
"text": strip(el.find(self.ns + "comment_content").text),
239+
"text": self._process_comment_content(el.find(self.ns + "comment_content").text),
231240
"author": strip(el.find(self.ns + "comment_author").text),
232241
"email": strip(el.find(self.ns + "comment_author_email").text),
233242
"website": strip(el.find(self.ns + "comment_author_url").text),

isso/tests/test_migration.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def test_wordpress(self):
8787
self.assertEqual(
8888
len(db.execute("SELECT id FROM threads").fetchall()), 2)
8989
self.assertEqual(
90-
len(db.execute("SELECT id FROM comments").fetchall()), 7)
90+
len(db.execute("SELECT id FROM comments").fetchall()), 8)
9191

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

104-
last = db.comments.get(6)
104+
# Ensure newlines in wordpress translate to two newlines in isso, to render the same
105+
multiline = db.comments.get(6)
106+
self.assertIn("multiple lines: \nWordPress", multiline["text"])
107+
108+
last = db.comments.get(7)
105109
self.assertEqual(last["author"], "Letzter :/")
106110
self.assertEqual(last["parent"], None)
107111

isso/tests/wordpress.xml

+18-2
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,29 @@
101101
</wp:comment>
102102
<wp:comment>
103103
<wp:comment_id>10</wp:comment_id>
104+
<wp:comment_author><![CDATA[Multiline Author]]></wp:comment_author>
105+
<wp:comment_author_email>[email protected]
106+
</wp:comment_author_email>
107+
<wp:comment_author_url></wp:comment_author_url>
108+
<wp:comment_author_IP>::ffff:86.52.1.0</wp:comment_author_IP>
109+
<wp:comment_date>2022-06-06 12:13:14</wp:comment_date>
110+
<wp:comment_date_gmt>2022-06-06 02:13:14</wp:comment_date_gmt>
111+
<wp:comment_content><![CDATA[When you have a comment with multiple lines:
112+
WordPress exports it like this.]]></wp:comment_content>
113+
<wp:comment_approved>1</wp:comment_approved>
114+
<wp:comment_type></wp:comment_type>
115+
<wp:comment_parent>0</wp:comment_parent>
116+
<wp:comment_user_id>1</wp:comment_user_id>
117+
</wp:comment>
118+
<wp:comment>
119+
<wp:comment_id>11</wp:comment_id>
104120
<wp:comment_author><![CDATA[Letzter :/]]></wp:comment_author>
105121
<wp:comment_author_email>[email protected]
106122
</wp:comment_author_email>
107123
<wp:comment_author_url></wp:comment_author_url>
108124
<wp:comment_author_IP>::ffff:86.56.63.0</wp:comment_author_IP>
109-
<wp:comment_date>2014-04-29 15:21:56</wp:comment_date>
110-
<wp:comment_date_gmt>2014-04-29 15:21:56</wp:comment_date_gmt>
125+
<wp:comment_date>2022-06-07 15:21:56</wp:comment_date>
126+
<wp:comment_date_gmt>2022-06-07 15:21:56</wp:comment_date_gmt>
111127
<wp:comment_content><![CDATA[...]]></wp:comment_content>
112128
<wp:comment_approved>1</wp:comment_approved>
113129
<wp:comment_type></wp:comment_type>

0 commit comments

Comments
 (0)