ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilUIMarkdownPreviewGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23use ILIAS\Refinery\Factory as Refinery;
24use ILIAS\HTTP\Services as HTTPServices;
26
32{
33 protected const CMD_RENDER_ASYNC = 'renderAsync';
34
35 protected Refinery $refinery;
37 protected HTTPServices $http;
38
39 public function __construct()
40 {
41 global $DIC;
42
43 $this->refinery = $DIC->refinery();
44 $this->ctrl = $DIC->ctrl();
45 $this->http = $DIC->http();
46 }
47
48 public function executeCommand(): void
49 {
50 if (!$this->ctrl->isAsynch() && self::CMD_RENDER_ASYNC !== $this->ctrl->getCmd()) {
51 $this->http->saveResponse(
52 $this->http
53 ->response()
54 ->withBody(Streams::ofString('Whoops, something went wrong!'))
55 ->withStatus(404)
56 );
57
58 $this->http->sendResponse();
59 $this->http->close();
60 }
61
62 $this->renderAsync();
63 }
64
65 public function getAsyncUrl(): string
66 {
67 return $this->ctrl->getLinkTargetByClass(
68 self::class,
69 self::CMD_RENDER_ASYNC,
70 null,
71 true
72 );
73 }
74
75 public function getParameterName(): string
76 {
77 return 'raw_markdown';
78 }
79
80 public function renderAsync(): void
81 {
82 $parameter_name = $this->getParameterName();
83
84 if (!$this->http->wrapper()->post()->has($parameter_name)) {
85 $this->sendResponse('');
86 }
87
88 $raw_markdown = $this->http->wrapper()->post()->retrieve(
89 $parameter_name,
90 $this->refinery->to()->string()
91 );
92
93 $this->sendResponse($this->render($raw_markdown));
94 }
95
96 public function render(string $markdown_text): string
97 {
98 return $this->refinery->string()->markdown()->toHTML()->transform($markdown_text);
99 }
100
101 protected function sendResponse(string $html): void
102 {
103 $this->http->saveResponse(
104 $this->http->response()->withBody(Streams::ofString($html))
105 );
106
107 $this->http->sendResponse();
108 $this->http->close();
109 }
110}
Builds data types.
Definition: Factory.php:36
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
Class Services.
Definition: Services.php:38
renderAsync()
Sends a JSON-response with the rendered preview of the submitted input.
getAsyncUrl()
Returns an endpoint where the clientside input can submit it's value to and receive the current previ...
getParameterName()
Returns the name of the $_POST variable the asynchronous input submits to.
render(string $markdown_text)
Returns the rendered preview of the given string.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static http()
Fetches the global http state from ILIAS.
global $DIC
Definition: shib_login.php:26