helios-ag / FMBbCodeBundle

:capital_abcd: BBCode bundle for Symfony projects
Other
57 stars 35 forks source link

Create personalised filters #24

Closed remontees closed 11 years ago

remontees commented 12 years ago

Hello,

I tried to use a personalised filter to FMBbCodeBundle, but my Twig Filter don't run with your bundle. And in the doc, there is no documentation to create personalised Filters for this bundle. What can I do ?

piratadelfuturo commented 12 years ago

I don't use twig, so the part of the personalized Filters maybe is not compatible, I think I will have to write a little document for the personalized filters, my config looks like this:

fm_bbcode:
    config:
      filters:
        - { classname: boom_video, class: \Boom\Bundle\FrontBundle\Decoda\Filter\BoomVideoFilter }
        - { classname: boom_gallery, class: "@boom_front.gallery.decoda.filter" }
      templates:
        - path: %kernel.root_dir%/Resources/Decoda/templates/
    filter_sets:
        default:
          locale: es
          xhtml: true
          filters: [ default, quote, url, image, boom_video, boom_gallery, list, li, olist, p]

If you see there is a section named filters where you can pass classnames or service references.

remontees commented 12 years ago

Yes, but Filters class. How I can write them ?

piratadelfuturo commented 12 years ago

For that you will have to take a look at the current filters that come from PHP-Decoda

piratadelfuturo commented 12 years ago

Oh @helios-ag already wrote the tutorial, thanks :)

remontees commented 12 years ago

Because I've done this code : public function galerieParse($code) { // PARSE REGEX $code = preg_replace_callback('#[galerie][photo]\/(\w+)\/([^\/]+)\/([0-9]+).([a-z]{3})[\/photo][\/galerie]#', array($this,'photoCallback'), $code);

    $code = preg_replace_callback('#\[galerie\]\[photo\]\/(\w+)\/([^\/]+)\/([0-9]+)\.([a-z]{3})\[\/photo\]\[legende\](\w+)\[\/legende\]\[\/galerie\]#', array($this,'photoCallback2'), $code);

    return $code;
}

public function photoCallback($code)
{
    if(file_exists(__DIR__ . '/../../../../web/uploads/galerie/' . $code[1] . '/photo_' . $code[3] . '.' . $code[4]))
    {
        return '<img src="/' . $code[1] . '/' . $code[2] . '/photo_' . $code[3] . '.' . $code[4] . '" alt="Galerie photo" />';
    }

    return __DIR__ . '/../../../../web/uploads/galerie/' . $code[1] . '/photo_' . $code[3] . '.' . $code[4];
}

public function photoCallback2($code)
{
    if(file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . $code[1] . '/' . $code[2] . '/photo_' . $code[3] . '.' . $code[4]))
    {
        return '<figure><img src="/' . $code[1] . '/' . $code[2] . '/photo_' . $code[3] . '.' . $code[4] . '" alt="' . $code[5] . '" /><figcaption>' . $code[4] . '</figcation></figure>';
    }

    return null;
}

But I don't know how I can integrate this to FMBbCodeBundle. The documentation doesn't tell it very well.

remontees commented 12 years ago

Here is my code :

fm_bbcode:
 config:
   hooks:
     - { classname: galerie, class: \Rms\GalerieBundle\Service\RmsGalerieBBCode }
 filter_sets:
     bbcode_basic:
         locale: ru
         xhtml: true
         filters: [ default, block ]
     bbcode_forum:
         locale: ru
         xhtml: true
         filters: [ default, block, image, list, quote, text, url ]
     bbcode_reportages:
         locale: ru
         xhtml: true
         filters: [ default, block, image, list, quote, text, url, galerie ]

But it doesn't work. galerie doesn't work. Here is my hook :

 <?php

 namespace Rms\GalerieBundle\Service;

 use mjohnson\decoda\Decoda;
 use mjohnson\decoda\hooks\HookAbstract;

 /**
  * Système pour parser les photos de la galerie photo
  *
  * @author remontees
  */
 class GalerieHook extends HookAbstract
 {
public function beforeParse($content)
{
    return galerieParse($content);
}

/**
 * Parse la photo de la galerie
 *
 * @param string $code
 */
public function galerieParse($code)
{
    // PARSE REGEX
    $code = preg_replace_callback('#\[galerie\]\[photo\]\/(\w+)\/([^\/]+)\/([0-9]+)\.([a-z]{3})\[\/photo\]\[\/galerie\]#', array($this,'photoCallback'),  $code);

    $code = preg_replace_callback('#\[galerie\]\[photo\]\/(\w+)\/([^\/]+)\/([0-9]+)\.([a-z]{3})\[\/photo\]\[legende\](\w+)\[\/legende\]\[\/galerie\]#', array($this,'photoCallback2'), $code);

    return $code;
}

public function photoCallback($code)
{
    if(file_exists(__DIR__ . '/../../../../web/uploads/galerie/' . $code[1] . '/photo_' . $code[3] . '.' . $code[4]))
    {
        return '<img src="/' . $code[1] . '/' . $code[2] . '/photo_' . $code[3] . '.' . $code[4] . '" alt="Galerie photo" />';
    }

    return __DIR__ . '/../../../../web/uploads/galerie/' . $code[1] . '/photo_' . $code[3] . '.' . $code[4];
}

public function photoCallback2($code)
{
    if(file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . $code[1] . '/' . $code[2] . '/photo_' . $code[3] . '.' . $code[4]))
    {
        return '<figure><img src="/' . $code[1] . '/' . $code[2] . '/photo_' . $code[3] . '.' . $code[4] . '" alt="' . $code[5] . '" /><figcaption>' . $code[4] . '</figcation></figure>';
    }

    return null;
}
 }
piratadelfuturo commented 12 years ago

For filter development you should read PHP-Decoda, FMBbCodeBundle uses PHP-Decoda 3.x, I think you are making a filter for 4.x

Check both packages documentation.

remontees commented 12 years ago

And where is the doc for 3.x ?

helios-ag commented 12 years ago

Select appropriate branch in php-decoda repository.

helios-ag commented 12 years ago

And i think that checking files for existence in your filter is bad idea.

remontees commented 12 years ago

Yes, but I can't find also the documentation for 3.x branch. But Hooks are the good solution to integrate my work ? You could add this in the doc, isn't it ?