Repository: nickpegg/posty

Issue:
Scrub URL on posty1 pages
We don't use this page URL anymore and instead rely solely on the slug

Discussion:

Patch:
diff --git a/posty/importers.py b/posty/importers.py
index 9e00c70..bf8c198 100644
--- a/posty/importers.py
+++ b/posty/importers.py
@@ -59,7 +59,16 @@ class Posty1Importer(Importer):
         self._copy_files('_templates', 'templates')
 
     def import_pages(self):
-        self._copy_files('_pages', 'pages')
+        src_dir = os.path.join(self.src_path, '_pages')
+        dst_dir = os.path.join(self.site.site_path, 'pages')
+
+        for page in os.listdir(src_dir):
+            src_file = os.path.join(src_dir, page)
+            dst_file = os.path.join(dst_dir, page)
+
+            new_page = self._convert_page(open(src_file).read())
+            with open(dst_file, 'w') as fh:
+                fh.write(new_page)
 
     def import_posts(self):
         src_dir = os.path.join(self.src_path, '_posts')
@@ -99,6 +108,25 @@ class Posty1Importer(Importer):
                 print(("  Looks like {} isn't a file nor dir, "
                        "not copying.").format(src_path))
 
+    def _convert_page(self, old_page):
+        """
+        Converts an old Posty 1.x page into a new-style one. Notably just
+        throws away any existing `url`
+        """
+        old_page = old_page.replace("\r\n", "\n")
+        docs = old_page.split("---\n")
+        new_page = ''
+
+        meta = yaml.load(docs[1])
+        if 'url' in meta.keys():
+            del meta['url']
+        new_page += yaml.dump(meta, default_flow_style=False)
+
+        new_page += "---\n"
+        new_page += docs[2]
+
+        return new_page
+
     def _convert_post(self, old_post):
         """
         Converts an old Posty post (a string) into a new-style post with a
diff --git a/posty/site.py b/posty/site.py
index ff63cec..cc0a442 100644
--- a/posty/site.py
+++ b/posty/site.py
@@ -63,7 +63,7 @@ class Site(object):
         page_dir = os.path.join(self.site_path, 'pages')
         for filename in os.listdir(page_dir):
             contents = open(os.path.join(page_dir, filename)).read()
-            _, meta_yaml, body = contents.split("---\n")
+            meta_yaml, body = contents.split("---\n")
             page = yaml.load(meta_yaml)
 
             page['body'] = body.strip()


Test patch:
diff --git a/tests/fixtures/site/pages/test-child.yaml b/tests/fixtures/site/pages/test-child.yaml
index 86692a9..fa7d1df 100644
--- a/tests/fixtures/site/pages/test-child.yaml
+++ b/tests/fixtures/site/pages/test-child.yaml
@@ -1,4 +1,3 @@
----
 title: Test's child
 url: child.html
 parent: Test
diff --git a/tests/fixtures/site/pages/test.yaml b/tests/fixtures/site/pages/test.yaml
index 61bdf2a..7a1578d 100644
--- a/tests/fixtures/site/pages/test.yaml
+++ b/tests/fixtures/site/pages/test.yaml
@@ -1,4 +1,3 @@
----
 title: Test
 url: test.html
 ---
diff --git a/tests/fixtures/site/pages/yup.yaml b/tests/fixtures/site/pages/yup.yaml
index b140315..159ded5 100644
--- a/tests/fixtures/site/pages/yup.yaml
+++ b/tests/fixtures/site/pages/yup.yaml
@@ -1,4 +1,3 @@
----
 title: Yup
 url: yup.html
 ---
diff --git a/tests/test_import.py b/tests/test_import.py
index e8b9ed5..079d06a 100644
--- a/tests/test_import.py
+++ b/tests/test_import.py
@@ -58,12 +58,11 @@ class TestPosty1Importer(object):
         importer = importer_with_directories
         importer.import_pages()
 
-        src_path = os.path.join(importer.src_path, '_pages')
-        dst_path = os.path.join(importer.site.site_path, 'pages')
-        for f in os.listdir(src_path):
-            src_file = open(os.path.join(src_path, f)).read()
-            dst_file = open(os.path.join(dst_path, f)).read()
-            assert src_file == dst_file
+        # Ensure `url` is not set on any pages
+        site = importer.site
+        site._load_pages()
+        for page in site.payload['pages']:
+            assert page.get('url') is None
 
     def test_import_posts(self, importer_with_directories):
         """