• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 17771165489

16 Sep 2025 03:37PM UTC coverage: 81.785% (+0.03%) from 81.757%
17771165489

push

github

ArnaudLigny
refactor: better handle of image convertion

7 of 12 new or added lines in 2 files covered. (58.33%)

17 existing lines in 4 files now uncovered.

3161 of 3865 relevant lines covered (81.79%)

0.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.37
/src/Generator/Section.php
1
<?php
2

3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <arnaud@ligny.fr>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace Cecil\Generator;
15

16
use Cecil\Collection\Page\Collection as PagesCollection;
17
use Cecil\Collection\Page\Page;
18
use Cecil\Collection\Page\Type;
19
use Cecil\Exception\RuntimeException;
20

21
/**
22
 * Section generator class.
23
 *
24
 * This class is responsible for generating sections from the pages in the builder.
25
 * It identifies sections based on the 'section' variable in each page, and
26
 * creates a new page for each section. The generated pages are added to the
27
 * collection of generated pages. It also handles sorting of subpages and
28
 * adding navigation links (next and previous) to the section pages.
29
 */
30
class Section extends AbstractGenerator implements GeneratorInterface
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function generate(): void
36
    {
37
        $sections = [];
1✔
38

39
        // identifying sections from all pages
40
        /** @var Page $page */
41
        foreach ($this->builder->getPages() ?? [] as $page) {
1✔
42
            // top level (root) sections
43
            if ($page->getSection()) {
1✔
44
                // do not add "not published" and "not excluded" pages to its section
45
                if (
46
                    $page->getVariable('published') !== true
1✔
47
                    || ($page->getVariable('excluded') || $page->getVariable('exclude'))
1✔
48
                ) {
49
                    continue;
1✔
50
                }
51
                $sections[$page->getSection()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page;
1✔
52
            }
53
        }
54

55
        // adds each section to pages collection
56
        if (\count($sections) > 0) {
1✔
57
            $menuWeight = 100;
1✔
58

59
            foreach ($sections as $section => $languages) {
1✔
60
                foreach ($languages as $language => $pagesAsArray) {
1✔
61
                    $pageId = $path = Page::slugify($section);
1✔
62
                    if ($language != $this->config->getLanguageDefault()) {
1✔
63
                        $pageId = "$language/$pageId";
1✔
64
                    }
65
                    $page = (new Page($pageId))->setVariable('title', ucfirst($section))
1✔
66
                        ->setPath($path);
1✔
67
                    if ($this->builder->getPages()->has($pageId)) {
1✔
68
                        $page = clone $this->builder->getPages()->get($pageId);
1✔
69
                    }
70
                    $pages = new PagesCollection("section-$pageId", $pagesAsArray);
1✔
71
                    // cascade variables
72
                    if ($page->hasVariable('cascade')) {
1✔
73
                        $cascade = $page->getVariable('cascade');
1✔
74
                        if (\is_array($cascade)) {
1✔
75
                            $pages->map(function (Page $page) use ($cascade) {
1✔
76
                                foreach ($cascade as $key => $value) {
1✔
77
                                    if (!$page->hasVariable($key)) {
1✔
78
                                        $page->setVariable($key, $value);
1✔
79
                                    }
80
                                }
81
                            });
1✔
82
                        }
83
                    }
84
                    // sorts pages
85
                    $sortBy = $page->getVariable('sortby') ?? $this->config->get('pages.sortby');
1✔
86
                    $pages = $pages->sortBy($sortBy);
1✔
87
                    // adds navigation links (excludes taxonomy pages)
88
                    $sortBy = $page->getVariable('sortby')['variable'] ?? $page->getVariable('sortby') ?? $this->config->get('pages.sortby')['variable'] ?? $this->config->get('pages.sortby') ?? 'date';
1✔
89
                    if (!\in_array($page->getId(), array_keys((array) $this->config->get('taxonomies')))) {
1✔
90
                        $this->addNavigationLinks($pages, $sortBy, $page->getVariable('circular') ?? false);
1✔
91
                    }
92
                    // creates page for each section
93
                    $page->setType(Type::SECTION->value)
1✔
94
                        ->setSection($path)
1✔
95
                        ->setPages($pages)
1✔
96
                        ->setVariable('language', $language)
1✔
97
                        ->setVariable('date', $pages->first()->getVariable('date'))
1✔
98
                        ->setVariable('langref', $path);
1✔
99
                    // human readable title
100
                    if ($page->getVariable('title') == 'index') {
1✔
101
                        $page->setVariable('title', $section);
1✔
102
                    }
103
                    // default menu
104
                    if (!$page->getVariable('menu')) {
1✔
105
                        $page->setVariable('menu', ['main' => ['weight' => $menuWeight]]);
1✔
106
                    }
107

108
                    try {
109
                        $this->generatedPages->add($page);
1✔
110
                    } catch (\DomainException) {
×
UNCOV
111
                        $this->generatedPages->replace($page->getId(), $page);
×
112
                    }
113
                }
114
                $menuWeight += 10;
1✔
115
            }
116
        }
117
    }
118

119
    /**
120
     * Adds navigation (next and prev) to each pages of a section.
121
     */
122
    protected function addNavigationLinks(PagesCollection $pages, string|null $sortBy = null, bool $circular = false): void
123
    {
124
        $pagesAsArray = $pages->toArray();
1✔
125
        if ($sortBy === null || $sortBy == 'date' || $sortBy == 'updated') {
1✔
126
            $pagesAsArray = array_reverse($pagesAsArray);
1✔
127
        }
128
        $count = \count($pagesAsArray);
1✔
129
        if ($count > 1) {
1✔
130
            foreach ($pagesAsArray as $position => $page) {
1✔
131
                switch ($position) {
132
                    case 0: // first
1✔
133
                        if ($circular) {
1✔
134
                            $page->setVariables([
1✔
135
                                'prev' => $pagesAsArray[$count - 1],
1✔
136
                            ]);
1✔
137
                        }
138
                        $page->setVariables([
1✔
139
                            'next' => $pagesAsArray[$position + 1],
1✔
140
                        ]);
1✔
141
                        break;
1✔
142
                    case $count - 1: // last
1✔
143
                        $page->setVariables([
1✔
144
                            'prev' => $pagesAsArray[$position - 1],
1✔
145
                        ]);
1✔
146
                        if ($circular) {
1✔
147
                            $page->setVariables([
1✔
148
                                'next' => $pagesAsArray[0],
1✔
149
                            ]);
1✔
150
                        }
151
                        break;
1✔
152
                    default:
153
                        $page->setVariables([
1✔
154
                            'prev' => $pagesAsArray[$position - 1],
1✔
155
                            'next' => $pagesAsArray[$position + 1],
1✔
156
                        ]);
1✔
157
                        break;
1✔
158
                }
159
                $this->generatedPages->add($page);
1✔
160
            }
161
        }
162
    }
163
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc