Skip to content

Commit e76e70c

Browse files
committed
Add JekyllPageGenerator
Jekyll is the underlying technology used for GitHub Pages. Jekyll parses markdown and liquid files to HTML pages for a site. Generally Jekyll expects "frontmatter" - a `---` delimited block at the top of the markdown file -. The existence of the frontmatter indicates to Jekyll that the page should be _processed_ by the transformation engine and not just copied over. Frontmatter also allows for adding variables to the page which can be used in the page/theme to do certain things. Even though for GitHub Pages, a plugin is active which make the frontmatter not _strictly_ necessary, for this "PHPCS docs" type of website it is useful. By default, the first `#` (H1) header will be regarded as the `page.title` and this page title is then subsequently used in the website menu and such. As for these sniff pages, the default title is the full sniffname `Standard.Category.SniffName` and the `Standard` and `Category` are already "levels" in a typical menu due to the folder structure, it is less noisy to use just the plain `SniffName` as the `page.title`. To that end, I'm adding a separate `JekyllPageGenerator` class which extends the standard `MarkdownGenerator` class and adds the _frontmatter_ to the page. This will also allow for extending the available frontmatter with additional keys in the future if deemed necessary. Includes test.
1 parent 466e040 commit e76e70c

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/Generator/JekyllPageGenerator.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Generator;
5+
6+
use App\Value\Sniff;
7+
8+
class JekyllPageGenerator extends MarkdownGenerator implements Generator
9+
{
10+
public function createSniffDoc(Sniff $sniff): string
11+
{
12+
$sniffDoc = $this->getFrontMatter($sniff) . "\n";
13+
$sniffDoc .= parent::createSniffDoc($sniff);
14+
15+
return $sniffDoc;
16+
}
17+
18+
private function getFrontMatter(Sniff $sniff): string
19+
{
20+
$sniffName = $sniff->getSniffName();
21+
if ($sniffName === '') {
22+
return <<<'MD'
23+
---
24+
---
25+
26+
MD;
27+
}
28+
29+
return <<<MD
30+
---
31+
title: {$sniffName}
32+
---
33+
34+
MD;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Tests\Generator;
5+
6+
use App\Generator\JekyllPageGenerator;
7+
use App\Value\Sniff;
8+
use App\Value\UrlList;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/** @covers \App\Generator\JekyllPage */
12+
class JekyllPageTest extends TestCase
13+
{
14+
private JekyllPageGenerator $generator;
15+
16+
/** @test */
17+
public function fromSniff_WithMinimalData_WriteMinimalDetails()
18+
{
19+
$doc = new Sniff(
20+
'Standard.Category.My',
21+
'',
22+
[],
23+
new UrlList([]),
24+
'',
25+
[],
26+
[]
27+
);
28+
29+
self::assertSame(
30+
<<<MD
31+
---
32+
title: My
33+
---
34+
35+
# Standard.Category.My
36+
37+
MD,
38+
$this->generator->createSniffDoc($doc)
39+
);
40+
}
41+
42+
protected function setUp(): void
43+
{
44+
$this->generator = new JekyllPageGenerator();
45+
}
46+
}

0 commit comments

Comments
 (0)