|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Twig; |
|
4
|
|
|
|
|
5
|
|
|
use Cocur\Slugify\Slugify; |
|
6
|
|
|
use PiedWeb\CMSBundle\Service\PageCanonicalService; |
|
7
|
|
|
use PiedWeb\RenderAttributes\AttributesTrait; |
|
8
|
|
|
use Twig\Extension\AbstractExtension; |
|
9
|
|
|
use Twig\TwigFilter; |
|
10
|
|
|
use Twig\TwigFunction; |
|
11
|
|
|
use Twig_Environment; |
|
12
|
|
|
|
|
13
|
|
|
class AppExtension extends AbstractExtension |
|
14
|
|
|
{ |
|
15
|
|
|
use AttributesTrait; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(PageCanonicalService $pageCanonical) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->pageCanonical = $pageCanonical; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function getFilters() |
|
23
|
|
|
{ |
|
24
|
|
|
return [ |
|
25
|
|
|
new TwigFilter('html_entity_decode', 'html_entity_decode'), |
|
26
|
|
|
new TwigFilter('punctuation_beautifer', [AppExtension::class, 'punctuationBeautifer'], ['is_safe' => ['html']]), |
|
27
|
|
|
]; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function getFunctions() |
|
31
|
|
|
{ |
|
32
|
|
|
return [ |
|
33
|
|
|
new TwigFunction('homepage', [$this->pageCanonical, 'generatePathForHomepage']), |
|
34
|
|
|
new TwigFunction('page', [$this->pageCanonical, 'generatePathForPage']), |
|
35
|
|
|
new TwigFunction('jslink', [AppExtension::class, 'renderJavascriptLink'], [ |
|
36
|
|
|
'is_safe' => ['html'], |
|
37
|
|
|
'needs_environment' => true, |
|
38
|
|
|
]), |
|
39
|
|
|
new TwigFunction('link', [AppExtension::class, 'renderJavascriptLink'], [ |
|
40
|
|
|
'is_safe' => ['html'], |
|
41
|
|
|
'needs_environment' => true, |
|
42
|
|
|
]), |
|
43
|
|
|
new TwigFunction('mail', [AppExtension::class, 'renderEncodedMail'], [ |
|
44
|
|
|
'is_safe' => ['html'], |
|
45
|
|
|
'needs_environment' => true, |
|
46
|
|
|
]), |
|
47
|
|
|
new TwigFunction( |
|
48
|
|
|
'bookmark', |
|
49
|
|
|
[AppExtension::class, 'renderTxtBookbmark'], |
|
50
|
|
|
['is_safe' => ['html'], 'needs_environment' => true] |
|
51
|
|
|
), |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public static function renderTxtBookbmark(Twig_Environment $env, $name) |
|
56
|
|
|
{ |
|
57
|
|
|
$slugify = new Slugify(); |
|
58
|
|
|
$name = $slugify->slugify($name); |
|
59
|
|
|
|
|
60
|
|
|
return $env->render('@PiedWebCMS/component/_txt_bookmark.html.twig', ['name' => $name]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public static function renderEncodedMail(Twig_Environment $env, $mail) |
|
64
|
|
|
{ |
|
65
|
|
|
return $env->render('@PiedWebCMS/component/_encoded_mail.html.twig', ['mail' => str_rot13($mail)]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public static function punctuationBeautifer($text) |
|
69
|
|
|
{ |
|
70
|
|
|
return str_replace( |
|
71
|
|
|
[' ;', ' :', ' ?', ' !', '« ', ' »', '« ', ' »'], |
|
72
|
|
|
[' ;', ' :', ' ?', ' !', '« ', ' »', '« ', ' »'], |
|
73
|
|
|
$text |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public static function renderJavascriptLink(Twig_Environment $env, $anchor, $path, $attr = []) |
|
78
|
|
|
{ |
|
79
|
|
|
if (0 === strpos($path, 'http://')) { |
|
80
|
|
|
$path = '-'.substr($path, 7); |
|
81
|
|
|
} elseif (0 === strpos($path, 'https://')) { |
|
82
|
|
|
$path = '_'.substr($path, 8); |
|
83
|
|
|
} elseif (0 === strpos($path, 'mailto:')) { |
|
84
|
|
|
$path = '@'.substr($path, 7); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $env->render('@PiedWebCMS/component/_javascript_link.html.twig', [ |
|
88
|
|
|
'attr' => self::mergeAndMapAttributes($attr, ['data-rot' => str_rot13($path)]), |
|
89
|
|
|
'anchor' => $anchor, |
|
90
|
|
|
]); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|