Skip to content

Commit

Permalink
Added emoticons dump command (closes #35), introduced bbcode_clean fi…
Browse files Browse the repository at this point in the history
…lter (closes #36), fixed short/long locale codes (closes #33)
  • Loading branch information
helios-ag committed Feb 1, 2013
1 parent 6e1b796 commit 13ed250
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 53 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
1.02.2013
* Now can accept short and long locale codes

30.01.2013
* Upgraded to Decoda 5.x
* Added clean filter (bbcode_clean, to strip-out tags)
* Emoticons Dump Command

14.03.2012:
* Root node for a configuration now called fm_bbcode
* Changed a lot of files, adda php template helper
Expand Down
65 changes: 65 additions & 0 deletions Command/DumpEmoticonsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace FM\BbcodeBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\HelperInterface;
/**
*
* @author Al Ganiev <[email protected]>
* @copyright 2013 Al Ganiev
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
class DumpEmoticonsCommand extends ContainerAwareCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('bbcode:dump')
->setDescription('dump emoticons to public folder');
}

/**
* Copies one folder to another
* @param $src
* @param $dst
*/
private function recurse_copy($src,$dst) {
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
$this->recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}

/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return int|null|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$webFolder = $this->getContainer()->get('kernel')->getRootDir().'/../web/emoticons';
@mkdir($webFolder);
$emoticonsFolder = $this->getContainer()->get('kernel')->getRootDir().'/../vendor/mjohnson/decoda/emoticons';
$this->recurse_copy($emoticonsFolder,$webFolder);

$output->writeln('<comment>Emoticons dumped succesfully</comment>');
}

}
43 changes: 43 additions & 0 deletions Composer/ScriptHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace FM\BbcodeBundle\Composer;

use Symfony\Component\ClassLoader\ClassCollectionLoader;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\PhpExecutableFinder;

/**
* @author Al Ganiev, original by Jordi Boggiano <[email protected]>
*/
class ScriptHandler
{
public static function installEmoticons($event)
{
static::executeCommand($event, 'app', 'bbcode:dump');
}

protected static function getPhp()
{
$phpFinder = new PhpExecutableFinder;
if (!$phpPath = $phpFinder->find()) {
throw new \RuntimeException('The php executable could not be found, add it to your PATH environment variable and try again');
}

return $phpPath;
}

protected static function executeCommand($event, $appDir, $cmd, $timeout = 300)
{
$php = escapeshellarg(self::getPhp());
$console = escapeshellarg($appDir.'/console');
if ($event->getIO()->isDecorated()) {
$console.= ' --ansi';
}

$process = new Process($php.' '.$console.' '.$cmd, null, null, null, $timeout);
$process->run(function ($type, $buffer) { echo $buffer; });
if (!$process->isSuccessful()) {
throw new \RuntimeException(sprintf('An error occurred when executing the "%s" command.', escapeshellarg($cmd)));
}
}
}
25 changes: 23 additions & 2 deletions Decoda/Decoda.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php

namespace FM\BbcodeBundle\Decoda;

use mjohnson\decoda\Decoda as BaseDecoda;
define('DECODA', __DIR__.'/../../../../../mjohnson/decoda/src/Decoda');
define('DECODA_FILTERS', DECODA.'/Filter');
define('DECODA_HOOKS', DECODA.'/Hook');

use Decoda\Decoda as BaseDecoda;
use Symfony\Component\DependencyInjection\ContainerInterface;
use \DomainException;

class Decoda extends BaseDecoda
{
Expand Down Expand Up @@ -36,6 +40,23 @@ public function loadFile($class)
}
}


public function setLocale($locale) {
$this->message(null);
if(strlen($locale)<3)
foreach($this->_messages as $key => $value){
$this->_messages[substr($key, 0, 2)] = $value;
unset($this->_messages[$key]);
}
if (empty($this->_messages[$locale])) {
throw new DomainException(sprintf('Localized strings for %s do not exist', $locale));
}

$this->_config['locale'] = $locale;

return $this;
}

