Skip to content

Commit

Permalink
Updated MarkdownService.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Dec 11, 2024
1 parent ccb2c22 commit 751d4c2
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 75 deletions.
32 changes: 14 additions & 18 deletions src/Controller/AboutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,6 @@
#[IsGranted(RoleInterface::ROLE_USER)]
class AboutController extends AbstractController
{
// the reverse order is important!
private const REPLACE = [
'<h4>' => '<h6 class="bookmark bookmark-3">',
'<h3>' => '<h5 class="bookmark bookmark-2">',
'<h2>' => '<h4 class="bookmark bookmark-1">',
'<h1>' => '<h3 class="bookmark">',
'</h4>' => '</h6>',
'</h3>' => '</h5>',
'</h2>' => '</h4>',
'</h1>' => '</h3>',
'<p>' => '<p class="text-justify">',
];

#[Get(path: '', name: 'index')]
public function index(
#[Autowire('%kernel.environment%')]
Expand Down Expand Up @@ -99,13 +86,22 @@ public function word(
return $this->renderWordDocument($doc);
}

private function loadContent(string $projectDir, MarkdownService $service): string
private function convertFile(string $projectDir, MarkdownService $service, string $name): string
{
$path = FileUtils::buildPath($projectDir, AboutLicenceController::LICENCE_FILE);
$license = $service->convertFile($path, false, self::REPLACE);
$path = FileUtils::buildPath($projectDir, $name);
$content = $service->convertFile($path);
$content = $service->updateTag('h4', 'h6', 'bookmark bookmark-3', $content);
$content = $service->updateTag('h3', 'h5', 'bookmark bookmark-2', $content);
$content = $service->updateTag('h2', 'h4', 'bookmark bookmark-1', $content);
$content = $service->updateTag('h1', 'h3', 'bookmark', $content);

return $service->addTagClass('p', 'text-justify', $content);
}

$path = FileUtils::buildPath($projectDir, AboutPolicyController::POLICY_FILE);
$policy = $service->convertFile($path, false, self::REPLACE);
private function loadContent(string $projectDir, MarkdownService $service): string
{
$license = $this->convertFile($projectDir, $service, AboutLicenceController::LICENCE_FILE);
$policy = $this->convertFile($projectDir, $service, AboutPolicyController::POLICY_FILE);

return \sprintf('%s<p class="%s" />%s', $license, HtmlTag::PAGE_BREAK->value, $policy);
}
Expand Down
12 changes: 4 additions & 8 deletions src/Controller/AboutLicenceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,10 @@ class AboutLicenceController extends AbstractController
*/
public const LICENCE_FILE = 'LICENSE.md';

private const REPLACE = [
'<h2>' => '<h4 class="bookmark">',
'</h2>' => '</h4>',
'<p>' => '<p class="text-justify">',
];

public function __construct(
#[Autowire('%kernel.project_dir%')]
private readonly string $projectDir,
private readonly MarkdownService $markdownService
private readonly MarkdownService $service
) {
}

Expand Down Expand Up @@ -106,7 +100,9 @@ public function word(): WordResponse
private function loadContent(): string
{
$path = FileUtils::buildPath($this->projectDir, self::LICENCE_FILE);
$content = $this->service->convertFile($path, true);
$content = $this->service->updateTag('h2', 'h4', 'bookmark', $content);

return $this->markdownService->convertFile($path, true, self::REPLACE);
return $this->service->addTagClass('p', 'text-justify', $content);
}
}
19 changes: 6 additions & 13 deletions src/Controller/AboutPolicyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,10 @@ class AboutPolicyController extends AbstractController
*/
public const POLICY_FILE = 'POLICY.md';

// the reverse order is important!
private const REPLACE = [
'<h4>' => '<h6 class="bookmark bookmark-2">',
'<h3>' => '<h5 class="bookmark bookmark-1">',
'<h2>' => '<h4 class="bookmark">',
'</h4>' => '</h6>',
'</h3>' => '</h5>',
'</h2>' => '</h4>',
'<p>' => '<p class="text-justify">',
];

public function __construct(
#[Autowire('%kernel.project_dir%')]
private readonly string $projectDir,
private readonly MarkdownService $markdownService
private readonly MarkdownService $service
) {
}

Expand Down Expand Up @@ -107,7 +96,11 @@ public function word(): WordResponse
private function loadContent(): string
{
$path = FileUtils::buildPath($this->projectDir, self::POLICY_FILE);
$content = $this->service->convertFile($path, true);
$content = $this->service->updateTag('h4', 'h6', 'bookmark bookmark-2', $content);
$content = $this->service->updateTag('h3', 'h5', 'bookmark bookmark-1', $content);
$content = $this->service->updateTag('h2', 'h4', 'bookmark', $content);

return $this->markdownService->convertFile($path, true, self::REPLACE);
return $this->service->addTagClass('p', 'text-justify', $content);
}
}
54 changes: 44 additions & 10 deletions src/Service/MarkdownService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,66 @@ public function __construct(private MarkdownInterface $markdown)
{
}

