aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--markdown/__init__.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/markdown/__init__.py b/markdown/__init__.py
index 641cbab..086fde9 100644
--- a/markdown/__init__.py
+++ b/markdown/__init__.py
@@ -397,9 +397,17 @@ class Markdown:
# Serialize _properly_. Strip top-level tags.
output, length = codecs.utf_8_decode(self.serializer(root, encoding="utf8"))
if self.stripTopLevelTags:
- start = output.index('<%s>'%DOC_TAG)+len(DOC_TAG)+2
- end = output.rindex('</%s>'%DOC_TAG)
- output = output[start:end].strip()
+ try:
+ start = output.index('<%s>'%DOC_TAG)+len(DOC_TAG)+2
+ end = output.rindex('</%s>'%DOC_TAG)
+ output = output[start:end].strip()
+ except ValueError:
+ if output.strip().endswith('<%s />'%DOC_TAG):
+ # We have an empty document
+ output = ''
+ else:
+ # We have a serious problem
+ message(CRITICAL, 'Failed to strip top level tags.')
# Run the text post-processors
for pp in self.postprocessors.values():