From 30de27f108210e7740eb1d2c0def2252c7e60e5b Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Sun, 28 May 2023 13:55:25 +0200 Subject: [PATCH 01/39] test: enhances fixtures --- .../website/themes/a-theme/config.yml | 5 +-- .../themes/a-theme/layouts/index.html.twig | 15 ++++--- .../layouts/partials/pages_as_table.html.twig | 41 +++++++++++++++++++ 3 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 tests/fixtures/website/themes/a-theme/layouts/partials/pages_as_table.html.twig diff --git a/tests/fixtures/website/themes/a-theme/config.yml b/tests/fixtures/website/themes/a-theme/config.yml index 719341503..627acc541 100644 --- a/tests/fixtures/website/themes/a-theme/config.yml +++ b/tests/fixtures/website/themes/a-theme/config.yml @@ -1,3 +1,2 @@ -site: - title: Imported config - foo: bar +title: Imported config +foo: bar 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 bace94f2b..36bae5211 100644 --- a/tests/fixtures/website/themes/a-theme/layouts/index.html.twig +++ b/tests/fixtures/website/themes/a-theme/layouts/index.html.twig @@ -1,14 +1,17 @@ {% extends '_default/page.html.twig' %} {% block content %} - +{# All pages: +#} +{% include 'partials/pages_as_table.html.twig' %} +{# {% if site.foo is defined %} Imported config (from theme(s)):

Variable `foo`=`{{ site.foo }}`

@@ -29,5 +32,5 @@ Alternates: {{ dump(page.alternates) }} {% endif %} - -{% endblock %} +#} +{% endblock %} \ No newline at end of file diff --git a/tests/fixtures/website/themes/a-theme/layouts/partials/pages_as_table.html.twig b/tests/fixtures/website/themes/a-theme/layouts/partials/pages_as_table.html.twig new file mode 100644 index 000000000..6e4984679 --- /dev/null +++ b/tests/fixtures/website/themes/a-theme/layouts/partials/pages_as_table.html.twig @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + {%- for item in site.pages ~%} + + + + + + + + + + + + + + {%- endfor ~%} +
All pages
titlefilepathidtypevirtualsectionpathfolderslugmenulang
+ {{ item.title }}
+ {{ item.date|date("d/m/Y") }}
+ {{ item.updated|date("d/m/Y") }} +
{{ item.filepath }}{{ item.id }}{{ item.type|upper }}{{ item.virtual ? 'virtual' : 'file' }}{{ item.section ?? 'root' }}{{ item.path }}{{ item.folder }}{{ item.slug }} + {%- if item.menu is defined %} + {{- dump(item.menu) ~}} + {%- endif ~%} + + {{- item.language|default ~}} +
\ No newline at end of file From 3c678cff11625a18861ed27639b50c60ff61c2d8 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Sun, 28 May 2023 13:55:44 +0200 Subject: [PATCH 02/39] test: add fixtures --- .../website/pages/Section/Sub section/Sub sub section/index.md | 0 tests/fixtures/website/pages/Section/Sub section/index.md | 3 +++ 2 files changed, 3 insertions(+) create mode 100644 tests/fixtures/website/pages/Section/Sub section/Sub sub section/index.md create mode 100644 tests/fixtures/website/pages/Section/Sub section/index.md diff --git a/tests/fixtures/website/pages/Section/Sub section/Sub sub section/index.md b/tests/fixtures/website/pages/Section/Sub section/Sub sub section/index.md new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/website/pages/Section/Sub section/index.md b/tests/fixtures/website/pages/Section/Sub section/index.md new file mode 100644 index 000000000..fc7b05989 --- /dev/null +++ b/tests/fixtures/website/pages/Section/Sub section/index.md @@ -0,0 +1,3 @@ +--- +title: Sub section +--- From 7209c31c8283249b903369836f2b788f2aa77f9b Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Sun, 28 May 2023 13:56:03 +0200 Subject: [PATCH 03/39] feat: nested sections support --- src/Collection/Page/Page.php | 37 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/Collection/Page/Page.php b/src/Collection/Page/Page.php index dbaf7bd3c..d57795e6d 100644 --- a/src/Collection/Page/Page.php +++ b/src/Collection/Page/Page.php @@ -26,7 +26,7 @@ class Page extends Item { public const SLUGIFY_PATTERN = '/(^\/|[^._a-z0-9\/]|-)+/'; // should be '/^\/|[^_a-z0-9\/]+/' - /** @var bool True if page is not created from a Markdown file. */ + /** @var bool True if page is not created from a file. */ protected $virtual; /** @var SplFileInfo */ @@ -41,7 +41,7 @@ class Page extends Item /** @var string */ protected $slug; - /** @var string folder + slug. */ + /** @var string path = folder + slug. */ protected $path; /** @var string */ @@ -56,13 +56,13 @@ class Page extends Item /** @var string Body before conversion. */ protected $body; - /** @var string Body after Markdown conversion. */ + /** @var string Body after conversion. */ protected $html; - /** @var array Output by format */ + /** @var array Output, by format */ protected $rendered = []; - /** @var \Cecil\Collection\Page\Collection Subpages of a section */ + /** @var Collection Subpages of a list page */ protected $subPages; /** @var array */ @@ -79,7 +79,6 @@ public function __construct(string $id) parent::__construct($id); $this->setVirtual(true); $this->setType(Type::PAGE); - // default variables $this->setVariables([ 'title' => 'Page Title', 'date' => new \DateTime(), @@ -156,20 +155,12 @@ public function setFile(SplFileInfo $file): self $fileRelativePath = str_replace(DIRECTORY_SEPARATOR, '/', $this->file->getRelativePath()); $fileExtension = $this->file->getExtension(); $fileName = $this->file->getBasename('.' . $fileExtension); - // case of "README" -> "index" - $fileName = (string) str_ireplace('readme', 'index', $fileName); - // case of "index" = home page - if (empty($this->file->getRelativePath()) && PrefixSuffix::sub($fileName) == 'index') { - $this->setType(Type::HOMEPAGE); - } - /* - * Set protected variables - */ + $fileName = (string) str_ireplace('readme', 'index', $fileName); // converts "README" to "index" $this->setFolder($fileRelativePath); // ie: "blog" $this->setSlug($fileName); // ie: "post-1" $this->setPath($this->getFolder() . '/' . $this->getSlug()); // ie: "blog/post-1" /* - * Set default variables + * Update default variables */ $this->setVariables([ 'title' => PrefixSuffix::sub($fileName), @@ -177,9 +168,15 @@ public function setFile(SplFileInfo $file): self 'updated' => (new \DateTime())->setTimestamp($this->file->getMTime()), 'filepath' => $this->file->getRelativePathname(), ]); - /* - * Set specific variables - */ + // is the home page? + if (PrefixSuffix::sub($fileName) == 'index' && empty($this->file->getRelativePath())) { + $this->setType(Type::HOMEPAGE); + } + // is a section? + if (PrefixSuffix::sub($fileName) == 'index' && !empty($this->getFolder())) { + $this->setType(Type::SECTION); + $this->setVariable('title', explode('/', $fileRelativePath)[count(explode('/', $fileRelativePath)) - 1]); + } // is file has a prefix? if (PrefixSuffix::hasPrefix($fileName)) { $prefix = PrefixSuffix::getPrefix($fileName); @@ -324,6 +321,8 @@ public function getSlug(): string */ public function setPath(string $path): self { + $path = trim($path, '/'); + // case of homepage if ($path == 'index') { $this->path = ''; From 33f466a7acd513bdd2461aabe25146b8de4b9b8e Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Tue, 30 May 2023 00:05:38 +0200 Subject: [PATCH 04/39] Update Load.php --- src/Step/Pages/Load.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Step/Pages/Load.php b/src/Step/Pages/Load.php index bd0867af5..8f0a715e7 100644 --- a/src/Step/Pages/Load.php +++ b/src/Step/Pages/Load.php @@ -61,7 +61,18 @@ public function process(): void $content = Finder::create() ->files() ->in($this->config->getPagesPath()) - ->sortByName(true); + //->sortByName(true) + ->sort(function (\SplFileInfo $a, \SplFileInfo $b) { + // section's index first + if ($a->getBasename('.' . $a->getExtension()) == 'index' && $b->getBasename('.' . $b->getExtension()) != 'index') { + return -1; + } + if ($b->getBasename('.' . $b->getExtension()) == 'index' && $a->getBasename('.' . $a->getExtension()) != 'index') { + return 1; + } + + return strnatcmp($a->getRealPath(), $b->getRealPath()); + }); // load only one page? if ($this->page) { // is the page path starts with the `pages.dir` configuration option? From bca272eb249f93a65f5079fb48d7cdd026dd9aaf Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Tue, 30 May 2023 00:05:44 +0200 Subject: [PATCH 05/39] Update Create.php --- src/Step/Pages/Create.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Step/Pages/Create.php b/src/Step/Pages/Create.php index 432350fc1..c09144792 100644 --- a/src/Step/Pages/Create.php +++ b/src/Step/Pages/Create.php @@ -108,7 +108,7 @@ public function process(): void $this->builder->getPages()->add($page); } - $message = sprintf('Page "%s" created', $page->getId()); + $message = sprintf('Page "%s" (%s) created', $page->getId(), $page->getType()); $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]); } } From 30b38531021f814990b6c6f3313677cf7280edb0 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Tue, 30 May 2023 00:05:51 +0200 Subject: [PATCH 06/39] Update Pagination.php --- src/Generator/Pagination.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Generator/Pagination.php b/src/Generator/Pagination.php index a22ef779f..233e8f98c 100644 --- a/src/Generator/Pagination.php +++ b/src/Generator/Pagination.php @@ -38,10 +38,14 @@ public function generate(): void }); /** @var Page $page */ foreach ($filteredPages as $page) { + // if no sub-pages: by-pass + if ($page->getPages() === null) { + continue; + } $pages = $page->getPages()->filter(function (Page $page) { - return $page->getVariable('published'); + return $page->getType() == Type::PAGE && $page->getVariable('published'); }); - // if no sub-pages: by-pass + // if no published sub-pages: by-pass if ($pages === null) { continue; } From 6be1b24fcd074f00b845eeac6311a6365951e6dc Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Tue, 30 May 2023 00:05:57 +0200 Subject: [PATCH 07/39] Update Page.php --- src/Collection/Page/Page.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Collection/Page/Page.php b/src/Collection/Page/Page.php index d57795e6d..12ef85645 100644 --- a/src/Collection/Page/Page.php +++ b/src/Collection/Page/Page.php @@ -168,14 +168,15 @@ public function setFile(SplFileInfo $file): self 'updated' => (new \DateTime())->setTimestamp($this->file->getMTime()), 'filepath' => $this->file->getRelativePathname(), ]); - // is the home page? - if (PrefixSuffix::sub($fileName) == 'index' && empty($this->file->getRelativePath())) { - $this->setType(Type::HOMEPAGE); - } // is a section? - if (PrefixSuffix::sub($fileName) == 'index' && !empty($this->getFolder())) { + if (PrefixSuffix::sub($fileName) == 'index') { $this->setType(Type::SECTION); - $this->setVariable('title', explode('/', $fileRelativePath)[count(explode('/', $fileRelativePath)) - 1]); + $this->setVariable('title', ucfirst(explode('/', $fileRelativePath)[count(explode('/', $fileRelativePath)) - 1])); + // is the home page? + if (empty($this->getFolder())) { + $this->setType(Type::HOMEPAGE); + $this->setVariable('title', 'Homepage'); + } } // is file has a prefix? if (PrefixSuffix::hasPrefix($fileName)) { From 96df21ccfee546f7a11e1b0f483e1f5ca6861f8b Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Tue, 30 May 2023 15:14:22 +0200 Subject: [PATCH 08/39] WIP --- src/Collection/Page/Page.php | 23 +++++++++++++++++++++++ src/Generator/Section.php | 16 +++++++++++++--- src/Step/Pages/Create.php | 13 ++++++++++--- src/Step/Pages/Load.php | 10 +++++----- 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/Collection/Page/Page.php b/src/Collection/Page/Page.php index 12ef85645..d97f43135 100644 --- a/src/Collection/Page/Page.php +++ b/src/Collection/Page/Page.php @@ -71,6 +71,9 @@ class Page extends Item /** @var \Cecil\Collection\Taxonomy\Vocabulary Terms of a vocabulary */ protected $terms; + /** @var self */ + protected $parent; + /** @var Slugify */ private static $slugifier; @@ -662,4 +665,24 @@ public function setId(string $id): self { return parent::setId($id); } + + /** + * Set parent page. + */ + public function setParent(self $page): self + { + if ($page->getId() != $this->getId()) { + $this->parent = $page; + } + + return $this; + } + + /** + * Returns parent page if exists. + */ + public function getParent(): ?self + { + return $this->parent; + } } diff --git a/src/Generator/Section.php b/src/Generator/Section.php index 27a8c8444..39653b6d5 100644 --- a/src/Generator/Section.php +++ b/src/Generator/Section.php @@ -30,15 +30,25 @@ public function generate(): void { $sections = []; - // identifying sections + // identifying sections from all pages + /** @var Page $page */ foreach ($this->builder->getPages() as $page) { - /** @var Page $page */ + // top level (root) sections if ($page->getSection()) { - // do not add not published and not excluded pages to its section + // do not add "not published" and "not excluded" pages to its section if ($page->getVariable('published') !== true || $page->getVariable('exclude')) { continue; } $sections[$page->getSection()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page; + + // DEBUG + if ($page->getParent() !== null) { + dump([ + 'pid' => $page->getId(), + 'ppid' => $page->getParent()->getId() + ]); + //$sections[$page->getParent()->getId()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page; + } } } diff --git a/src/Step/Pages/Create.php b/src/Step/Pages/Create.php index c09144792..d55463bbb 100644 --- a/src/Step/Pages/Create.php +++ b/src/Step/Pages/Create.php @@ -53,12 +53,19 @@ public function process(): void $total = \count($this->builder->getPagesFiles()); $count = 0; - /** @var \Symfony\Component\Finder\SplFileInfo $file */ foreach ($this->builder->getPagesFiles() as $file) { $count++; - /** @var Page $page */ + // create a page from its (Markdown) file $page = new Page(Page::createIdFromFile($file)); - $page->setFile($file)->parse(); + $page->setFile($file); + // parse frontmatter and body + $page->parse(); + // has a parent page? + if (count(explode('/', $page->getFolder())) > 1) { + if ($this->builder->getPages()->has($page->getFolder())) { + $page->setParent($this->builder->getPages()->get($page->getFolder())); + } + } /* * Apply an - optional - custom path to pages of a section. diff --git a/src/Step/Pages/Load.php b/src/Step/Pages/Load.php index 8f0a715e7..b2d0bb058 100644 --- a/src/Step/Pages/Load.php +++ b/src/Step/Pages/Load.php @@ -61,17 +61,17 @@ public function process(): void $content = Finder::create() ->files() ->in($this->config->getPagesPath()) - //->sortByName(true) - ->sort(function (\SplFileInfo $a, \SplFileInfo $b) { + //->sortByName(true); + ->sort(function (\Symfony\Component\Finder\SplFileInfo $a, \Symfony\Component\Finder\SplFileInfo $b) { // section's index first - if ($a->getBasename('.' . $a->getExtension()) == 'index' && $b->getBasename('.' . $b->getExtension()) != 'index') { + if ($a->getRelativePath() == $b->getRelativePath() && $a->getBasename('.' . $a->getExtension()) == 'index') { return -1; } - if ($b->getBasename('.' . $b->getExtension()) == 'index' && $a->getBasename('.' . $a->getExtension()) != 'index') { + if ($b->getRelativePath() == $a->getRelativePath() && $b->getBasename('.' . $b->getExtension()) == 'index') { return 1; } - return strnatcmp($a->getRealPath(), $b->getRealPath()); + return strnatcasecmp($a->getRealPath(), $b->getRealPath()); }); // load only one page? if ($this->page) { From af1a4040382839c36a58c283f289200b55b0c0e4 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Wed, 31 May 2023 00:09:27 +0200 Subject: [PATCH 09/39] Update Section.php --- src/Generator/Section.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Generator/Section.php b/src/Generator/Section.php index 39653b6d5..c5b8d3386 100644 --- a/src/Generator/Section.php +++ b/src/Generator/Section.php @@ -40,14 +40,9 @@ public function generate(): void continue; } $sections[$page->getSection()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page; - - // DEBUG + // nested sections if ($page->getParent() !== null) { - dump([ - 'pid' => $page->getId(), - 'ppid' => $page->getParent()->getId() - ]); - //$sections[$page->getParent()->getId()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page; + $sections[$page->getParent()->getId()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page; } } } @@ -101,7 +96,12 @@ public function generate(): void if (!$page->getVariable('menu')) { $page->setVariable('menu', ['main' => ['weight' => $menuWeight]]); } - $this->generatedPages->add($page); + + try { + $this->generatedPages->add($page); + } catch (\DomainException $e) { + $this->generatedPages->replace($page->getId(), $page); + } } $menuWeight += 10; } From c50b4c8ef0c13ef52f45bf4705faada8993a7cb0 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Wed, 31 May 2023 01:23:16 +0200 Subject: [PATCH 10/39] Update Pagination.php --- src/Generator/Pagination.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Generator/Pagination.php b/src/Generator/Pagination.php index 233e8f98c..ee53d942f 100644 --- a/src/Generator/Pagination.php +++ b/src/Generator/Pagination.php @@ -16,7 +16,6 @@ use Cecil\Collection\Page\Collection as PagesCollection; use Cecil\Collection\Page\Page; use Cecil\Collection\Page\Type; -use Cecil\Exception\RuntimeException; /** * Class Generator\Pagination. From d99b6712f38dd07b54eeb7c9310690bf1e05165f Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Wed, 31 May 2023 22:16:52 +0200 Subject: [PATCH 11/39] chore: update deps --- composer.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.lock b/composer.lock index e2602c48e..7a6fc6af5 100644 --- a/composer.lock +++ b/composer.lock @@ -6764,16 +6764,16 @@ }, { "name": "phpdocumentor/type-resolver", - "version": "1.7.1", + "version": "1.7.2", "source": { "type": "git", "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "dfc078e8af9c99210337325ff5aa152872c98714" + "reference": "b2fe4d22a5426f38e014855322200b97b5362c0d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/dfc078e8af9c99210337325ff5aa152872c98714", - "reference": "dfc078e8af9c99210337325ff5aa152872c98714", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/b2fe4d22a5426f38e014855322200b97b5362c0d", + "reference": "b2fe4d22a5426f38e014855322200b97b5362c0d", "shasum": "" }, "require": { @@ -6816,9 +6816,9 @@ "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", "support": { "issues": "https://github.com/phpDocumentor/TypeResolver/issues", - "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.7.1" + "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.7.2" }, - "time": "2023-03-27T19:02:04+00:00" + "time": "2023-05-30T18:13:47+00:00" }, { "name": "phpmd/phpmd", From c5be500d8e21a3a19ac8c9f385fb121d51a3a533 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Wed, 31 May 2023 22:18:15 +0200 Subject: [PATCH 12/39] Update Page.php --- src/Collection/Page/Page.php | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Collection/Page/Page.php b/src/Collection/Page/Page.php index d97f43135..4d4d3caad 100644 --- a/src/Collection/Page/Page.php +++ b/src/Collection/Page/Page.php @@ -62,16 +62,16 @@ class Page extends Item /** @var array Output, by format */ protected $rendered = []; - /** @var Collection Subpages of a list page */ + /** @var Collection Subpages of a list page. */ protected $subPages; /** @var array */ protected $paginator = []; - /** @var \Cecil\Collection\Taxonomy\Vocabulary Terms of a vocabulary */ + /** @var \Cecil\Collection\Taxonomy\Vocabulary Terms of a vocabulary. */ protected $terms; - /** @var self */ + /** @var self Parent page of a PAGE page or a SECTION page */ protected $parent; /** @var Slugify */ @@ -106,7 +106,7 @@ public static function slugify(string $path): string } /** - * Creates the ID from the file path. + * Creates the ID from the file (path). */ public static function createIdFromFile(SplFileInfo $file): string { @@ -149,8 +149,8 @@ public function getIdWithoutLang(): string */ public function setFile(SplFileInfo $file): self { - $this->setVirtual(false); $this->file = $file; + $this->setVirtual(false); /* * File path components @@ -163,8 +163,11 @@ public function setFile(SplFileInfo $file): self $this->setSlug($fileName); // ie: "post-1" $this->setPath($this->getFolder() . '/' . $this->getSlug()); // ie: "blog/post-1" /* - * Update default variables + * Set page properties and variables */ + $this->setFolder($fileRelativePath); + $this->setSlug($fileName); + $this->setPath($this->getFolder() . '/' . $this->getSlug()); $this->setVariables([ 'title' => PrefixSuffix::sub($fileName), 'date' => (new \DateTime())->setTimestamp($this->file->getMTime()), @@ -174,7 +177,7 @@ public function setFile(SplFileInfo $file): self // is a section? if (PrefixSuffix::sub($fileName) == 'index') { $this->setType(Type::SECTION); - $this->setVariable('title', ucfirst(explode('/', $fileRelativePath)[count(explode('/', $fileRelativePath)) - 1])); + $this->setVariable('title', ucfirst(explode('/', $fileRelativePath)[\count(explode('/', $fileRelativePath)) - 1])); // is the home page? if (empty($this->getFolder())) { $this->setType(Type::HOMEPAGE); @@ -334,23 +337,26 @@ public function setPath(string $path): self return $this; } - // case of custom sections' index (ie: content/section/index.md) + // case of custom sections' index (ie: file: section/index.md -> path: section) if (substr($path, -6) == '/index') { $path = substr($path, 0, \strlen($path) - 6); } - $this->path = $path; - // case of root pages + $this->path = $path; $lastslash = strrpos($this->path, '/'); + + // case of root/top-level pages if ($lastslash === false) { $this->slug = $this->path; return $this; } + // case of sections' pages: set section if (!$this->virtual && $this->getSection() === null) { $this->section = explode('/', $this->path)[0]; } + // set/update folder and slug $this->folder = substr($this->path, 0, $lastslash); $this->slug = substr($this->path, -(\strlen($this->path) - $lastslash - 1)); From 8f2194a5e87cb62f9fe0ccdca2e6ef164218bc64 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Wed, 31 May 2023 22:19:16 +0200 Subject: [PATCH 13/39] Update Create.php --- src/Step/Pages/Create.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Step/Pages/Create.php b/src/Step/Pages/Create.php index d55463bbb..c77e1aa7f 100644 --- a/src/Step/Pages/Create.php +++ b/src/Step/Pages/Create.php @@ -61,7 +61,7 @@ public function process(): void // parse frontmatter and body $page->parse(); // has a parent page? - if (count(explode('/', $page->getFolder())) > 1) { + if (\count(explode('/', $page->getFolder())) > 1) { if ($this->builder->getPages()->has($page->getFolder())) { $page->setParent($this->builder->getPages()->get($page->getFolder())); } From 497226eb7f45d3d7c23a00f6766a7cd0f10c16ba Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Wed, 31 May 2023 22:19:58 +0200 Subject: [PATCH 14/39] Update Page.php --- src/Collection/Page/Page.php | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Collection/Page/Page.php b/src/Collection/Page/Page.php index 4d4d3caad..92973f9eb 100644 --- a/src/Collection/Page/Page.php +++ b/src/Collection/Page/Page.php @@ -647,23 +647,6 @@ public function getFmVariables(): array return $this->fmVariables; } - /** - * Cast "boolean" string (or array of strings) to boolean. - * - * @param mixed $value Value to filter - * - * @return bool|mixed - * - * @see strToBool() - */ - private function filterBool(&$value) - { - \Cecil\Util\Str::strToBool($value); - if (\is_array($value)) { - array_walk_recursive($value, '\Cecil\Util\Str::strToBool'); - } - } - /** * {@inheritdoc} */ @@ -691,4 +674,21 @@ public function getParent(): ?self { return $this->parent; } + + /** + * Cast "boolean" string (or array of strings) to boolean. + * + * @param mixed $value Value to filter + * + * @return bool|mixed + * + * @see strToBool() + */ + private function filterBool(&$value) + { + \Cecil\Util\Str::strToBool($value); + if (\is_array($value)) { + array_walk_recursive($value, '\Cecil\Util\Str::strToBool'); + } + } } From 2d40b2fc0f7777c7d05cb6f2ebe5e76576d11605 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Wed, 31 May 2023 22:20:31 +0200 Subject: [PATCH 15/39] WIP --- src/Collection/Page/Page.php | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/Collection/Page/Page.php b/src/Collection/Page/Page.php index 92973f9eb..ecccec7cd 100644 --- a/src/Collection/Page/Page.php +++ b/src/Collection/Page/Page.php @@ -110,7 +110,14 @@ public static function slugify(string $path): string */ public static function createIdFromFile(SplFileInfo $file): string { - $relativePath = self::slugify(str_replace(DIRECTORY_SEPARATOR, '/', $file->getRelativePath())); + $fileComponents = self::getFileComponents($file); + + $fileComponents['path']; + $fileComponents['name']; + $fileComponents['ext']; + + + $relativePath = self::slugify($fileComponents['path']); $basename = self::slugify(PrefixSuffix::subPrefix($file->getBasename('.' . $file->getExtension()))); // if file is "README.md", ID is "index" $basename = (string) str_ireplace('readme', 'index', $basename); @@ -158,10 +165,9 @@ public function setFile(SplFileInfo $file): self $fileRelativePath = str_replace(DIRECTORY_SEPARATOR, '/', $this->file->getRelativePath()); $fileExtension = $this->file->getExtension(); $fileName = $this->file->getBasename('.' . $fileExtension); - $fileName = (string) str_ireplace('readme', 'index', $fileName); // converts "README" to "index" - $this->setFolder($fileRelativePath); // ie: "blog" - $this->setSlug($fileName); // ie: "post-1" - $this->setPath($this->getFolder() . '/' . $this->getSlug()); // ie: "blog/post-1" + // renames "README" to "index" + $fileName = (string) str_ireplace('readme', 'index', $fileName); + /* * Set page properties and variables */ @@ -691,4 +697,22 @@ private function filterBool(&$value) array_walk_recursive($value, '\Cecil\Util\Str::strToBool'); } } + + /** + * Get file components. + * + * [ + * path => relative path, + * name => name, + * ext => extension, + * ] + */ + private static function getFileComponents(SplFileInfo $file): array + { + return [ + 'path' => str_replace(DIRECTORY_SEPARATOR, '/', $file->getRelativePath()), + 'name' => (string) str_ireplace('readme', 'index', $file->getBasename('.' . $file->getExtension())), + 'ext' => $file->getExtension(), + ]; + } } From e2100c08e9b8a1069d8382656d8d2aa688d61eab Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Mon, 12 Feb 2024 16:18:02 +0100 Subject: [PATCH 16/39] fix: comments in fixture template --- tests/fixtures/website/themes/a-theme/layouts/index.html.twig | 1 + 1 file changed, 1 insertion(+) 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 dac13dd9a..df385860e 100644 --- a/tests/fixtures/website/themes/a-theme/layouts/index.html.twig +++ b/tests/fixtures/website/themes/a-theme/layouts/index.html.twig @@ -18,5 +18,6 @@ Imported config (from theme(s)):

Variable `foo`=`{{ site.foo }}`

{% endif %} +#} {% endblock %} From 5bb2cd1d7958c3b5624fa4371dd65219a81ae380 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Mon, 12 Feb 2024 16:18:33 +0100 Subject: [PATCH 17/39] fix: errors during main branch merging --- src/Collection/Page/Page.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Collection/Page/Page.php b/src/Collection/Page/Page.php index f7df90888..d9f398eac 100644 --- a/src/Collection/Page/Page.php +++ b/src/Collection/Page/Page.php @@ -196,11 +196,11 @@ public function setFile(SplFileInfo $file): self ]); // is a section? if (PrefixSuffix::sub($fileName) == 'index') { - $this->setType(Type::SECTION); + $this->setType(Type::SECTION->value); $this->setVariable('title', ucfirst(explode('/', $fileRelativePath)[\count(explode('/', $fileRelativePath)) - 1])); // is the home page? if (empty($this->getFolder())) { - $this->setType(Type::HOMEPAGE); + $this->setType(Type::HOMEPAGE->value); $this->setVariable('title', 'Homepage'); } } @@ -363,8 +363,9 @@ public function setPath(string $path): self // case of custom sections' index (ie: section/index.md -> section) if (substr($path, -6) == '/index') { - $this->path = substr($path, 0, \strlen($path) - 6); + $path = substr($path, 0, \strlen($path) - 6); } + $this->path = $path; $lastslash = strrpos($this->path, '/'); From 025782bffb909f853d954c58ad24d4afda928399 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Tue, 13 Feb 2024 03:02:24 +0100 Subject: [PATCH 18/39] WIP --- src/Collection/Page/Page.php | 29 ++++++++++++------- src/Generator/Section.php | 2 +- src/Step/Pages/Create.php | 13 +++++---- .../website/layouts/nested-page.html.twig | 16 ++++++++++ .../pages/Others/Sub dir/Nested page.md | 3 ++ .../website/pages/Others/Sub dir/index.md | 3 ++ 6 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 tests/fixtures/website/layouts/nested-page.html.twig create mode 100644 tests/fixtures/website/pages/Others/Sub dir/Nested page.md create mode 100644 tests/fixtures/website/pages/Others/Sub dir/index.md diff --git a/src/Collection/Page/Page.php b/src/Collection/Page/Page.php index d9f398eac..caa02e01e 100644 --- a/src/Collection/Page/Page.php +++ b/src/Collection/Page/Page.php @@ -353,29 +353,22 @@ public function getSlug(): string public function setPath(string $path): self { $path = trim($path, '/'); - // case of homepage if ($path == 'index') { $this->path = ''; - return $this; } - // case of custom sections' index (ie: section/index.md -> 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 if ($lastslash === false) { $this->slug = $this->path; - return $this; } - // case of sections' pages: set section if (!$this->virtual && $this->getSection() === null) { $this->section = explode('/', $this->path)[0]; @@ -383,7 +376,6 @@ public function setPath(string $path): self // set/update folder and slug $this->folder = substr($this->path, 0, $lastslash); $this->slug = substr($this->path, -(\strlen($this->path) - $lastslash - 1)); - return $this; } @@ -695,9 +687,9 @@ public function setId(string $id): self */ public function setParent(self $page): self { - if ($page->getId() != $this->getId()) { - $this->parent = $page; - } + //if ($page->getId() != $this->getId()) { + $this->parent = $page; + //} return $this; } @@ -710,6 +702,21 @@ public function getParent(): ?self return $this->parent; } + /** + * Returns array of ancestors pages. + */ + public function getAncestors(): ?array + { + $parent = $this->getParent(); + $ancestors[] = $parent; + do { + $parent = $parent->getParent(); + $ancestors[] = $parent; + } while ($parent !== null && !empty($parent->getParent())); + + return $ancestors; + } + /** * Cast "boolean" string (or array of strings) to boolean. * diff --git a/src/Generator/Section.php b/src/Generator/Section.php index f8491ef32..6aaad6e42 100644 --- a/src/Generator/Section.php +++ b/src/Generator/Section.php @@ -42,7 +42,7 @@ public function generate(): void $sections[$page->getSection()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page; // nested sections if ($page->getParent() !== null) { - $sections[$page->getParent()->getId()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page; + //$sections[$page->getParent()->getId()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page; } } } diff --git a/src/Step/Pages/Create.php b/src/Step/Pages/Create.php index 004dcf597..ffd136788 100644 --- a/src/Step/Pages/Create.php +++ b/src/Step/Pages/Create.php @@ -60,11 +60,14 @@ public function process(): void $page->setFile($file); // parse frontmatter and body $page->parse(); - // has a parent page? - if (\count(explode('/', $page->getFolder())) > 1) { - if ($this->builder->getPages()->has($page->getFolder())) { - $page->setParent($this->builder->getPages()->get($page->getFolder())); - } + // set parent page + // if section: home + if ($page->getType() == \Cecil\Collection\Page\Type::SECTION->value) { + //$page->setParent($this->builder->getPages()->get('index')); + } + // if subpage: folder + if (!empty($page->getFolder()) && $this->builder->getPages()->has($page->getFolder())) { + $page->setParent($this->builder->getPages()->get($page->getFolder())); } /* diff --git a/tests/fixtures/website/layouts/nested-page.html.twig b/tests/fixtures/website/layouts/nested-page.html.twig new file mode 100644 index 000000000..cdd04628f --- /dev/null +++ b/tests/fixtures/website/layouts/nested-page.html.twig @@ -0,0 +1,16 @@ +{% extends ['page.html.twig', '_default/page.html.twig'] %} + +{% block content %} + +Parent page : {{ d(page.parent) }} + +
+

Ancestors

+ {#{{ page.ancestors|join(' > ') }}#} + Home + {% for item in page.ancestors|reverse %} + > {{ item.title }} + {% endfor %} +
+ +{% endblock %} \ No newline at end of file diff --git a/tests/fixtures/website/pages/Others/Sub dir/Nested page.md b/tests/fixtures/website/pages/Others/Sub dir/Nested page.md new file mode 100644 index 000000000..ab893fef6 --- /dev/null +++ b/tests/fixtures/website/pages/Others/Sub dir/Nested page.md @@ -0,0 +1,3 @@ +--- +layout: nested-page +--- diff --git a/tests/fixtures/website/pages/Others/Sub dir/index.md b/tests/fixtures/website/pages/Others/Sub dir/index.md new file mode 100644 index 000000000..76604628f --- /dev/null +++ b/tests/fixtures/website/pages/Others/Sub dir/index.md @@ -0,0 +1,3 @@ +--- +#layout: nested-page +--- From 6180fc81628c869e1777a290aa63321ff46ec7b6 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Tue, 13 Feb 2024 18:26:09 +0100 Subject: [PATCH 19/39] WIP --- src/Collection/Page/Page.php | 22 +++++++------------ src/Generator/Section.php | 7 ++++++ src/Step/Pages/Create.php | 11 +++++++--- .../website/layouts/nested-page.html.twig | 15 +++++-------- .../Sub dir/Sub sub dir/Deep nested page.md | 3 +++ .../pages/Others/Sub dir/Sub sub dir/index.md | 3 +++ .../website/pages/Others/Sub dir/index.md | 2 +- 7 files changed, 36 insertions(+), 27 deletions(-) create mode 100644 tests/fixtures/website/pages/Others/Sub dir/Sub sub dir/Deep nested page.md create mode 100644 tests/fixtures/website/pages/Others/Sub dir/Sub sub dir/index.md diff --git a/src/Collection/Page/Page.php b/src/Collection/Page/Page.php index caa02e01e..e37c94d1b 100644 --- a/src/Collection/Page/Page.php +++ b/src/Collection/Page/Page.php @@ -122,12 +122,6 @@ public static function slugify(string $path): string public static function createIdFromFile(SplFileInfo $file): string { $fileComponents = self::getFileComponents($file); - - $fileComponents['path']; - $fileComponents['name']; - $fileComponents['ext']; - - $relativePath = self::slugify($fileComponents['path']); $basename = self::slugify(PrefixSuffix::subPrefix($file->getBasename('.' . $file->getExtension()))); // if file is "README.md", ID is "index" @@ -687,9 +681,7 @@ public function setId(string $id): self */ public function setParent(self $page): self { - //if ($page->getId() != $this->getId()) { $this->parent = $page; - //} return $this; } @@ -705,14 +697,16 @@ public function getParent(): ?self /** * Returns array of ancestors pages. */ - public function getAncestors(): ?array + public function getAncestors(): array { - $parent = $this->getParent(); - $ancestors[] = $parent; - do { - $parent = $parent->getParent(); + $ancestors = []; + + if (null !== $parent = $this->getParent()) { $ancestors[] = $parent; - } while ($parent !== null && !empty($parent->getParent())); + while (null !== $parent = $parent->getParent()) { + $ancestors[] = $parent; + }; + } return $ancestors; } diff --git a/src/Generator/Section.php b/src/Generator/Section.php index 6aaad6e42..16cec6d4a 100644 --- a/src/Generator/Section.php +++ b/src/Generator/Section.php @@ -102,6 +102,13 @@ public function generate(): void } catch (\DomainException) { $this->generatedPages->replace($page->getId(), $page); } + // set section as parent page + $subPages->map(function (Page $page) use ($pageId) { + if ($page->getParent() === null && \count(explode('/', $page->getFolder())) == 1) { + $page->setParent($this->generatedPages->get($pageId)); + } + }); + $this->generatedPages->replace($page->getId(), $page); } $menuWeight += 10; } diff --git a/src/Step/Pages/Create.php b/src/Step/Pages/Create.php index ffd136788..8b8fb7696 100644 --- a/src/Step/Pages/Create.php +++ b/src/Step/Pages/Create.php @@ -62,10 +62,15 @@ public function process(): void $page->parse(); // set parent page // if section: home - if ($page->getType() == \Cecil\Collection\Page\Type::SECTION->value) { - //$page->setParent($this->builder->getPages()->get('index')); + // @todo + /*if ($page->getType() == \Cecil\Collection\Page\Type::SECTION->value) { + $page->setParent($this->builder->getPages()->get('index')); + }*/ + // if section page: parent is section + if ($page->getSection() !== null && $this->builder->getPages()->has($page->getSection())) { + $page->setParent($this->builder->getPages()->get($page->getSection())); } - // if subpage: folder + // if sub page: parent is "folder" if (!empty($page->getFolder()) && $this->builder->getPages()->has($page->getFolder())) { $page->setParent($this->builder->getPages()->get($page->getFolder())); } diff --git a/tests/fixtures/website/layouts/nested-page.html.twig b/tests/fixtures/website/layouts/nested-page.html.twig index cdd04628f..630b7c8e1 100644 --- a/tests/fixtures/website/layouts/nested-page.html.twig +++ b/tests/fixtures/website/layouts/nested-page.html.twig @@ -2,15 +2,12 @@ {% block content %} -Parent page : {{ d(page.parent) }} +

Ancestors

+Home +{% for item in page.ancestors|reverse %} + > {{ item.title }} +{% endfor %} -
-

Ancestors

- {#{{ page.ancestors|join(' > ') }}#} - Home - {% for item in page.ancestors|reverse %} - > {{ item.title }} - {% endfor %} -
+
{% endblock %} \ No newline at end of file diff --git a/tests/fixtures/website/pages/Others/Sub dir/Sub sub dir/Deep nested page.md b/tests/fixtures/website/pages/Others/Sub dir/Sub sub dir/Deep nested page.md new file mode 100644 index 000000000..ab893fef6 --- /dev/null +++ b/tests/fixtures/website/pages/Others/Sub dir/Sub sub dir/Deep nested page.md @@ -0,0 +1,3 @@ +--- +layout: nested-page +--- diff --git a/tests/fixtures/website/pages/Others/Sub dir/Sub sub dir/index.md b/tests/fixtures/website/pages/Others/Sub dir/Sub sub dir/index.md new file mode 100644 index 000000000..ab893fef6 --- /dev/null +++ b/tests/fixtures/website/pages/Others/Sub dir/Sub sub dir/index.md @@ -0,0 +1,3 @@ +--- +layout: nested-page +--- diff --git a/tests/fixtures/website/pages/Others/Sub dir/index.md b/tests/fixtures/website/pages/Others/Sub dir/index.md index 76604628f..ab893fef6 100644 --- a/tests/fixtures/website/pages/Others/Sub dir/index.md +++ b/tests/fixtures/website/pages/Others/Sub dir/index.md @@ -1,3 +1,3 @@ --- -#layout: nested-page +layout: nested-page --- From cd3fad1dc425f5a52f4136c6bb61e1d14eb28bbb Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Thu, 15 Feb 2024 01:00:30 +0100 Subject: [PATCH 20/39] Update Section.php --- src/Generator/Section.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generator/Section.php b/src/Generator/Section.php index 16cec6d4a..cf5897ebf 100644 --- a/src/Generator/Section.php +++ b/src/Generator/Section.php @@ -102,7 +102,7 @@ public function generate(): void } catch (\DomainException) { $this->generatedPages->replace($page->getId(), $page); } - // set section as parent page + // update parent of sub pages $subPages->map(function (Page $page) use ($pageId) { if ($page->getParent() === null && \count(explode('/', $page->getFolder())) == 1) { $page->setParent($this->generatedPages->get($pageId)); From 59af3d4b6627ca4c15365d46b0b94621926476d1 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Thu, 15 Feb 2024 01:57:19 +0100 Subject: [PATCH 21/39] Update page.html.twig --- resources/layouts/_default/page.html.twig | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/resources/layouts/_default/page.html.twig b/resources/layouts/_default/page.html.twig index f88b30d57..99a87251f 100644 --- a/resources/layouts/_default/page.html.twig +++ b/resources/layouts/_default/page.html.twig @@ -52,6 +52,11 @@ nav a:hover:not(main) { text-decoration: underline; } + nav.breadcrumb li:not(:last-child)::after { + display: inline-block; + content: "→"; + opacity: .5; + } main header { background: unset; border-bottom: unset; @@ -157,6 +162,23 @@ {%- endblock header ~%}
+ {%- if page.type != 'homepage' ~%} + + {%- endif ~%} {%- block content ~%} {{ page.content }} {%- endblock content ~%} From a4ed2d55476e6b4eed1e78a1c232a51f27ff9f58 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Thu, 15 Feb 2024 17:41:30 +0100 Subject: [PATCH 22/39] Create breadcrumb.html.twig --- resources/layouts/partials/breadcrumb.html.twig | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 resources/layouts/partials/breadcrumb.html.twig diff --git a/resources/layouts/partials/breadcrumb.html.twig b/resources/layouts/partials/breadcrumb.html.twig new file mode 100644 index 000000000..f0095917d --- /dev/null +++ b/resources/layouts/partials/breadcrumb.html.twig @@ -0,0 +1,17 @@ + {%- if page.type != 'homepage' and page.ancestors|default([])|length > 0 ~%} + + {%- endif ~%} \ No newline at end of file From dbb56fa2d8424702971a8e6454c025191ad9055a Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Thu, 15 Feb 2024 17:41:35 +0100 Subject: [PATCH 23/39] Update page.html.twig --- resources/layouts/_default/page.html.twig | 33 ++++++++++------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/resources/layouts/_default/page.html.twig b/resources/layouts/_default/page.html.twig index 99a87251f..cc5ad475f 100644 --- a/resources/layouts/_default/page.html.twig +++ b/resources/layouts/_default/page.html.twig @@ -52,9 +52,20 @@ nav a:hover:not(main) { text-decoration: underline; } + nav.breadcrumb ol { + display: flex; + flex-wrap: wrap; + list-style: none; + margin: 0; + padding: 0; + } + nav.breadcrumb li { + margin: 0; + font-size: 0.875rem; + } nav.breadcrumb li:not(:last-child)::after { - display: inline-block; - content: "→"; + margin: .25rem; + content: "›"; opacity: .5; } main header { @@ -162,23 +173,7 @@ {%- endblock header ~%}
- {%- if page.type != 'homepage' ~%} - - {%- endif ~%} + {%- include 'partials/breadcrumb.html.twig' with {page: page} only ~%} {%- block content ~%} {{ page.content }} {%- endblock content ~%} From d69b6461acc5c69c205ba06a1bf174b8290276f8 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Thu, 15 Feb 2024 17:41:43 +0100 Subject: [PATCH 24/39] Update Create.php --- src/Step/Pages/Create.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Step/Pages/Create.php b/src/Step/Pages/Create.php index 8b8fb7696..8e42f1289 100644 --- a/src/Step/Pages/Create.php +++ b/src/Step/Pages/Create.php @@ -60,10 +60,12 @@ public function process(): void $page->setFile($file); // parse frontmatter and body $page->parse(); - // set parent page - // if section: home - // @todo - /*if ($page->getType() == \Cecil\Collection\Page\Type::SECTION->value) { + + /* + * Set parent page + */ + // if root page or section: home + /*if (empty($page->getFolder()) || $page->getType() == \Cecil\Collection\Page\Type::SECTION->value) { $page->setParent($this->builder->getPages()->get('index')); }*/ // if section page: parent is section From ab19a16ca2a1b019fe2b7d15f1ef2db44d0468a2 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Thu, 15 Feb 2024 17:42:11 +0100 Subject: [PATCH 25/39] Update Load.php --- src/Step/Pages/Load.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Step/Pages/Load.php b/src/Step/Pages/Load.php index c23c060b4..40299d3e5 100644 --- a/src/Step/Pages/Load.php +++ b/src/Step/Pages/Load.php @@ -58,6 +58,13 @@ 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') { return -1; @@ -66,7 +73,7 @@ public function process(): void return 1; } // sort by name - return strnatcasecmp($a->getRealPath(), $b->getRealPath()); + return strnatcasecmp($a->getRelativePath(), $b->getRelativePath()); }); // load only one page? if ($this->page) { From 8e6510fdd516b3728c8c637890216a6bd38aefc9 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Tue, 20 Feb 2024 11:02:35 +0100 Subject: [PATCH 26/39] Update Page.php --- src/Collection/Page/Page.php | 41 ++++++++++++++---------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/src/Collection/Page/Page.php b/src/Collection/Page/Page.php index e37c94d1b..d421b600f 100644 --- a/src/Collection/Page/Page.php +++ b/src/Collection/Page/Page.php @@ -121,21 +121,18 @@ public static function slugify(string $path): string */ public static function createIdFromFile(SplFileInfo $file): string { - $fileComponents = self::getFileComponents($file); - $relativePath = self::slugify($fileComponents['path']); - $basename = self::slugify(PrefixSuffix::subPrefix($file->getBasename('.' . $file->getExtension()))); - // if file is "README.md", ID is "index" - $basename = (string) str_ireplace('readme', 'index', $basename); - // if file is section's index: "section/index.md", ID is "section" + $relativePath = self::slugify(self::getFileComponents($file)['path']); + $basename = self::slugify(PrefixSuffix::subPrefix(self::getFileComponents($file)['name'])); + // if file is a section's index: "
/index.md", "
" is the ID if (!empty($relativePath) && PrefixSuffix::sub($basename) == 'index') { - // case of a localized section's index: "section/index.fr.md", ID is "fr/section" + // case of a localized section's index: "
/index.fr.md", "" is the ID if (PrefixSuffix::hasSuffix($basename)) { return PrefixSuffix::getSuffix($basename) . '/' . $relativePath; } return $relativePath; } - // localized page + // localized page: ".fr.md" -> "fr/" if (PrefixSuffix::hasSuffix($basename)) { return trim(Util::joinPath(PrefixSuffix::getSuffix($basename), $relativePath, PrefixSuffix::sub($basename)), '/'); } @@ -167,15 +164,9 @@ public function setFile(SplFileInfo $file): self /* * File path components */ - $fileRelativePath = str_replace(DIRECTORY_SEPARATOR, '/', $this->file->getRelativePath()); - $fileExtension = $this->file->getExtension(); - $fileName = $this->file->getBasename('.' . $fileExtension); - // renames "README" to "index" - $fileName = (string) str_ireplace('readme', 'index', $fileName); - // case of "index" = home page - if (empty($this->file->getRelativePath()) && PrefixSuffix::sub($fileName) == 'index') { - $this->setType(Type::HOMEPAGE->value); - } + $fileRelativePath = self::getFileComponents($file)['path']; + $fileExtension = self::getFileComponents($file)['ext']; + $fileName = self::getFileComponents($file)['name']; /* * Set page properties and variables */ @@ -668,14 +659,6 @@ public function getFmVariables(): array return $this->fmVariables; } - /** - * {@inheritdoc} - */ - public function setId(string $id): self - { - return parent::setId($id); - } - /** * Set parent page. */ @@ -711,6 +694,14 @@ public function getAncestors(): array return $ancestors; } + /** + * {@inheritdoc} + */ + public function setId(string $id): self + { + return parent::setId($id); + } + /** * Cast "boolean" string (or array of strings) to boolean. * From 91a2d5019ae28b2c3d0ab63ae7852cf177bdf49d Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Wed, 21 Feb 2024 01:53:33 +0100 Subject: [PATCH 27/39] WIP --- src/Generator/Section.php | 8 +++++--- src/Step/Pages/Create.php | 16 ---------------- src/Step/Pages/Render.php | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/Generator/Section.php b/src/Generator/Section.php index cf5897ebf..e15207520 100644 --- a/src/Generator/Section.php +++ b/src/Generator/Section.php @@ -41,9 +41,9 @@ public function generate(): void } $sections[$page->getSection()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page; // nested sections - if ($page->getParent() !== null) { - //$sections[$page->getParent()->getId()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page; - } + /*if ($page->getParent() !== null) { + $sections[$page->getParent()->getId()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page; + }*/ } } @@ -103,12 +103,14 @@ public function generate(): void $this->generatedPages->replace($page->getId(), $page); } // update parent of sub pages + /* $subPages->map(function (Page $page) use ($pageId) { if ($page->getParent() === null && \count(explode('/', $page->getFolder())) == 1) { $page->setParent($this->generatedPages->get($pageId)); } }); $this->generatedPages->replace($page->getId(), $page); + */ } $menuWeight += 10; } diff --git a/src/Step/Pages/Create.php b/src/Step/Pages/Create.php index 8e42f1289..25ddd21e6 100644 --- a/src/Step/Pages/Create.php +++ b/src/Step/Pages/Create.php @@ -61,22 +61,6 @@ public function process(): void // parse frontmatter and body $page->parse(); - /* - * Set parent page - */ - // if root page or section: home - /*if (empty($page->getFolder()) || $page->getType() == \Cecil\Collection\Page\Type::SECTION->value) { - $page->setParent($this->builder->getPages()->get('index')); - }*/ - // if section page: parent is section - if ($page->getSection() !== null && $this->builder->getPages()->has($page->getSection())) { - $page->setParent($this->builder->getPages()->get($page->getSection())); - } - // if sub page: parent is "folder" - if (!empty($page->getFolder()) && $this->builder->getPages()->has($page->getFolder())) { - $page->setParent($this->builder->getPages()->get($page->getFolder())); - } - /* * Apply an - optional - custom path to pages of a section. * diff --git a/src/Step/Pages/Render.php b/src/Step/Pages/Render.php index 6d26758af..18918228a 100644 --- a/src/Step/Pages/Render.php +++ b/src/Step/Pages/Render.php @@ -16,6 +16,7 @@ use Cecil\Builder; use Cecil\Collection\Page\Collection; use Cecil\Collection\Page\Page; +use Cecil\Collection\Page\Type; use Cecil\Exception\RuntimeException; use Cecil\Renderer\Config; use Cecil\Renderer\Layout; @@ -69,15 +70,44 @@ public function process(): void ->filter(function (Page $page) { return (bool) $page->getVariable('published'); }) - // enrichs some variables + // enriched some variables ->map(function (Page $page) { $formats = $this->getOutputFormats($page); - // output formats - $page->setVariable('output', $formats); // alternates formats + $page->setVariable('output', $formats); $page->setVariable('alternates', $this->getAlternates($formats)); // translations $page->setVariable('translations', $this->getTranslations($page)); + // parent + if ($page->getType() != Type::HOMEPAGE->value) { + //$page->setParent($this->builder->getPages()->get('index')); + } + /* + if (!empty($page->getFolder())) { + $folderAsArray = explode('/', $page->getFolder()); + do { + $parentFolder = implode('/', $folderAsArray); + array_pop($folderAsArray); + } while (!$this->builder->getPages()->has($parentFolder)); + $parent = $this->builder->getPages()->get($parentFolder); + $page->setParent($parent); + } + */ + + // if root page or section: home + /*if (empty($page->getFolder()) || $page->getType() == \Cecil\Collection\Page\Type::SECTION->value) { + $page->setParent($this->builder->getPages()->get('index')); + }*/ + /* + // if section page: parent is section + if ($page->getSection() !== null && $this->builder->getPages()->has($page->getSection())) { + $page->setParent($this->builder->getPages()->get($page->getSection())); + } + // if sub page: parent is "folder" + if (!empty($page->getFolder()) && $this->builder->getPages()->has($page->getFolder())) { + $page->setParent($this->builder->getPages()->get($page->getFolder())); + } + */ return $page; }); From 89379006876f93ea38e9ec6b4826baf2d021b95c Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Thu, 22 Feb 2024 17:06:00 +0100 Subject: [PATCH 28/39] It works \o/ --- .../layouts/partials/breadcrumb.html.twig | 10 ++-- src/Collection/Page/Page.php | 4 +- src/Generator/Section.php | 9 ---- src/Step/Pages/Render.php | 54 +++++++------------ .../website/layouts/nested-page.html.twig | 9 ++-- .../Nested page in Sub Section.md} | 0 .../Nested page in Sub Sub Section.md} | 0 .../Sub Sub Section}/index.md | 0 .../Others/{Sub dir => Sub Section}/index.md | 0 .../Sub Sub Dir/Nested page in Sub Sub Dir.md | 3 ++ 10 files changed, 36 insertions(+), 53 deletions(-) rename tests/fixtures/website/pages/Others/{Sub dir/Nested page.md => Sub Section/Nested page in Sub Section.md} (100%) rename tests/fixtures/website/pages/Others/{Sub dir/Sub sub dir/Deep nested page.md => Sub Section/Sub Sub Section/Nested page in Sub Sub Section.md} (100%) rename tests/fixtures/website/pages/Others/{Sub dir/Sub sub dir => Sub Section/Sub Sub Section}/index.md (100%) rename tests/fixtures/website/pages/Others/{Sub dir => Sub Section}/index.md (100%) create mode 100644 tests/fixtures/website/pages/Others/Sub Sub Dir/Nested page in Sub Sub Dir.md diff --git a/resources/layouts/partials/breadcrumb.html.twig b/resources/layouts/partials/breadcrumb.html.twig index f0095917d..f53e014b1 100644 --- a/resources/layouts/partials/breadcrumb.html.twig +++ b/resources/layouts/partials/breadcrumb.html.twig @@ -1,12 +1,14 @@ {%- if page.type != 'homepage' and page.ancestors|default([])|length > 0 ~%}