diff --git a/src/calibre/ebooks/fb2/fb2ml.py b/src/calibre/ebooks/fb2/fb2ml.py
index 12dd539e53..197f063baa 100644
--- a/src/calibre/ebooks/fb2/fb2ml.py
+++ b/src/calibre/ebooks/fb2/fb2ml.py
@@ -85,6 +85,9 @@ class FB2MLizer(object):
# Put the paragraph following a paragraph on a separate line.
text = re.sub(r'(?mu)
\s*', '
\n', text)
+ if self.opts.insert_blank_line:
+ text = re.sub(r'(?mu)
', '', text)
+
# Clean up title endings.
text = re.sub(r'(?mu)\s+', '', text)
# Remove empty title elements.
@@ -92,6 +95,10 @@ class FB2MLizer(object):
# Put the paragraph following a title on a separate line.
text = re.sub(r'(?mu)\s*', '\n
', text)
+ # Put line breaks between paragraphs on a separate line.
+ text = re.sub(r'(?mu)(p|title)>\s*', r'\1>\n', text)
+ text = re.sub(r'(?mu)\s*
', '\n
', text)
+
# Remove empty sections.
text = re.sub(r'(?mu)', '', text)
# Clean up sections starts and ends.
@@ -99,15 +106,6 @@ class FB2MLizer(object):
text = re.sub(r'(?mu)\s*', '\n', text)
text = re.sub(r'(?mu)\s*', '\n', text)
text = re.sub(r'(?mu)\s*', '\n', text)
- # Put the section following a section on a separate line.
- text = re.sub(r'(?mu)\s*\n', text)
-
- if self.opts.insert_blank_line:
- text = re.sub(r'(?mu)
', '', text)
-
- # Put line breaks between paragraphs on a separate line.
- text = re.sub(r'(?mu)(p|title)>\s*', r'\1>\n', text)
- text = re.sub(r'(?mu)\s*', '\n
', text)
return text