-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLightbox.php
46 lines (30 loc) · 993 Bytes
/
Lightbox.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/*
* Automad/Lightbox
* Copyright (c) 2020 by Marc Anton Dahmen
* https://marcdahmen.de
* Licensed under the MIT license.
*/
namespace Automad;
use Automad\Core as Core;
defined('AUTOMAD') or die('Direct access not permitted!');
class Lightbox {
public function Lightbox($str) {
// The pattern skips images already wrapped in a link.
$regex = '/(?<!\[)\!\[[^\]]*\]\(([\w\-\/\?\&\=\:\.]+)(?:\?(\d+)x(\d+))?\)/i';
$str = preg_replace_callback($regex, function($match) {
$file = AM_BASE_DIR . $match[1];
$link = $match[1];
$caption = '';
if (is_readable($file)) {
$caption = Core\Str::stripTags(Core\Parse::caption($file));
if (!empty($match[2]) && !empty($match[3])) {
$image = new Core\Image($file, $match[2], $match[3], true);
$link = $image->file;
}
}
return '<a href="' . $match[1] . '" class="automad-lightbox-item" data-caption="' . $caption . '"><img src="' . $link . '"></a>';
}, $str);
return $str;;
}
}