Repository: getnikola/nikola

Issue:
Drop support for old-style metadata
The old style metadata, 7 fields with no explanations, can go. Nobody’s been using it — and if they have, they’re very good at ignoring constant warnings.


Discussion:

Resolving patch:
diff --git a/nikola/post.py b/nikola/post.py
--- a/nikola/post.py
+++ b/nikola/post.py
@@ -80,7 +80,6 @@
 __all__ = ('Post',)
 
 TEASER_REGEXP = re.compile('<!--\s*TEASER_END(:(.+))?\s*-->', re.IGNORECASE)
-_UPGRADE_METADATA_ADVERTISED = False
 
 
 class Post(object):
@@ -142,7 +141,6 @@ def __init__(
         self.skip_untranslated = not self.config['SHOW_UNTRANSLATED_POSTS']
         self._template_name = template_name
         self.is_two_file = True
-        self.newstylemeta = True
         self._reading_time = None
         self._remaining_reading_time = None
         self._paragraph_count = None
@@ -158,7 +156,7 @@ def __init__(
             if os.path.isfile(get_translation_candidate(self.config, self.source_path, lang)):
                 self.translated_to.add(lang)
 
-        default_metadata, self.newstylemeta = get_meta(self, self.config['FILE_METADATA_REGEXP'], self.config['UNSLUGIFY_TITLES'])
+        default_metadata = get_meta(self, self.config['FILE_METADATA_REGEXP'], self.config['UNSLUGIFY_TITLES'])
 
         self.meta = Functionary(lambda: None, self.default_lang)
         self.meta[self.default_lang] = default_metadata
@@ -167,8 +165,7 @@ def __init__(
             if lang != self.default_lang:
                 meta = defaultdict(lambda: '')
                 meta.update(default_metadata)
-                _meta, _nsm = get_meta(self, self.config['FILE_METADATA_REGEXP'], self.config['UNSLUGIFY_TITLES'], lang)
-                self.newstylemeta = self.newstylemeta and _nsm
+                _meta = get_meta(self, self.config['FILE_METADATA_REGEXP'], self.config['UNSLUGIFY_TITLES'], lang)
                 meta.update(_meta)
                 self.meta[lang] = meta
 
@@ -1053,64 +1050,21 @@ def _get_metadata_from_file(meta_data, config=None):
 
 def get_metadata_from_meta_file(path, config=None, lang=None):
     """Take a post path, and gets data from a matching .meta file."""
-    global _UPGRADE_METADATA_ADVERTISED
     meta_path = os.path.splitext(path)[0] + '.meta'
     if lang and config:
         meta_path = get_translation_candidate(config, meta_path, lang)
     elif lang:
         meta_path += '.' + lang
     if os.path.isfile(meta_path):
-        with io.open(meta_path, "r", encoding="utf8") as meta_file:
-            meta_data = meta_file.readlines()
-
-        # Detect new-style metadata.
-        newstyleregexp = re.compile(r'\.\. .*?: .*')
-        newstylemeta = False
-        for l in meta_data:
-            if l.strip():
-                if re.match(newstyleregexp, l):
-                    newstylemeta = True
-
-        if newstylemeta:
-            # New-style metadata is basically the same as reading metadata from
-            # a 1-file post.
-            return get_metadata_from_file(path, config, lang), newstylemeta
-        else:
-            if not _UPGRADE_METADATA_ADVERTISED:
-                LOGGER.warn("Some posts on your site have old-style metadata. You should upgrade them to the new format, with support for extra fields.")
-                LOGGER.warn("Install the 'upgrade_metadata' plugin (with 'nikola plugin -i upgrade_metadata') and run 'nikola upgrade_metadata'.")
-                _UPGRADE_METADATA_ADVERTISED = True
-            while len(meta_data) < 7:
-                meta_data.append("")
-            (title, slug, date, tags, link, description, _type) = [
-                x.strip() for x in meta_data][:7]
-
-            meta = {}
-
-            if title:
-                meta['title'] = title
-            if slug:
-                meta['slug'] = slug
-            if date:
-                meta['date'] = date
-            if tags:
-                meta['tags'] = tags
-            if link:
-                meta['link'] = link
-            if description:
-                meta['description'] = description
-            if _type:
-                meta['type'] = _type
-
-            return meta, newstylemeta
+        return get_metadata_from_file(path, config, lang)
 
     elif lang:
         # Metadata file doesn't exist, but not default language,
         # So, if default language metadata exists, return that.
         # This makes the 2-file format detection more reliable (Issue #525)
         return get_metadata_from_meta_file(path, config, lang=None)
-    else:
-        return {}, True
+    else:  # No 2-file metadata
+        return {}
 
 
 def get_meta(post, file_metadata_regexp=None, unslugify_titles=False, lang=None):
@@ -1129,8 +1083,7 @@ def get_meta(post, file_metadata_regexp=None, unslugify_titles=False, lang=None)
     except AttributeError:
         config = None
 
-    _, newstylemeta = get_metadata_from_meta_file(post.metadata_path, config, lang)
-    meta.update(_)
+    meta.update(get_metadata_from_meta_file(post.metadata_path, config, lang))
 
     if not meta:
         post.is_two_file = False
@@ -1149,7 +1102,7 @@ def get_meta(post, file_metadata_regexp=None, unslugify_titles=False, lang=None)
 
     if not post.is_two_file and not compiler_meta:
         # Meta file has precedence over file, which can contain garbage.
-        # Moreover, we should not to talk to the file if we have compiler meta.
+        # Moreover, we should not read the file if we have compiler meta.
         meta.update(get_metadata_from_file(post.source_path, config, lang))
 
     if lang is None:
@@ -1165,7 +1118,7 @@ def get_meta(post, file_metadata_regexp=None, unslugify_titles=False, lang=None)
             meta['title'] = os.path.splitext(
                 os.path.basename(post.source_path))[0]
 
-    return meta, newstylemeta
+    return meta
 
 
 def hyphenate(dom, _lang):


Test patch:
diff --git a/tests/test_rss_feeds.py b/tests/test_rss_feeds.py
--- a/tests/test_rss_feeds.py
+++ b/tests/test_rss_feeds.py
@@ -48,23 +48,20 @@ def setUp(self):
         self.blog_url = "http://some.blog"
 
         with mock.patch('nikola.post.get_meta',
-                        mock.Mock(return_value=(
-                            (defaultdict(str, {'title': 'post title',
-                                               'slug': 'awesome_article',
-                                               'date': '2012-10-01 22:41',
-                                               'author': None,
-                                               'tags': 'tags',
-                                               'link': 'link',
-                                               'description': 'description',
-                                               'enclosure': 'http://www.example.org/foo.mp3',
-                                               'enclosure_length': '5'}),
-                             True)
-                        ))):
+                        mock.Mock(return_value=defaultdict(str, {
+                                  'title': 'post title',
+                                  'slug': 'awesome_article',
+                                  'date': '2012-10-01 22:41',
+                                  'author': None,
+                                  'tags': 'tags',
+                                  'link': 'link',
+                                  'description': 'description',
+                                  'enclosure': 'http://www.example.org/foo.mp3',
+                                  'enclosure_length': '5'}))):
             with mock.patch('nikola.nikola.utils.os.path.isdir',
                             mock.Mock(return_value=True)):
                 with mock.patch('nikola.nikola.Post.text',
                                 mock.Mock(return_value='some long text')):
-
                     example_post = nikola.nikola.Post('source.file',
                                                       fake_conf,
                                                       'blog_folder',
diff --git a/tests/test_utils.py b/tests/test_utils.py
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -31,7 +31,7 @@ def test_getting_metadata_from_content(self):
         post.metadata_path = 'file_with_metadata.meta'
 
         with mock.patch('nikola.post.io.open', opener_mock, create=True):
-            meta, nsm = get_meta(post)
+            meta = get_meta(post)
 
         self.assertEqual('Nikola needs more tests!', meta['title'])
         self.assertEqual('write-tests-now', meta['slug'])
@@ -39,7 +39,6 @@ def test_getting_metadata_from_content(self):
         self.assertFalse('tags' in meta)
         self.assertFalse('link' in meta)
         self.assertFalse('description' in meta)
-        self.assertTrue(nsm)
 
     def test_get_title_from_fname(self):
         file_metadata = ".. slug: write-tests-now\n"\
@@ -55,7 +54,7 @@ def test_get_title_from_fname(self):
         post.metadata_path = 'file_with_metadata.meta'
 
         with mock.patch('nikola.post.io.open', opener_mock, create=True):
-            meta, nsm = get_meta(post, 'file_with_metadata')
+            meta = get_meta(post, 'file_with_metadata')
 
         self.assertEqual('file_with_metadata', meta['title'])
         self.assertEqual('write-tests-now', meta['slug'])
@@ -63,7 +62,6 @@ def test_get_title_from_fname(self):
         self.assertFalse('tags' in meta)
         self.assertFalse('link' in meta)
         self.assertFalse('description' in meta)
-        self.assertTrue(nsm)
 
     def test_use_filename_as_slug_fallback(self):
         file_metadata = ".. title: Nikola needs more tests!\n"\
@@ -80,23 +78,22 @@ def test_use_filename_as_slug_fallback(self):
         post.metadata_path = 'Slugify this.meta'
 
         with mock.patch('nikola.post.io.open', opener_mock, create=True):
-            meta, nsm = get_meta(post, 'Slugify this')
+            meta = get_meta(post, 'Slugify this')
         self.assertEqual('Nikola needs more tests!', meta['title'])
         self.assertEqual('slugify-this', meta['slug'])
         self.assertEqual('2012/09/15 19:52:05', meta['date'])
         self.assertFalse('tags' in meta)
         self.assertFalse('link' in meta)
         self.assertFalse('description' in meta)
-        self.assertTrue(nsm)
 
     def test_extracting_metadata_from_filename(self):
         post = dummy()
         post.source_path = '2013-01-23-the_slug-dubdubtitle.md'
         post.metadata_path = '2013-01-23-the_slug-dubdubtitle.meta'
         with mock.patch('nikola.post.io.open', create=True):
-            meta, _ = get_meta(
+            meta = get_meta(
                 post,
-                '(?P<date>\d{4}-\d{2}-\d{2})-(?P<slug>.*)-(?P<title>.*)\.md')
+                r'(?P<date>\d{4}-\d{2}-\d{2})-(?P<slug>.*)-(?P<title>.*)\.md')
 
         self.assertEqual('dubdubtitle', meta['title'])
         self.assertEqual('the_slug', meta['slug'])
@@ -107,7 +104,7 @@ def test_get_meta_slug_only_from_filename(self):
         post.source_path = 'some/path/the_slug.md'
         post.metadata_path = 'some/path/the_slug.meta'
         with mock.patch('nikola.post.io.open', create=True):
-            meta, _ = get_meta(post)
+            meta = get_meta(post)
 
         self.assertEqual('the_slug', meta['slug'])