Skip to content
This repository has been archived by the owner on Jun 20, 2018. It is now read-only.

Commit

Permalink
Merge pull request #2 from EdgedesignCZ/feature/zd-366
Browse files Browse the repository at this point in the history
User can define custom attributes
  • Loading branch information
zdenekdrahos committed Mar 30, 2015
2 parents 2b530b0 + 61e77f1 commit 713ffe4
Show file tree
Hide file tree
Showing 26 changed files with 417 additions and 546 deletions.
20 changes: 0 additions & 20 deletions Configurator/IConfigurator.php

This file was deleted.

20 changes: 11 additions & 9 deletions DependencyInjection/EdgeTexyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,18 @@
namespace Edge\TexyBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Texy;

class EdgeTexyExtension extends Extension
{

public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('config.yml');

$config = $this->mergeConfigs($configs);

if(array_key_exists('filters', $config)){
$managerDefinition = $container->getDefinition('edge_texy.manager');
$managerDefinition->addMethodCall('setDefinitions', array($config['filters']));
}
$this->updateParameters($config, $container);
}

/**
Expand Down Expand Up @@ -89,4 +81,14 @@ public function mergeConfigs(array $configs)

return $config;
}

public function updateParameters(array $config, ContainerBuilder $container)
{
$parameters = array('filters', 'custom_attributes');
foreach ($parameters as $param) {
if (array_key_exists($param, $config)) {
$container->setParameter("edge_texy.{$param}", $config[$param]);
}
}
}
}
7 changes: 4 additions & 3 deletions EdgeTexyBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Edge texy bundle
*/
class EdgeTexyBundle extends Bundle
{
public function getContainerExtension()
{
return new DependencyInjection\EdgeTexyExtension();
}
}
14 changes: 0 additions & 14 deletions Exceptions/InstanceNotFoundException.php

This file was deleted.

42 changes: 0 additions & 42 deletions Manager/IManager.php

This file was deleted.

75 changes: 0 additions & 75 deletions Manager/TexyManager.php

This file was deleted.

25 changes: 0 additions & 25 deletions Processor/IProcessor.php

This file was deleted.

35 changes: 0 additions & 35 deletions Processor/TexyProcessor.php

This file was deleted.

52 changes: 40 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ This is a simple bundle wrapping up Texy! text processor (http://texy.info/en) i

## Setup

`composer require edgedesign/texy-bundle` should do the trick.

Don't forget to register `Edge\TexyBundle\EdgeTexyBundle` in your AppKernel.
1. `composer require edgedesign/texy-bundle`
2. Add `new Edge\TexyBundle\EdgeTexyBundle()` to your AppKernel.

## Configuration

Feel free to override `Resources/config/config.yml` directives.
You can change classes which will be instantiated, if you are not satisfied with our implementation,
just implement correct interfaces please.

### Custom attributes

In section `edge_texy_bundle.custom_attributes` you can define custom html attributes which will
be added to every html element. Useful for allowing html5 attributes (`download`, `data-something`, ...).

### Filters

In section `edge_texy_bundle.filters` you can set-up your filters.
First-level key determines name of filter.

Expand All @@ -31,7 +37,23 @@ If this settings is not enough, you can extend Texy, set it up in constructor an
```
edge_texy_bundle.filters:
filter_from_my_class:
class: MyClassExtendingTexy
# Name of class which will be used to instantiate Texy instance.
class: MyClassExtendingTexy
# equivalent to $texy->allowed['block/code'] = FALSE;
allowed:
"block/code": false
# equivalent to $texy->allowedTags = array('a' => array('href', 'target'), 'em' => Texy::NONE, 'img' => Texy::ALL)
variables:
allowedTags:
a:
- href
- target
em: '-'
img: '*'
# equivalent to $texy->{name}Module->variable = value
modules:
link:
forceNoFollow: true;
```

In this case, settings allowed, module and variables will be passed to this extended class too.
Expand All @@ -41,11 +63,18 @@ In this case, settings allowed, module and variables will be passed to this exte
There are two ways how to use this bundle.

### Service
You can get TexyProcessor service named `edge_texy_bundle.class.processor` and call method `$processor->process($filterId, $text)`.

You can get TexyProcessor service named `edge_texy.processor` and call method

```
$processor->multiLineText($text, $filterId);
$processor->singleLineText($text, $filterId);
```

This will process your `$text` via filter set in config with id `$filterId`.

### Twig macros
### Twig

Second way is to use registered Twig macros '`texy_process`' and '`texy_process_line`'.

```twig
Expand All @@ -69,11 +98,14 @@ This way, given variable is passed through filter named `filterId`. If no filter

Difference between macros is that when using texy_process_line, Texy will not wrap given code in block tags like <p>.

### Settings examples
### Example config.yml

Settings example for sanitizing html output (put this into your config.yml):

```yaml
edge_texy:
custom_attributes: &custom_attributes
500: download
attribute_settings:
html5_attributes: &html5_attributes
100: itemid
Expand All @@ -86,6 +118,7 @@ edge_texy:
201: id
global_attributes: &global_attributes
<< : [*html5_attributes, *html_identifiers]
<< : *custom_attributes
filters:
sanitize:
allowed:
Expand Down Expand Up @@ -126,8 +159,3 @@ edge_texy:
Now, you can use filter `sanitize` to sanitize your HTML output from user
and filter `strip` to make user no HTML from user will appear in your code.
## Tests

Run tests with PHPUnit from root directory.

No reasonable tests are done though. My bad..
Loading

0 comments on commit 713ffe4

Please # to comment.