Skip to content

Commit e89b6fb

Browse files
Steveb-padriendupuis
authored andcommitted
Stabilized render of REST HTML (#1896)
* Stabilized render of REST HTML * raml2html: Upgrade to PHP 7.4 (#1898)
1 parent d479dcf commit e89b6fb

File tree

6 files changed

+57
-6
lines changed

6 files changed

+57
-6
lines changed

tools/raml2html/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
}
1616
],
1717
"require": {
18-
"php": "^7.2",
18+
"php": "^7.4",
1919
"symfony/console": "^4.2",
2020
"raml-org/raml-php-parser": "dev-master",
2121
"twig/twig": "^2.0",

tools/raml2html/src/Application.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
use EzSystems\Raml2Html\Command\LintTypesCommand;
1010
use EzSystems\Raml2Html\Generator\Generator;
1111
use EzSystems\Raml2Html\RAML\ParserFactory;
12+
use EzSystems\Raml2Html\Twig\Extension\HashExtension;
1213
use EzSystems\Raml2Html\Twig\Extension\RenderExtension;
1314
use Symfony\Component\Console\Application as BaseApplication;
1415
use Twig as Twig;
16+
use Twig\Extra\Markdown\DefaultMarkdown;
17+
use Twig\Extra\Markdown\MarkdownExtension;
18+
use Twig\Extra\Markdown\MarkdownRuntime;
19+
use Twig\RuntimeLoader\RuntimeLoaderInterface;
1520

1621
final class Application extends BaseApplication
1722
{
@@ -49,6 +54,17 @@ public function getTwig(): Twig\Environment
4954

5055
$this->twig = new Twig\Environment($loader, $options);
5156
$this->twig->addExtension(new RenderExtension());
57+
$this->twig->addExtension(new MarkdownExtension());
58+
$this->twig->addRuntimeLoader(new class implements RuntimeLoaderInterface {
59+
public function load($class): ?MarkdownRuntime {
60+
if (MarkdownRuntime::class === $class) {
61+
return new MarkdownRuntime(new DefaultMarkdown());
62+
}
63+
64+
return null;
65+
}
66+
});
67+
$this->twig->addExtension(new HashExtension());
5268
}
5369

5470
return $this->twig;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace EzSystems\Raml2Html\Twig\Extension;
10+
11+
use RuntimeException;
12+
use Twig\Extension\AbstractExtension;
13+
use Twig\TwigFunction;
14+
15+
class HashExtension extends AbstractExtension
16+
{
17+
private $hashes = [];
18+
19+
public function getFunctions(): array
20+
{
21+
return [
22+
new TwigFunction('hash', function (string ...$values): string {
23+
$hash = hash('sha256', json_encode($values));
24+
25+
if (isset($this->hashes[$hash])) {
26+
throw new RuntimeException('Hash is generated twice for ' . json_encode($values));
27+
}
28+
29+
$this->hashes[$hash] = $hash;
30+
31+
return $hash;
32+
}),
33+
];
34+
}
35+
}

tools/raml2html/src/Twig/Extension/RenderExtension.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ public function getExamplesTabs(array $responseBodies): array
6868

6969
/** @var \Raml\Body $body */
7070
foreach ($responseBodies as $body) {
71-
72-
if (!empty($body->getExample())) {
71+
if (!empty($body->getExamples()) && !empty($body->getExample())) {
7372
$tabs[] = $this->getSchemaFormat($body->getMediaType());
7473
}
7574
}
@@ -85,7 +84,7 @@ public function getExamplesBody(array $responseBodies): array
8584
foreach ($responseBodies as $body) {
8685
$schemaFormat = $this->getSchemaFormat($body->getMediaType());
8786

88-
if (!empty($body->getExample())) {
87+
if (!empty($body->getExamples()) && !empty($body->getExample())) {
8988
$examples[$schemaFormat][] = $body->getExample();
9089
}
9190
}

tools/raml2html/themes/default/example-modal.html.twig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@
5252
{% for body in example %}
5353
{% set margin = example|length > 1 and loop.index == 1 ? 'mb-4' : '' %}
5454
{% set border = loop.index > 1 ? 'border-top' : '' %}
55+
{% set outer_loop = loop %}
5556
{% for body in example %}
5657
{% set margin = example|length > 1 and loop.index == 1 ? 'mb-4' : '' %}
5758
{% set border = loop.index > 1 ? 'border-top' : '' %}
5859
<div class="position-relative {{ margin }} {{ border }}">
59-
{% set clipboardId = type ~ '_' ~ uuid() %}
60+
{% set clipboardId = hash(type, outer_loop.index, loop.index, tabId) %}
6061
<p class="mb-0 icon icon--copy-clipboard">
6162
<i class="material-icons-outlined clipboard"
6263
data-toggle="tooltip"

tools/raml2html/themes/default/example.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
{% set margin = example|length > 1 and loop.index == 1 ? 'mb-4' : '' %}
1111
{% set border = loop.index > 1 ? 'border-top' : '' %}
1212
<div class="position-relative {{ margin }} {{ border }}">
13-
{% set clipboardId = type ~ '_' ~ uuid() %}
13+
{% set clipboardId = hash(type, tabId, loop.index) %}
1414
<p class="mb-0 icon icon--copy-clipboard">
1515
<i class="material-icons-outlined clipboard"
1616
data-toggle="tooltip"

0 commit comments

Comments
 (0)