Permalink
Browse files

Adds flags implementation for sundown

  • Loading branch information...
1 parent 3f86b03 commit 95bcb11e01037894bb076910a207f37d4d1a44a7 vincent.paulin committed Apr 18, 2012
View
27 DependencyInjection/Configuration.php
@@ -2,11 +2,11 @@
/**
* This file is part of the KwattroMarkdownBundle package.
- *
+ *
* (c) Christophe Willemsen <willemsen.christophe@gmail.com>
- *
+ *
* Released under the MIT License.
- *
+ *
* For the full copyright and license information, please view the LICENSE
* file that is bundled with this package.
*/
@@ -26,7 +26,7 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('kwattro_markdown');
-
+
$validRenderers = array('base', 'html', 'xhtml', 'custom');
$rootNode
@@ -53,15 +53,28 @@ public function getConfigTreeBuilder()
->booleanNode('superscript')->defaultFalse()->end()
->end()
->end()
+ ->arrayNode('flags')
+ ->addDefaultsIfNotSet()
+ ->children()
+ ->booleanNode('filter_html')->defaultFalse()->end()
+ ->booleanNode('no_images')->defaultFalse()->end()
+ ->booleanNode('no_links')->defaultFalse()->end()
+ ->booleanNode('no_styles')->defaultFalse()->end()
+ ->booleanNode('safe_links_only')->defaultFalse()->end()
+ ->booleanNode('with_toc_data')->defaultFalse()->end()
+ ->booleanNode('hard_wrap')->defaultTrue()->end()
+ ->booleanNode('xhtml')->defaultTrue()->end()
+ ->end()
+ ->end()
->end()
-
+
->validate()
->ifTrue(function($v){return 'custom' === $v['renderer'] && empty($v['render_class']); })
->thenInvalid('You need to specify your custom Renderer class when using the "custom" option')
->end()
-
- ->end();
+
+ ->end();
return $treeBuilder;
}
View
48 DependencyInjection/KwattroMarkdownExtension.php
@@ -2,11 +2,11 @@
/**
* This file is part of the KwattroMarkdownBundle package.
- *
+ *
* (c) Christophe Willemsen <willemsen.christophe@gmail.com>
- *
+ *
* Released under the MIT License.
- *
+ *
* For the full copyright and license information, please view the LICENSE
* file that is bundled with this package.
*/
@@ -27,33 +27,31 @@ public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
-
+
$this->bindParameters($container, 'kwattro_markdown', $config);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$loader->load('extensions.yml');
+ }
+
+ public function getAlias()
+ {
+ return 'kwattro_markdown';
+ }
+ public function bindParameters(ContainerBuilder $container, $name, $config)
+ {
+ if(is_array($config))
+ {
+ foreach ($config as $key => $value)
+ {
+ $this->bindParameters($container, $name.'.'.$key, $value);
+ }
+ }
+ else
+ {
+ $container->setParameter($name, $config);
+ }
}
-
-
- public function getAlias()
- {
- return 'kwattro_markdown';
- }
-
- public function bindParameters(ContainerBuilder $container, $name, $config)
- {
- if(is_array($config))
- {
- foreach ($config as $key => $value)
- {
- $this->bindParameters($container, $name.'.'.$key, $value);
- }
- }
- else
- {
- $container->setParameter($name, $config);
- }
- }
}
View
151 Markdown/KwattroMarkdown.php
@@ -2,11 +2,11 @@
/**
* This file is part of the KwattroMarkdownBundle package.
- *
+ *
* (c) Christophe Willemsen <willemsen.christophe@gmail.com>
- *
+ *
* Released under the MIT License.
- *
+ *
* For the full copyright and license information, please view the LICENSE
* file that is bundled with this package.
*/
@@ -17,14 +17,14 @@
class KwattroMarkdown
{
-
+
/**
* @var Parser $parser
*/
private $parser;
-
+
/**
- * @var array $extensions configuration
+ * @var array $extensions configuration
*/
private $extensions = array(
'no_intra_emphasis' => false,
@@ -36,101 +36,127 @@ class KwattroMarkdown
'space_after_headers' => true,
'superscript' => false,
);
-
+
+ /**
+ * @var array $flags configuration
+ */
+ private $flags = array(
+ 'filter_html' => false,
+ 'no_images' => false,
+ 'no_links' => false,
+ 'no_styles' => false,
+ 'safe_links_only' => false,
+ 'with_toc_data' => false,
+ 'hard_wrap' => true,
+ 'xhtml' => true,
+ );
+
/**
* @var array $renderers available renderers
* Default renderer is 'html', you can specify the desired renderer
* in the constructor
- *
+ *
*/
private $renderers = array(
'base' => '\Sundown\Render\Base',
'html' => '\Sundown\Render\HTML',
'xhtml' => '\Sundown\Render\XHTML',
'custom' => '',
);
-
+
/**
- * @var string $renderer The default renderer specified by the service configuration
+ * @var string $renderer The default renderer specified by the service configuration
*/
private $renderer;
-
+
/**
* Creates a new Markdown instance
- * @param array $extensions_config The enabled extensions
+ * @param array $extensions_config The enabled extensions
*/
-
- public function __construct(array $extensions_config, $renderer, $render_class = null)
- {
- $this->configure($extensions_config, $renderer, $render_class);
+
+ public function __construct(array $extensions_config, array $flags_config, $renderer, $render_class = null)
+ {
+ $this->configure($extensions_config, $flags_config, $renderer, $render_class);
}
-
+
/**
* Parse the given string with the Sundown Parser
* @param string $text The text to transform
+ * @param array $extensions The extensions configuration
+ * @param array $flags The flags configuration
* @param string $renderer The desired renderer
- * @param array $options The extensions configuration
- * @return string The transformed text
+ * @return string The transformed text
*/
- public function render($text, array $options = array(), $renderer = null)
+ public function render($text, array $extensions = array(), array $flags = array(), $renderer = null)
{
- $this->configure($options, $renderer);
+ $this->configure($extensions, $flags, $renderer);
return $this->transform($text);
}
-
+
/**
- * Set up the Markdown instance if it does not exist
+ * Set up the Markdown instance if it does not exist
*/
public function setUpMarkdown()
{
$this->parser = new Parser($this->renderer, $this->extensions);
}
-
+
/**
* Transforms the text into a markdown style
* @param string $text
- * @return string $transform
+ * @return string $transform
*/
public function transform($text)
{
return $this->parser->render($text);
-
+
}
-
+
/**
* Configures the Markdown with extensions and renderer sepcified
* @param array $extensions
- * @param string $renderer
+ * @param array $flags
+ * @param string $renderer
*/
- public function configure(array $extensions = array(), $renderer = null, $render_class = null)
+ public function configure(array $extensions = array(), array $flags = array(), $renderer = null, $render_class = null)
{
if(!empty($extensions))
{
- foreach($extensions as $key => $value)
+ foreach($extensions as $key => $value)
{
$this->checkIfValidExtension($key);
}
-
- $this->extensions = array_merge($this->extensions, $extensions);
+
+ $this->extensions = array_merge($this->extensions, $extensions);
}
-
+
+ if(!empty($flags))
+ {
+ foreach($flags as $key => $value)
+ {
+ $this->checkIfValidFlag($key);
+ }
+
+ $this->flags = array_merge($this->flags, $flags);
+ }
+
if(!empty($renderer) && $this->isValidRenderer($renderer, $render_class))
- {
- $this->renderer = new $this->renderers[$renderer]();
+ {
+ $this->renderer = new $this->renderers[$renderer]($this->flags);
}
else
{
- $this->renderer = new $this->renderers['html'];
+ $this->renderer = new $this->renderers['html']($this->flags);
}
-
+
$this->setUpMarkdown();
}
-
+
/**
* Checks if the given extension is a valid markdown extension
- * @param string $extension
+ * @param string $extension
* @return boolean
- * @throws \InvalidArgumentException
+ * @throws \InvalidArgumentException
*/
public function checkIfValidExtension($extension)
{
@@ -140,12 +166,27 @@ public function checkIfValidExtension($extension)
}
throw new \InvalidArgumentException('The extension '.$extension.' is not a valid Markdown extension');
}
-
+
+ /**
+ * Checks if the given extension is a valid markdown flag
+ * @param string $flag
+ * @return boolean
+ * @throws \InvalidArgumentException
+ */
+ public function checkIfValidFlag($flag)
+ {
+ if(!empty($flag) && array_key_exists($flag, $this->flags))
+ {
+ return true;
+ }
+ throw new \InvalidArgumentException('The flag '.$flag.' is not a valid Markdown flag');
+ }
+
/**
* Checks if the given renderer is a valid renderer
* @param type $renderer
* @return boolean
- * @throws \InvalidArgumentException
+ * @throws \InvalidArgumentException
*/
public function isValidRenderer($renderer, $render_class = null)
{
@@ -160,10 +201,10 @@ public function isValidRenderer($renderer, $render_class = null)
}
throw new \InvalidArgumentException('The renderer specified is not a valid renderer for the Markdown service');
}
-
+
/**
* Returns an array of the enabled extensions
- * @return array
+ * @return array
*/
public function getEnabledExtensions()
{
@@ -175,10 +216,28 @@ public function getEnabledExtensions()
$enabled[$key] = $value;
}
}
-
+
return $enabled;
}
-
+
+ /**
+ * Returns an array of the enabled flags
+ * @return array
+ */
+ public function getEnabledFlags()
+ {
+ $enabled = array();
+ foreach($this->flags as $key => $value)
+ {
+ if($value)
+ {
+ $enabled[$key] = $value;
+ }
+ }
+
+ return $enabled;
+ }
+
public function getParser()
{
return $this->parser;
View
19 README.markdown
@@ -15,7 +15,7 @@ This bundle is in development phase. Do not hesitate to contribute.
* ~~Merge the parameters with the DI config~~
* ~~Add all possible extensions to DI config~~
-* Add flags feature
+* ~~Add flags feature~~
* Convert config files to .xml
* ~~Make the Twig extension use of Markdown class~~
* Add some tests
@@ -45,7 +45,7 @@ Installation
# this command will fetch submodule and copy neccesary files to src dir and compile it.
rake submodule compile
sudo rake install
-
+
# enable the sundown extension by adding the following line to your php.ini
extension=sundown.so
@@ -97,18 +97,18 @@ You can easily use the markdown parser in your Twig templates:
$string = $body; //Some string to transform
$output = $markdown->render($string);
-#### You can custom the extensions and the render to use on the fly
+#### You can custom the extensions and the render to use on the fly. The second array parameter is for the flags.
##### In your controllers :
````
$md = $this->container->get('kwattro_markdown');
$string = $body; // some string to transform
-$output = $md->render($string, array('autolink' => false), 'xhtml')
+$output = $md->render($string, array('autolink' => false), array(), 'xhtml')
````
##### In your templates
````
-{{ body | markdown( {'autolink': false}, 'html') }}
+{{ body | markdown( {'autolink': false}, {}, 'html') }}
````
Syntax
@@ -135,4 +135,13 @@ kwattro_markdown:
lax_html_blocks: false
space_after_headers: true
superscript: false
+ flags:
+ filter_html: false
+ no_images: false
+ no_links: false
+ no_styles: false
+ safe_links_only: false
+ with_toc_data: false
+ hard_wrap: true
+ xhtml: true
````
View
10 Resources/config/extensions.yml
@@ -8,3 +8,13 @@ parameters:
lax_html_blocks: %kwattro_markdown.extensions.lax_html_blocks%
space_after_headers: %kwattro_markdown.extensions.space_after_headers%
superscript: %kwattro_markdown.extensions.superscript%
+
+ kwattro_markdown.flags_config:
+ filter_html: %kwattro_markdown.flags.filter_html%
+ no_images: %kwattro_markdown.flags.no_images%
+ no_links: %kwattro_markdown.flags.no_links%
+ no_styles: %kwattro_markdown.flags.no_styles%
+ safe_links_only: %kwattro_markdown.flags.safe_links_only%
+ with_toc_data: %kwattro_markdown.flags.with_toc_data%
+ hard_wrap: %kwattro_markdown.flags.hard_wrap%
+ xhtml: %kwattro_markdown.flags.xhtml%
View
6 Resources/config/services.yml
@@ -4,10 +4,10 @@ parameters:
services:
kwattro_markdown.twig_filter:
class: %kwattro_markdown.twig_extension%
- arguments: [@kwattro_markdown]
+ arguments: ["@kwattro_markdown"]
tags:
- { name: twig.extension }
-
+
kwattro_markdown:
class: Kwattro\MarkdownBundle\Markdown\KwattroMarkdown
- arguments: [%kwattro_markdown.extensions_config%, %kwattro_markdown.renderer%, %kwattro_markdown.render_class%]
+ arguments: [%kwattro_markdown.extensions_config%, %kwattro_markdown.flags_config%, %kwattro_markdown.renderer%, %kwattro_markdown.render_class%]
View
8 Resources/doc/index.rst
@@ -10,11 +10,11 @@ This bundle is in development phase. Do not hesitate to contribute.
#### Todo's:
-* Merge the parameters with the DI config
+* ~~Merge the parameters with the DI config~~
* ~~Add all possible extensions to DI config~~
-* Add flags feature
+* ~~Add flags feature~~
* Convert config files to .xml
-* Make the Twig extension use of Markdown class
+* ~~Make the Twig extension use of Markdown class~~
* Add some tests
* Request feedbacks
@@ -41,7 +41,7 @@ Installation
# this command will fetch submodule and copy neccesary files to src dir and compile it.
rake submodule compile
sudo rake install
-
+
# enable the sundown extension by adding the following line to your php.ini
# extension=sundown.so
View
28 Tests/Markdown/KwattroMarkdownTest.php
@@ -2,11 +2,11 @@
/**
* This file is part of the KwattroMarkdownBundle package.
- *
+ *
* (c) Christophe Willemsen <willemsen.christophe@gmail.com>
- *
+ *
* Released under the MIT License.
- *
+ *
* For the full copyright and license information, please view the LICENSE
* file that is bundled with this package.
*/
@@ -18,42 +18,42 @@ class KwattroMarkdownTest extends \PHPUnit_Framework_TestCase
{
public function testMarkdown()
{
- $md = new Markdown(array(), 'html');
+ $md = new Markdown(array(), array(), 'html');
$this->assertTrue($md instanceof Markdown);
}
-
+
public function testMarkdownParser()
{
- $md = new Markdown(array(), 'html');
+ $md = new Markdown(array(), array(), 'html');
$this->assertTrue($md->getParser() instanceof \Sundown\Markdown);
}
-
+
public function testDefaultExtensions()
{
- $md = new Markdown(array(), 'html');
+ $md = new Markdown(array(), array(), 'html');
$extensions = $md->getEnabledExtensions();
$this->assertTrue($extensions['autolink']);
}
-
+
public function testRender()
{
- $md = new Markdown(array(), 'html');
+ $md = new Markdown(array(), array(), 'html');
$link = "https://github.com";
$expected = '<p><a href="https://github.com">https://github.com</a></p>'."\n";
$this->assertEquals($expected, $md->render($link));
}
-
+
public function testSetUpWithArgs()
{
- $md = new Markdown(array('autolink' => false), 'html');
+ $md = new Markdown(array('autolink' => false), array(), 'html');
$link = "https://github.com";
$expected = '<p>'.$link.'</p>'."\n";
$this->assertEquals($expected, $md->render($link));
}
-
+
public function testRenderWithArgs()
{
- $md = new Markdown(array(), 'html');
+ $md = new Markdown(array(), array(), 'html');
$link = "https://github.com";
$expected = '<p>'.$link.'</p>'."\n";
$this->assertEquals($expected, $md->render($link, array('autolink' => false)));
View
8 Tests/Twig/Extension/KwattroMarkdownExtensionTest.php
@@ -2,11 +2,11 @@
/**
* This file is part of the KwattroMarkdownBundle package.
- *
+ *
* (c) Christophe Willemsen <willemsen.christophe@gmail.com>
- *
+ *
* Released under the MIT License.
- *
+ *
* For the full copyright and license information, please view the LICENSE
* file that is bundled with this package.
*/
@@ -21,7 +21,7 @@ public function testRender()
{
$link = 'https://github.com';
$expected = '<p><a href="https://github.com">https://github.com</a></p>'."\n";
- $md = new Markdown(array(), 'html');
+ $md = new Markdown(array(), array(), 'html');
$twig = new TwigMarkdown($md);
$this->assertEquals($expected, $twig->markdown($link));
}
View
35 Twig/Extension/KwattroMarkdownExtension.php
@@ -2,66 +2,67 @@
/**
* This file is part of the KwattroMarkdownBundle package.
- *
+ *
* (c) Christophe Willemsen <willemsen.christophe@gmail.com>
- *
+ *
* Released under the MIT License.
- *
+ *
* For the full copyright and license information, please view the LICENSE
* file that is bundled with this package.
*/
namespace Kwattro\MarkdownBundle\Twig\Extension;
-
+
use Kwattro\MarkdownBundle\Markdown\KwattroMarkdown as Markdown;
-
+
class KwattroMarkdownExtension extends \Twig_Extension
{
/**
*
- * @var Markdown
+ * @var Markdown
*/
private $markdown;
-
+
/**
* Creates the Extension instance
- * @param Markdown $markdown
+ * @param Markdown $markdown
*/
public function __construct(Markdown $markdown)
{
$this->markdown = $markdown;
}
-
+
/**
* Returns the name of the extension
- *
+ *
* @return string The name of the extension
*/
public function getName()
{
return 'kwattro_markdown';
}
-
+
/**
* Add the extension filters to the twig registered filters
*/
public function getFilters()
{
return array(
'markdown' => new \Twig_Filter_Method($this, 'markdown', array('is_safe'=>array('html')))
- );
+ );
}
-
+
/**
* Transforms the text into a markdown style
- *
+ *
* @param string The text to be transformed
- * @param array $options Optional : An array with the extensions options
+ * @param array $extensions Optional : An array with the extensions options
+ * @param array $flags Optional : An array with the flags options
* @param string $renderer Optional: The desired renderer
* @return string The transformed text
*/
- public function markdown($string, array $options = array(), $renderer = null)
+ public function markdown($string, array $extensions = array(), array $flags = array(), $renderer = null)
{
- return $this->markdown->render($string, $options, $renderer);
+ return $this->markdown->render($string, $extensions, $flags, $renderer);
}
}
View
24 vendor/vendors.php
@@ -18,18 +18,18 @@
$vendorDir = __DIR__;
$deps = array(
- array('symfony', 'git://github.com/symfony/symfony.git', isset($_SERVER['SYMFONY_VERSION']) ? $_SERVER['SYMFONY_VERSION'] : 'origin/master'),
- array('twig', 'git://github.com/fabpot/Twig.git', 'origin/master'),
- array('swiftmailer', 'git://github.com/swiftmailer/swiftmailer.git', 'origin/master'),
- array('doctrine-common', 'git://github.com/doctrine/common.git', 'origin/master'),
- array('doctrine-dbal', 'git://github.com/doctrine/dbal.git', 'origin/master'),
- array('doctrine', 'git://github.com/doctrine/doctrine2.git', 'origin/master'),
- array('doctrine-mongodb-odm', 'git://github.com/doctrine/mongodb-odm.git', 'origin/master'),
- array('doctrine-mongodb', 'git://github.com/doctrine/mongodb.git', 'origin/master'),
- array('doctrine-couchdb', 'git://github.com/doctrine/couchdb-odm.git', 'origin/master'),
- array('propel', 'git://github.com/propelorm/Propel.git', 'origin/master'),
- array('propel-behavior', 'git://github.com/willdurand/TypehintableBehavior.git', 'origin/master'),
- array('phing', 'git://github.com/Xosofox/phing.git', 'origin/master'),
+ array('symfony', 'https://github.com/symfony/symfony.git', isset($_SERVER['SYMFONY_VERSION']) ? $_SERVER['SYMFONY_VERSION'] : 'origin/master'),
+ array('twig', 'https://github.com/fabpot/Twig.git', 'origin/master'),
+ array('swiftmailer', 'https://github.com/swiftmailer/swiftmailer.git', 'origin/master'),
+ array('doctrine-common', 'https://github.com/doctrine/common.git', 'origin/master'),
+ array('doctrine-dbal', 'https://github.com/doctrine/dbal.git', 'origin/master'),
+ array('doctrine', 'https://github.com/doctrine/doctrine2.git', 'origin/master'),
+ array('doctrine-mongodb-odm', 'https://github.com/doctrine/mongodb-odm.git', 'origin/master'),
+ array('doctrine-mongodb', 'https://github.com/doctrine/mongodb.git', 'origin/master'),
+ array('doctrine-couchdb', 'https://github.com/doctrine/couchdb-odm.git', 'origin/master'),
+ array('propel', 'https://github.com/propelorm/Propel.git', 'origin/master'),
+ array('propel-behavior', 'https://github.com/willdurand/TypehintableBehavior.git', 'origin/master'),
+ array('phing', 'https://github.com/Xosofox/phing.git', 'origin/master'),
);
foreach ($deps as $dep) {

0 comments on commit 95bcb11

Please sign in to comment.