Skip to content

Commit 71daaa8

Browse files
authored
Merge pull request #50 from tbreuss/develop
Develop
2 parents c739f52 + 627a03e commit 71daaa8

15 files changed

Lines changed: 151 additions & 72 deletions

src/Zack.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public function initContainer(): void
9797
->addMethodCall('addExtension', [new MarkdownExtension()])
9898
->addMethodCall('addGlobal', ['config', $this->config])
9999
->addMethodCall('addRuntimeLoader', [new class implements RuntimeLoaderInterface {
100-
public function load($class) {
100+
public function load($class)
101+
{
101102
if (MarkdownRuntime::class === $class) {
102103
return new MarkdownRuntime(new DefaultMarkdown());
103104
}

src/funcs/main.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ function file_read(string $path): string
1616
return $contents;
1717
}
1818

19+
function html_contains_full_html(string $html): bool
20+
{
21+
return stripos($html, '<html') !== false || stripos($html, '<!doctype') !== false;
22+
}
23+
24+
function html_extract_layout(string $html): string
25+
{
26+
$status = preg_match('/<!--\s*layout:\s*(.+?)\s*-->/', $html, $matches);
27+
28+
if ($status !== false && isset($matches[1])) {
29+
return trim($matches[1]);
30+
}
31+
32+
return 'default.html.twig';
33+
}
34+
1935
function html_extract_title(string $html, string $default): string
2036
{
2137
if ($html === '') {

src/routing/HtmlRouteHandler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\HttpFoundation\Response;
77

88
use function tebe\zack\file_read;
9+
use function tebe\zack\html_extract_layout;
910
use function tebe\zack\html_extract_title;
1011

1112
class HtmlRouteHandler
@@ -20,9 +21,10 @@ public function __invoke(Request $request): Response
2021
}
2122

2223
$html = file_read($path);
24+
$layout = html_extract_layout($html);
2325
$title = html_extract_title($html, basename($path));
2426

25-
$content = $container->get('twig')->render('route-handler.html.twig', [
27+
$content = $container->get('twig')->render($layout, [
2628
'title' => $title,
2729
'html' => $html,
2830
]);

src/routing/MarkdownRouteHandler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Twig\Extra\Markdown\ErusevMarkdown;
1313

1414
use function tebe\zack\file_read;
15+
use function tebe\zack\html_extract_layout;
1516
use function tebe\zack\html_extract_title;
1617

1718
class MarkdownRouteHandler
@@ -34,10 +35,10 @@ public function __invoke(Request $request): Response
3435
}
3536

3637
$html = (string) $converter->convert($markdown);
37-
38+
$layout = html_extract_layout($html);
3839
$title = html_extract_title($html, basename($path));
3940

40-
$content = $container->get('twig')->render('route-handler.html.twig', [
41+
$content = $container->get('twig')->render($layout, [
4142
'title' => $title,
4243
'html' => $html,
4344
]);

src/routing/PhpRouteHandler.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Symfony\Component\HttpFoundation\Request;
88
use Symfony\Component\HttpFoundation\Response;
99

10+
use function tebe\zack\html_contains_full_html;
11+
use function tebe\zack\html_extract_layout;
1012
use function tebe\zack\html_extract_title;
1113

1214
class PhpRouteHandler
@@ -31,8 +33,13 @@ public function __invoke(Request $request): Response
3133
$outputValue = ob_get_clean();
3234

3335
if ($returnValue === 1 && is_string($outputValue)) {
34-
$title = html_extract_title($outputValue, basename($path));
35-
return $this->html('route-handler.html.twig', ['title' => $title, 'html' => $outputValue]);
36+
if (html_contains_full_html($outputValue)) {
37+
return new Response($outputValue, 200);
38+
} else {
39+
$layout = html_extract_layout($outputValue);
40+
$title = html_extract_title($outputValue, basename($path));
41+
return $this->html($layout, ['title' => $title, 'html' => $outputValue]);
42+
}
3643
} elseif (is_string($returnValue)) {
3744
if (is_string($outputValue) && strlen($outputValue) > 0) {
3845
throw new \Exception('In the PHP file the return value must be omitted if an output was made via echo: ' . $path);
@@ -68,6 +75,7 @@ public function redirect(string $url, int $status = 302): Response
6875

6976
public function render(string $template, array $context = []): string
7077
{
78+
/** @var \Twig\Environment $twig */
7179
$twig = $this->container->get('twig');
7280
return $twig->render($template, $context);
7381
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!doctype html>
2+
<h2><?= pathinfo(__FILE__, PATHINFO_FILENAME) ?></h2>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html lang="en">
2+
<body>
3+
<h2><?= pathinfo(__FILE__, PATHINFO_FILENAME) ?></h2>
4+
</body>
5+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h2><?= pathinfo(__FILE__, PATHINFO_FILENAME) ?></h2>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!-- layout: test.html.twig -->
2+
<h2><?= pathinfo(__FILE__, PATHINFO_FILENAME) ?></h2>

0 commit comments

Comments
 (0)