/**
* @param non-empty-string $tag
* @param non-empty-string $class
*/
public function addTagClass(string $tag, string $class, string $content): string
{
$search = \sprintf('<%s>', $tag);
$replace = \sprintf('<%s class="%s">', $tag, $class);

return \str_replace($search, $replace, $content);
}

/**
* Convert the given content to HTML.
*
* @param string $content the content to convert
* @param bool $removeTitle true to remove H1 tags
* @param array<string, string> $replaces the values to replace
* @param string $content the content to convert
* @param bool $removeTitle true to remove H1 tags
*/
public function convertContent(string $content, bool $removeTitle = false, array $replaces = []): string
public function convertContent(string $content, bool $removeTitle = false): string
{
$content = $this->markdown->convert($content);
if ($removeTitle) {
$content = \trim(StringUtils::pregReplace('/<h1[^>]*>.*?<\/h1>/', '', $content));
}

return [] === $replaces ? $content : StringUtils::replace($replaces, $content);
return StringUtils::pregReplace('/[^>]$/m', '$0 ', $content);
}

/**
* Convert the content of the given file to HTML.
*
* @param string $path the file to load and to convert
* @param bool $removeTitle true to remove H1 tags
* @param array<string, string> $replaces the values to replace
* @param string $path the file to load and to convert
* @param bool $removeTitle true to remove H1 tags
*/
public function convertFile(string $path, bool $removeTitle = false): string
{
return $this->convertContent(FileUtils::readFile($path), $removeTitle);
}

/**
* @param non-empty-string $oldTag
* @param non-empty-string $newTag
*/
public function convertFile(string $path, bool $removeTitle = false, array $replaces = []): string
public function replaceTag(string $oldTag, string $newTag, string $content): string
{
return $this->convertContent(FileUtils::readFile($path), $removeTitle, $replaces);
$pattern = \sprintf('/<(\/?)%s>/m', $oldTag);
$replacement = \sprintf('<$1%s>', $newTag);

return StringUtils::pregReplace($pattern, $replacement, $content);
}

/**
* @param non-empty-string $oldTag
* @param non-empty-string $newTag
* @param non-empty-string $class
*/
public function updateTag(string $oldTag, string $newTag, string $class, string $content): string
{
$content = $this->replaceTag($oldTag, $newTag, $content);

return $this->addTagClass($newTag, $class, $content);
}
}
99 changes: 73 additions & 26 deletions tests/Service/MarkdownServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace App\Tests\Service;

