Skip to content

Commit

Permalink
Adds flags implementation for sundown
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent.paulin committed Apr 18, 2012
1 parent 3f86b03 commit 95bcb11
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 137 deletions.
27 changes: 20 additions & 7 deletions DependencyInjection/Configuration.php
Expand Up @@ -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.
*/
Expand All @@ -26,7 +26,7 @@ public function getConfigTreeBuilder()

$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('kwattro_markdown');

$validRenderers = array('base', 'html', 'xhtml', 'custom');

$rootNode
Expand All @@ -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;
}
Expand Down
48 changes: 23 additions & 25 deletions DependencyInjection/KwattroMarkdownExtension.php
Expand Up @@ -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.
*/
Expand All @@ -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);
}
}
}
151 changes: 105 additions & 46 deletions Markdown/KwattroMarkdown.php
Expand Up @@ -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.
*/
Expand All @@ -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,
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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()
{
Expand All @@ -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;
Expand Down

0 comments on commit 95bcb11

Please sign in to comment.