1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (C) 2016 Billie Thompson |
5
|
|
|
* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace PurpleBooth; |
11
|
|
|
|
12
|
|
|
use ReflectionClass; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This is sets up the twig extension. |
16
|
|
|
*/ |
17
|
|
|
class HtmlStripperExtension extends \Twig_Extension |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var HtmlStripper |
21
|
|
|
*/ |
22
|
|
|
private $htmlStripper; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* HtmlStripperExtension constructor. |
26
|
|
|
*/ |
27
|
3 |
|
public function __construct() |
28
|
|
|
{ |
29
|
3 |
|
$this->htmlStripper = new HtmlStripperImplementation(); |
30
|
3 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* This gets an array of the filters that this extension provides. |
34
|
|
|
* |
35
|
|
|
* It'll return Twig_SimpleFilter on versions of twig less than ^1.0.0 of twig, and Twig_Filter on versions of |
36
|
|
|
* twig ^2.0.0 |
37
|
|
|
* |
38
|
|
|
* @return \Twig_SimpleFilter[]|\Twig_Filter[] |
39
|
|
|
*/ |
40
|
|
|
public function getFilters() |
41
|
|
|
{ |
42
|
|
|
$filters = []; |
43
|
|
|
|
44
|
|
|
$reflection = new ReflectionClass('\Twig_Filter'); |
45
|
|
|
if ($reflection->isInstantiable()) { |
46
|
|
|
$filters[] = new \Twig_Filter( |
47
|
|
|
'html_strip', |
48
|
|
|
[$this->htmlStripper, 'toText'] |
49
|
|
|
); |
50
|
|
|
} else { |
51
|
|
|
// BC Twig ^1.0.0 |
52
|
|
|
$filters[] = new \Twig_SimpleFilter( |
53
|
|
|
'html_strip', |
54
|
|
|
[$this->htmlStripper, 'toText'] |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $filters; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* A convenient name for this extension. |
63
|
|
|
* |
64
|
|
|
* BC Twig ^v1.0.0 |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
1 |
|
public function getName() |
69
|
|
|
{ |
70
|
1 |
|
return 'html_stripper'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* This is the extension itself, it's mostly a small wrapper around the actual parser. |
75
|
|
|
* |
76
|
|
|
* @see https://github.com/PurpleBooth/htmlstrip |
77
|
|
|
* @deprecated v1.1.0 This will be removed in the future, please use the PurpleBooth/htmlstrip library |
78
|
|
|
* |
79
|
|
|
* @param string $html |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
1 |
|
public function toText($html) |
84
|
|
|
{ |
85
|
1 |
|
return $this->htmlStripper->toText($html); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|