Skip to content
This repository has been archived by the owner on Dec 27, 2018. It is now read-only.

Commit

Permalink
Fix page prefix logic and sort. (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed Nov 15, 2018
1 parent 003865c commit 0808945
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 28 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.10.1
2.10.2
13 changes: 11 additions & 2 deletions src/Collection/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 1 addition & 15 deletions src/Renderer/Twig/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 9 additions & 7 deletions src/Step/ConvertPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Post with `2017-10-20` prefix.
17 changes: 17 additions & 0 deletions tests/fixtures/website/layouts/section/pages.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends '_default/list.html.twig' %}

{% block content %}
<div class="posts">
{% for post in site.pages|filterBy('section', page.pathname)|sortByTitle %}
<div class="post">
{% block post %}
<h1 class="post-title">
<a href="{{ url(post) }}">{{ post.title|e }}</a>
</h1>
<span class="post-date">{{ post.date|date("j M Y") }}</span>
{{ post.content|excerptHtml }}
{% endblock %}
</div>
{% endfor %}
</div>
{% endblock %}
6 changes: 3 additions & 3 deletions tests/fixtures/website/themes/a-theme/layouts/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

All pages:
<ul>
{% for item in site.pages|sortByDate %}
{% for item in site.pages|sortByTitle %}
{% include 'includes/page_li.html.twig' %}
{% endfor %}
</ul>

Blog pages:
<ul>
{% for item in site.pages|filterBy('section', 'blog') %}
{% for item in site.pages|filterBy('section', 'blog')|sortByTitle %}
{% include 'includes/page_li.html.twig' %}
{% endfor %}
</ul>

Virtuals pages:
<ul>
{% for item in site.pages|filterBy('virtual', true) %}
{% for item in site.pages|filterBy('virtual', true)|sortByTitle %}
{% include 'includes/page_li.html.twig' %}
{% endfor %}
</ul>
Expand Down

0 comments on commit 0808945

Please sign in to comment.