/**
* Set messages
*
Expand Down
38 changes: 20 additions & 18 deletions Decoda/DecodaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
use FM\BbcodeBundle\Decoda\Decoda;
use FM\BbcodeBundle\Decoda\DecodaPhpEngine;
use Symfony\Component\DependencyInjection\ContainerInterface;
use mjohnson\decoda\filters\Filter,
mjohnson\decoda\filters\DefaultFilter,
mjohnson\decoda\filters\BlockFilter,
mjohnson\decoda\filters\CodeFilter,
mjohnson\decoda\filters\EmailFilter,
mjohnson\decoda\filters\ImageFilter,
mjohnson\decoda\filters\ListFilter,
mjohnson\decoda\filters\QuoteFilter,
mjohnson\decoda\filters\TextFilter,
mjohnson\decoda\filters\UrlFilter,
mjohnson\decoda\filters\VideoFilter;
use mjohnson\decoda\hooks\CensorHook,
mjohnson\decoda\hooks\ClickableHook,
mjohnson\decoda\hooks\CodeHook,
mjohnson\decoda\hooks\EmoticonHook,
mjohnson\decoda\hooks\Hook;
use Decoda\Filter,
Decoda\Filter\DefaultFilter,
Decoda\Filter\BlockFilter,
Decoda\Filter\CodeFilter,
Decoda\Filter\EmailFilter,
Decoda\Filter\ImageFilter,
Decoda\Filter\ListFilter,
Decoda\Filter\QuoteFilter,
Decoda\Filter\TextFilter,
Decoda\Filter\UrlFilter,
Decoda\Filter\VideoFilter;
use Decoda\Hook\CensorHook,
Decoda\Hook\ClickableHook,
Decoda\Hook\CodeHook,
Decoda\Hook\EmoticonHook,
Decoda\Hook;

/**
* @author Al Ganiev <[email protected]>
* @copyright 2012 Al Ganiev
* @copyright 2013 Al Ganiev
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
class DecodaManager
Expand Down Expand Up @@ -162,7 +162,7 @@ protected function applyHook(Decoda $code, $hook)
$code->addHook(new ClickableHook());
break;
case 'emoticon':
$code->addHook(new EmoticonHook());
$code->addHook(new EmoticonHook(array('path' => '/emoticons/')));
break;
case 'code':
$code->addHook(new CodeHook());
Expand Down Expand Up @@ -194,6 +194,8 @@ public function getResult()

$this->value->setEngine($decodaPhpEngine);

$this->value->addPath(DECODA.'/config');

foreach($this->filters as $filter)
{
$this->value = $this->applyFilter($this->value, $filter);
Expand Down
10 changes: 5 additions & 5 deletions Decoda/DecodaPhpEngine.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace FM\BbcodeBundle\Decoda;
use mjohnson\decoda\engines\PhpEngine;
use mjohnson\decoda\filters\Filter;
use Decoda\Engine\PhpEngine;
use Decoda\Filter;
use Exception;
/**
* DecodaPhpEngine
Expand Down Expand Up @@ -115,8 +115,8 @@ public function render(array $tag, $content)
* Sets the current filter.
*
* @access public
* @param \mjohnson\decoda\filters\Filter $filter
* @return DecodaTemplateEngineInterface
* @param \Decoda\Filter $filter
* @return \Decoda\Engine|\FM\BbcodeBundle\Decoda\DecodaPhpEngine
*/
public function setFilter(Filter $filter)
{
Expand All @@ -130,7 +130,7 @@ public function setFilter(Filter $filter)
*
* @access public
* @param string $path
* @return DecodaTemplateEngineInterface
* @return \Decoda\Engine|\FM\BbcodeBundle\Decoda\DecodaPhpEngine
*/
public function setPath($path)
{
Expand Down
17 changes: 0 additions & 17 deletions Decoda/Hook/EmoticonHook.php

This file was deleted.

19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ public function registerBundles()
);
}
```
### Step 3: Dump emoticons (optional)

To enable emoticons via emoticon hook, use the following command to copy emoticons images to
public folder (web/emoticons)

``` bash
./app/console bbcode:dump
```

## Basic configuration

Expand All @@ -58,7 +66,6 @@ public function registerBundles()
for [b], [i], [u], [s], [sub], [sup], [abbr], [br], [hr], [time]
BBCodes


### Examples to use the extension in your Twig template

Define BBCode filter in your config.yml:
Expand Down Expand Up @@ -119,6 +126,16 @@ Also you can define multiple filter sets under filter_sets parameter like this:

Please keep in mind, that whitelist tags suppress tags, that applied by filters configuration.


### Strip filter
To clear text from any bbcodes use bbcode_clean filter:
example:
``` jinja
{{'[b]some text[/b]'|bbcode_clean}}
```
This filter will eliminate any known to decoda tags


## Advanced configuration

### Overriding messages
Expand Down
42 changes: 34 additions & 8 deletions Templating/BbcodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,20 @@ public function __construct(ContainerInterface $container)
/**
* (non-PHPdoc)
* @see Twig_Extension::getFilters()
* @return array
*/
public function getFilters()
{
return array(
'bbcode_filter' => new \Twig_Filter_Method($this, 'filter', array('is_safe' => array('html'))),
'bbcode_clean' => new \Twig_Filter_Method($this, 'clean', array('is_safe' => array('html')))
);
}

/**
* @param $value
* @param $filter
* @return string
* @throws \Twig_Error_Runtime
* @return \FM\BbcodeBundle\Decoda\Decoda
*/
Expand All @@ -84,26 +87,49 @@ public function filter($value, $filter)
}

$code = new Decoda($value, $messages);

$currentFilter = $this->filterSets[$filter];

$locale = $currentFilter['locale'];
$xhtml = $currentFilter['xhtml'];
$strict = $currentFilter['strict'];
$isXhtml = $currentFilter['xhtml'];
$isStrict = $currentFilter['strict'];

if (empty($locale) || 'default' == $locale) {
$code->setLocale($this->container->get('request')->getLocale());
}
else {
$code->setLocale($locale);
}

if (true === $xhtml) {
$code->setXhtml(true);
}

$code->setStrict($strict);

$decoda_manager = new DecodaManager($code, $currentFilter['filters'], $currentFilter['hooks'], $currentFilter['whitelist']);
if ($isXhtml)
$code->setXhtml();

if($isStrict)
$code->setStrict();

$decodaManager = new DecodaManager($code, $currentFilter['filters'], $currentFilter['hooks'], $currentFilter['whitelist']);

return $decodaManager->getResult()->parse();
}

/**
*
* Strip tags
* @param $value
* @return string
* @throws \Twig_Error_Runtime
*/
public function clean($value)
{
if (!is_string($value)) {
throw new \Twig_Error_Runtime('The filter can be applied to strings only.');
}
$code = new Decoda($value);
$decodaManager = new DecodaManager($code,array('default','block','code','email','image','list','quote','text','url','video'));

return $decoda_manager->getResult()->parse();
return $decodaManager->getResult()->strip(true);
}

/**
Expand Down
Loading

0 comments on commit 13ed250

Please sign in to comment.