diff --git a/.gitignore b/.gitignore
index aafa360..722e980 100755
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@ phpunit.xml
vendor/*
!vendor/parser
composer.lock
+Tests/fixtures/app/cache
diff --git a/DependencyInjection/Compiler/ParsersCompilerPass.php b/DependencyInjection/Compiler/ParsersCompilerPass.php
index 55b59f4..5a260fc 100644
--- a/DependencyInjection/Compiler/ParsersCompilerPass.php
+++ b/DependencyInjection/Compiler/ParsersCompilerPass.php
@@ -10,7 +10,7 @@ class ParsersCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
- if (!$container->hasDefinition('templating.helper.markdown')) {
+ if (!$container->hasDefinition('markdown.parser.parser_manager')) {
return;
}
@@ -24,7 +24,7 @@ class ParsersCompilerPass implements CompilerPassInterface
$defaultAlias = isset($defaultAlias['alias']) ? $defaultAlias['alias'] : null;
}
- $definition = $container->getDefinition('templating.helper.markdown');
+ $definition = $container->getDefinition('markdown.parser.parser_manager');
if (empty($defaultAlias)) {
$definition->addMethodCall('addParser', array(new Reference('markdown.parser'), 'default'));
}
diff --git a/Helper/MarkdownHelper.php b/Helper/MarkdownHelper.php
index e4d3718..09754c5 100644
--- a/Helper/MarkdownHelper.php
+++ b/Helper/MarkdownHelper.php
@@ -2,24 +2,17 @@
namespace Knp\Bundle\MarkdownBundle\Helper;
+use Knp\Bundle\MarkdownBundle\Parser\ParserManager;
use Symfony\Component\Templating\Helper\HelperInterface;
-use Knp\Bundle\MarkdownBundle\MarkdownParserInterface;
class MarkdownHelper implements HelperInterface
{
- /**
- * @var MarkdownParserInterface[]
- */
- private $parsers = array();
+ private $parserManager;
private $charset = 'UTF-8';
- /**
- * @param MarkdownParserInterface $parser
- * @param string $alias
- */
- public function addParser(MarkdownParserInterface $parser, $alias)
+ public function __construct(ParserManager $parserManager)
{
- $this->parsers[$alias] = $parser;
+ $this->parserManager = $parserManager;
}
/**
@@ -34,17 +27,7 @@ class MarkdownHelper implements HelperInterface
*/
public function transform($markdownText, $parserName = null)
{
- if (null === $parserName) {
- $parserName = 'default';
- }
-
- if (!isset($this->parsers[$parserName])) {
- throw new \RuntimeException(sprintf('Unknown parser selected ("%s"), available are: %s', $parserName, implode(', ', array_keys($this->parsers))));
- }
-
- $parser = $this->parsers[$parserName];
-
- return $parser->transformMarkdown($markdownText);
+ return $this->parserManager->transform($markdownText, $parserName);
}
/**
diff --git a/Parser/ParserManager.php b/Parser/ParserManager.php
new file mode 100644
index 0000000..dd31316
--- /dev/null
+++ b/Parser/ParserManager.php
@@ -0,0 +1,47 @@
+parsers[$alias] = $parser;
+ }
+
+ /**
+ * Transforms markdown syntax to HTML
+ *
+ * @param string $markdownText The markdown syntax text
+ * @param null|string $parserName
+ *
+ * @return string The HTML code
+ *
+ * @throws \RuntimeException
+ */
+ public function transform($markdownText, $parserName = null)
+ {
+ if (null === $parserName) {
+ $parserName = 'default';
+ }
+
+ if (!isset($this->parsers[$parserName])) {
+ throw new \RuntimeException(sprintf('Unknown parser selected ("%s"), available are: %s', $parserName, implode(', ', array_keys($this->parsers))));
+ }
+
+ $parser = $this->parsers[$parserName];
+
+ return $parser->transformMarkdown($markdownText);
+ }
+}
diff --git a/Resources/config/parser.xml b/Resources/config/parser.xml
index a2e8955..f94c532 100644
--- a/Resources/config/parser.xml
+++ b/Resources/config/parser.xml
@@ -19,5 +19,8 @@
hi
\n", $actual, 'There is a default parser'); + + $actual = $parserManager->transform('*hi*', 'light'); + $this->assertEquals("hi
\n", $actual, 'Specific parsers are registered'); + } +} diff --git a/Tests/fixtures/app/TestKernel.php b/Tests/fixtures/app/TestKernel.php new file mode 100644 index 0000000..13b4653 --- /dev/null +++ b/Tests/fixtures/app/TestKernel.php @@ -0,0 +1,30 @@ +load(function(ContainerBuilder $c) { + $c->loadFromExtension('framework', array( + 'secret' => 'MarkdownTesting' + )); + }); + } +} \ No newline at end of file diff --git a/Twig/Extension/MarkdownTwigExtension.php b/Twig/Extension/MarkdownTwigExtension.php index 9dfdcf6..0460612 100644 --- a/Twig/Extension/MarkdownTwigExtension.php +++ b/Twig/Extension/MarkdownTwigExtension.php @@ -2,15 +2,15 @@ namespace Knp\Bundle\MarkdownBundle\Twig\Extension; -use Knp\Bundle\MarkdownBundle\Helper\MarkdownHelper; +use Knp\Bundle\MarkdownBundle\Parser\ParserManager; class MarkdownTwigExtension extends \Twig_Extension { - protected $helper; + protected $parserManager; - public function __construct(MarkdownHelper $helper) + public function __construct(ParserManager $parserManager) { - $this->helper = $helper; + $this->parserManager = $parserManager; } public function getFilters() @@ -22,7 +22,7 @@ class MarkdownTwigExtension extends \Twig_Extension public function markdown($text, $parser = null) { - return $this->helper->transform($text, $parser); + return $this->parserManager->transform($text, $parser); } public function getName()