Fix way how Sundown parser is added

Fix detection of default parser when none is set
Add Flavored preset from KnpBundles.com
This commit is contained in:
Joseph Bielawski 2012-10-17 12:00:37 +02:00
parent 3e57c2a866
commit bd597e66a1
5 changed files with 67 additions and 27 deletions

View file

@ -14,23 +14,22 @@ class ParsersCompilerPass implements CompilerPassInterface
return;
}
if (!$container->hasDefinition('markdown.parser')) {
if (!$definition = $container->findDefinition('markdown.parser')) {
return;
}
$defaultParserTag = $container->getDefinition('markdown.parser')->getTag('markdown.parser');
$definition = $container->getDefinition('templating.helper.markdown');
$defaultAlias = current($definition->getTag('markdown.parser'));
$defaultAlias = $defaultAlias['alias'];
$definition = $container->getDefinition('templating.helper.markdown');
foreach ($container->findTaggedServiceIds('markdown.parser') as $id => $tags) {
if ($defaultParserTag == $id) {
$definition->addMethodCall('addParser', array(new Reference($id), 'default'));
continue;
}
foreach ($tags as $attributes) {
$alias = empty($attributes['alias']) ? $id : $attributes['alias'];
$definition->addMethodCall('addParser', array(new Reference($id), $alias));
if ($defaultAlias == $alias) {
$definition->addMethodCall('addParser', array(new Reference($id), 'default'));
} else {
$definition->addMethodCall('addParser', array(new Reference($id), $alias));
}
}
}
}

View file

@ -29,12 +29,20 @@ class KnpMarkdownExtension extends Extension
$loader->load('helper.xml');
$loader->load('twig.xml');
if ($config['parser']['service'] == 'markdown.parser.sundown' && !class_exists('Sundown\Markdown')) {
throw new InvalidConfigurationException('Sundown parser selected, but required extension is not installed or configured.');
if ('markdown.parser.sundown' == $config['parser']['service']) {
if (!class_exists('Sundown\\Markdown')) {
throw new InvalidConfigurationException('Sundown parser selected, but required extension is not installed or configured.');
}
$loader->load('sundown.xml');
$definition = $container->getDefinition('markdown.parser.sundown');
$definition->addTag('markdown.parser', array('alias' => 'sundown'));
$container->setParameter('markdown.sundown.extensions', $config['sundown']['extensions']);
$container->setParameter('markdown.sundown.render_flags', $config['sundown']['render_flags']);
}
$container->setParameter('markdown.sundown.extensions', $config['sundown']['extensions']);
$container->setParameter('markdown.sundown.render_flags', $config['sundown']['render_flags']);
$container->setAlias('markdown.parser', $config['parser']['service']);
}
}

View file

@ -0,0 +1,24 @@
<?php
namespace Knp\Bundle\MarkdownBundle\Parser\Preset;
/**
* Copyrights KnpBundle.com
*/
class Flavored extends Max
{
/**
* {@inheritDoc}
*/
public function transformMarkdown($text)
{
$types = array();
$markdown = preg_replace_callback("@```[ ]*([^\n]*)(.+?)```@smi", function ($m) use (&$types) {
$types[] = trim($m[1]);
return ' '.str_replace("\n", "\n ", trim($m[2], "\r\n"));
}, parent::transformMarkdown($text));
return $markdown;
}
}

View file

@ -16,19 +16,8 @@
<service id="markdown.parser.max" class="Knp\Bundle\MarkdownBundle\Parser\Preset\Max" public="false">
<tag name="markdown.parser" alias="max" />
</service>
<service id="markdown.parser.sundown" class="Knp\Bundle\MarkdownBundle\Parser\SundownParser" public="false">
<argument type="service" id="markdown.sundown.base_parser" />
<tag name="markdown.parser" alias="sundown" />
</service>
<service id="markdown.sundown.base_parser" public="false" class="Sundown\Markdown">
<argument type="service" id="markdown.sundown.renderer" />
<argument>%markdown.sundown.extensions%</argument>
</service>
<service id="markdown.sundown.renderer" public="false" class="Sundown\Render\HTML">
<argument>%markdown.sundown.render_flags%</argument>
<service id="markdown.parser.flavored" class="Knp\Bundle\MarkdownBundle\Parser\Preset\Flavored" public="false">
<tag name="markdown.parser" alias="flavored" />
</service>
</services>
</container>

View file

@ -0,0 +1,20 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="markdown.parser.sundown" class="Knp\Bundle\MarkdownBundle\Parser\SundownParser" public="false">
<argument type="service" id="markdown.sundown.base_parser" />
</service>
<service id="markdown.sundown.base_parser" public="false" class="Sundown\Markdown">
<argument type="service" id="markdown.sundown.renderer" />
<argument>%markdown.sundown.extensions%</argument>
</service>
<service id="markdown.sundown.renderer" public="false" class="Sundown\Render\HTML">
<argument>%markdown.sundown.render_flags%</argument>
</service>
</services>
</container>