diff --git a/README.md b/README.md index 5880611..fa689a4 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,8 @@ composer require jolicode/jolitypo Integrations =========== -- (Official) [Symfony2 Bundle and twig extension](https://github.com/jolicode/JoliTypoBundle) +- (Built-in) [Symfony Bundle](src/JoliTypo/Bridge/Symfony) +- (Built-in) [Twig extension](src/JoliTypo/Bridge/Twig) - [Wordpress plugin](http://wordpress.org/plugins/typofr/) - [Drupal module](https://github.com/Anaethelion/JoliTypo-for-Drupal) diff --git a/TODO.md b/TODO.md index 3287d61..fbfbfd2 100644 --- a/TODO.md +++ b/TODO.md @@ -47,3 +47,12 @@ en-GB ----- - [x] Make sure all the rules described [here](http://practicaltypography.com/summary-of-key-rules.html) are respected; + +Bridge +====== + +Symfony +------- + +- Allow to set service as Fixer via an `@` +- Use Lazy services for all the presets diff --git a/composer.json b/composer.json index e4470a3..22dac3c 100644 --- a/composer.json +++ b/composer.json @@ -18,17 +18,17 @@ "lib-libxml": "*", "org_heigl/hyphenator": "~2.0.3" }, + "require-dev": { + "friendsofphp/php-cs-fixer": "~2.2.0", + "phpunit/phpunit": "~4.5", + "symfony/framework-bundle": "^2.3|^3.0|^4.0" + }, "conflict": { "ext-apc": "3.1.11" }, "autoload": { - "psr-0": { - "JoliTypo": "src/" - } - }, - "require-dev": { - "friendsofphp/php-cs-fixer": "~2.2.0", - "phpunit/phpunit": "~4.5" + "psr-0": { "JoliTypo": "src/" }, + "psr-4": { "JoliTypo\\": "" } }, "scripts": { "test": "vendor/bin/phpunit -c phpunit.xml.dist", diff --git a/src/JoliTypo/Bridge/Symfony/DependencyInjection/Configuration.php b/src/JoliTypo/Bridge/Symfony/DependencyInjection/Configuration.php new file mode 100644 index 0000000..c628049 --- /dev/null +++ b/src/JoliTypo/Bridge/Symfony/DependencyInjection/Configuration.php @@ -0,0 +1,38 @@ +root('joli_typo'); + + $rootNode + ->fixXmlConfig('preset') + ->children() + ->arrayNode('presets') + ->useAttributeAsKey('name') + ->isRequired() + ->prototype('array') + ->children() + ->scalarNode('locale')->end() + ->arrayNode('fixers') + ->prototype('scalar') + ->end() + ->end() + ->end() + ->end() + ->end() + ; + + return $treeBuilder; + } +} diff --git a/src/JoliTypo/Bridge/Symfony/DependencyInjection/JoliTypoExtension.php b/src/JoliTypo/Bridge/Symfony/DependencyInjection/JoliTypoExtension.php new file mode 100644 index 0000000..2012098 --- /dev/null +++ b/src/JoliTypo/Bridge/Symfony/DependencyInjection/JoliTypoExtension.php @@ -0,0 +1,54 @@ +processConfiguration($configuration, $configs); + $presets = $this->createPresetDefinition($container, $config); + + // Twig extension + $twig_extension = new Definition('JoliTypo\Bridge\Twig\JoliTypoExtension'); + $twig_extension->addTag('twig.extension'); + $twig_extension->setArguments(array($presets)); + + $container->setDefinition('joli_typo.twig_extension', $twig_extension); + } + + private function createPresetDefinition(ContainerBuilder $container, $config) + { + $presets = array(); + + foreach ($config['presets'] as $name => $preset) { + $definition = new Definition('JoliTypo\Fixer'); + + if ($preset['locale']) { + $definition->addMethodCall('setLocale', array($preset['locale'])); + } + + $fixers = array(); + foreach ($preset['fixers'] as $fixer) { + // Allow to use services as fixer? + $fixers[] = $fixer; + } + + $definition->addArgument($fixers); + $container->setDefinition(sprintf('joli_typo.fixer.%s', $name), $definition); + + $presets[$name] = new Reference(sprintf('joli_typo.fixer.%s', $name)); + } + + return $presets; + } +} diff --git a/src/JoliTypo/Bridge/Symfony/JoliTypoBundle.php b/src/JoliTypo/Bridge/Symfony/JoliTypoBundle.php new file mode 100644 index 0000000..aef4821 --- /dev/null +++ b/src/JoliTypo/Bridge/Symfony/JoliTypoBundle.php @@ -0,0 +1,9 @@ +Hi folk!

', 'fr') | raw }} + +{# or #} + +{{ '

Hi folk!

' | jolitypo('fr') }} +``` + +Another way to use it is by passing a whole block to it: + +```twig +{% block content %} + {{ jolitypo(block('real_content'), 'fr') | raw }} +{% endblock %} + +{% block real_content %} +

My whole dynamic page

+{% endblock %} +``` diff --git a/src/JoliTypo/Bridge/Twig/JoliTypoExtension.php b/src/JoliTypo/Bridge/Twig/JoliTypoExtension.php new file mode 100644 index 0000000..a0a19c7 --- /dev/null +++ b/src/JoliTypo/Bridge/Twig/JoliTypoExtension.php @@ -0,0 +1,42 @@ +presets = $presets; + } + + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('jolitypo', array($this, 'translate')), + ); + } + + public function getFilters() + { + return array( + new \Twig_SimpleFilter('jolitypo', array($this, 'translate'), array('pre_escape' => 'html', 'is_safe' => array('html'))), + ); + } + + public function translate($text, $preset = "default") + { + if (!isset($this->presets[$preset])) { + throw new InvalidConfigurationException(sprintf("There is no '%s' preset configured.", $preset)); + } + + return $this->presets[$preset]->fix($text); + } + + public function getName() + { + return 'jolitypo'; + } +}