From 3b8d769128922a939ca7b6c72578d1eaad779b95 Mon Sep 17 00:00:00 2001
From: Andrey Efremov
Date: Tue, 8 Oct 2019 12:16:06 +0700
Subject: [PATCH] FB2 Output: Simplify regular expressions
---
src/calibre/ebooks/fb2/fb2ml.py | 27 ++++++++++++++-------------
1 file changed, 14 insertions(+), 13 deletions(-)
diff --git a/src/calibre/ebooks/fb2/fb2ml.py b/src/calibre/ebooks/fb2/fb2ml.py
index a9d604da65..a5745e1bc2 100644
--- a/src/calibre/ebooks/fb2/fb2ml.py
+++ b/src/calibre/ebooks/fb2/fb2ml.py
@@ -76,27 +76,28 @@ class FB2MLizer(object):
# Remove pointless tags, but keep their contents.
text = re.sub(r'(?miu)<(strong|emphasis|strikethrough|sub|sup)>(\s*)\1>', r'\2', text)
+ # Clean up paragraphs endings.
+ text = re.sub(r'(?miu)\s+
', '', text)
# Condense empty paragraphs into a line break.
- text = re.sub(r'(?miu)(\s*
\s*){3,}', '', text)
+ text = re.sub(r'(?miu)(?:\s*){3,}', '', text)
# Remove empty paragraphs.
- text = re.sub(r'(?miu)\s*
', '', text)
- # Clean up pargraph endings.
- text = re.sub(r'(?miu)\s*', '', text)
- # Put paragraphs following a paragraph on a separate line.
+ text = re.sub(r'(?miu)', '', text)
+ # Put the paragraph following a paragraph on a separate line.
text = re.sub(r'(?miu)\s*', '
\n', text)
- # Remove empty title elements.
- text = re.sub(r'(?miu)
\s*', '', text)
+ # Clean up title endings.
text = re.sub(r'(?miu)\s+', '', text)
+ # Remove empty title elements.
+ text = re.sub(r'(?miu)', '', text)
+ # Put the paragraph following a title on a separate line.
+ text = re.sub(r'(?miu)\s*', '\n
', text)
# Remove empty sections.
text = re.sub(r'(?miu)', '', text)
- # Clean up sections start and ends.
- text = re.sub(r'(?miu)\s*', '\n', text)
- text = re.sub(r'(?miu)\s*', '\n', text)
- text = re.sub(r'(?miu)\s*', '\n', text)
- text = re.sub(r'(?miu)\s*', '\n', text)
- # Put sectnions followed by sections on a separate line.
+ # Clean up sections starts and ends.
+ text = re.sub(r'(?miu)\s*\s*', '\n\n', text)
+ text = re.sub(r'(?miu)\s*\s*', '\n\n', text)
+ # Put the section following a section on a separate line.
text = re.sub(r'(?miu)\s*\n', text)
if self.opts.insert_blank_line: