From 56f1918b05fc1a4f376b133cc173986606327862 Mon Sep 17 00:00:00 2001 From: Stefano Novelli Date: Wed, 3 Jul 2024 16:20:57 +0200 Subject: [PATCH 1/5] Refactor blacklist handling in AdsPostParser to use separate variables for before and after items, improving readability and maintainability. --- config/ads-post-parser.php | 7 +++++-- src/AdsPostParser.php | 14 ++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/config/ads-post-parser.php b/config/ads-post-parser.php index 16126e1..7ee442d 100644 --- a/config/ads-post-parser.php +++ b/config/ads-post-parser.php @@ -22,7 +22,10 @@ * Blacklist to avoid adding advertising in some elements * The blacklist is a regex * Example: - * '/\[shortcode|\[image|\[product|\[widget|\[twitter|\[facebook]|\[youtube|\[gallery|\[media| '/\[shortcode|\[image|\[product|\[widget|\[twitter|\[facebook|\[instagram|\[youtube|\[gallery|\[media|\[video|\[quiz|\[challenge|\[spotify|\[faq|\[movie|\[tv|\[spoiler| [ + 'before' => '/ '/blacklist = config('ads-post-parser.blacklist'); + $this->blacklistBefore = config('ads-post-parser.blacklist.before'); + $this->blacklistAfter = config('ads-post-parser.blacklist.after'); $this->dom = HtmlDomParser::str_get_html("
$content
"); $this->content = $content; } @@ -52,14 +54,14 @@ public function appendSingleAdvertising(int $index, int $advIndex): string return $this->dom->save(); } - $currentItem = $items[$index]; + $beforeItem = $items[$index]; $nextItem = $index < $maxLoop - 1 ? $items[$index + 1] : null; if ( - ! preg_match($this->blacklist, $currentItem->outertext) - && ($nextItem === null || ! preg_match($this->blacklist, $nextItem->outertext)) + ! preg_match($this->blacklistBefore, $beforeItem->outertext) + && ($nextItem === null || ! preg_match($this->blacklistAfter, $nextItem->outertext)) ) { - $currentItem->outertext .= Blade::render('ads-post-parser::ads'.$advIndex); + $beforeItem->outertext .= Blade::render('ads-post-parser::ads'.$advIndex); } else { $this->appendSingleAdvertising($index + 1, $advIndex); } From b61682f99d647f0aba49a2c83bc657a5f49dd13d Mon Sep 17 00:00:00 2001 From: Stefano Novelli Date: Wed, 3 Jul 2024 16:21:24 +0200 Subject: [PATCH 2/5] Refactored test case to append single advertising with index greater than the number of paragraphs --- tests/AdsPostParserTest.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/AdsPostParserTest.php b/tests/AdsPostParserTest.php index 04e4682..1ec2f1b 100644 --- a/tests/AdsPostParserTest.php +++ b/tests/AdsPostParserTest.php @@ -38,7 +38,8 @@ expect($content)->toContain('YOUR AD1 HERE'); }); -//it('can remove wrapping div', function () { -// $content = (new AdsPostParser($this->content))->appendAdvertising()->removeWrappingDiv(); -// expect($content)->not()->toContain('
'); -//}); +it('can append single advertising with index greater than the number of paragraphs', function () { + $content = (new AdsPostParser($this->content)) + ->appendSingleAdvertising(100, 1); + expect($content)->not()->toContain('YOUR AD1 HERE'); +}); From 6079ac69f975f084375c3f797799f33d0d725978 Mon Sep 17 00:00:00 2001 From: Stefano Novelli Date: Wed, 3 Jul 2024 16:21:29 +0200 Subject: [PATCH 3/5] Add feature to encapsulate post content in a `div` tag with id `adv__parsed__content` for easier parsing and appending advertising using `AdsPostParser` class. --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 41f9b8f..771c301 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,16 @@ php artisan vendor:publish --tag="laravel-ads-post-parser-views" ## Usage +You must encapsulate your post content in a `div` tag with the id `adv__parsed__content`: + +```html +
+ {!! $parsedContent !!} +
+``` + +Then you can use the `AdsPostParser` class to append advertising to your post: + ```php $parsedContent = (new AdsPostParser($content))->appendAdvertising(); ``` From 5968718f033b19bdcb46c4067b67789a035591cf Mon Sep 17 00:00:00 2001 From: Stefano Novelli Date: Wed, 3 Jul 2024 16:30:36 +0200 Subject: [PATCH 4/5] Refactor AdsPostParser.php by removing the unnecessary removeWrappingDiv method --- src/AdsPostParser.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/AdsPostParser.php b/src/AdsPostParser.php index f987321..f01e3d7 100755 --- a/src/AdsPostParser.php +++ b/src/AdsPostParser.php @@ -69,14 +69,4 @@ public function appendSingleAdvertising(int $index, int $advIndex): string return $this->dom->save(); } - /** - * Remove the wrapping div - */ - public function removeWrappingDiv(): string - { - $contentDiv = $this->dom->find('#adv__parsed__content', 0); - $html = $contentDiv->save(); - - return $html; - } } From 376bdbd3e70f1c45ed153bef5ff0f0608d95f4e9 Mon Sep 17 00:00:00 2001 From: murdercode Date: Wed, 3 Jul 2024 14:31:14 +0000 Subject: [PATCH 5/5] Fix styling --- config/ads-post-parser.php | 2 +- src/AdsPostParser.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/ads-post-parser.php b/config/ads-post-parser.php index 7ee442d..4fb9d2c 100644 --- a/config/ads-post-parser.php +++ b/config/ads-post-parser.php @@ -27,5 +27,5 @@ 'blacklist' => [ 'before' => '/ '/dom->save(); } - }