Added GeshiBundle to highlight source code

Signed-off-by: Gergely Polonkai (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Gergely Polonkai (W00d5t0ck)
2012-09-04 10:42:20 +02:00
parent 4a07b2e120
commit ea325aab7f
8 changed files with 180 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
<?php
namespace GergelyPolonkai\GeshiBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('gergely_polonkai_geshi');
// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
return $treeBuilder;
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace GergelyPolonkai\GeshiBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class GergelyPolonkaiGeshiExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}

View File

@@ -0,0 +1,9 @@
<?php
namespace GergelyPolonkai\GeshiBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class GergelyPolonkaiGeshiBundle extends Bundle
{
}

View File

@@ -0,0 +1,8 @@
parameters:
gergely_polonkai_geshi.geshi_highlighter.class: GergelyPolonkai\GeshiBundle\Twig\GeshiHighlight
services:
gergely_polonkai_geshi.geshi_highlighter:
class: %gergely_polonkai_geshi.geshi_highlighter.class%
tags:
- { name: 'twig.extension' }

View File

@@ -0,0 +1,45 @@
<?php
namespace GergelyPolonkai\GeshiBundle\Twig;
use GeSHi;
/**
* Description of GeshiHighlight
*
* @author polonkai.gergely
*/
class GeshiHighlight extends \Twig_Extension
{
/**
*
* @var \GeSHi $geshi
*/
private $geshi;
public function __construct()
{
$this->geshi = new GeSHi();
}
public function getFilters()
{
return array(
'geshi_highlight' => new \Twig_Filter_Method($this, 'geshiFilter', array('is_safe' => array('html'))),
);
}
public function geshiFilter($code, $lang)
{
$this->geshi->set_language($lang);
$this->geshi->set_source($code);
$this->geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
$this->geshi->enable_keyword_links(false);
$this->geshi->enable_classes();
return $this->geshi->parse_code();
}
public function getName() {
return 'geshi_highlighter';
}
}