Skip to content

Commit 47d7a22

Browse files
committed
feat: merge excerpt logic into the main template
1 parent e6ac2f8 commit 47d7a22

File tree

4 files changed

+43
-68
lines changed

4 files changed

+43
-68
lines changed

extend.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Flarum\Extend;
1515
use Flectar\Fancybox\WrapImagesInGallery;
1616
use Flectar\Fancybox\DefineGalleryTemplate;
17-
use Flectar\Fancybox\AddExcerptToDiscussion;
1817
use Flarum\Discussion\Discussion;
1918
use Flarum\Api\Resource\DiscussionResource;
2019
use Flarum\Api\Schema;
@@ -26,7 +25,6 @@
2625

2726
(new Extend\Formatter)
2827
->configure(DefineGalleryTemplate::class)
29-
->configure(AddExcerptToDiscussion::class)
3028
->render(WrapImagesInGallery::class),
3129

3230
(new Extend\ApiResource(DiscussionResource::class))

src/AddExcerptToDiscussion.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/DefineGalleryTemplate.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,48 @@ public function __invoke(Configurator $config)
1616

1717
if ($config->tags->exists('IMG')) {
1818
$tag = $config->tags->get('IMG');
19-
$newTemplate = $tag->template;
20-
$tag->template = <<<XML
19+
$tag->template = <<<'XML'
2120
<xsl:choose>
22-
<xsl:when test="parent::FANCYBOX-GALLERY-ITEM">
21+
<xsl:when test="ancestor::FANCYBOX-GALLERY-ITEM">
2322
<a data-fancybox="gallery" href="{@src}">
2423
<img data-lazy-src="{@src}" alt="{@alt}" loading="lazy"/>
2524
</a>
2625
</xsl:when>
26+
<xsl:when test="ancestor::DISCUSSION-EXCERPT">
27+
<a data-fancybox="excerpt-gallery" href="{@src}">
28+
<img data-lazy-src="{@src}" alt="{@alt}" class="excerpt-image"/>
29+
</a>
30+
</xsl:when>
2731
<xsl:otherwise>
2832
<a data-fancybox="single" href="{@src}">
2933
<img src="{@src}" alt="{@alt}" loading="lazy"/>
3034
</a>
3135
</xsl:otherwise>
3236
</xsl:choose>
33-
XML;
37+
XML;
3438
}
3539

3640
if ($config->tags->exists('UPL-IMAGE-PREVIEW')) {
3741
$tag = $config->tags->get('UPL-IMAGE-PREVIEW');
38-
$tag->template = <<<XML
42+
$tag->template = <<<'XML'
3943
<xsl:choose>
40-
<xsl:when test="parent::FANCYBOX-GALLERY-ITEM">
44+
<xsl:when test="ancestor::FANCYBOX-GALLERY-ITEM">
4145
<a data-fancybox="gallery" href="{@url}">
4246
<img data-lazy-src="{@url}" alt="" loading="lazy"/>
4347
</a>
4448
</xsl:when>
49+
<xsl:when test="ancestor::DISCUSSION-EXCERPT">
50+
<a data-fancybox="excerpt-gallery" href="{@url}">
51+
<img data-lazy-src="{@url}" alt="" class="excerpt-image"/>
52+
</a>
53+
</xsl:when>
4554
<xsl:otherwise>
4655
<a data-fancybox="single" href="{@url}">
4756
<img src="{@url}" alt="" loading="lazy"/>
4857
</a>
4958
</xsl:otherwise>
5059
</xsl:choose>
51-
XML;
60+
XML;
5261
}
5362
}
54-
}
63+
}

src/WrapImagesInGallery.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,37 @@
33
namespace Flectar\Fancybox;
44

55
use s9e\TextFormatter\Renderer;
6-
use s9e\TextFormatter\Utils;
76

87
class WrapImagesInGallery
98
{
10-
const MATCH_IMG_TAGS = '((?:<UPL-IMAGE-PREVIEW[^>]*>(?:(?!<\/UPL-IMAGE-PREVIEW>).)*<\/UPL-IMAGE-PREVIEW>)|(?:<IMG[^>]*>(?:(?!<\/IMG>).)*<\/IMG>))';
11-
const MATCH_GALLERY_REGEX = '((?:'.self::MATCH_IMG_TAGS.'(?:\s*(?:<br\/>|<br>|\n)\s*)?){2,})';
12-
139
public function __invoke(Renderer $renderer, $context, string $xml): string
1410
{
15-
return preg_replace_callback('/'.self::MATCH_GALLERY_REGEX.'/m', function ($matches) {
16-
$images = array_filter(preg_split('/\s*(?:<br\/>|<br>|\n)\s*/', $matches[0]), function($img) {
17-
return !empty($img);
18-
});
19-
$galleryItems = array_map(function($img) {
20-
return '<FANCYBOX-GALLERY-ITEM>' . trim($img) . '</FANCYBOX-GALLERY-ITEM>';
21-
}, $images);
11+
$pattern = '/' .
12+
'(?:<[IPr]\s[^>]*>)*' .
13+
'(' .
14+
'(?:' .
15+
'(?:<IMG[^>]*>(?:<\/IMG>)?|<UPL-IMAGE-PREVIEW[^>]*>(?:<\/UPL-IMAGE-PREVIEW>)?)' .
16+
'(?:\s*<[br]\s*\/?>\s*)*' .
17+
'){2,}' .
18+
')' .
19+
'(?:<\/[IPr]>)*' .
20+
'/s';
21+
22+
return preg_replace_callback($pattern, function ($matches) {
23+
$imagesBlock = $matches[1];
2224

23-
return '<FANCYBOX-GALLERY>' . implode('', $galleryItems) . '</FANCYBOX-GALLERY>';
25+
$images = preg_split('/<[br]\s*\/?>/', $imagesBlock);
26+
$images = array_filter(array_map('trim', $images));
27+
28+
if (count($images) < 2) {
29+
return $matches[0];
30+
}
31+
32+
$wrappedImages = array_map(function($img) {
33+
return '<FANCYBOX-GALLERY-ITEM>' . $img . '</FANCYBOX-GALLERY-ITEM>';
34+
}, $images);
35+
36+
return '<FANCYBOX-GALLERY>' . implode('', $wrappedImages) . '</FANCYBOX-GALLERY>';
2437
}, $xml);
2538
}
26-
}
39+
}

0 commit comments

Comments
 (0)