Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 90 additions & 91 deletions composer.lock

Large diffs are not rendered by default.

89 changes: 44 additions & 45 deletions src/Collection/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
throw new RuntimeException('Create a page with a string ID or a SplFileInfo.');
}

// default properties
// set default properties and variables
$this->setVirtual(true);
$this->setType(Type::PAGE->value);
$this->setVariables([
Expand All @@ -96,6 +96,7 @@
'content_template' => 'page.content.twig',
]);

// if file, create ID from its pathname
if ($id instanceof SplFileInfo) {
$file = $id;
$this->setFile($file);
Expand Down Expand Up @@ -154,40 +155,38 @@
public function setFile(SplFileInfo $file): self
{
$this->file = $file;
$this->setVirtual(false);

/*
* File path components
*/
$fileRelativePath = str_replace(DIRECTORY_SEPARATOR, '/', $this->file->getRelativePath());
$fileExtension = $this->file->getExtension();
$fileName = $this->file->getBasename('.' . $fileExtension);
$relativePath = str_replace(DIRECTORY_SEPARATOR, '/', $this->file->getRelativePath());
$filename = $this->file->getFilenameWithoutExtension();
// renames "README" to "index"
$fileName = strtolower($fileName) == 'readme' ? 'index' : $fileName;
// case of "index" = home page
if (empty($this->file->getRelativePath()) && PrefixSuffix::sub($fileName) == 'index') {
$this->setType(Type::HOMEPAGE->value);
}
/*
* Set page properties and variables
*/
$this->setFolder($fileRelativePath);
$this->setSlug($fileName);
$filename = strtolower(PrefixSuffix::sub($filename)) == 'readme' ? 'index' : $filename;

// set properties
$this->setVirtual(false);
$this->setFolder($relativePath);
$this->setSlug($filename);
$this->setPath($this->getFolder() . '/' . $this->getSlug());
// if "index" : type = section or homepage
if (PrefixSuffix::sub($filename) == 'index') {
// section by default
$this->setType(Type::SECTION->value);
// homepage
if (empty($this->file->getRelativePath())) {
$this->setType(Type::HOMEPAGE->value);
}
}
// set variables
$this->setVariables([
'title' => PrefixSuffix::sub($fileName),
'title' => PrefixSuffix::sub($filename),
'date' => (new \DateTime())->setTimestamp($this->file->getMTime()),
'updated' => (new \DateTime())->setTimestamp($this->file->getMTime()),
'filepath' => $this->file->getRelativePathname(),
]);
/*
* Set specific variables
*/
// is file has a prefix?
if (PrefixSuffix::hasPrefix($fileName)) {
$prefix = PrefixSuffix::getPrefix($fileName);
// if prefix : set weight or date
if (PrefixSuffix::hasPrefix($filename)) {
$prefix = PrefixSuffix::getPrefix($filename);
if ($prefix !== null) {
// prefix is an integer: used for sorting
// prefix is an integer: weight (used for sorting)
if (is_numeric($prefix)) {
$this->setVariable('weight', (int) $prefix);
}
Expand All @@ -197,11 +196,11 @@
}
}
}
// is file has a language suffix?
if (PrefixSuffix::hasSuffix($fileName)) {
$this->setVariable('language', PrefixSuffix::getSuffix($fileName));
// if suffix : set language
if (PrefixSuffix::hasSuffix($filename)) {
$this->setVariable('language', PrefixSuffix::getSuffix($filename));
}
// set reference between page's translations, even if it exist in only one language
// set reference between page's translations (even if it exist in only one language)
$this->setVariable('langref', $this->getPath());

return $this;
Expand Down Expand Up @@ -346,29 +345,29 @@
{
$path = trim($path, '/');

// case of homepage
// homepage : path is empty
if ($path == 'index') {
$this->path = '';

return $this;
}

// case of custom sections' index (ie: section/index.md -> section)
// section/index : path = section
if (substr($path, -6) == '/index') {
$path = substr($path, 0, \strlen($path) - 6);
}
$this->path = $path;

$lastslash = strrpos($this->path, '/');

// case of root/top-level pages
// top-level pages : slug = path
if ($lastslash === false) {
$this->slug = $this->path;

return $this;
}

// case of sections' pages: set section
// set section
if (!$this->virtual && $this->getSection() === null) {
$this->section = explode('/', $this->path)[0];
}
Expand Down Expand Up @@ -680,24 +679,24 @@
private static function createIdFromFile(SplFileInfo $file): string
{
$relativePath = self::slugify(str_replace(DIRECTORY_SEPARATOR, '/', $file->getRelativePath()));
$basename = self::slugify(PrefixSuffix::subPrefix($file->getBasename('.' . $file->getExtension())));
$filename = self::slugify(PrefixSuffix::subPrefix($file->getFilenameWithoutExtension()));
// if file is "README.md", ID is "index"
$basename = strtolower($basename) == 'readme' ? 'index' : $basename;
// if file is section's index: "section/index.md", ID is "section"
if (!empty($relativePath) && PrefixSuffix::sub($basename) == 'index') {
// case of a localized section's index: "section/index.fr.md", ID is "fr/section"
if (PrefixSuffix::hasSuffix($basename)) {
return PrefixSuffix::getSuffix($basename) . '/' . $relativePath;
$filename = strtolower($filename) == 'readme' ? 'index' : $filename;
// if file is section's index (ie: if file is "section/index.md", ID is "section")
if (!empty($relativePath) && PrefixSuffix::sub($filename) == 'index') {
// localized section (ie: if file is "section/index.fr.md", ID is "fr/section")
if (PrefixSuffix::hasSuffix($filename)) {
return PrefixSuffix::getSuffix($filename) . '/' . $relativePath;
}

return $relativePath;
}
// localized page
if (PrefixSuffix::hasSuffix($basename)) {
return trim(Util::joinPath(PrefixSuffix::getSuffix($basename), $relativePath, PrefixSuffix::sub($basename)), '/');
// localized page (ie: if file is "page.fr.md", ID is "fr/page")
if (PrefixSuffix::hasSuffix($filename)) {
return trim(Util::joinPath(PrefixSuffix::getSuffix($filename), $relativePath, PrefixSuffix::sub($filename)), '/');

Check notice on line 696 in src/Collection/Page/Page.php

View check run for this annotation

Scrutinizer / Inspection

src/Collection/Page/Page.php#L696

It seems like ``Cecil\Collection\Page\PrefixSuffix::getSuffix($filename)`` can also be of type ``null``; however, parameter ``$path`` of ``Cecil\Util::joinPath()`` does only seem to accept ``string``, maybe add an additional type check?
}

return trim(Util::joinPath($relativePath, $basename), '/');
return trim(Util::joinPath($relativePath, $filename), '/');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* This class is responsible for generating sections from the pages in the builder.
* It identifies sections based on the 'section' variable in each page, and
* creates a new page for each section. The generated pages are added to the
* collection of generated pages. It also handles sorting of subpages and
* collection of generated pages. It also handles sorting of sub-pages and
* adding navigation links (next and previous) to the section pages.
*/
class Section extends AbstractGenerator implements GeneratorInterface
Expand Down
13 changes: 10 additions & 3 deletions src/Step/Pages/Load.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,22 @@ public function process(): void
->files()
->in($this->config->getPagesPath())
->sort(function (SplFileInfo $a, SplFileInfo $b): int {
// root pages first
if (empty($a->getRelativePath()) && !empty($b->getRelativePath())) {
return -1;
}
if (empty($b->getRelativePath()) && !empty($a->getRelativePath())) {
return 1;
}
// section's index first
if ($a->getRelativePath() == $b->getRelativePath() && $a->getBasename('.' . $a->getExtension()) == 'index') {
if ($a->getRelativePath() == $b->getRelativePath() && \in_array(strtolower($a->getFilenameWithoutExtension()), ['index', 'readme'])) {
return -1;
}
if ($b->getRelativePath() == $a->getRelativePath() && $b->getBasename('.' . $b->getExtension()) == 'index') {
if ($b->getRelativePath() == $a->getRelativePath() && \in_array(strtolower($b->getFilenameWithoutExtension()), ['index', 'readme'])) {
return 1;
}
// sort by name
return strnatcasecmp($a->getRealPath(), $b->getRealPath());
return strnatcasecmp($a->getRelativePath(), $b->getRelativePath());
});
// load only one page?
if ($this->page) {
Expand Down
23 changes: 23 additions & 0 deletions tests/fixtures/website/layouts/page.html.twig
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
{% extends '_default/page.html.twig' %}

{% block content %}

{{ parent() }}

{#- prev/next page ~#}
{%- if page.prev is defined or page.next is defined ~%}
<p>Navigation between pages of a section:
{%- if page.prev is defined ~%}
<a href="{{ url(page.prev.path) }}" title="Previous">
← {{ page.prev.title }}
</a>
{%- endif -%}
&nbsp;|&nbsp;
{%- if page.next is defined ~%}
<a href="{{ url(page.next.path) }}" title="Next">
{{ page.next.title }} →
</a>
{%- endif ~%}
</p>
{%- endif ~%}

{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Sub section
---

Home > Section > (folder) > Sub section
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Page in a sub section
---
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends '_default/page.html.twig' %}
{% extends ['page.html.twig', '_default/page.html.twig'] %}

{% block content %}
{{ page.content }}
{{ parent() }}
<quote>Page from 'a-theme > layouts > project > page.html.twig'</quote>
{% if page.categories is defined %}
<ul>
Expand Down
Loading