Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions Classes/Hooks/CodeblockPreviewRenderer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace B13\Codeblock\Hooks;

/*
Expand All @@ -9,8 +11,11 @@
* of the License, or any later version.
*/

use Highlight\Highlighter;
use TYPO3\CMS\Backend\View\PageLayoutView;
use TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
Expand All @@ -33,14 +38,46 @@ public function preProcess(
&$headerContent,
&$itemContent,
array &$row
) {
if ($row['CType'] === 'codeblock') {
if ($row['bodytext']) {
$bodytext = GeneralUtility::fixed_lgd_cs($row['bodytext'], 1000);
$itemContent .= $parentObject->linkEditContent(nl2br(htmlentities($bodytext)), $row) . '<br />';
)
{
if ($row['CType'] === 'codeblock' && $row['bodytext']) {
$highlight = GeneralUtility::makeInstance(Highlighter::class);

if (!$row['code_language']) {
$languages = $highlight->listLanguages();
$highlight->setAutodetectLanguages($languages);
$highlighted = $highlight->highlightAuto($row['bodytext']);
} else {
$highlighted = $highlight->highlight($row['code_language'], $row['bodytext']);
}

$drawItem = false;
$bodytext = '<pre style="padding:2px;"><code class="hljs ' . $highlighted->language . '">' . $highlighted->value . '</code></pre>';
$itemContent .= $parentObject->linkEditContent((($bodytext)), $row);

$styles = $this->getStyles();
if ($styles) {
$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
$pageRenderer->addCssInlineBlock('ext-codeblock', $styles);
}
}

$drawItem = false;
}

protected function getStyles(): string
{
$previewFile = '';
try {
$previewFile = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('codeblock', 'backendPreviewStyles');
} catch (\Exception $e) {
// do nothing
}
$previewFile = GeneralUtility::getFileAbsFileName($previewFile);

if (!is_file($previewFile)) {
$previewFile = GeneralUtility::getFileAbsFileName('EXT:codeblock/Resources/Public/Styles/GitHub.css');
}

return file_get_contents($previewFile);
}
}
93 changes: 93 additions & 0 deletions Resources/Public/Styles/GitHub.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
.hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
color: #333;
background: #f8f8f8;
}

.hljs-comment,
.hljs-quote {
color: #998;
font-style: italic;
}

.hljs-keyword,
.hljs-selector-tag,
.hljs-subst {
color: #333;
font-weight: bold;
}

.hljs-number,
.hljs-literal,
.hljs-variable,
.hljs-template-variable,
.hljs-tag .hljs-attr {
color: #008080;
}

.hljs-string,
.hljs-doctag {
color: #d14;
}

.hljs-title,
.hljs-section,
.hljs-selector-id {
color: #900;
font-weight: bold;
}

.hljs-subst {
font-weight: normal;
}

.hljs-type,
.hljs-class .hljs-title {
color: #458;
font-weight: bold;
}

.hljs-tag,
.hljs-name,
.hljs-attribute {
color: #000080;
font-weight: normal;
}

.hljs-regexp,
.hljs-link {
color: #009926;
}

.hljs-symbol,
.hljs-bullet {
color: #990073;
}

.hljs-built_in,
.hljs-builtin-name {
color: #0086b3;
}

.hljs-meta {
color: #999;
font-weight: bold;
}

.hljs-deletion {
background: #fdd;
}

.hljs-addition {
background: #dfd;
}

.hljs-emphasis {
font-style: italic;
}

.hljs-strong {
font-weight: bold;
}
2 changes: 2 additions & 0 deletions ext_conf_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# cat=Backend; type=string; label=CSS file for backend preview
backendPreviewStyles = EXT:codeblock/Resources/Public/Styles/GitHub.css