Skip to content
Snippets Groups Projects
Commit 747df0d2 authored by Stefan Galinski's avatar Stefan Galinski :video_game:
Browse files

[TASK] Support EXT:headless (Thanks to Matthias Adrowski)

Resolves: #4
parent fb121a7a
No related branches found
No related tags found
No related merge requests found
<?php <?php
namespace SGalinski\ContentReplacer\Service;
/*************************************************************** /***************************************************************
* Copyright notice * Copyright notice
* *
...@@ -25,7 +23,10 @@ namespace SGalinski\ContentReplacer\Service; ...@@ -25,7 +23,10 @@ namespace SGalinski\ContentReplacer\Service;
* This copyright notice MUST APPEAR in all copies of the script! * This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/ ***************************************************************/
namespace SGalinski\ContentReplacer\Service;
use TYPO3\CMS\Core\Log\Logger; use TYPO3\CMS\Core\Log\Logger;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
/** /**
...@@ -53,19 +54,33 @@ class SpanParserService extends AbstractParserService { ...@@ -53,19 +54,33 @@ class SpanParserService extends AbstractParserService {
* @param string $content * @param string $content
* @return array * @return array
*/ */
public function parse($content) { public function parse($content): array {
$matches = []; $matches = [];
$prefix = (string) ($this->extensionConfiguration['prefix'] ?? ''); $prefix = (string) ($this->extensionConfiguration['prefix'] ?? '');
$pattern = '/' . if (ExtensionManagementUtility::isLoaded('headless')) {
'<span' . // This expression includes any span nodes and parses // when headless is present, content will be escaped (JSON output), so we got to add some small details:
'(?=[^>]+' . // any attributes of the beginning start tag. /** @noinspection RegExpRedundantEscape */
'(?=(class="([^"]*?' . preg_quote($prefix, '/') . '[^"]+?)"))' . $pattern = '/' .
')' . // Use only spans which start with the defined class prefix '<span' . // This expression includes any span nodes and parses
' (.*?)\1(.*?)>' . // and stop if the closing character is reached. '(?=[^>]+' . // any attributes of the beginning start tag.
'(.*?)<\/span>' . // Finally we fetch the span content! '(?=(class=\\\\"([^"]*?' . preg_quote($prefix, '/') . '[^"]+?)\\\\"))' .
'/is'; ')' . // Use only spans which start with the defined class prefix
preg_match_all($pattern, $content, $matches); ' (.*?)\1(.*?)>' . // and stop if the closing character is reached.
'(.*?)<\\\\\\/span>' . // Finally we fetch the span content!
'/is';
} else {
/** @noinspection RegExpRedundantEscape */
$pattern = '/' .
'<span' . // This expression includes any span nodes and parses
'(?=[^>]+' . // any attributes of the beginning start tag.
'(?=(class="([^"]*?' . preg_quote($prefix, '/') . '[^"]+?)"))' .
')' . // Use only spans which start with the defined class prefix
' (.*?)\1(.*?)>' . // and stop if the closing character is reached.
'(.*?)<\/span>' . // Finally we fetch the span content!
'/is';
}
preg_match_all($pattern, $content, $matches);
$categories = []; $categories = [];
foreach ($matches[5] as $index => $term) { foreach ($matches[5] as $index => $term) {
$term = trim($term); $term = trim($term);
...@@ -110,7 +125,7 @@ class SpanParserService extends AbstractParserService { ...@@ -110,7 +125,7 @@ class SpanParserService extends AbstractParserService {
* @return string * @return string
* @throws \UnexpectedValueException * @throws \UnexpectedValueException
*/ */
public function replaceByCategory($category, array $terms, $content) { public function replaceByCategory($category, array $terms, $content): string {
$search = $replace = []; $search = $replace = [];
$defaultReplacement = $this->prepareFoundTerms($terms, $category); $defaultReplacement = $this->prepareFoundTerms($terms, $category);
foreach ($terms as $termName => $term) { foreach ($terms as $termName => $term) {
...@@ -120,13 +135,24 @@ class SpanParserService extends AbstractParserService { ...@@ -120,13 +135,24 @@ class SpanParserService extends AbstractParserService {
} }
$searchClass = preg_quote($this->extensionConfiguration['prefix'] . $category, '/'); $searchClass = preg_quote($this->extensionConfiguration['prefix'] . $category, '/');
$search[$termName] = '/' . if (ExtensionManagementUtility::isLoaded('headless')) {
'<span ' . preg_quote($term['pre'], '/') . // when EXT:headless is present, content will be escaped (JSON output), so we got to add some small details:
'class="([^"]*?)' . $searchClass . '([^"]*?)"' . $search[$termName] = '/' .
preg_quote($term['post'], '/') . '>' . '<span ' . preg_quote($term['pre'], '/') .
'\s*?' . preg_quote($term['term'], '/') . '\s*?' . 'class=\\\\"([^"]*?)' . $searchClass . '([^"]*?)\\\\"' .
'<\/span>' . preg_quote($term['post'], '/') . '>' .
'/i'; '\s*?' . preg_quote($term['term'], '/') . '\s*?' .
'<\\\\\\/span>' .
'/i';
} else {
$search[$termName] = '/' .
'<span ' . preg_quote($term['pre'], '/') .
'class="([^"]*?)' . $searchClass . '([^"]*?)"' .
preg_quote($term['post'], '/') . '>' .
'\s*?' . preg_quote($term['term'], '/') . '\s*?' .
'<\/span>' .
'/i';
}
$replace[$termName] = $this->prepareReplacementTerm( $replace[$termName] = $this->prepareReplacementTerm(
$term['replacement'] ?? '', $term['replacement'] ?? '',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment