Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
content_replacer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TYPO3
content_replacer
Commits
747df0d2
Commit
747df0d2
authored
2 years ago
by
Stefan Galinski
Browse files
Options
Downloads
Patches
Plain Diff
[TASK] Support EXT:headless (Thanks to Matthias Adrowski)
Resolves:
#4
parent
fb121a7a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Classes/Service/SpanParserService.php
+46
-20
46 additions, 20 deletions
Classes/Service/SpanParserService.php
with
46 additions
and
20 deletions
Classes/Service/SpanParserService.php
+
46
−
20
View file @
747df0d2
<?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'
]
??
''
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment