]> Raphaƫl G. Git Repositories - airbundle/blob - Twig/Bb2htmlExtension.php
Rewrite user in organizer route
[airbundle] / Twig / Bb2htmlExtension.php
1 <?php
2 // src/Rapsys/AirBundle/Twig/Bb2htmlExtension.php
3 namespace Rapsys\AirBundle\Twig;
4
5 class Bb2htmlExtension extends \Twig_Extension {
6 public function getFilters() {
7 return array(
8 new \Twig\TwigFilter(
9 'bb2html',
10 function($text) {
11 $ctx = bbcode_create(
12 array(
13 '' => array('type' => BBCODE_TYPE_ROOT),
14 'code' => array(
15 'type' => BBCODE_TYPE_OPTARG,
16 'open_tag' => '<pre class="{PARAM}">',
17 'close_tag' => '</pre>',
18 'default_arg' => '{CONTENT}'
19 ),
20 'ul' => array(
21 'type' => BBCODE_TYPE_NOARG,
22 'open_tag' => '<ul>',
23 'close_tag' => '</ul>',
24 'childs' => 'li'
25 ),
26 'li' => array(
27 'type' => BBCODE_TYPE_NOARG,
28 'open_tag' => '<li>',
29 'close_tag' => '</li>',
30 'parent' => 'ul',
31 'childs' => 'url'
32 ),
33 'url' => array(
34 'type' => BBCODE_TYPE_OPTARG,
35 'open_tag' => '<a href="{PARAM}">',
36 'close_tag' => '</a>',
37 'default_arg' => '{CONTENT}',
38 'parent' => 'p,li'
39 )
40 )
41 );
42 $text = nl2br(bbcode_parse($ctx, htmlspecialchars($text)));
43 if (preg_match_all('#\<pre[^>]*\>(.*?)\</pre\>#s', $text, $matches) && !empty($matches[1])) {
44 foreach($matches[1] as $string) {
45 $text = str_replace($string, str_replace('<br />', '', $string), $text);
46 }
47 }
48 if (preg_match_all('#\<ul[^>]*\>(.*?)\</ul\>#s', $text, $matches) && !empty($matches[1])) {
49 foreach($matches[1] as $string) {
50 $text = str_replace($string, str_replace('<br />', '', $string), $text);
51 }
52 }
53 $text = preg_replace(
54 array('#(<br />(\r?\n?))*<pre#s', '#</pre>(<br />(\r?\n?))*#', '#(<br />(\r?\n?))*<ul#s', '#</ul>(<br />(\r?\n?))*#', '#(<br />(\r?\n?)){2,}#'),
55 array('</p>\2<pre', '</pre>\2<p>', '</p>\2<ul', '</ul>\2<p>', '</p>\2<p>'),
56 $text
57 );
58 return $text;
59 },
60 array('is_safe' => array('html'))
61 )
62 );
63 }
64 }