use App\Service\MarkdownService;
use App\Utils\StringUtils;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;
use Twig\Extra\Markdown\MarkdownInterface;
Expand All @@ -23,33 +24,32 @@ class MarkdownServiceTest extends TestCase
/**
* @throws Exception
*/
public function testConvertContentRemoveTitle(): void
public function testAddTagClass(): void
{
$content = '<h1>Hello</h1>';
$markdown = $this->createMock(MarkdownInterface::class);
$markdown->method('convert')
->willReturnArgument(0);
$service = new MarkdownService($markdown);
$actual = $service->convertContent($content, true);
self::assertSame('', $actual);
$service = $this->createService();
$actual = $service->addTagClass('h1', 'my-class', '<h1>Hello</h1>');
self::assertSame('<h1 class="my-class">Hello</h1>', $actual);
}

/**
* @throws Exception
*/
public function testConvertContentWithTag(): void
public function testAddTagClassNotFound(): void
{
$service = $this->createService();
$actual = $service->addTagClass('fake', 'my-class', '<h1>Hello</h1>');
self::assertSame('<h1>Hello</h1>', $actual);
}

/**
* @throws Exception
*/
public function testConvertContentRemoveTitle(): void
{
$content = '<h1>Hello</h1>';
$tags = [
'<h1>' => '<h5>',
'</h1>' => '</h5>',
];
$markdown = $this->createMock(MarkdownInterface::class);
$markdown->method('convert')
->willReturnArgument(0);
$service = new MarkdownService($markdown);
$actual = $service->convertContent($content, false, $tags);
self::assertSame('<h5>Hello</h5>', $actual);
$service = $this->createService();
$actual = $service->convertContent($content, true);
self::assertSame('', $actual);
}

/**
Expand All @@ -58,10 +58,7 @@ public function testConvertContentWithTag(): void
public function testConvertContentWithTitle(): void
{
$content = '<h1>Hello</h1>';
$markdown = $this->createMock(MarkdownInterface::class);
$markdown->method('convert')
->willReturnArgument(0);
$service = new MarkdownService($markdown);
$service = $this->createService();
$actual = $service->convertContent($content);
self::assertSame($content, $actual);
}
Expand All @@ -73,11 +70,61 @@ public function testConvertFile(): void
{
$path = __DIR__ . '/../Data/markdown.md';
$content = \file_get_contents($path);
$content = StringUtils::pregReplace('/[^>]$/m', '$0 ', $content);

Check failure on line 73 in tests/Service/MarkdownServiceTest.php

View workflow job for this annotation

GitHub Actions / build

Parameter #3 $subject of static method App\Utils\StringUtils::pregReplace() expects string, string|false given.
$service = $this->createService();
$actual = $service->convertFile($path);
self::assertSame($content, $actual);
}

/**
* @throws Exception
*/
public function testReplaceTag(): void
{
$service = $this->createService();
$actual = $service->replaceTag('h1', 'h4', '<h1>Hello</h1>');
self::assertSame('<h4>Hello</h4>', $actual);
}

/**
* @throws Exception
*/
public function testReplaceTagNotFound(): void
{
$service = $this->createService();
$actual = $service->replaceTag('fake', 'h4', '<h1>Hello</h1>');
self::assertSame('<h1>Hello</h1>', $actual);
}

/**
* @throws Exception
*/
public function testUpdateTag(): void
{
$service = $this->createService();
$actual = $service->updateTag('h1', 'h4', 'my-class', '<h1>Hello</h1>');
self::assertSame('<h4 class="my-class">Hello</h4>', $actual);
}

/**
* @throws Exception
*/
public function testUpdateTagNotFound(): void
{
$service = $this->createService();
$actual = $service->updateTag('fake', 'h4', 'my-class', '<h1>Hello</h1>');
self::assertSame('<h1>Hello</h1>', $actual);
}

/**
* @throws Exception
*/
private function createService(): MarkdownService
{
$markdown = $this->createMock(MarkdownInterface::class);
$markdown->method('convert')
->willReturnArgument(0);
$service = new MarkdownService($markdown);
$actual = $service->convertFile($path);
self::assertSame($content, $actual);

return new MarkdownService($markdown);
}
}

0 comments on commit 751d4c2

Please sign in to comment.