From 5d860471ac6e14b4ada641eb1c3a5b7c4f3f3940 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Thu, 11 Aug 2016 07:46:53 +0100 Subject: [PATCH 1/4] pelican_open: don't raise IndexError on empty files If the file is empty, content[0] raises IndexError. --- pelican/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pelican/utils.py b/pelican/utils.py index 4e729361..06290d16 100644 --- a/pelican/utils.py +++ b/pelican/utils.py @@ -244,7 +244,7 @@ def pelican_open(filename, mode='rb', strip_crs=(sys.platform == 'win32')): with codecs.open(filename, mode, encoding='utf-8') as infile: content = infile.read() - if content[0] == codecs.BOM_UTF8.decode('utf8'): + if content[:1] == codecs.BOM_UTF8.decode('utf8'): content = content[1:] if strip_crs: content = content.replace('\r\n', '\n') From 904f57d9c33d1ed9831d24ef360ca1ceaea790a3 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Thu, 11 Aug 2016 07:51:39 +0100 Subject: [PATCH 2/4] MarkdownReader: don't raise AttributeError on empty files Markdown.convert() returns early, without running any preprocessors, if source.strip() is empty. Before, Pelican would raise AttributeError in this case; now, it logs a more friendly error: ERROR: Skipping ./foo.md: could not find information about 'NameError: title' which is more consistent with the error from empty .rst files: ERROR: Skipping ./foo.rst: could not find information about 'NameError: date' --- pelican/readers.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pelican/readers.py b/pelican/readers.py index a3e89af8..48985ed6 100644 --- a/pelican/readers.py +++ b/pelican/readers.py @@ -288,7 +288,10 @@ class MarkdownReader(BaseReader): with pelican_open(source_path) as text: content = self._md.convert(text) - metadata = self._parse_metadata(self._md.Meta) + if hasattr(self._md, 'Meta'): + metadata = self._parse_metadata(self._md.Meta) + else: + metadata = {} return content, metadata From 129b144852979521126937f7e7e999295ea5cfc7 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Tue, 16 Aug 2016 07:12:04 +0100 Subject: [PATCH 3/4] test_cache: fix comment listing uncached files This file was added in d333ed1; the number '3' in code was incremented to '4' but the comment wasn't updated. --- pelican/tests/test_cache.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pelican/tests/test_cache.py b/pelican/tests/test_cache.py index 3da3f789..2e6786b3 100644 --- a/pelican/tests/test_cache.py +++ b/pelican/tests/test_cache.py @@ -56,7 +56,8 @@ class TestCache(unittest.TestCase): generator.readers.read_file = MagicMock() generator.generate_context() """ - 3 Files don't get cached because they were not valid + 4 files don't get cached because they were not valid + - article_with_attributes_containing_double_quotes.html - article_with_comments.html - article_with_null_attributes.html - 2012-11-30_md_w_filename_meta#foo-bar.md From 85a860d5dd1b1e05a1161d103d371bd83be8574a Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Mon, 15 Aug 2016 11:18:24 +0100 Subject: [PATCH 4/4] test_readers: test reading empty Markdown files --- pelican/tests/content/empty.md | 0 pelican/tests/content/empty_with_bom.md | 1 + pelican/tests/test_cache.py | 6 ++++-- pelican/tests/test_readers.py | 16 ++++++++++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 pelican/tests/content/empty.md create mode 100644 pelican/tests/content/empty_with_bom.md diff --git a/pelican/tests/content/empty.md b/pelican/tests/content/empty.md new file mode 100644 index 00000000..e69de29b diff --git a/pelican/tests/content/empty_with_bom.md b/pelican/tests/content/empty_with_bom.md new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/pelican/tests/content/empty_with_bom.md @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/pelican/tests/test_cache.py b/pelican/tests/test_cache.py index 2e6786b3..e8ea4967 100644 --- a/pelican/tests/test_cache.py +++ b/pelican/tests/test_cache.py @@ -56,13 +56,15 @@ class TestCache(unittest.TestCase): generator.readers.read_file = MagicMock() generator.generate_context() """ - 4 files don't get cached because they were not valid + 6 files don't get cached because they were not valid - article_with_attributes_containing_double_quotes.html - article_with_comments.html - article_with_null_attributes.html - 2012-11-30_md_w_filename_meta#foo-bar.md + - empty.md + - empty_with_bom.md """ - self.assertEqual(generator.readers.read_file.call_count, 4) + self.assertEqual(generator.readers.read_file.call_count, 6) @unittest.skipUnless(MagicMock, 'Needs Mock module') def test_article_reader_content_caching(self): diff --git a/pelican/tests/test_readers.py b/pelican/tests/test_readers.py index e9100241..43279179 100644 --- a/pelican/tests/test_readers.py +++ b/pelican/tests/test_readers.py @@ -546,6 +546,22 @@ class MdReaderTest(ReaderTest): } self.assertDictHasSubset(metadata, expected) + def test_empty_file(self): + reader = readers.MarkdownReader(settings=get_settings()) + content, metadata = reader.read( + _path('empty.md')) + + self.assertEqual(metadata, {}) + self.assertEqual(content, '') + + def test_empty_file_with_bom(self): + reader = readers.MarkdownReader(settings=get_settings()) + content, metadata = reader.read( + _path('empty_with_bom.md')) + + self.assertEqual(metadata, {}) + self.assertEqual(content, '') + class HTMLReaderTest(ReaderTest): def test_article_with_comments(self):