From ea3837a3e89d6fe8dd617d0acd30535a90c99810 Mon Sep 17 00:00:00 2001 From: John Schember Date: Sun, 4 Sep 2011 16:31:41 -0400 Subject: [PATCH] Markdown Output: List fixes. --- src/calibre/ebooks/txt/markdownml.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/calibre/ebooks/txt/markdownml.py b/src/calibre/ebooks/txt/markdownml.py index 1f974fda60..c87836bf7d 100644 --- a/src/calibre/ebooks/txt/markdownml.py +++ b/src/calibre/ebooks/txt/markdownml.py @@ -77,7 +77,7 @@ class MarkdownMLizer(OEB2HTML): text = text.replace('\r', ' ') # Condense redundant spaces created by replacing newlines with spaces. text = re.sub(r'[ ]{2,}', ' ', text) - text = re.sub(r'\t+', '', text) + #text = re.sub(r'\t+', '', text) if self.remove_space_after_newline == True: text = re.sub(r'^ +', '', text) self.remove_space_after_newline = False @@ -189,20 +189,30 @@ class MarkdownMLizer(OEB2HTML): txt += '(' + attribs['src'] + ')' text.append(txt) elif tag in ('ol', 'ul'): - self.list.append({'name': tag, 'num': 0}) tags.append(tag) + # Add the list to our lists of lists so we can track + # nested lists. + self.list.append({'name': tag, 'num': 0}) elif tag == 'li': + # Get the last list from our list of lists if self.list: li = self.list[-1] else: li = {'name': 'ul', 'num': 0} + # Add a new line to start the item text.append('\n') + # Add indent if we have nested lists. + list_count = len(self.list) + # We only care about indenting nested lists. + if (list_count - 1) > 0: + text.append('\t' * (list_count - 1)) + # Add blockquote if we have a blockquote in a list item. text.append(bq) + # Write the proper sign for ordered and unorded lists. if li['name'] == 'ul': text.append('+ ') elif li['name'] == 'ol': text.append(unicode(len(self.list)) + '. ') - tags.append('') # Process tags that contain text. if hasattr(elem, 'text') and elem.text: @@ -222,7 +232,7 @@ class MarkdownMLizer(OEB2HTML): # Close all open tags. tags.reverse() for t in tags: - if t in ('pre', 'ul', 'ol', 'li', '>'): + if t in ('pre', 'ul', 'ol', '>'): if t == 'pre': self.in_pre = False text.append('\n')