Skip to content
Open
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
21 changes: 18 additions & 3 deletions Classes/DataProcessing/HighlightProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,28 @@ public function process(ContentObjectRenderer $cObj, array $contentObjectConfigu
$targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, 'bodytext_formatted');
$highlight = GeneralUtility::makeInstance(Highlighter::class);

// The column is nullable, so a record can carry NULL rather than the empty
// string the TCA default writes. Normalise before deciding, because "??"
// only substitutes for NULL and would send it on to the highlighter.
$language = trim((string)($processedData['data']['code_language'] ?? ''));

$highlighted = null;
if ($language !== '') {
try {
$highlighted = $highlight->highlight($language, $processedData['data'][$fieldName]);
} catch (\DomainException $e) {
// The stored language is not registered - it may have been dropped from
// the list, or the record may come from an installation that had it.
// Detect instead, so a stale value does not take the page down.
$highlighted = null;
}
}

// Let highlight.php decide which code language to use from all registered if "detect automatically" is selected.
if (!($processedData['data']['code_language'] ?? true)) {
if ($highlighted === null) {
$languages = $highlight->listLanguages();
$highlight->setAutodetectLanguages($languages);
$highlighted = $highlight->highlightAuto($processedData['data'][$fieldName]);
} else {
$highlighted = $highlight->highlight($processedData['data']['code_language'], $processedData['data'][$fieldName]);
}

$processedData[$targetVariableName]['code'] = $highlighted->value;
Expand Down