Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontmatter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def loads(
... post = frontmatter.loads(f.read())

"""
text = u(text, encoding)
text = u(text, encoding).strip()
handler = handler or detect_format(text, handlers)
metadata, content = parse(text, encoding, handler, **defaults)
return Post(content, handler, **metadata)
Expand Down
21 changes: 21 additions & 0 deletions tests/unit_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import io
import os
import shutil
import tempfile
Expand Down Expand Up @@ -186,6 +187,26 @@ def test_sanity_all(self):
for filename, Handler in self.TEST_FILES.items():
self.sanity_check(filename, Handler)

def test_leading_whitespace_preserves_handler(self):
"Preserve the detected format when parsing strips leading whitespace"
for filename, Handler in self.TEST_FILES.items():
with open(filename, encoding="utf-8") as f:
text = f.read()
expected = frontmatter.loads(text)
for prefix in ("\n", " \t\r\n"):
for loader in (
frontmatter.loads,
lambda text: frontmatter.load(io.StringIO(text)),
):
with self.subTest(filename=filename, prefix=prefix, loader=loader):
post = loader(prefix + text)
self.assertEqual(post.metadata, expected.metadata)
self.assertEqual(post.content, expected.content)
self.assertIsInstance(post.handler, Handler)
self.assertEqual(
frontmatter.dumps(post), frontmatter.dumps(expected)
)

def test_no_handler(self):
"default to YAMLHandler when no handler is attached"
post = frontmatter.load("tests/yaml/hello-world.txt")
Expand Down