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
namespace SGalinski\ContentReplacer\Service;
/***************************************************************
* Copyright notice
*
......@@ -25,7 +23,10 @@ namespace SGalinski\ContentReplacer\Service;
* 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\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
......@@ -53,19 +54,33 @@ class SpanParserService extends AbstractParserService {
* @param string $content
* @return array
*/
public function parse($content) {
public function parse($content): array {
$matches = [];
$prefix = (string) ($this->extensionConfiguration['prefix'] ?? '');
$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);
if (ExtensionManagementUtility::isLoaded('headless')) {
// when headless is present, content will be escaped (JSON output), so we got to add some small details:
/** @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';
} 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 = [];
foreach ($matches[5] as $index => $term) {
$term = trim($term);
......@@ -110,7 +125,7 @@ class SpanParserService extends AbstractParserService {
* @return string
* @throws \UnexpectedValueException
*/
public function replaceByCategory($category, array $terms, $content) {
public function replaceByCategory($category, array $terms, $content): string {
$search = $replace = [];
$defaultReplacement = $this->prepareFoundTerms($terms, $category);
foreach ($terms as $termName => $term) {
......@@ -120,13 +135,24 @@ class SpanParserService extends AbstractParserService {
}
$searchClass = preg_quote($this->extensionConfiguration['prefix'] . $category, '/');
$search[$termName] = '/' .
'<span ' . preg_quote($term['pre'], '/') .
'class="([^"]*?)' . $searchClass . '([^"]*?)"' .
preg_quote($term['post'], '/') . '>' .
'\s*?' . preg_quote($term['term'], '/') . '\s*?' .
'<\/span>' .
'/i';
if (ExtensionManagementUtility::isLoaded('headless')) {
// when EXT:headless is present, content will be escaped (JSON output), so we got to add some small details:
$search[$termName] = '/' .
'<span ' . preg_quote($term['pre'], '/') .
'class=\\\\"([^"]*?)' . $searchClass . '([^"]*?)\\\\"' .
preg_quote($term['post'], '/') . '>' .
'\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(
$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