From 0808945c3e04a6d100b4e45058334b775e14a3d8 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Fri, 16 Nov 2018 00:11:40 +0100 Subject: [PATCH] Fix page prefix logic and sort. (#388) --- VERSION | 2 +- src/Collection/Page/Page.php | 13 +++++++++++-- src/Renderer/Twig/Extension.php | 16 +--------------- src/Step/ConvertPages.php | 16 +++++++++------- .../Blog/2017-10-20-another-post-with-prefix.md | 1 + .../website/layouts/section/pages.html.twig | 17 +++++++++++++++++ .../themes/a-theme/layouts/index.html.twig | 6 +++--- 7 files changed, 43 insertions(+), 28 deletions(-) create mode 100644 tests/fixtures/website/layouts/section/pages.html.twig diff --git a/VERSION b/VERSION index 8bbb6e4..c6436a8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.10.1 +2.10.2 diff --git a/src/Collection/Page/Page.php b/src/Collection/Page/Page.php index 0a3c369..b42d6ba 100644 --- a/src/Collection/Page/Page.php +++ b/src/Collection/Page/Page.php @@ -123,10 +123,19 @@ public function __construct(SplFileInfo $file = null) $this->setTitle(self::subPrefix($this->fileName)); // section - ie: "blog" $this->setSection(explode('/', $this->path)[0]); - // date + // date from file meta $this->setDate(filemtime($this->file->getPathname())); + // file as a prefix? if (false !== self::getPrefix($this->fileId)) { - $this->setDate(self::getPrefix($this->fileId)); + // prefix is a valid date? + $isValidDate = function ($date, $format = 'Y-m-d') { + $d = \DateTime::createFromFormat($format, $date); + + return $d && $d->format($format) === $date; + }; + if ($isValidDate(self::getPrefix($this->fileId))) { + $this->setDate(self::getPrefix($this->fileId)); + } } // permalink $this->setPermalink($this->pathname); diff --git a/src/Renderer/Twig/Extension.php b/src/Renderer/Twig/Extension.php index 184c36e..e342298 100644 --- a/src/Renderer/Twig/Extension.php +++ b/src/Renderer/Twig/Extension.php @@ -140,25 +140,11 @@ public function filterBy($pages, $variable, $value) */ public function sortByTitle($array) { - $callback = function ($a, $b) { - if (!isset($a['title'])) { - return 1; - } - if (!isset($b['title'])) { - return -1; - } - if ($a['title'] == $b['title']) { - return 0; - } - - return ($a['title'] > $b['title']) ? -1 : 1; - }; - if ($array instanceof Collection) { $array = $array->toArray(); } if (is_array($array)) { - usort($array, $callback); + array_multisort(array_keys($array), SORT_NATURAL | SORT_FLAG_CASE, $array); } return $array; diff --git a/src/Step/ConvertPages.php b/src/Step/ConvertPages.php index 1a3c706..096e73e 100644 --- a/src/Step/ConvertPages.php +++ b/src/Step/ConvertPages.php @@ -76,15 +76,17 @@ public function process() public function convertPage(Page $page, $format = 'yaml') { // converts frontmatter - try { - $variables = Converter::convertFrontmatter($page->getFrontmatter(), $format); - } catch (Exception $e) { - $message = sprintf("Unable to convert frontmatter of '%s': %s", $page->getId(), $e->getMessage()); - call_user_func_array($this->phpoole->getMessageCb(), ['CONVERT_ERROR', $message]); + if ($page->getFrontmatter()) { + try { + $variables = Converter::convertFrontmatter($page->getFrontmatter(), $format); + } catch (Exception $e) { + $message = sprintf("Unable to convert frontmatter of '%s': %s", $page->getId(), $e->getMessage()); + call_user_func_array($this->phpoole->getMessageCb(), ['CONVERT_ERROR', $message]); - return false; + return false; + } + $page->setVariables($variables); } - $page->setVariables($variables); // converts body $html = Converter::convertBody($page->getBody()); diff --git a/tests/fixtures/website/content/Blog/2017-10-20-another-post-with-prefix.md b/tests/fixtures/website/content/Blog/2017-10-20-another-post-with-prefix.md index e69de29..bbfb0fc 100644 --- a/tests/fixtures/website/content/Blog/2017-10-20-another-post-with-prefix.md +++ b/tests/fixtures/website/content/Blog/2017-10-20-another-post-with-prefix.md @@ -0,0 +1 @@ +Post with `2017-10-20` prefix. diff --git a/tests/fixtures/website/layouts/section/pages.html.twig b/tests/fixtures/website/layouts/section/pages.html.twig new file mode 100644 index 0000000..e6678e3 --- /dev/null +++ b/tests/fixtures/website/layouts/section/pages.html.twig @@ -0,0 +1,17 @@ +{% extends '_default/list.html.twig' %} + +{% block content %} +
+{% for post in site.pages|filterBy('section', page.pathname)|sortByTitle %} +
+{% block post %} +

+ {{ post.title|e }} +

+ + {{ post.content|excerptHtml }} +{% endblock %} +
+{% endfor %} +
+{% endblock %} diff --git a/tests/fixtures/website/themes/a-theme/layouts/index.html.twig b/tests/fixtures/website/themes/a-theme/layouts/index.html.twig index 87371f6..da2cebe 100644 --- a/tests/fixtures/website/themes/a-theme/layouts/index.html.twig +++ b/tests/fixtures/website/themes/a-theme/layouts/index.html.twig @@ -4,21 +4,21 @@ All pages: Blog pages: Virtuals pages: